Remove global lua_save_p and use savebuffer_t for LUA_Archive / LUA_UnArchive

This commit is contained in:
James R 2022-12-25 14:11:38 -08:00
parent ea41f91e7f
commit 842f375920
6 changed files with 29 additions and 30 deletions

View file

@ -5798,7 +5798,7 @@ static void Command_Archivetest_f(void)
// test archive // test archive
CONS_Printf("LUA_Archive...\n"); CONS_Printf("LUA_Archive...\n");
LUA_Archive(&save.p, true); LUA_Archive(&save, true);
WRITEUINT8(save.p, 0x7F); WRITEUINT8(save.p, 0x7F);
wrote = (UINT32)(save.p - save.buffer); wrote = (UINT32)(save.p - save.buffer);
@ -5809,7 +5809,7 @@ static void Command_Archivetest_f(void)
// test unarchive // test unarchive
save.p = save.buffer; save.p = save.buffer;
CONS_Printf("LUA_UnArchive...\n"); CONS_Printf("LUA_UnArchive...\n");
LUA_UnArchive(&save.p, true); LUA_UnArchive(&save, true);
i = READUINT8(save.p); i = READUINT8(save.p);
if (i != 0x7F || wrote != (UINT32)(save.p - save.buffer)) if (i != 0x7F || wrote != (UINT32)(save.p - save.buffer))
CONS_Printf("Savegame corrupted. (write %u, read %u)\n", wrote, (UINT32)(save.p - save.buffer)); CONS_Printf("Savegame corrupted. (write %u, read %u)\n", wrote, (UINT32)(save.p - save.buffer));

View file

@ -2502,7 +2502,7 @@ void G_BeginRecording(void)
// player lua vars, always saved even if empty // player lua vars, always saved even if empty
if (demoflags & DF_LUAVARS) if (demoflags & DF_LUAVARS)
LUA_Archive(&demobuf.p, false); LUA_Archive(&demobuf, false);
memset(&oldcmd,0,sizeof(oldcmd)); memset(&oldcmd,0,sizeof(oldcmd));
memset(&oldghost,0,sizeof(oldghost)); memset(&oldghost,0,sizeof(oldghost));
@ -3366,7 +3366,7 @@ void G_DoPlayDemo(char *defdemoname)
LUA_ClearState(); LUA_ClearState();
// No modeattacking check, DF_LUAVARS won't be present here. // No modeattacking check, DF_LUAVARS won't be present here.
LUA_UnArchive(&demobuf.p, false); LUA_UnArchive(&demobuf, false);
} }
splitscreen = 0; splitscreen = 0;

View file

@ -834,7 +834,7 @@ int LUA_HookHurtMsg(player_t *player, mobj_t *inflictor, mobj_t *source, UINT8 d
return hook.status; 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)]; const hook_t * map = &hookIds[HOOK(NetVars)];
Hook_State hook; Hook_State hook;
@ -852,8 +852,9 @@ void LUA_HookNetArchive(lua_CFunction archFunc)
// tables becomes an upvalue of archFunc // tables becomes an upvalue of archFunc
lua_pushvalue(gL, -1); lua_pushvalue(gL, -1);
lua_pushcclosure(gL, archFunc, 1); lua_pushlightuserdata(gL, save);
// stack: tables, archFunc lua_pushcclosure(gL, archFunc, 2);
// stack: tables, savebuffer_t, archFunc
init_hook_call(&hook, 0, res_none); init_hook_call(&hook, 0, res_none);
call_mapped(&hook, map); call_mapped(&hook, map);

View file

