Pass save_t to NetVar save/load function

This commit is contained in:
Hanicef 2024-08-25 12:24:03 +02:00
parent 0cf3ce91e9
commit 7061c30744
5 changed files with 65 additions and 46 deletions

View file

@ -1722,8 +1722,7 @@ badinput:
static boolean serverloading = false; static boolean serverloading = false;
static consvar_t * static consvar_t *ReadNetVar(save_t *p, char **return_value, boolean *return_stealth)
ReadNetVar (UINT8 **p, char **return_value, boolean *return_stealth)
{ {
UINT16 netid; UINT16 netid;
char *val; char *val;
@ -1731,10 +1730,10 @@ ReadNetVar (UINT8 **p, char **return_value, boolean *return_stealth)
consvar_t *cvar; consvar_t *cvar;
netid = READUINT16 (*p); netid = P_ReadUINT16(p);
val = (char *)*p; val = (char *)&p->buf[p->pos];
SKIPSTRING (*p); P_SkipString(p);
stealth = READUINT8 (*p); stealth = P_ReadUINT8(p);
cvar = CV_FindNetVar(netid); cvar = CV_FindNetVar(netid);
@ -1752,8 +1751,7 @@ ReadNetVar (UINT8 **p, char **return_value, boolean *return_stealth)
} }
#ifdef OLD22DEMOCOMPAT #ifdef OLD22DEMOCOMPAT
static consvar_t * static consvar_t *ReadOldDemoVar(save_t *p, char **return_value, boolean *return_stealth)
ReadOldDemoVar (UINT8 **p, char **return_value, boolean *return_stealth)
{ {
UINT16 id; UINT16 id;
char *val; char *val;
@ -1761,10 +1759,10 @@ ReadOldDemoVar (UINT8 **p, char **return_value, boolean *return_stealth)
old_demo_var_t *demovar; old_demo_var_t *demovar;
id = READUINT16 (*p); id = P_ReadUINT16(p);
val = (char *)*p; val = (char *)&p->buf[p->pos];
SKIPSTRING (*p); P_SkipString(p);
stealth = READUINT8 (*p); stealth = P_ReadUINT8(p);
demovar = CV_FindOldDemoVar(id); demovar = CV_FindOldDemoVar(id);
@ -1783,8 +1781,7 @@ ReadOldDemoVar (UINT8 **p, char **return_value, boolean *return_stealth)
} }
#endif/*OLD22DEMOCOMPAT*/ #endif/*OLD22DEMOCOMPAT*/
static consvar_t * static consvar_t *ReadDemoVar(save_t *p, char **return_value, boolean *return_stealth)
ReadDemoVar (UINT8 **p, char **return_value, boolean *return_stealth)
{ {
char *name; char *name;
char *val; char *val;
@ -1792,11 +1789,11 @@ ReadDemoVar (UINT8 **p, char **return_value, boolean *return_stealth)
consvar_t *cvar; consvar_t *cvar;
name = (char *)*p; name = (char *)&p->buf[p->pos];
SKIPSTRING (*p); P_SkipString(p);
val = (char *)*p; val = (char *)&p->buf[p->pos];
SKIPSTRING (*p); P_SkipString(p);
stealth = READUINT8 (*p); stealth = P_ReadUINT8(p);
cvar = CV_FindVar(name); cvar = CV_FindVar(name);
@ -1826,41 +1823,46 @@ static void Got_NetVar(UINT8 **p, INT32 playernum)
return; return;
} }
cvar = ReadNetVar(p, &svalue, &stealth); save_t save_p;
save_p.buf = *p;
save_p.size = MAXTEXTCMD;
save_p.pos = 0;
cvar = ReadNetVar(&save_p, &svalue, &stealth);
*p = &save_p.buf[save_p.pos];
if (cvar) if (cvar)
Setvalue(cvar, svalue, stealth); Setvalue(cvar, svalue, stealth);
} }
void CV_SaveVars(UINT8 **p, boolean in_demo) void CV_SaveVars(save_t *p, boolean in_demo)
{ {
consvar_t *cvar; consvar_t *cvar;
UINT8 *count_p = *p; UINT8 *count_p = &p->buf[p->pos];
UINT16 count = 0; UINT16 count = 0;
// send only changed cvars ... // send only changed cvars ...
// the client will reset all netvars to default before loading // the client will reset all netvars to default before loading
WRITEUINT16(*p, 0x0000); P_WriteUINT16(p, 0x0000);
for (cvar = consvar_vars; cvar; cvar = cvar->next) for (cvar = consvar_vars; cvar; cvar = cvar->next)
if ((cvar->flags & CV_NETVAR) && !CV_IsSetToDefault(cvar)) if ((cvar->flags & CV_NETVAR) && !CV_IsSetToDefault(cvar))
{ {
if (in_demo) if (in_demo)
{ {
WRITESTRING(*p, cvar->name); P_WriteString(p, cvar->name);
} }
else else
{ {
WRITEUINT16(*p, cvar->netid); P_WriteUINT16(p, cvar->netid);
} }
WRITESTRING(*p, cvar->string); P_WriteString(p, cvar->string);
WRITEUINT8(*p, false); P_WriteUINT8(p, false);
++count; ++count;
} }
WRITEUINT16(count_p, count); WRITEUINT16(count_p, count);
} }
static void CV_LoadVars(UINT8 **p, static void CV_LoadVars(save_t *p,
consvar_t *(*got)(UINT8 **p, char **ret_value, boolean *ret_stealth)) consvar_t *(*got)(save_t *p, char **ret_value, boolean *ret_stealth))
{ {
const boolean store = (client || demoplayback); const boolean store = (client || demoplayback);
@ -1888,7 +1890,7 @@ static void CV_LoadVars(UINT8 **p,
} }
} }
count = READUINT16(*p); count = P_ReadUINT16(p);
while (count--) while (count--)
{ {
cvar = (*got)(p, &val, &stealth); cvar = (*got)(p, &val, &stealth);
@ -1921,19 +1923,19 @@ void CV_RevertNetVars(void)
} }
} }
void CV_LoadNetVars(UINT8 **p) void CV_LoadNetVars(save_t *p)
{ {
CV_LoadVars(p, ReadNetVar); CV_LoadVars(p, ReadNetVar);
} }
#ifdef OLD22DEMOCOMPAT #ifdef OLD22DEMOCOMPAT
void CV_LoadOldDemoVars(UINT8 **p) void CV_LoadOldDemoVars(save_t *p)
{ {
CV_LoadVars(p, ReadOldDemoVar); CV_LoadVars(p, ReadOldDemoVar);
} }
#endif #endif
void CV_LoadDemoVars(UINT8 **p) void CV_LoadDemoVars(save_t *p)
{ {
CV_LoadVars(p, ReadDemoVar); CV_LoadVars(p, ReadDemoVar);
} }

