From 686cab81846fecc79959656ef9355be82c2214e6 Mon Sep 17 00:00:00 2001 From: jakubg1 <24206305+jakubg1@users.noreply.github.com> Date: Wed, 1 Apr 2026 02:10:00 +0200 Subject: [PATCH] Fix a segfault when calling `api.dynobj_putvalues()` An undocumented change from the previous commit: - When the Lua script is not able to find an object, the message is now printed to `errors.txt`, not just to `log.txt`. --- scripting/lua.cpp | 30 ++++++++++++++++++++++++------ scripting/lua.h | 4 ++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/scripting/lua.cpp b/scripting/lua.cpp index e9932f19..91fb8df2 100644 --- a/scripting/lua.cpp +++ b/scripting/lua.cpp @@ -98,6 +98,27 @@ void lua::push_memcell_values(lua_State *L, const TMemCell *mc) lua_settable(L, -3); } +memcell_values lua::get_memcell_values(lua_State *L) +{ + memcell_values mc{nullptr, 0.0, 0.0}; + lua_pushstring(L, "str"); + lua_gettable(L, -2); + if (lua_isstring(L, -1)) + mc.str = lua_tostring(L, -1); + lua_pop(L, 1); + lua_pushstring(L, "num1"); + lua_gettable(L, -2); + if (lua_isnumber(L, -1)) + mc.num1 = lua_tonumber(L, -1); + lua_pop(L, 1); + lua_pushstring(L, "num2"); + lua_gettable(L, -2); + if (lua_isnumber(L, -1)) + mc.num2 = lua_tonumber(L, -1); + lua_pop(L, 1); + return mc; +} + int lua::scriptapi_event_create(lua_State *L) { std::string name = lua_tostring(L, 1); @@ -261,17 +282,14 @@ int lua::scriptapi_train_getname(lua_State *L) int lua::scriptapi_dynobj_putvalues(lua_State *L) { auto *dyn = static_cast(lua_touserdata(L, 1)); - auto *mc = static_cast(lua_touserdata(L, 2)); - std::string str = mc->str; - double num1 = mc->num1; - double num2 = mc->num2; + auto [str, num1, num2] = get_memcell_values(L); if (!dyn) return 0; TLocation loc{}; if (dyn->Mechanik) - dyn->Mechanik->PutCommand(std::string(str), num1, num2, loc); + dyn->Mechanik->PutCommand(str, num1, num2, loc); else - dyn->MoverParameters->PutCommand(std::string(str), num1, num2, loc); + dyn->MoverParameters->PutCommand(str, num1, num2, loc); return 0; } diff --git a/scripting/lua.h b/scripting/lua.h index bb5359f9..1578ffd8 100644 --- a/scripting/lua.h +++ b/scripting/lua.h @@ -5,6 +5,7 @@ class basic_event; class TMemCell; class TDynamicObject; +struct memcell_values { const char *str; double num1; double num2; }; class lua { @@ -44,6 +45,5 @@ public: static void unref(lua_State *L, int ref); static void dispatch_event(lua_State *L, int handler, basic_event *event, const TDynamicObject *activator); static void push_memcell_values(lua_State *L, const TMemCell *mc); - - struct memcell_values { const char *str; double num1; double num2; }; + static memcell_values get_memcell_values(lua_State *L); };