summaryrefslogtreecommitdiff
path: root/src/cat
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2013-05-04 13:42:23 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2013-05-04 13:42:23 (GMT)
commitcab667001dd1944cae646349430b969912c0b157 (patch)
tree53a425ae892e4f7f643822abdf1bb26449a443a0 /src/cat
parentdd0f5f5efb4f108c2ce329a2ceabc7d8fa2b63f8 (diff)
parent630216c2531545c232edb45d3054c903c2c205c4 (diff)
downloadpowder-cab667001dd1944cae646349430b969912c0b157.zip
powder-cab667001dd1944cae646349430b969912c0b157.tar.gz
Merge branch 'master' of github.com:FacialTurd/The-Powder-Toy
Diffstat (limited to 'src/cat')
-rw-r--r--src/cat/LegacyLuaAPI.cpp69
-rw-r--r--src/cat/LuaScriptHelper.h1
-rw-r--r--src/cat/LuaScriptInterface.cpp48
-rw-r--r--src/cat/TPTScriptInterface.cpp2
4 files changed, 83 insertions, 37 deletions
diff --git a/src/cat/LegacyLuaAPI.cpp b/src/cat/LegacyLuaAPI.cpp
index 75b6780..4d3c0c8 100644
--- a/src/cat/LegacyLuaAPI.cpp
+++ b/src/cat/LegacyLuaAPI.cpp
@@ -514,7 +514,7 @@ int luacon_keyevent(int key, int modifier, int event)
callret = lua_pcall(l, 4, 1, 0);
if (callret)
{
- if (!strcmp(luaL_optstring(l, -1, ""), "Error: Script not responding"))
+ if (!strcmp(luacon_geterror(), "Error: Script not responding"))
{
ui::Engine::Ref().LastTick(clock());
for(j=i;j<=c-1;j++)
@@ -566,7 +566,7 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel)
callret = lua_pcall(l, 5, 1, 0);
if (callret)
{
- if (!strcmp(luaL_optstring(l, -1, ""), "Error: Script not responding"))
+ if (!strcmp(luacon_geterror(), "Error: Script not responding"))
{
ui::Engine::Ref().LastTick(clock());
for(j=i;j<=c-1;j++)
@@ -579,7 +579,7 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel)
c--;
i--;
}
- luacon_ci->Log(CommandInterface::LogError, luaL_optstring(l, -1, ""));
+ luacon_ci->Log(CommandInterface::LogError, luacon_geterror());
lua_pop(l, 1);
}
else
@@ -627,7 +627,7 @@ int luacon_step(int mx, int my, std::string selectl, std::string selectr, std::s
callret = lua_pcall(l, 0, 0, 0);
if (callret)
{
- if (!strcmp(luaL_optstring(l, -1, ""), "Error: Script not responding"))
+ if (!strcmp(luacon_geterror(), "Error: Script not responding"))
{
ui::Engine::Ref().LastTick(clock());
for(j=i;j<=c-1;j++)
@@ -640,7 +640,7 @@ int luacon_step(int mx, int my, std::string selectl, std::string selectr, std::s
c--;
i--;
}
- luacon_ci->Log(CommandInterface::LogError, luaL_optstring(l, -1, ""));
+ luacon_ci->Log(CommandInterface::LogError, luacon_geterror());
lua_pop(l, 1);
}
}
@@ -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";
+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);
@@ -776,7 +796,7 @@ int luacon_graphicsReplacement(GRAPHICS_FUNC_ARGS, int i)
callret = lua_pcall(luacon_ci->l, 4, 10, 0);
if (callret)
{
- luacon_ci->Log(CommandInterface::LogError, luaL_optstring(luacon_ci->l, -1, ""));
+ luacon_ci->Log(CommandInterface::LogError, luacon_geterror());
lua_pop(luacon_ci->l, 1);
}
else
@@ -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);
@@ -958,7 +957,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;
diff --git a/src/cat/LuaScriptHelper.h b/src/cat/LuaScriptHelper.h
index fde1eb4..d1679a6 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 74757e5..452e47a 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 = &currentCommand;
luacon_lastError = &lastError;
+ lastCode = "";
+
//Replace print function with our screen logging thingy
lua_pushcfunction(l, luatpt_log);
lua_setglobal(l, "print");
@@ -2119,14 +2122,53 @@ int LuaScriptInterface::Command(std::string command)
}
else
{
- int ret;
+ int level = lua_gettop(l), ret;
+ std::string text = "";
lastError = "";
currentCommand = true;
+ if(lastCode.length())
+ lastCode += "\n";
+ lastCode += command;
+ std::string tmp = "return " + lastCode;
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, lastCode.c_str(), lastCode.length(), "@console");
+ }
+ if(lua_type(l, -1) != LUA_TFUNCTION)
{
lastError = luacon_geterror();
- //Log(LogError, lastError);
+ if(std::string(lastError).find("near '<eof>'")!=-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();
+ 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;
diff --git a/src/cat/TPTScriptInterface.cpp b/src/cat/TPTScriptInterface.cpp
index bf08f1e..43bb67b 100644
--- a/src/cat/TPTScriptInterface.cpp
+++ b/src/cat/TPTScriptInterface.cpp
@@ -137,7 +137,7 @@ int TPTScriptInterface::parseNumber(char * stringData)
if(cc >= '0' && cc <= '9')
currentNumber += cc - '0';
else if(cc >= 'a' && cc <= 'f')
- currentNumber += (cc - 'A') + 10;
+ currentNumber += (cc - 'a') + 10;
else if(cc >= 'A' && cc <= 'F')
currentNumber += (cc - 'A') + 10;
else