View file

@ -15,6 +15,7 @@
#include <stdio.h> #include <stdio.h>
#include "doomdef.h" #include "doomdef.h"
#include "p_saveg.h"
//=================================== //===================================
// Command buffer & command execution // Command buffer & command execution
@ -218,19 +219,19 @@ void CV_AddValue(consvar_t *var, INT32 increment);
void CV_SaveVariables(FILE *f); void CV_SaveVariables(FILE *f);
// load/save gamesate (load and save option and for network join in game) // load/save gamesate (load and save option and for network join in game)
void CV_SaveVars(UINT8 **p, boolean in_demo); void CV_SaveVars(save_t *p, boolean in_demo);
#define CV_SaveNetVars(p) CV_SaveVars(p, false) #define CV_SaveNetVars(p) CV_SaveVars(p, false)
void CV_LoadNetVars(UINT8 **p); void CV_LoadNetVars(save_t *p);
// then revert after leaving a netgame // then revert after leaving a netgame
void CV_RevertNetVars(void); void CV_RevertNetVars(void);
#define CV_SaveDemoVars(p) CV_SaveVars(p, true) #define CV_SaveDemoVars(p) CV_SaveVars(p, true)
void CV_LoadDemoVars(UINT8 **p); void CV_LoadDemoVars(save_t *p);
#ifdef OLD22DEMOCOMPAT #ifdef OLD22DEMOCOMPAT
void CV_LoadOldDemoVars(UINT8 **p); void CV_LoadOldDemoVars(save_t *p);
#endif #endif
// reset cheat netvars after cheats is deactivated // reset cheat netvars after cheats is deactivated

