From 29496cb6d3418eaed63f78a6624458ecf410bfc3 Mon Sep 17 00:00:00 2001 From: mniip Date: Thu, 2 May 2013 23:49:53 +0400 Subject: [PATCH 1/4] fix tpt.log so that when called multiple times, it doesn't overwrite the text --- src/cat/LegacyLuaAPI.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cat/LegacyLuaAPI.cpp b/src/cat/LegacyLuaAPI.cpp index 75b67806b..4abd041a3 100644 --- a/src/cat/LegacyLuaAPI.cpp +++ b/src/cat/LegacyLuaAPI.cpp @@ -958,7 +958,11 @@ int luatpt_log(lua_State* l) lua_pop(l, 2); } if((*luacon_currentCommand)) - (*luacon_lastError) = text; + { + if(luacon_lastError->length()) + *luacon_lastError += "; "; + *luacon_lastError += text; + } else luacon_ci->Log(CommandInterface::LogNotice, text.c_str()); return 0; From d8023d21cd8240d4aedc9d63a0e1cd056b15c919 Mon Sep 17 00:00:00 2001 From: mniip Date: Fri, 3 May 2013 01:09:13 +0400 Subject: [PATCH 2/4] better luacon_geterror --- src/cat/LegacyLuaAPI.cpp | 51 ++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/cat/LegacyLuaAPI.cpp b/src/cat/LegacyLuaAPI.cpp index 4abd041a3..45b7f963f 100644 --- a/src/cat/LegacyLuaAPI.cpp +++ b/src/cat/LegacyLuaAPI.cpp @@ -664,12 +664,32 @@ void luacon_hook(lua_State * l, lua_Debug * ar) } } -char *luacon_geterror(){ - char *error = (char*)lua_tostring(luacon_ci->l, -1); - if(error==NULL || !error[0]){ - error = "failed to execute"; +static int luaL_tostring (lua_State *L, int n) { + luaL_checkany(L, n); + switch (lua_type(L, n)) { + case LUA_TNUMBER: + lua_pushstring(L, lua_tostring(L, n)); + break; + case LUA_TSTRING: + lua_pushvalue(L, n); + break; + case LUA_TBOOLEAN: + lua_pushstring(L, (lua_toboolean(L, n) ? "true" : "false")); + break; + case LUA_TNIL: + lua_pushliteral(L, "nil"); + break; + default: + lua_pushfstring(L, "%s: %p", luaL_typename(L, n), lua_topointer(L, n)); + break; } - return error; + return 1; +} +char *luacon_geterror(){ + luaL_tostring(luacon_ci->l, -1); + char* err = (char*)luaL_optstring(luacon_ci->l, -1, "failed to execute"); + lua_pop(luacon_ci->l, 1); + return err; } /*void luacon_close(){ lua_close(l); @@ -923,27 +943,6 @@ int luatpt_setconsole(lua_State* l) luacon_controller->HideConsole(); return 0; } -static int luaL_tostring (lua_State *L, int n) { - luaL_checkany(L, n); - switch (lua_type(L, n)) { - case LUA_TNUMBER: - lua_pushstring(L, lua_tostring(L, n)); - break; - case LUA_TSTRING: - lua_pushvalue(L, n); - break; - case LUA_TBOOLEAN: - lua_pushstring(L, (lua_toboolean(L, n) ? "true" : "false")); - break; - case LUA_TNIL: - lua_pushliteral(L, "nil"); - break; - default: - lua_pushfstring(L, "%s: %p", luaL_typename(L, n), lua_topointer(L, n)); - break; - } - return 1; -} int luatpt_log(lua_State* l) { int args = lua_gettop(l); From 0233d8db466c6ef402e5008671cfa6555a2d896d Mon Sep 17 00:00:00 2001 From: mniip Date: Fri, 3 May 2013 01:54:21 +0400 Subject: [PATCH 3/4] print returned values; implicit return in console; console source --- src/cat/LegacyLuaAPI.cpp | 2 +- src/cat/LuaScriptHelper.h | 1 + src/cat/LuaScriptInterface.cpp | 35 +++++++++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/cat/LegacyLuaAPI.cpp b/src/cat/LegacyLuaAPI.cpp index 45b7f963f..2166b1411 100644 --- a/src/cat/LegacyLuaAPI.cpp +++ b/src/cat/LegacyLuaAPI.cpp @@ -664,7 +664,7 @@ void luacon_hook(lua_State * l, lua_Debug * ar) } } -static int luaL_tostring (lua_State *L, int n) { +int luaL_tostring (lua_State *L, int n) { luaL_checkany(L, n); switch (lua_type(L, n)) { case LUA_TNUMBER: diff --git a/src/cat/LuaScriptHelper.h b/src/cat/LuaScriptHelper.h index fde1eb4a1..d1679a651 100644 --- a/src/cat/LuaScriptHelper.h +++ b/src/cat/LuaScriptHelper.h @@ -9,6 +9,7 @@ extern Graphics * luacon_g; extern Renderer * luacon_ren; extern bool *luacon_currentCommand; +extern int luaL_tostring(lua_State* l, int n); extern std::string *luacon_lastError; extern int *lua_el_func, *lua_el_mode, *lua_gr_func; diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 74757e5d3..8d499a0ea 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -2119,14 +2119,43 @@ int LuaScriptInterface::Command(std::string command) } else { - int ret; + int level = lua_gettop(l), ret; + std::string text = ""; lastError = ""; currentCommand = true; + std::string tmp = "return " + command; ui::Engine::Ref().LastTick(clock()); - if((ret = luaL_dostring(l, command.c_str()))) + luaL_loadbuffer(l, tmp.c_str(), tmp.length(), "@console"); + if(lua_type(l, -1) != LUA_TFUNCTION) { + lua_pop(l, 1); + luaL_loadbuffer(l, command.c_str(), command.length(), "@console"); + } + if(lua_type(l, -1) != LUA_TFUNCTION) lastError = luacon_geterror(); - //Log(LogError, lastError); + else + { + ret = lua_pcall(l, 0, LUA_MULTRET, 0); + if(ret) + lastError = luacon_geterror(); + else + { + for(level++;level<=lua_gettop(l);level++) + { + luaL_tostring(l, level); + if(text.length()) + text += ", " + std::string(luaL_optstring(l, -1, "")); + else + text = std::string(luaL_optstring(l, -1, "")); + lua_pop(l, 1); + } + if(text.length()) + if(lastError.length()) + lastError += "; " + text; + else + lastError = text; + + } } currentCommand = false; return ret; From 4cd12e8561b71ce32598d791ddedf5f1aef9203c Mon Sep 17 00:00:00 2001 From: mniip Date: Fri, 3 May 2013 09:56:13 +0400 Subject: [PATCH 4/4] allow multiline code input, command will be executed when enough code given --- src/cat/LuaScriptInterface.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 8d499a0ea..587be8c14 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -57,6 +57,7 @@ Renderer * luacon_ren; bool *luacon_currentCommand; std::string *luacon_lastError; +std::string lastCode; int *lua_el_func, *lua_el_mode, *lua_gr_func; @@ -178,6 +179,8 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m): luacon_currentCommand = ¤tCommand; luacon_lastError = &lastError; + lastCode = ""; + //Replace print function with our screen logging thingy lua_pushcfunction(l, luatpt_log); lua_setglobal(l, "print"); @@ -2123,18 +2126,28 @@ int LuaScriptInterface::Command(std::string command) std::string text = ""; lastError = ""; currentCommand = true; - std::string tmp = "return " + command; + if(lastCode.length()) + lastCode += "\n"; + lastCode += command; + std::string tmp = "return " + lastCode; ui::Engine::Ref().LastTick(clock()); luaL_loadbuffer(l, tmp.c_str(), tmp.length(), "@console"); if(lua_type(l, -1) != LUA_TFUNCTION) { lua_pop(l, 1); - luaL_loadbuffer(l, command.c_str(), command.length(), "@console"); + luaL_loadbuffer(l, lastCode.c_str(), lastCode.length(), "@console"); } if(lua_type(l, -1) != LUA_TFUNCTION) + { lastError = luacon_geterror(); + if(std::string(lastError).find("near ''")!=-1) //the idea stolen from lua-5.1.5/lua.c + lastError = ">"; + else + lastCode = ""; + } else { + lastCode = ""; ret = lua_pcall(l, 0, LUA_MULTRET, 0); if(ret) lastError = luacon_geterror();