@ -41,8 +41,6 @@ lua_State *gL = NULL;
int hook_defrosting; int hook_defrosting;
static UINT8 **lua_save_p = NULL; // FIXME: Remove this horrible hack
// List of internal libraries to load from SRB2 // List of internal libraries to load from SRB2
static lua_CFunction liblist[] = { static lua_CFunction liblist[] = {
LUA_EnumLib, // global metatable for enums 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) static int NetArchive(lua_State *L)
{ {
int TABLESINDEX = lua_upvalueindex(1); int TABLESINDEX = lua_upvalueindex(1);
savebuffer_t *save = lua_touserdata(L, lua_upvalueindex(2));
int i, n = lua_gettop(L); int i, n = lua_gettop(L);
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
ArchiveValue(lua_save_p, TABLESINDEX, i); ArchiveValue(&save->p, TABLESINDEX, i);
return n; return n;
} }
@ -1568,9 +1567,10 @@ static void UnArchiveExtVars(UINT8 **p, void *pointer)
static int NetUnArchive(lua_State *L) static int NetUnArchive(lua_State *L)
{ {
int TABLESINDEX = lua_upvalueindex(1); int TABLESINDEX = lua_upvalueindex(1);
savebuffer_t *save = lua_touserdata(L, lua_upvalueindex(2));
int i, n = lua_gettop(L); int i, n = lua_gettop(L);
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
UnArchiveValue(lua_save_p, TABLESINDEX); UnArchiveValue(&save->p, TABLESINDEX);
return n; return n;
} }
@ -1628,7 +1628,7 @@ void LUA_Step(void)
lua_gc(gL, LUA_GCSTEP, 1); lua_gc(gL, LUA_GCSTEP, 1);
} }
void LUA_Archive(UINT8 **p, boolean network) void LUA_Archive(savebuffer_t *save, boolean network)
{ {
INT32 i; INT32 i;
thinker_t *th; 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. if (!playeringame[i] && i > 0) // NEVER skip player 0, this is for dedi servs.
continue; continue;
// all players in game will be archived, even if they just add a 0. // 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) if (network == true)
@ -1655,23 +1655,22 @@ void LUA_Archive(UINT8 **p, boolean network)
// archive function will determine when to skip mobjs, // archive function will determine when to skip mobjs,
// and write mobjnum in otherwise. // 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, save); // call the NetArchive hook in archive mode
LUA_HookNetArchive(NetArchive); // call the NetArchive hook in archive mode
} }
ArchiveTables(p); ArchiveTables(&save->p);
if (gL) if (gL)
lua_pop(gL, 1); // pop tables lua_pop(gL, 1); // pop tables
} }
void LUA_UnArchive(UINT8 **p, boolean network) void LUA_UnArchive(savebuffer_t *save, boolean network)
{ {
UINT32 mobjnum; UINT32 mobjnum;
INT32 i; 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. if (!playeringame[i] && i > 0) // same here, this is to synch dediservs properly.
continue; continue;
UnArchiveExtVars(p, &players[i]); UnArchiveExtVars(&save->p, &players[i]);
} }
if (network == true) if (network == true)
{ {
do { 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) for (th = thlist[THINK_MOBJ].next; th != &thlist[THINK_MOBJ]; th = th->next)
{ {
if (th->function.acp1 == (actionf_p1)P_RemoveThinkerDelayed) if (th->function.acp1 == (actionf_p1)P_RemoveThinkerDelayed)
continue; continue;
if (((mobj_t *)th)->mobjnum != mobjnum) // find matching mobj if (((mobj_t *)th)->mobjnum != mobjnum) // find matching mobj
continue; continue;
UnArchiveExtVars(p, th); // apply variables UnArchiveExtVars(&save->p, th); // apply variables
} }
} while(mobjnum != UINT32_MAX); // repeat until end of mobjs marker. } while(mobjnum != UINT32_MAX); // repeat until end of mobjs marker.
lua_save_p = p; LUA_HookNetArchive(NetUnArchive, save); // call the NetArchive hook in unarchive mode
LUA_HookNetArchive(NetUnArchive); // call the NetArchive hook in unarchive mode
} }
UnArchiveTables(p); UnArchiveTables(&save->p);
if (gL) if (gL)
lua_pop(gL, 1); // pop tables lua_pop(gL, 1); // pop tables

View file

@ -53,8 +53,8 @@ void LUA_DumpFile(const char *filename);
#endif #endif
fixed_t LUA_EvalMath(const char *word); fixed_t LUA_EvalMath(const char *word);
void LUA_Step(void); void LUA_Step(void);
void LUA_Archive(UINT8 **p, boolean network); void LUA_Archive(savebuffer_t *save, boolean network);
void LUA_UnArchive(UINT8 **p, boolean network); void LUA_UnArchive(savebuffer_t *save, boolean network);
int LUA_PushGlobals(lua_State *L, const char *word); int LUA_PushGlobals(lua_State *L, const char *word);
int LUA_WriteGlobals(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 void LUA_CVarChanged(void *cvar); // lua_consolelib.c
int Lua_optoption(lua_State *L, int narg, int Lua_optoption(lua_State *L, int narg,
const char *def, const char *const lst[]); 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 void LUA_PushTaggableObjectArray
( lua_State *L, ( lua_State *L,

View file

@ -5164,7 +5164,7 @@ void P_SaveNetGame(savebuffer_t *save, boolean resending)
P_NetArchiveTubeWaypoints(save); P_NetArchiveTubeWaypoints(save);
P_NetArchiveWaypoints(save); P_NetArchiveWaypoints(save);
} }
LUA_Archive(&save->p, true); LUA_Archive(save, true);
P_NetArchiveRNG(save); P_NetArchiveRNG(save);
@ -5214,7 +5214,7 @@ boolean P_LoadNetGame(savebuffer_t *save, boolean reloading)
P_FinishMobjs(); P_FinishMobjs();
} }
LUA_UnArchive(&save->p, true); LUA_UnArchive(save, true);
P_NetUnArchiveRNG(save); P_NetUnArchiveRNG(save);