View file

@ -1454,6 +1454,7 @@ void G_BeginRecording(void)
char *filename; char *filename;
UINT16 totalfiles; UINT16 totalfiles;
UINT8 *m; UINT8 *m;
save_t savebuffer;
if (demo_p) if (demo_p)
return; return;
@ -1603,7 +1604,11 @@ void G_BeginRecording(void)
} }
// Save netvar data // Save netvar data
CV_SaveDemoVars(&demo_p); savebuffer.buf = demo_p;
savebuffer.size = demoend - demo_p;
savebuffer.pos = 0;
CV_SaveDemoVars(&savebuffer);
demo_p = &savebuffer.buf[savebuffer.pos];
memset(&oldcmd,0,sizeof(oldcmd)); memset(&oldcmd,0,sizeof(oldcmd));
memset(&oldghost,0,sizeof(oldghost)); memset(&oldghost,0,sizeof(oldghost));
@ -2236,10 +2241,24 @@ void G_DoPlayDemo(char *defdemoname)
// net var data // net var data
#ifdef OLD22DEMOCOMPAT #ifdef OLD22DEMOCOMPAT
if (demoversion < 0x000d) if (demoversion < 0x000d)
CV_LoadOldDemoVars(&demo_p); {
save_t savebuffer;
savebuffer.buf = demo_p;
savebuffer.size = demoend - demo_p;
savebuffer.pos = 0;
CV_LoadOldDemoVars(&savebuffer);
demo_p = &savebuffer.buf[savebuffer.pos];
}
else else
#endif #endif
CV_LoadDemoVars(&demo_p); {
save_t savebuffer;
savebuffer.buf = demo_p;
savebuffer.size = demoend - demo_p;
savebuffer.pos = 0;
CV_LoadDemoVars(&savebuffer);
demo_p = &savebuffer.buf[savebuffer.pos];
}
// Sigh ... it's an empty demo. // Sigh ... it's an empty demo.
if (*demo_p == DEMOMARKER) if (*demo_p == DEMOMARKER)

View file

@ -80,6 +80,7 @@ void SV_SendSaveGame(INT32 node, boolean resending)
if (!compressedsave) if (!compressedsave)
{ {
CONS_Alert(CONS_ERROR, M_GetText("No more free memory for savegame\n")); CONS_Alert(CONS_ERROR, M_GetText("No more free memory for savegame\n"));
free(savebuffer.buf);
return; return;
} }

View file

@ -5356,9 +5356,7 @@ void P_SaveNetGame(save_t *save_p, boolean resending)
mobj_t *mobj; mobj_t *mobj;
INT32 i = 1; // don't start from 0, it'd be confused with a blank pointer otherwise INT32 i = 1; // don't start from 0, it'd be confused with a blank pointer otherwise
UINT8 *p = &save_p->buf[save_p->pos]; CV_SaveNetVars(save_p);
CV_SaveNetVars(&p);
save_p->pos = p - save_p->buf;
P_NetArchiveMisc(save_p, resending); P_NetArchiveMisc(save_p, resending);
P_NetArchiveEmblems(save_p); P_NetArchiveEmblems(save_p);
@ -5411,9 +5409,7 @@ boolean P_LoadGame(save_t *save_p, INT16 mapoverride)
boolean P_LoadNetGame(save_t *save_p, boolean reloading) boolean P_LoadNetGame(save_t *save_p, boolean reloading)
{ {
UINT8 *p = &save_p->buf[save_p->pos]; CV_LoadNetVars(save_p);
CV_LoadNetVars(&p);
save_p->pos = p - save_p->buf;
if (!P_NetUnArchiveMisc(save_p, reloading)) if (!P_NetUnArchiveMisc(save_p, reloading))
return false; return false;
P_NetUnArchiveEmblems(save_p); P_NetUnArchiveEmblems(save_p);