From 3a236ca2ade7118677398d14938199c5524524c8 Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 21 Nov 2024 20:00:52 +0100 Subject: [PATCH] fix lugstuff crash --- src/lua_hooklib.c | 35 ++++++++++++++++++++++++++++++++++- src/lua_script.c | 8 ++++---- src/lua_script.h | 4 ++-- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 6e4f41d63..a2e53b14c 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -873,6 +873,7 @@ int LUA_HookHurtMsg(player_t *player, mobj_t *inflictor, mobj_t *source) return hook.status; } +#if 0 void LUA_HookNetArchive(lua_CFunction archFunc) { const hook_t * map = &hookIds[HOOK(NetVars)]; @@ -891,7 +892,7 @@ void LUA_HookNetArchive(lua_CFunction archFunc) // tables becomes an upvalue of archFunc lua_pushvalue(gL, -1); - lua_pushcclosure(gL, archFunc, 1); + lua_pushcclosure(gL, archFunc, 2); // stack: tables, archFunc init_hook_call(&hook, 0, res_none); @@ -902,6 +903,38 @@ void LUA_HookNetArchive(lua_CFunction archFunc) // stack: tables } } +#endif + +void LUA_HookNetArchive(lua_CFunction archFunc, savebuffer_t *save) // +{ + const hook_t * map = &hookIds[HOOK(NetVars)]; + Hook_State hook; + /* this is a remarkable case where the stack isn't reset */ + if (map->numHooks > 0) + { + // stack: tables + I_Assert(lua_gettop(gL) > 0); + I_Assert(lua_istable(gL, -1)); + + push_error_handler(); + lua_insert(gL, EINDEX); + + begin_hook_values(&hook); + + // tables becomes an upvalue of archFunc + lua_pushvalue(gL, -1); + lua_pushlightuserdata(gL, save); + lua_pushcclosure(gL, archFunc, 2); + // stack: tables, savebuffer_t, archFunc + + init_hook_call(&hook, 0, res_none); + call_mapped(&hook, map); + + lua_pop(gL, 1); // pop archFunc + lua_remove(gL, EINDEX); // pop error handler + // stack: tables + } +} void LUA_HookPlayerQuit(player_t *plr, int reason) { diff --git a/src/lua_script.c b/src/lua_script.c index e3c51fcd7..7cf69fb02 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -1397,8 +1397,8 @@ void LUA_Archive(savebuffer_t *save, boolean network) } WRITEUINT32(save->p, UINT32_MAX); // end of mobjs marker, replaces mobjnum. - //LUA_HookNetArchive(NetArchive, save); // call the NetArchive hook in archive mode - LUA_HookNetArchive(NetArchive); // call the NetArchive hook in archive mode + LUA_HookNetArchive(NetArchive, save); // call the NetArchive hook in archive mode + //LUA_HookNetArchive(NetArchive); // call the NetArchive hook in archive mode } ArchiveTables(&save->p); @@ -1438,8 +1438,8 @@ void LUA_UnArchive(savebuffer_t *save, boolean network) } } while(mobjnum != UINT32_MAX); // repeat until end of mobjs marker. - //LUA_HookNetArchive(NetUnArchive, save); // call the NetArchive hook in unarchive mode - LUA_HookNetArchive(NetUnArchive); // call the NetArchive hook in unarchive mode + LUA_HookNetArchive(NetUnArchive, save); // call the NetArchive hook in unarchive mode + //LUA_HookNetArchive(NetUnArchive); // call the NetArchive hook in unarchive mode } UnArchiveTables(&save->p, network); diff --git a/src/lua_script.h b/src/lua_script.h index 7fd8b82f3..2c4a31f4e 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -74,8 +74,8 @@ 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, int def, int list_ref); int Lua_CreateFieldTable(lua_State *L, const char *const lst[]); -//void LUA_HookNetArchive(lua_CFunction archFunc, savebuffer_t *save); -void LUA_HookNetArchive(lua_CFunction archFunc); +void LUA_HookNetArchive(lua_CFunction archFunc, savebuffer_t *save); +//void LUA_HookNetArchive(lua_CFunction archFunc); // Console wrapper void COM_Lua_f(void);