Fix empty string being discarded at beginning of lua log/return lists

This commit is contained in:
jacob1 2022-10-19 17:28:04 -04:00
parent b306c2c33c
commit 6338da7cb7
No known key found for this signature in database
GPG Key ID: 4E58A32D510E1995
3 changed files with 29 additions and 10 deletions

View File

@ -413,24 +413,32 @@ int luatpt_setconsole(lua_State* l)
luacon_controller->HideConsole(); luacon_controller->HideConsole();
return 0; return 0;
} }
int luatpt_log(lua_State* l) int luatpt_log(lua_State* l)
{ {
int args = lua_gettop(l); int args = lua_gettop(l);
String text; String text;
bool hasText = false;
for(int i = 1; i <= args; i++) for(int i = 1; i <= args; i++)
{ {
luaL_tostring(l, -1); luaL_tostring(l, -1);
if(text.length()) if (hasText)
text=tpt_lua_optString(l, -1, "") + ", " + text; {
text = tpt_lua_optString(l, -1, "") + ", " + text;
}
else else
text=tpt_lua_optString(l, -1, ""); {
text = tpt_lua_optString(l, -1, "");
hasText = true;
}
lua_pop(l, 2); lua_pop(l, 2);
} }
if((*luacon_currentCommand)) if ((*luacon_currentCommand))
{ {
if(luacon_lastError->length()) if (luacon_hasLastError)
*luacon_lastError += "; "; *luacon_lastError += "; ";
*luacon_lastError += text; *luacon_lastError += text;
luacon_hasLastError = true;
} }
else else
luacon_ci->Log(CommandInterface::LogNotice, text); luacon_ci->Log(CommandInterface::LogNotice, text);

View File

@ -22,6 +22,7 @@ extern Renderer * luacon_ren;
extern bool *luacon_currentCommand; extern bool *luacon_currentCommand;
extern String *luacon_lastError; extern String *luacon_lastError;
extern bool luacon_hasLastError;
class LuaSmartRef; class LuaSmartRef;
extern int *lua_el_mode; extern int *lua_el_mode;

View File

@ -82,6 +82,7 @@ Renderer * luacon_ren;
bool *luacon_currentCommand; bool *luacon_currentCommand;
String *luacon_lastError; String *luacon_lastError;
bool luacon_hasLastError;
String lastCode; String lastCode;
int *lua_el_mode; int *lua_el_mode;
@ -231,6 +232,7 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m):
luacon_currentCommand = &currentCommand; luacon_currentCommand = &currentCommand;
luacon_lastError = &lastError; luacon_lastError = &lastError;
luacon_hasLastError = false;
lastCode = ""; lastCode = "";
@ -4343,9 +4345,10 @@ void LuaScriptInterface::OnTick()
int LuaScriptInterface::Command(String command) int LuaScriptInterface::Command(String command)
{ {
lastError = "";
luacon_hasLastError = false;
if (command[0] == '!') if (command[0] == '!')
{ {
lastError = "";
int ret = legacy->Command(command.Substr(1)); int ret = legacy->Command(command.Substr(1));
lastError = legacy->GetLastError(); lastError = legacy->GetLastError();
return ret; return ret;
@ -4353,8 +4356,6 @@ int LuaScriptInterface::Command(String command)
else else
{ {
int level = lua_gettop(l), ret = -1; int level = lua_gettop(l), ret = -1;
String text = "";
lastError = "";
currentCommand = true; currentCommand = true;
if (lastCode.length()) if (lastCode.length())
lastCode += "\n"; lastCode += "\n";
@ -4382,16 +4383,25 @@ int LuaScriptInterface::Command(String command)
lastCode = ""; lastCode = "";
ret = lua_pcall(l, 0, LUA_MULTRET, 0); ret = lua_pcall(l, 0, LUA_MULTRET, 0);
if (ret) if (ret)
{
lastError = luacon_geterror(); lastError = luacon_geterror();
}
else else
{ {
for (level++;level<=lua_gettop(l);level++) String text = "";
bool hasText = false;
for (level++; level <= lua_gettop(l); level++)
{ {
luaL_tostring(l, level); luaL_tostring(l, level);
if (text.length()) if (hasText)
{
text += ", " + tpt_lua_optString(l, -1, ""); text += ", " + tpt_lua_optString(l, -1, "");
}
else else
{
text = tpt_lua_optString(l, -1, ""); text = tpt_lua_optString(l, -1, "");
hasText = true;
}
lua_pop(l, 1); lua_pop(l, 1);
} }
if (text.length()) if (text.length())