diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 22aca4b35..1ab357996 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -5798,7 +5798,7 @@ static void Command_Archivetest_f(void) // test archive CONS_Printf("LUA_Archive...\n"); - LUA_Archive(&save.p, true); + LUA_Archive(&save, true); WRITEUINT8(save.p, 0x7F); wrote = (UINT32)(save.p - save.buffer); @@ -5809,7 +5809,7 @@ static void Command_Archivetest_f(void) // test unarchive save.p = save.buffer; CONS_Printf("LUA_UnArchive...\n"); - LUA_UnArchive(&save.p, true); + LUA_UnArchive(&save, true); i = READUINT8(save.p); if (i != 0x7F || wrote != (UINT32)(save.p - save.buffer)) CONS_Printf("Savegame corrupted. (write %u, read %u)\n", wrote, (UINT32)(save.p - save.buffer)); diff --git a/src/g_demo.c b/src/g_demo.c index 57dfdc9fa..95353790e 100644 --- a/src/g_demo.c +++ b/src/g_demo.c @@ -2502,7 +2502,7 @@ void G_BeginRecording(void) // player lua vars, always saved even if empty if (demoflags & DF_LUAVARS) - LUA_Archive(&demobuf.p, false); + LUA_Archive(&demobuf, false); memset(&oldcmd,0,sizeof(oldcmd)); memset(&oldghost,0,sizeof(oldghost)); @@ -3366,7 +3366,7 @@ void G_DoPlayDemo(char *defdemoname) LUA_ClearState(); // No modeattacking check, DF_LUAVARS won't be present here. - LUA_UnArchive(&demobuf.p, false); + LUA_UnArchive(&demobuf, false); } splitscreen = 0; diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 142d01490..fd222cdd1 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -834,7 +834,7 @@ int LUA_HookHurtMsg(player_t *player, mobj_t *inflictor, mobj_t *source, UINT8 d return hook.status; } -void LUA_HookNetArchive(lua_CFunction archFunc) +void LUA_HookNetArchive(lua_CFunction archFunc, savebuffer_t *save) { const hook_t * map = &hookIds[HOOK(NetVars)]; Hook_State hook; @@ -852,8 +852,9 @@ void LUA_HookNetArchive(lua_CFunction archFunc) // tables becomes an upvalue of archFunc lua_pushvalue(gL, -1); - lua_pushcclosure(gL, archFunc, 1); - // stack: tables, archFunc + 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); diff --git a/src/lua_script.c b/src/lua_script.c index 5f12ab416..3a6d53c98 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -41,8 +41,6 @@ lua_State *gL = NULL; int hook_defrosting; -static UINT8 **lua_save_p = NULL; // FIXME: Remove this horrible hack - // List of internal libraries to load from SRB2 static lua_CFunction liblist[] = { LUA_EnumLib, // global metatable for enums @@ -1355,9 +1353,10 @@ static void ArchiveExtVars(UINT8 **p, void *pointer, const char *ptype) static int NetArchive(lua_State *L) { int TABLESINDEX = lua_upvalueindex(1); + savebuffer_t *save = lua_touserdata(L, lua_upvalueindex(2)); int i, n = lua_gettop(L); for (i = 1; i <= n; i++) - ArchiveValue(lua_save_p, TABLESINDEX, i); + ArchiveValue(&save->p, TABLESINDEX, i); return n; } @@ -1568,9 +1567,10 @@ static void UnArchiveExtVars(UINT8 **p, void *pointer) static int NetUnArchive(lua_State *L) { int TABLESINDEX = lua_upvalueindex(1); + savebuffer_t *save = lua_touserdata(L, lua_upvalueindex(2)); int i, n = lua_gettop(L); for (i = 1; i <= n; i++) - UnArchiveValue(lua_save_p, TABLESINDEX); + UnArchiveValue(&save->p, TABLESINDEX); return n; } @@ -1628,7 +1628,7 @@ void LUA_Step(void) lua_gc(gL, LUA_GCSTEP, 1); } -void LUA_Archive(UINT8 **p, boolean network) +void LUA_Archive(savebuffer_t *save, boolean network) { INT32 i; thinker_t *th; @@ -1641,7 +1641,7 @@ void LUA_Archive(UINT8 **p, boolean network) if (!playeringame[i] && i > 0) // NEVER skip player 0, this is for dedi servs. continue; // all players in game will be archived, even if they just add a 0. - ArchiveExtVars(p, &players[i], "player"); + ArchiveExtVars(&save->p, &players[i], "player"); } if (network == true) @@ -1655,23 +1655,22 @@ void LUA_Archive(UINT8 **p, boolean network) // archive function will determine when to skip mobjs, // and write mobjnum in otherwise. - ArchiveExtVars(p, th, "mobj"); + ArchiveExtVars(&save->p, th, "mobj"); } } - WRITEUINT32(*p, UINT32_MAX); // end of mobjs marker, replaces mobjnum. + WRITEUINT32(save->p, UINT32_MAX); // end of mobjs marker, replaces mobjnum. - lua_save_p = p; - LUA_HookNetArchive(NetArchive); // call the NetArchive hook in archive mode + LUA_HookNetArchive(NetArchive, save); // call the NetArchive hook in archive mode } - ArchiveTables(p); + ArchiveTables(&save->p); if (gL) lua_pop(gL, 1); // pop tables } -void LUA_UnArchive(UINT8 **p, boolean network) +void LUA_UnArchive(savebuffer_t *save, boolean network) { UINT32 mobjnum; INT32 i; @@ -1684,28 +1683,27 @@ void LUA_UnArchive(UINT8 **p, boolean network) { if (!playeringame[i] && i > 0) // same here, this is to synch dediservs properly. continue; - UnArchiveExtVars(p, &players[i]); + UnArchiveExtVars(&save->p, &players[i]); } if (network == true) { do { - mobjnum = READUINT32(*p); // read a mobjnum + mobjnum = READUINT32(save->p); // read a mobjnum for (th = thlist[THINK_MOBJ].next; th != &thlist[THINK_MOBJ]; th = th->next) { if (th->function.acp1 == (actionf_p1)P_RemoveThinkerDelayed) continue; if (((mobj_t *)th)->mobjnum != mobjnum) // find matching mobj continue; - UnArchiveExtVars(p, th); // apply variables + UnArchiveExtVars(&save->p, th); // apply variables } } while(mobjnum != UINT32_MAX); // repeat until end of mobjs marker. - lua_save_p = p; - LUA_HookNetArchive(NetUnArchive); // call the NetArchive hook in unarchive mode + LUA_HookNetArchive(NetUnArchive, save); // call the NetArchive hook in unarchive mode } - UnArchiveTables(p); + UnArchiveTables(&save->p); if (gL) lua_pop(gL, 1); // pop tables diff --git a/src/lua_script.h b/src/lua_script.h index bab4345fd..2d19a5a04 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -53,8 +53,8 @@ void LUA_DumpFile(const char *filename); #endif fixed_t LUA_EvalMath(const char *word); void LUA_Step(void); -void LUA_Archive(UINT8 **p, boolean network); -void LUA_UnArchive(UINT8 **p, boolean network); +void LUA_Archive(savebuffer_t *save, boolean network); +void LUA_UnArchive(savebuffer_t *save, boolean network); int LUA_PushGlobals(lua_State *L, const char *word); int LUA_WriteGlobals(lua_State *L, const char *word); @@ -63,7 +63,7 @@ void Got_Luacmd(UINT8 **cp, INT32 playernum); // lua_consolelib.c void LUA_CVarChanged(void *cvar); // lua_consolelib.c int Lua_optoption(lua_State *L, int narg, const char *def, const char *const lst[]); -void LUA_HookNetArchive(lua_CFunction archFunc); +void LUA_HookNetArchive(lua_CFunction archFunc, savebuffer_t *save); void LUA_PushTaggableObjectArray ( lua_State *L, diff --git a/src/p_saveg.c b/src/p_saveg.c index ed009c189..562616b63 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -5164,7 +5164,7 @@ void P_SaveNetGame(savebuffer_t *save, boolean resending) P_NetArchiveTubeWaypoints(save); P_NetArchiveWaypoints(save); } - LUA_Archive(&save->p, true); + LUA_Archive(save, true); P_NetArchiveRNG(save); @@ -5214,7 +5214,7 @@ boolean P_LoadNetGame(savebuffer_t *save, boolean reloading) P_FinishMobjs(); } - LUA_UnArchive(&save->p, true); + LUA_UnArchive(save, true); P_NetUnArchiveRNG(save);