From 0b704ba6188ee47763ab682c64b0d43138152846 Mon Sep 17 00:00:00 2001 From: Yukita Mayako Date: Thu, 3 Mar 2016 17:07:05 -0500 Subject: [PATCH 1/5] Updated NetArchiveHook to lua_hooklib.c Fixes I_Assert failure crash due to hooks working differently now. --- src/lua_hooklib.c | 24 ++++++++++++++++++++++++ src/lua_script.c | 28 +++------------------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 0415d23e..4a3325cf 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -768,4 +768,28 @@ boolean LUAh_HurtMsg(player_t *player, mobj_t *inflictor, mobj_t *source) return hooked; } +void LUAh_NetArchiveHook(lua_CFunction archFunc) +{ + hook_p hookp; + + if (!gL || !(hooksAvailable[hook_NetVars/8] & (1<<(hook_NetVars%8)))) + return; + + // stack: tables + I_Assert(lua_gettop(gL) > 0); + I_Assert(lua_istable(gL, -1)); + + for (hookp = roothook; hookp; hookp = hookp->next) + if (hookp->type == hook_NetVars) + { + lua_pushfstring(gL, FMT_HOOKID, hookp->id); + lua_gettable(gL, LUA_REGISTRYINDEX); + lua_pushvalue(gL, -2); // tables + LUA_Call(gL, 1); + } + + // pop tables + lua_pop(gL, 1); +} + #endif diff --git a/src/lua_script.c b/src/lua_script.c index a7315ad6..2e076b02 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -915,29 +915,7 @@ static void UnArchiveTables(void) } } -static void NetArchiveHook(lua_CFunction archFunc) -{ - int TABLESINDEX; - - if (!gL) - return; - - TABLESINDEX = lua_gettop(gL); - lua_getfield(gL, LUA_REGISTRYINDEX, "hook"); - I_Assert(lua_istable(gL, -1)); - lua_rawgeti(gL, -1, hook_NetVars); - lua_remove(gL, -2); - I_Assert(lua_istable(gL, -1)); - - lua_pushvalue(gL, TABLESINDEX); - lua_pushcclosure(gL, archFunc, 1); - lua_pushnil(gL); - while (lua_next(gL, -3) != 0) { - lua_pushvalue(gL, -3); // function - LUA_Call(gL, 1); - } - lua_pop(gL, 2); -} +void LUAh_NetArchiveHook(lua_CFunction archFunc); void LUA_Step(void) { @@ -972,7 +950,7 @@ void LUA_Archive(void) } WRITEUINT32(save_p, UINT32_MAX); // end of mobjs marker, replaces mobjnum. - NetArchiveHook(NetArchive); // call the NetArchive hook in archive mode + LUAh_NetArchiveHook(NetArchive); // call the NetArchive hook in archive mode ArchiveTables(); if (gL) @@ -1003,7 +981,7 @@ void LUA_UnArchive(void) UnArchiveExtVars(th); // apply variables } while(mobjnum != UINT32_MAX); // repeat until end of mobjs marker. - NetArchiveHook(NetUnArchive); // call the NetArchive hook in unarchive mode + LUAh_NetArchiveHook(NetUnArchive); // call the NetArchive hook in unarchive mode UnArchiveTables(); if (gL) From 7ae871c7f860d2d76294a3c78379a1124d8af0e2 Mon Sep 17 00:00:00 2001 From: Yukita Mayako Date: Thu, 3 Mar 2016 17:19:21 -0500 Subject: [PATCH 2/5] Fix errenous stack pop. This function is intended to leave the stack in the same state it recieved it. --- src/lua_hooklib.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 4a3325cf..01d4314c 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -788,8 +788,7 @@ void LUAh_NetArchiveHook(lua_CFunction archFunc) LUA_Call(gL, 1); } - // pop tables - lua_pop(gL, 1); + // stack: tables } #endif From 0bdc976d50038db25386290339eb1c959d43dd04 Mon Sep 17 00:00:00 2001 From: Yukita Mayako Date: Thu, 3 Mar 2016 17:19:35 -0500 Subject: [PATCH 3/5] Shut up compiler warning. --- src/lua_hooklib.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 01d4314c..48c6df6d 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -24,6 +24,8 @@ #include "lua_hook.h" #include "lua_hud.h" // hud_running errors +void LUAh_NetArchiveHook(lua_CFunction archFunc); + static UINT8 hooksAvailable[(hook_MAX/8)+1]; const char *const hookNames[hook_MAX+1] = { From 9d6e75ae4f625cb5330b627b1777ef279e0344d9 Mon Sep 17 00:00:00 2001 From: Yukita Mayako Date: Thu, 3 Mar 2016 17:30:10 -0500 Subject: [PATCH 4/5] Cleanup LUAh_NetArchiveHook prototype mess. --- src/lua_hooklib.c | 2 -- src/lua_script.c | 2 -- src/lua_script.h | 1 + 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 48c6df6d..01d4314c 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -24,8 +24,6 @@ #include "lua_hook.h" #include "lua_hud.h" // hud_running errors -void LUAh_NetArchiveHook(lua_CFunction archFunc); - static UINT8 hooksAvailable[(hook_MAX/8)+1]; const char *const hookNames[hook_MAX+1] = { diff --git a/src/lua_script.c b/src/lua_script.c index 2e076b02..9925bac0 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -915,8 +915,6 @@ static void UnArchiveTables(void) } } -void LUAh_NetArchiveHook(lua_CFunction archFunc); - void LUA_Step(void) { if (!gL) diff --git a/src/lua_script.h b/src/lua_script.h index 292160a0..ec67703c 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -55,6 +55,7 @@ void Got_Luacmd(UINT8 **cp, INT32 playernum); // lua_consolelib.c void LUA_CVarChanged(const char *name); // lua_consolelib.c int Lua_optoption(lua_State *L, int narg, const char *def, const char *const lst[]); +void LUAh_NetArchiveHook(lua_CFunction archFunc); // Console wrapper void COM_Lua_f(void); From b368936b03b3e65e7cb8c5fdf417660806fc05cc Mon Sep 17 00:00:00 2001 From: Yukita Mayako Date: Thu, 3 Mar 2016 17:30:46 -0500 Subject: [PATCH 5/5] Fix bad logic in LUAh_NetArchiveHook rewrite... Argh, I knew I was forgetting something! archFunc is the argument to be passed to the hooks, not tables! --- src/lua_hooklib.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 01d4314c..5230886a 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -779,15 +779,21 @@ void LUAh_NetArchiveHook(lua_CFunction archFunc) I_Assert(lua_gettop(gL) > 0); I_Assert(lua_istable(gL, -1)); + // tables becomes an upvalue of archFunc + lua_pushvalue(gL, -1); + lua_pushcclosure(gL, archFunc, 1); + // stack: tables, archFunc + for (hookp = roothook; hookp; hookp = hookp->next) if (hookp->type == hook_NetVars) { lua_pushfstring(gL, FMT_HOOKID, hookp->id); lua_gettable(gL, LUA_REGISTRYINDEX); - lua_pushvalue(gL, -2); // tables + lua_pushvalue(gL, -2); // archFunc LUA_Call(gL, 1); } + lua_pop(gL, 1); // pop archFunc // stack: tables }