From 6338da7cb726c8d00503d41b8ebd30f78fd7f034 Mon Sep 17 00:00:00 2001 From: jacob1 Date: Wed, 19 Oct 2022 17:28:04 -0400 Subject: [PATCH] Fix empty string being discarded at beginning of lua log/return lists --- src/lua/LegacyLuaAPI.cpp | 18 +++++++++++++----- src/lua/LuaScriptHelper.h | 1 + src/lua/LuaScriptInterface.cpp | 20 +++++++++++++++----- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/lua/LegacyLuaAPI.cpp b/src/lua/LegacyLuaAPI.cpp index ecad10bc0..ecdb03fba 100644 --- a/src/lua/LegacyLuaAPI.cpp +++ b/src/lua/LegacyLuaAPI.cpp @@ -413,24 +413,32 @@ int luatpt_setconsole(lua_State* l) luacon_controller->HideConsole(); return 0; } + int luatpt_log(lua_State* l) { int args = lua_gettop(l); String text; + bool hasText = false; for(int i = 1; i <= args; i++) { luaL_tostring(l, -1); - if(text.length()) - text=tpt_lua_optString(l, -1, "") + ", " + text; + if (hasText) + { + text = tpt_lua_optString(l, -1, "") + ", " + text; + } else - text=tpt_lua_optString(l, -1, ""); + { + text = tpt_lua_optString(l, -1, ""); + hasText = true; + } lua_pop(l, 2); } - if((*luacon_currentCommand)) + if ((*luacon_currentCommand)) { - if(luacon_lastError->length()) + if (luacon_hasLastError) *luacon_lastError += "; "; *luacon_lastError += text; + luacon_hasLastError = true; } else luacon_ci->Log(CommandInterface::LogNotice, text); diff --git a/src/lua/LuaScriptHelper.h b/src/lua/LuaScriptHelper.h index ba261261f..4abaae5e9 100644 --- a/src/lua/LuaScriptHelper.h +++ b/src/lua/LuaScriptHelper.h @@ -22,6 +22,7 @@ extern Renderer * luacon_ren; extern bool *luacon_currentCommand; extern String *luacon_lastError; +extern bool luacon_hasLastError; class LuaSmartRef; extern int *lua_el_mode; diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 03a1d4238..ae84a98b5 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -82,6 +82,7 @@ Renderer * luacon_ren; bool *luacon_currentCommand; String *luacon_lastError; +bool luacon_hasLastError; String lastCode; int *lua_el_mode; @@ -231,6 +232,7 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m): luacon_currentCommand = ¤tCommand; luacon_lastError = &lastError; + luacon_hasLastError = false; lastCode = ""; @@ -4343,9 +4345,10 @@ void LuaScriptInterface::OnTick() int LuaScriptInterface::Command(String command) { + lastError = ""; + luacon_hasLastError = false; if (command[0] == '!') { - lastError = ""; int ret = legacy->Command(command.Substr(1)); lastError = legacy->GetLastError(); return ret; @@ -4353,8 +4356,6 @@ int LuaScriptInterface::Command(String command) else { int level = lua_gettop(l), ret = -1; - String text = ""; - lastError = ""; currentCommand = true; if (lastCode.length()) lastCode += "\n"; @@ -4382,16 +4383,25 @@ int LuaScriptInterface::Command(String command) lastCode = ""; ret = lua_pcall(l, 0, LUA_MULTRET, 0); if (ret) + { lastError = luacon_geterror(); + } else { - for (level++;level<=lua_gettop(l);level++) + String text = ""; + bool hasText = false; + for (level++; level <= lua_gettop(l); level++) { luaL_tostring(l, level); - if (text.length()) + if (hasText) + { text += ", " + tpt_lua_optString(l, -1, ""); + } else + { text = tpt_lua_optString(l, -1, ""); + hasText = true; + } lua_pop(l, 1); } if (text.length())