Skip to content

Commit

Permalink
reset length when running step functions in case a step was unregiste…
Browse files Browse the repository at this point in the history
…red, fixes first half of #210

also cleanup / make it easier to read
  • Loading branch information
jacob1 committed Jan 11, 2015
1 parent e92bbac commit a5797f6
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions src/lua/LegacyLuaAPI.cpp
Expand Up @@ -436,7 +436,7 @@ int luacon_elementwrite(lua_State* l)
bool shortcuts = true;
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_pushstring(l, "keyfunctions");
lua_rawget(l, LUA_REGISTRYINDEX);
Expand All @@ -448,28 +448,27 @@ int luacon_keyevent(int key, int modifier, int event)
lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX);
}
int c=lua_objlen(l, -1);
for(i=1;i<=c && kycontinue;i++)
int len = lua_objlen(l, -1);
for (int i = 1; i <= len && kycontinue; i++)
{
lua_rawgeti(l, -1, i);
lua_pushlstring(l, (const char*)&key, 1);
lua_pushinteger(l, key);
lua_pushinteger(l, modifier);
lua_pushinteger(l, event);
callret = lua_pcall(l, 4, 1, 0);
int callret = lua_pcall(l, 4, 1, 0);
if (callret)
{
if (!strcmp(luacon_geterror(), "Error: Script not responding"))
{
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_rawseti(l, -3, j);
}
lua_pushnil(l);
lua_rawseti(l, -3, c);
c--;
lua_rawseti(l, -3, len);
i--;
}
luacon_ci->Log(CommandInterface::LogError, luacon_geterror());
Expand All @@ -481,14 +480,15 @@ int luacon_keyevent(int key, int modifier, int event)
kycontinue = lua_toboolean(l, -1);
lua_pop(l, 1);
}
len = lua_objlen(l, -1);
}
lua_pop(l, 1);
return kycontinue && shortcuts;
}

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_pushstring(l, "mousefunctions");
lua_rawget(l, LUA_REGISTRYINDEX);
Expand All @@ -500,29 +500,28 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel)
lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX);
}
int c=lua_objlen(l, -1);
for(i=1;i<=c && mpcontinue;i++)
int len = lua_objlen(l, -1);
for (int i = 1; i <= len && mpcontinue; i++)
{
lua_rawgeti(l, -1, i);
lua_pushinteger(l, mx);
lua_pushinteger(l, my);
lua_pushinteger(l, mb);
lua_pushinteger(l, event);
lua_pushinteger(l, mouse_wheel);
callret = lua_pcall(l, 5, 1, 0);
int callret = lua_pcall(l, 5, 1, 0);
if (callret)
{
if (!strcmp(luacon_geterror(), "Error: Script not responding"))
{
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_rawseti(l, -3, j);
}
lua_pushnil(l);
lua_rawseti(l, -3, c);
c--;
lua_rawseti(l, -3, len);
i--;
}
luacon_ci->Log(CommandInterface::LogError, luacon_geterror());
Expand All @@ -534,52 +533,52 @@ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel)
mpcontinue = lua_toboolean(l, -1);
lua_pop(l, 1);
}
len = lua_objlen(l, -1);
}
lua_pop(l, 1);
return mpcontinue;
}

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, mx);
lua_setfield(l, tptProperties, "mousex");
lua_setfield(l, tptProperties, "mousey");
lua_pushstring(l, "stepfunctions");
lua_rawget(l, LUA_REGISTRYINDEX);
if(!lua_istable(l, -1))
if (!lua_istable(l, -1))
{
lua_pop(l, 1);
lua_newtable(l);
lua_pushstring(l, "stepfunctions");
lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX);
}
int c=lua_objlen(l, -1);
for(i=1;i<=c;i++)
int len = lua_objlen(l, -1);
for (int i = 1; i <= len; i++)
{
lua_rawgeti(l, -1, i);
callret = lua_pcall(l, 0, 0, 0);
int callret = lua_pcall(l, 0, 0, 0);
if (callret)
{
if (!strcmp(luacon_geterror(), "Error: Script not responding"))
{
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_rawseti(l, -3, j);
}
lua_pushnil(l);
lua_rawseti(l, -3, c);
c--;
lua_rawseti(l, -3, len);
i--;
}
luacon_ci->Log(CommandInterface::LogError, luacon_geterror());
lua_pop(l, 1);
}
len = lua_objlen(l, -1);
}
lua_pop(l, 1);
return 0;
Expand Down Expand Up @@ -1538,20 +1537,23 @@ int luatpt_unregister_step(lua_State* l)
lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX);
}
int c = lua_objlen(l, -1);
int d = 0;
int i = 0;
for (i=1;i<=c;i++)
int len = lua_objlen(l, -1);
int adjust = 0;
for (int i = 1; i <= len; i++)
{
lua_rawgeti(l, -1, i+d);
lua_rawgeti(l, -1, i+adjust);
//unregister the function
if (lua_equal(l, 1, -1))
{
lua_pop(l, 1);
d++;
adjust++;
i--;
}
//else, move everything down if we removed something earlier
else
{
lua_rawseti(l, -2, i);
}
}
}
return 0;
Expand Down

0 comments on commit a5797f6

Please sign in to comment.