reset length when running step functions in case a step was unregistered, fixes first half of #210

also cleanup / make it easier to read
This commit is contained in:
jacob1 2015-01-10 20:10:42 -05:00
parent e92bbac700
commit a5797f6262

View File

@ -436,7 +436,7 @@ int luacon_elementwrite(lua_State* l)
bool shortcuts = true; bool shortcuts = true;
int luacon_keyevent(int key, int modifier, int event) int luacon_keyevent(int key, int modifier, int event)
{ {
int kycontinue = 1, i, j, callret; int kycontinue = 1;
lua_State* l=luacon_ci->l; lua_State* l=luacon_ci->l;
lua_pushstring(l, "keyfunctions"); lua_pushstring(l, "keyfunctions");
lua_rawget(l, LUA_REGISTRYINDEX); lua_rawget(l, LUA_REGISTRYINDEX);
@ -448,28 +448,27 @@ int luacon_keyevent(int key, int modifier, int event)
lua_pushvalue(l, -2); lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX); lua_rawset(l, LUA_REGISTRYINDEX);
} }
int c=lua_objlen(l, -1); int len = lua_objlen(l, -1);
for(i=1;i<=c && kycontinue;i++) for (int i = 1; i <= len && kycontinue; i++)
{ {
lua_rawgeti(l, -1, i); lua_rawgeti(l, -1, i);
lua_pushlstring(l, (const char*)&key, 1); lua_pushlstring(l, (const char*)&key, 1);
lua_pushinteger(l, key); lua_pushinteger(l, key);
lua_pushinteger(l, modifier); lua_pushinteger(l, modifier);
lua_pushinteger(l, event); lua_pushinteger(l, event);
callret = lua_pcall(l, 4, 1, 0); int callret = lua_pcall(l, 4, 1, 0);
if (callret) if (callret)
{ {
if (!strcmp(luacon_geterror(), "Error: Script not responding")) if (!strcmp(luacon_geterror(), "Error: Script not responding"))
{ {
ui::Engine::Ref().LastTick(gettime()); ui::Engine::Ref().LastTick(gettime());
for(j=i;j<=c-1;j++) for (int j = i; j <= len-1; j++)
{ {
lua_rawgeti(l, -2, j+1); lua_rawgeti(l, -2, j+1);
lua_rawseti(l, -3, j); lua_rawseti(l, -3, j);
} }
lua_pushnil(l); lua_pushnil(l);
lua_rawseti(l, -3, c); lua_rawseti(l, -3, len);
c--;
i--; i--;
} }
luacon_ci->Log(CommandInterface::LogError, luacon_geterror()); luacon_ci->Log(CommandInterface::LogError, luacon_geterror());
@ -481,6 +480,7 @@ int luacon_keyevent(int key, int modifier, int event)
kycontinue = lua_toboolean(l, -1); kycontinue = lua_toboolean(l, -1);
lua_pop(l, 1); lua_pop(l, 1);
} }
len = lua_objlen(l, -1);
} }
lua_pop(l, 1); lua_pop(l, 1);
return kycontinue && shortcuts; return kycontinue && shortcuts;
@ -488,7 +488,7 @@ int luacon_keyevent(int key, int modifier, int event)
int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel) int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel)
{ {
int mpcontinue = 1, i, j, callret; int mpcontinue = 1;
lua_State* l=luacon_ci->l; lua_State* l=luacon_ci->l;
lua_pushstring(l, "mousefunctions"); lua_pushstring(l, "mousefunctions");
lua_rawget(l, LUA_REGISTRYINDEX); lua_rawget(l, LUA_REGISTRYINDEX);
@ -500,8 +500,8 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel)
lua_pushvalue(l, -2); lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX); lua_rawset(l, LUA_REGISTRYINDEX);
} }
int c=lua_objlen(l, -1); int len = lua_objlen(l, -1);
for(i=1;i<=c && mpcontinue;i++) for (int i = 1; i <= len && mpcontinue; i++)
{ {
lua_rawgeti(l, -1, i); lua_rawgeti(l, -1, i);
lua_pushinteger(l, mx); lua_pushinteger(l, mx);
@ -509,20 +509,19 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel)
lua_pushinteger(l, mb); lua_pushinteger(l, mb);
lua_pushinteger(l, event); lua_pushinteger(l, event);
lua_pushinteger(l, mouse_wheel); lua_pushinteger(l, mouse_wheel);
callret = lua_pcall(l, 5, 1, 0); int callret = lua_pcall(l, 5, 1, 0);
if (callret) if (callret)
{ {
if (!strcmp(luacon_geterror(), "Error: Script not responding")) if (!strcmp(luacon_geterror(), "Error: Script not responding"))
{ {
ui::Engine::Ref().LastTick(gettime()); ui::Engine::Ref().LastTick(gettime());
for(j=i;j<=c-1;j++) for (int j = i; j <= len-1; j++)
{ {
lua_rawgeti(l, -2, j+1); lua_rawgeti(l, -2, j+1);
lua_rawseti(l, -3, j); lua_rawseti(l, -3, j);
} }
lua_pushnil(l); lua_pushnil(l);
lua_rawseti(l, -3, c); lua_rawseti(l, -3, len);
c--;
i--; i--;
} }
luacon_ci->Log(CommandInterface::LogError, luacon_geterror()); luacon_ci->Log(CommandInterface::LogError, luacon_geterror());
@ -534,6 +533,7 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel)
mpcontinue = lua_toboolean(l, -1); mpcontinue = lua_toboolean(l, -1);
lua_pop(l, 1); lua_pop(l, 1);
} }
len = lua_objlen(l, -1);
} }
lua_pop(l, 1); lua_pop(l, 1);
return mpcontinue; return mpcontinue;
@ -541,15 +541,14 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel)
int luacon_step(int mx, int my) int luacon_step(int mx, int my)
{ {
int i, j, callret; lua_State* l = luacon_ci->l;
lua_State* l=luacon_ci->l;
lua_pushinteger(l, my); lua_pushinteger(l, my);
lua_pushinteger(l, mx); lua_pushinteger(l, mx);
lua_setfield(l, tptProperties, "mousex"); lua_setfield(l, tptProperties, "mousex");
lua_setfield(l, tptProperties, "mousey"); lua_setfield(l, tptProperties, "mousey");
lua_pushstring(l, "stepfunctions"); lua_pushstring(l, "stepfunctions");
lua_rawget(l, LUA_REGISTRYINDEX); lua_rawget(l, LUA_REGISTRYINDEX);
if(!lua_istable(l, -1)) if (!lua_istable(l, -1))
{ {
lua_pop(l, 1); lua_pop(l, 1);
lua_newtable(l); lua_newtable(l);
@ -557,29 +556,29 @@ int luacon_step(int mx, int my)
lua_pushvalue(l, -2); lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX); lua_rawset(l, LUA_REGISTRYINDEX);
} }
int c=lua_objlen(l, -1); int len = lua_objlen(l, -1);
for(i=1;i<=c;i++) for (int i = 1; i <= len; i++)
{ {
lua_rawgeti(l, -1, i); lua_rawgeti(l, -1, i);
callret = lua_pcall(l, 0, 0, 0); int callret = lua_pcall(l, 0, 0, 0);
if (callret) if (callret)
{ {
if (!strcmp(luacon_geterror(), "Error: Script not responding")) if (!strcmp(luacon_geterror(), "Error: Script not responding"))
{ {
ui::Engine::Ref().LastTick(gettime()); ui::Engine::Ref().LastTick(gettime());
for(j=i;j<=c-1;j++) for (int j = i; j <= len-1; j++)
{ {
lua_rawgeti(l, -2, j+1); lua_rawgeti(l, -2, j+1);
lua_rawseti(l, -3, j); lua_rawseti(l, -3, j);
} }
lua_pushnil(l); lua_pushnil(l);
lua_rawseti(l, -3, c); lua_rawseti(l, -3, len);
c--;
i--; i--;
} }
luacon_ci->Log(CommandInterface::LogError, luacon_geterror()); luacon_ci->Log(CommandInterface::LogError, luacon_geterror());
lua_pop(l, 1); lua_pop(l, 1);
} }
len = lua_objlen(l, -1);
} }
lua_pop(l, 1); lua_pop(l, 1);
return 0; return 0;
@ -1538,20 +1537,23 @@ int luatpt_unregister_step(lua_State* l)
lua_pushvalue(l, -2); lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX); lua_rawset(l, LUA_REGISTRYINDEX);
} }
int c = lua_objlen(l, -1); int len = lua_objlen(l, -1);
int d = 0; int adjust = 0;
int i = 0; for (int i = 1; i <= len; i++)
for (i=1;i<=c;i++)
{ {
lua_rawgeti(l, -1, i+d); lua_rawgeti(l, -1, i+adjust);
//unregister the function
if (lua_equal(l, 1, -1)) if (lua_equal(l, 1, -1))
{ {
lua_pop(l, 1); lua_pop(l, 1);
d++; adjust++;
i--; i--;
} }
//else, move everything down if we removed something earlier
else else
{
lua_rawseti(l, -2, i); lua_rawseti(l, -2, i);
}
} }
} }
return 0; return 0;