Combine LUA_Archive and LUA_Unarchive

This commit is contained in:
NepDisk 2025-10-07 12:35:33 -04:00
parent 0d7d460cb8
commit 151bb79d85
5 changed files with 54 additions and 63 deletions

View file

@ -6441,8 +6441,9 @@ static void Command_Archivetest_f(void)
}
// test archive
CONS_Printf("LUA_Archive...\n");
LUA_Archive(&save, true);
CONS_Printf("LUA_Sync Archive...\n");
save.write = true;
LUA_Sync(&save, true, false);
WRITEUINT8(save.p, 0x7F);
wrote = (UINT32)(save.p - save.buffer);
@ -6452,8 +6453,9 @@ static void Command_Archivetest_f(void)
// test unarchive
save.p = save.buffer;
CONS_Printf("LUA_UnArchive...\n");
LUA_UnArchive(&save, true, false);
save.write = false;
CONS_Printf("LUA_Sync Unarchive...\n");
LUA_Sync(&save, true, false);
i = READUINT8(save.p);
if (i != 0x7F || wrote != (UINT32)(save.p - save.buffer))
{

View file

@ -45,7 +45,7 @@
// SRB2Kart
#include "d_netfil.h" // nameonly
#include "lua_script.h" // LUA_ArchiveDemo and LUA_UnArchiveDemo
#include "lua_script.h" // LUA_Sync
#include "lua_libs.h" // gL (Lua state)
#include "k_kart.h"
@ -2940,7 +2940,7 @@ void G_BeginRecording(void)
// player lua vars, always saved even if empty
if (demoflags & DF_LUAVARS)
LUA_Archive(&demobuf, false);
LUA_Sync(&demobuf, false, false);
memset(&oldcmd,0,sizeof(oldcmd));
memset(&oldghost,0,sizeof(oldghost));
@ -3793,7 +3793,7 @@ void G_DoPlayDemo(char *defdemoname)
LUA_ClearState();
// No modeattacking check, DF_LUAVARS won't be present here.
LUA_UnArchive(&demobuf, false, G_CompatLevel(0x0002));
LUA_Sync(&demobuf, false, G_CompatLevel(0x0002));
}
splitscreen = 0;

View file

@ -1846,8 +1846,9 @@ void LUA_Step(void)
}
}
void LUA_Archive(savebuffer_t *save, boolean network)
void LUA_Sync(savebuffer_t *save, boolean network, boolean compat)
{
UINT32 mobjnum;
INT32 i;
thinker_t *th;
@ -1858,71 +1859,60 @@ void LUA_Archive(savebuffer_t *save, 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(&save->p, &players[i], "player");
if (save->write)
ArchiveExtVars(&save->p, &players[i], "player");
else
UnArchiveExtVars(&save->p, &players[i], compat);
}
if (network == true)
if (save->write)
{
if (gamestate == GS_LEVEL)
if (network == true)
{
for (th = thlist[THINK_MOBJ].next; th != &thlist[THINK_MOBJ]; th = th->next)
if (gamestate == GS_LEVEL)
{
if (th->function.acp1 == (actionf_p1)P_RemoveThinkerDelayed)
continue;
for (th = thlist[THINK_MOBJ].next; th != &thlist[THINK_MOBJ]; th = th->next)
{
if (th->function.acp1 == (actionf_p1)P_RemoveThinkerDelayed)
continue;
// archive function will determine when to skip mobjs,
// and write mobjnum in otherwise.
ArchiveExtVars(&save->p, th, "mobj");
// archive function will determine when to skip mobjs,
// and write mobjnum in otherwise.
ArchiveExtVars(&save->p, th, "mobj");
}
}
WRITEUINT32(save->p, UINT32_MAX); // end of mobjs marker, replaces mobjnum.
LUA_HookNetArchive(NetArchive, save); // call the NetArchive hook in archive mode
}
WRITEUINT32(save->p, UINT32_MAX); // end of mobjs marker, replaces mobjnum.
LUA_HookNetArchive(NetArchive, save); // call the NetArchive hook in archive mode
ArchiveTables(&save->p);
}
ArchiveTables(&save->p);
if (gL)
lua_pop(gL, 1); // pop tables
}
void LUA_UnArchive(savebuffer_t *save, boolean network, boolean compat)
{
UINT32 mobjnum;
INT32 i;
thinker_t *th;
if (gL)
lua_newtable(gL); // tables to be read
for (i = 0; i < MAXPLAYERS; i++)
else
{
if (!playeringame[i] && i > 0) // same here, this is to synch dediservs properly.
continue;
UnArchiveExtVars(&save->p, &players[i], compat);
if (network == true)
{
do {
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(&save->p, th, false); // apply variables
}
} while(mobjnum != UINT32_MAX); // repeat until end of mobjs marker.
LUA_HookNetArchive(NetUnArchive, save); // call the NetArchive hook in unarchive mode
}
UnArchiveTables(&save->p, compat);
}
if (network == true)
{
do {
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(&save->p, th, false); // apply variables
}
} while(mobjnum != UINT32_MAX); // repeat until end of mobjs marker.
LUA_HookNetArchive(NetUnArchive, save); // call the NetArchive hook in unarchive mode
}
UnArchiveTables(&save->p, compat);
if (gL)
lua_pop(gL, 1); // pop tables
}

View file

@ -61,8 +61,7 @@ void LUA_DumpFile(const char *filename);
#endif
fixed_t LUA_EvalMath(const char *word);
void LUA_Step(void);
void LUA_Archive(savebuffer_t *save, boolean network);
void LUA_UnArchive(savebuffer_t *save, boolean network, boolean compat);
void LUA_Sync(savebuffer_t *save, boolean network, boolean compat);
void LUA_SetCFunctionField(lua_State *L, const char *name, lua_CFunction value);

View file

@ -4476,7 +4476,7 @@ void P_SaveNetGame(savebuffer_t *save, boolean resending)
}
ACS_Archive(save);
LUA_Archive(save, true);
LUA_Sync(save, true, false);
P_ArchiveLuabanksAndConsistency(save);
@ -4529,7 +4529,7 @@ boolean P_LoadNetGame(savebuffer_t *save, boolean reloading)
}
ACS_UnArchive(save);
LUA_UnArchive(save, true, false);
LUA_Sync(save, true, false);
// This is stupid and hacky, but maybe it'll work!
P_SetRandSeed(P_GetInitSeed());