Merge pull request 'Refactor Netsaves' (#160) from refactornetsave into blankart-dev

Reviewed-on: https://codeberg.org/NepDisk/blankart/pulls/160
This commit is contained in:
NepDisk 2025-10-07 19:02:45 +02:00
commit 9f8af1a7dd
6 changed files with 3166 additions and 4711 deletions

View file

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

View file

@ -45,7 +45,7 @@
// SRB2Kart // SRB2Kart
#include "d_netfil.h" // nameonly #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 "lua_libs.h" // gL (Lua state)
#include "k_kart.h" #include "k_kart.h"
@ -2940,7 +2940,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, false); LUA_Sync(&demobuf, false, false);
memset(&oldcmd,0,sizeof(oldcmd)); memset(&oldcmd,0,sizeof(oldcmd));
memset(&oldghost,0,sizeof(oldghost)); memset(&oldghost,0,sizeof(oldghost));
@ -3793,7 +3793,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, false, G_CompatLevel(0x0002)); LUA_Sync(&demobuf, false, G_CompatLevel(0x0002));
} }
splitscreen = 0; 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; INT32 i;
thinker_t *th; 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. 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(&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) for (th = thlist[THINK_MOBJ].next; th != &thlist[THINK_MOBJ]; th = th->next)
continue; {
if (th->function.acp1 == (actionf_p1)P_RemoveThinkerDelayed)
continue;
// 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(&save->p, th, "mobj"); 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);
} }
else
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++)
{ {
if (!playeringame[i] && i > 0) // same here, this is to synch dediservs properly. if (network == true)
continue; {
UnArchiveExtVars(&save->p, &players[i], compat); 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) if (gL)
lua_pop(gL, 1); // pop tables lua_pop(gL, 1); // pop tables
} }

View file

@ -61,8 +61,7 @@ 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(savebuffer_t *save, boolean network); void LUA_Sync(savebuffer_t *save, boolean network, boolean compat);
void LUA_UnArchive(savebuffer_t *save, boolean network, boolean compat);
void LUA_SetCFunctionField(lua_State *L, const char *name, lua_CFunction value); void LUA_SetCFunctionField(lua_State *L, const char *name, lua_CFunction value);

File diff suppressed because it is too large Load diff

View file

@ -51,6 +51,7 @@ struct savebuffer_t
UINT8 *p; UINT8 *p;
UINT8 *end; UINT8 *end;
size_t size; size_t size;
boolean write;
}; };
boolean P_SaveBufferZAlloc(savebuffer_t *save, size_t alloc_size, INT32 tag, void *user); boolean P_SaveBufferZAlloc(savebuffer_t *save, size_t alloc_size, INT32 tag, void *user);