From 2a3a33226ed0efb37f074a237499bc614e601bb7 Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 16 Sep 2024 12:37:25 +0200 Subject: [PATCH] fixup demo savebuf --- src/d_netcmd.c | 4 +- src/g_game.c | 599 +++++++++++++++++++++++----------------------- src/g_game.h | 3 +- src/lua_hooklib.c | 7 +- src/lua_script.c | 211 +++++++--------- src/lua_script.h | 10 +- src/p_saveg.c | 4 +- 7 files changed, 390 insertions(+), 448 deletions(-) diff --git a/src/d_netcmd.c b/src/d_netcmd.c index d85280f08..8bd240db5 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -5841,7 +5841,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); @@ -5852,7 +5852,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_game.c b/src/g_game.c index 5f5cce736..c115aa257 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -293,9 +293,8 @@ UINT32 timesBeatenWithEmeralds; //@TODO put these all in a struct for namespacing purposes? static char demoname[128]; -static UINT8 *demobuffer = NULL; +savebuffer_t demobuf; static UINT8 *demotime_p, *demoinfo_p; -UINT8 *demo_p; static UINT8 *demoend; static UINT8 demoflags; static boolean demosynced = true; // console warning message @@ -509,8 +508,8 @@ static CV_PossibleValue_t driftsparkpulse_t[] = {{0, "MIN"}, {FRACUNIT*3, "MAX"} consvar_t cv_driftsparkpulse = {"driftsparkpulse", "1.4", CV_FLOAT | CV_SAVE, driftsparkpulse_t, NULL, 0, NULL, NULL, 0, 0, NULL}; static CV_PossibleValue_t stretchfactor_t[] = { - {0, "Off"}, {FRACUNIT/8, "0.125"}, {FRACUNIT/4, "0.250"}, - {3*FRACUNIT/8, "0.375"}, {FRACUNIT/2, "0.500"}, {5*FRACUNIT/8, "0.625"}, + {0, "Off"}, {FRACUNIT/8, "0.125"}, {FRACUNIT/4, "0.250"}, + {3*FRACUNIT/8, "0.375"}, {FRACUNIT/2, "0.500"}, {5*FRACUNIT/8, "0.625"}, {3*FRACUNIT/4, "0.750"}, {7*FRACUNIT/8, "0.875"}, {FRACUNIT, "Max"}, {0, NULL}}; consvar_t cv_gravstretch = {"gravstretch", "0", CV_SAVE, stretchfactor_t, NULL, 0, NULL, NULL, 0, 0, NULL}; @@ -978,7 +977,7 @@ static INT32 Joy3Axis(axis_input_e axissel) } if (flp) retaxis = -retaxis; //flip it around return retaxis; - + } else { @@ -998,7 +997,7 @@ static INT32 Joy3Axis(axis_input_e axissel) } if (flp) retaxis = -retaxis; //flip it around return retaxis; - + } } @@ -1073,7 +1072,7 @@ static INT32 Joy4Axis(axis_input_e axissel) } if (flp) retaxis = -retaxis; //flip it around return retaxis; - + } else { @@ -1093,7 +1092,7 @@ static INT32 Joy4Axis(axis_input_e axissel) } if (flp) retaxis = -retaxis; //flip it around return retaxis; - + } } @@ -4732,7 +4731,7 @@ INT32 G_FindMapByNameOrCode(const char *mapname, char **realmapnamep) INT32 newmapnum = -1; size_t mapnamelen = strlen(mapname); char *p; - + if (mapnamelen == 1) { if (mapname[0] == '*') // current map @@ -4934,7 +4933,7 @@ void G_ReadDemoExtraData(void) if (leveltime > starttime) { - rewind_t *rewind = CL_SaveRewindPoint(demo_p - demobuffer); + rewind_t *rewind = CL_SaveRewindPoint(demobuf.p - demobuf.buffer); if (rewind) { memcpy(rewind->oldcmd, oldcmd, sizeof (oldcmd)); @@ -4944,11 +4943,11 @@ void G_ReadDemoExtraData(void) memset(name, '\0', 17); - p = READUINT8(demo_p); + p = READUINT8(demobuf.p); while (p < DW_EXTRASTUFF) { - extradata = READUINT8(demo_p); + extradata = READUINT8(demobuf.p); if (extradata & DXD_RESPAWN) { @@ -4960,12 +4959,12 @@ void G_ReadDemoExtraData(void) UINT8 kartspeed, kartweight; // Skin - M_Memcpy(name, demo_p, 16); - demo_p += 16; + M_Memcpy(name, demobuf.p, 16); + demobuf.p += 16; SetPlayerSkin(p, name); - kartspeed = READUINT8(demo_p); - kartweight = READUINT8(demo_p); + kartspeed = READUINT8(demobuf.p); + kartweight = READUINT8(demobuf.p); if (stricmp(skins[players[p].skin].name, name) != 0) FindClosestSkinForStats(p, kartspeed, kartweight); @@ -4976,8 +4975,8 @@ void G_ReadDemoExtraData(void) if (extradata & DXD_COLOR) { // Color - M_Memcpy(name, demo_p, 16); - demo_p += 16; + M_Memcpy(name, demobuf.p, 16); + demobuf.p += 16; for (i = 0; i < MAXSKINCOLORS; i++) if (!stricmp(KartColor_Names[i], name)) // SRB2kart { @@ -4990,12 +4989,12 @@ void G_ReadDemoExtraData(void) if (extradata & DXD_NAME) { // Name - M_Memcpy(player_names[p],demo_p,16); - demo_p += 16; + M_Memcpy(player_names[p],demobuf.p,16); + demobuf.p += 16; } if (extradata & DXD_PLAYSTATE) { - extradata = READUINT8(demo_p); + extradata = READUINT8(demobuf.p); switch (extradata) { case DXD_PST_PLAYING: @@ -5040,7 +5039,7 @@ void G_ReadDemoExtraData(void) } - p = READUINT8(demo_p); + p = READUINT8(demobuf.p); } while (p != DW_END) @@ -5050,7 +5049,7 @@ void G_ReadDemoExtraData(void) switch (p) { case DW_RNG: - rng = READUINT32(demo_p); + rng = READUINT32(demobuf.p); if (P_GetRandSeed() != rng) { P_SetRandSeed(rng); @@ -5061,10 +5060,10 @@ void G_ReadDemoExtraData(void) } } - p = READUINT8(demo_p); + p = READUINT8(demobuf.p); } - if (!(demoflags & DF_GHOST) && *demo_p == DEMOMARKER) + if (!(demoflags & DF_GHOST) && *demobuf.p == DEMOMARKER) { // end of demo data stream G_CheckDemoStatus(); @@ -5081,8 +5080,8 @@ void G_WriteDemoExtraData(void) { if (demo_extradata[i]) { - WRITEUINT8(demo_p, i); - WRITEUINT8(demo_p, demo_extradata[i]); + WRITEUINT8(demobuf.p, i); + WRITEUINT8(demobuf.p, demo_extradata[i]); //if (demo_extradata[i] & DXD_RESPAWN) has no extra data if (demo_extradata[i] & DXD_SKIN) @@ -5090,40 +5089,40 @@ void G_WriteDemoExtraData(void) // Skin memset(name, 0, 16); strncpy(name, skins[players[i].skin].name, 16); - M_Memcpy(demo_p,name,16); - demo_p += 16; + M_Memcpy(demobuf.p,name,16); + demobuf.p += 16; - WRITEUINT8(demo_p, skins[players[i].skin].kartspeed); - WRITEUINT8(demo_p, skins[players[i].skin].kartweight); + WRITEUINT8(demobuf.p, skins[players[i].skin].kartspeed); + WRITEUINT8(demobuf.p, skins[players[i].skin].kartweight); } if (demo_extradata[i] & DXD_COLOR) { // Color memset(name, 0, 16); strncpy(name, KartColor_Names[players[i].skincolor], 16); - M_Memcpy(demo_p,name,16); - demo_p += 16; + M_Memcpy(demobuf.p,name,16); + demobuf.p += 16; } if (demo_extradata[i] & DXD_NAME) { // Name memset(name, 0, 16); memcpy(name, player_names[i], 15); // Keeping 1 null byte for safety, sorry players with name containing more than 15 characters - M_Memcpy(demo_p,name,16); - demo_p += 16; + M_Memcpy(demobuf.p,name,16); + demobuf.p += 16; } if (demo_extradata[i] & DXD_PLAYSTATE) { demo_writerng = 1; if (!playeringame[i]) - WRITEUINT8(demo_p, DXD_PST_LEFT); + WRITEUINT8(demobuf.p, DXD_PST_LEFT); else if ( players[i].spectator && !(players[i].pflags & PF_WANTSTOJOIN) // <= fuck you specifically ) - WRITEUINT8(demo_p, DXD_PST_SPECTATING); + WRITEUINT8(demobuf.p, DXD_PST_SPECTATING); else - WRITEUINT8(demo_p, DXD_PST_PLAYING); + WRITEUINT8(demobuf.p, DXD_PST_PLAYING); } } @@ -5143,36 +5142,36 @@ void G_WriteDemoExtraData(void) { demo_writerng = 0; timeout = 16; - WRITEUINT8(demo_p, DW_RNG); - WRITEUINT32(demo_p, P_GetRandSeed()); + WRITEUINT8(demobuf.p, DW_RNG); + WRITEUINT32(demobuf.p, P_GetRandSeed()); } } - WRITEUINT8(demo_p, DW_END); + WRITEUINT8(demobuf.p, DW_END); } void G_ReadDemoTiccmd(ticcmd_t *cmd, INT32 playernum) { UINT8 ziptic; - if (!demo_p || !demo.deferstart) + if (!demobuf.p || !demo.deferstart) return; - ziptic = READUINT8(demo_p); + ziptic = READUINT8(demobuf.p); if (ziptic & ZT_FWD) - oldcmd[playernum].forwardmove = READSINT8(demo_p); + oldcmd[playernum].forwardmove = READSINT8(demobuf.p); if (ziptic & ZT_SIDE) - oldcmd[playernum].sidemove = READSINT8(demo_p); + oldcmd[playernum].sidemove = READSINT8(demobuf.p); if (ziptic & ZT_ANGLE) - oldcmd[playernum].angleturn = READINT16(demo_p); + oldcmd[playernum].angleturn = READINT16(demobuf.p); if (ziptic & ZT_BUTTONS) - oldcmd[playernum].buttons = READUINT16(demo_p); + oldcmd[playernum].buttons = READUINT16(demobuf.p); if (ziptic & ZT_AIMING) - oldcmd[playernum].aiming = READINT16(demo_p); + oldcmd[playernum].aiming = READINT16(demobuf.p); if (ziptic & ZT_DRIFT) - oldcmd[playernum].driftturn = READINT16(demo_p); + oldcmd[playernum].driftturn = READINT16(demobuf.p); if (ziptic & ZT_LATENCY) - oldcmd[playernum].latency = READUINT8(demo_p); + oldcmd[playernum].latency = READUINT8(demobuf.p); G_CopyTiccmd(cmd, &oldcmd[playernum], 1); @@ -5184,7 +5183,7 @@ void G_ReadDemoTiccmd(ticcmd_t *cmd, INT32 playernum) && !(players[displayplayers[0]].kartstuff[k_spinouttimer] && players[displayplayers[0]].kartstuff[k_sneakertimer])) // Spinning and boosting cancels out spinout localangle[0] += (cmd->angleturn<<16); - if (!(demoflags & DF_GHOST) && *demo_p == DEMOMARKER) + if (!(demoflags & DF_GHOST) && *demobuf.p == DEMOMARKER) { // end of demo data stream G_CheckDemoStatus(); @@ -5197,55 +5196,55 @@ void G_WriteDemoTiccmd(ticcmd_t *cmd, INT32 playernum) char ziptic = 0; UINT8 *ziptic_p; - if (!demo_p) + if (!demobuf.p) return; - ziptic_p = demo_p++; // the ziptic, written at the end of this function + ziptic_p = demobuf.p++; // the ziptic, written at the end of this function if (cmd->forwardmove != oldcmd[playernum].forwardmove) { - WRITEUINT8(demo_p,cmd->forwardmove); + WRITEUINT8(demobuf.p,cmd->forwardmove); oldcmd[playernum].forwardmove = cmd->forwardmove; ziptic |= ZT_FWD; } if (cmd->sidemove != oldcmd[playernum].sidemove) { - WRITEUINT8(demo_p,cmd->sidemove); + WRITEUINT8(demobuf.p,cmd->sidemove); oldcmd[playernum].sidemove = cmd->sidemove; ziptic |= ZT_SIDE; } if (cmd->angleturn != oldcmd[playernum].angleturn) { - WRITEINT16(demo_p,cmd->angleturn); + WRITEINT16(demobuf.p,cmd->angleturn); oldcmd[playernum].angleturn = cmd->angleturn; ziptic |= ZT_ANGLE; } if (cmd->buttons != oldcmd[playernum].buttons) { - WRITEUINT16(demo_p,cmd->buttons); + WRITEUINT16(demobuf.p,cmd->buttons); oldcmd[playernum].buttons = cmd->buttons; ziptic |= ZT_BUTTONS; } if (cmd->aiming != oldcmd[playernum].aiming) { - WRITEINT16(demo_p,cmd->aiming); + WRITEINT16(demobuf.p,cmd->aiming); oldcmd[playernum].aiming = cmd->aiming; ziptic |= ZT_AIMING; } if (cmd->driftturn != oldcmd[playernum].driftturn) { - WRITEINT16(demo_p,cmd->driftturn); + WRITEINT16(demobuf.p,cmd->driftturn); oldcmd[playernum].driftturn = cmd->driftturn; ziptic |= ZT_DRIFT; } if (cmd->latency != oldcmd[playernum].latency) { - WRITEUINT8(demo_p,cmd->latency); + WRITEUINT8(demobuf.p,cmd->latency); oldcmd[playernum].latency = cmd->latency; ziptic |= ZT_LATENCY; } @@ -5327,8 +5326,7 @@ void G_GhostAddHit(INT32 playernum, mobj_t *victim) void G_WriteAllGhostTics(void) { - UINT8 *save_demo_p = demo_p; -#define CHECKSPACE(num) if (demo_p+(num) > demoend) { demo_p = save_demo_p; G_CheckDemoStatus(); return; } +#define CHECKSPACE(num) if (demobuf.p+(num) > demoend) {G_CheckDemoStatus(); return; } INT32 i, counter = leveltime; for (i = 0; i < MAXPLAYERS; i++) @@ -5346,12 +5344,12 @@ void G_WriteAllGhostTics(void) CHECKSPACE(1); - WRITEUINT8(demo_p, i); + WRITEUINT8(demobuf.p, i); G_WriteGhostTic(players[i].mo, i); } CHECKSPACE(1); - WRITEUINT8(demo_p, 0xFF); + WRITEUINT8(demobuf.p, 0xFF); #undef CHECKSPACE } @@ -5364,10 +5362,9 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) UINT8 sprite; UINT8 frame; - UINT8 *save_demo_p = demo_p; -#define CHECKSPACE(num) if (demo_p+(num) > demoend) { demo_p = save_demo_p; G_CheckDemoStatus(); return; } +#define CHECKSPACE(num) if (demobuf.p+(num) > demoend) {G_CheckDemoStatus(); return; } - if (!demo_p) + if (!demobuf.p) return; if (!(demoflags & DF_GHOST)) return; // No ghost data to write. @@ -5379,7 +5376,7 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) ghost = ghost->tracer; } - ziptic_p = demo_p++; // the ziptic, written at the end of this function + ziptic_p = demobuf.p++; // the ziptic, written at the end of this function #define MAXMOM (0x7FFF<<8) @@ -5395,9 +5392,9 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) ziptic |= GZT_XYZ; CHECKSPACE(sizeof(fixed_t)*3); - WRITEFIXED(demo_p,oldghost[playernum].x); - WRITEFIXED(demo_p,oldghost[playernum].y); - WRITEFIXED(demo_p,oldghost[playernum].z); + WRITEFIXED(demobuf.p,oldghost[playernum].x); + WRITEFIXED(demobuf.p,oldghost[playernum].y); + WRITEFIXED(demobuf.p,oldghost[playernum].z); } else { @@ -5414,8 +5411,8 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) CHECKSPACE(4); - WRITEINT16(demo_p,momx); - WRITEINT16(demo_p,momy); + WRITEINT16(demobuf.p,momx); + WRITEINT16(demobuf.p,momy); } momx = (INT16)((ghost->z-oldghost[playernum].z + (1<<4))>>8); if (momx != oldghost[playernum].momz) @@ -5425,7 +5422,7 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) CHECKSPACE(2); - WRITEINT16(demo_p,momx); + WRITEINT16(demobuf.p,momx); } // This SHOULD set oldghost.x/y/z to match ghost->x/y/z @@ -5448,7 +5445,7 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) CHECKSPACE(1); - WRITEUINT8(demo_p,oldghost[playernum].angle); + WRITEUINT8(demobuf.p,oldghost[playernum].angle); } // Store the sprite frame. @@ -5460,7 +5457,7 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) CHECKSPACE(1); - WRITEUINT8(demo_p,oldghost[playernum].frame); + WRITEUINT8(demobuf.p,oldghost[playernum].frame); } // Check for sprite set changes @@ -5495,37 +5492,37 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) if (ghostext[playernum].flags) { ziptic |= GZT_EXTRA; - WRITEUINT8(demo_p,ghostext[playernum].flags); + WRITEUINT8(demobuf.p,ghostext[playernum].flags); if (ghostext[playernum].flags & EZT_COLOR) { CHECKSPACE(1); - WRITEUINT8(demo_p,ghostext[playernum].color); + WRITEUINT8(demobuf.p,ghostext[playernum].color); ghostext[playernum].lastcolor = ghostext[playernum].color; } if (ghostext[playernum].flags & EZT_SCALE) { CHECKSPACE(sizeof(fixed_t)); - WRITEFIXED(demo_p,ghostext[playernum].scale); + WRITEFIXED(demobuf.p,ghostext[playernum].scale); ghostext[playernum].lastscale = ghostext[playernum].scale; } if (ghostext[playernum].flags & EZT_HIT) { CHECKSPACE(2); - WRITEUINT16(demo_p,ghostext[playernum].hits); + WRITEUINT16(demobuf.p,ghostext[playernum].hits); for (i = 0; i < ghostext[playernum].hits; i++) { mobj_t *mo = ghostext[playernum].hitlist[i]; CHECKSPACE(4+4+2+sizeof(fixed_t)*3+sizeof(angle_t)); - WRITEUINT32(demo_p,UINT32_MAX); // reserved for some method of determining exactly which mobj this is. (mobjnum doesn't work here.) - WRITEUINT32(demo_p,mo->type); - WRITEUINT16(demo_p,(UINT16)mo->health); - WRITEFIXED(demo_p,mo->x); - WRITEFIXED(demo_p,mo->y); - WRITEFIXED(demo_p,mo->z); - WRITEANGLE(demo_p,mo->angle); + WRITEUINT32(demobuf.p,UINT32_MAX); // reserved for some method of determining exactly which mobj this is. (mobjnum doesn't work here.) + WRITEUINT32(demobuf.p,mo->type); + WRITEUINT16(demobuf.p,(UINT16)mo->health); + WRITEFIXED(demobuf.p,mo->x); + WRITEFIXED(demobuf.p,mo->y); + WRITEFIXED(demobuf.p,mo->z); + WRITEANGLE(demobuf.p,mo->angle); } Z_Free(ghostext[playernum].hitlist); ghostext[playernum].hits = 0; @@ -5534,15 +5531,15 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) if (ghostext[playernum].flags & EZT_SPRITE) { CHECKSPACE(1); - WRITEUINT8(demo_p,sprite); + WRITEUINT8(demobuf.p,sprite); } if (ghostext[playernum].flags & EZT_KART) { CHECKSPACE(12); - WRITEINT32(demo_p, ghostext[playernum].kartitem); - WRITEINT32(demo_p, ghostext[playernum].kartamount); - WRITEINT32(demo_p, ghostext[playernum].kartbumpers); + WRITEINT32(demobuf.p, ghostext[playernum].kartitem); + WRITEINT32(demobuf.p, ghostext[playernum].kartamount); + WRITEINT32(demobuf.p, ghostext[playernum].kartbumpers); } ghostext[playernum].flags = 0; } @@ -5551,7 +5548,7 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) // attention here for the ticcmd size! // latest demos with mouse aiming byte in ticcmd - if (demo_p >= demoend - (13 + 9)) + if (demobuf.p >= demoend - (13 + 9)) { G_CheckDemoStatus(); // no more space return; @@ -5561,15 +5558,15 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) void G_ConsAllGhostTics(void) { - UINT8 p = READUINT8(demo_p); + UINT8 p = READUINT8(demobuf.p); while (p != 0xFF) { G_ConsGhostTic(p); - p = READUINT8(demo_p); + p = READUINT8(demobuf.p); } - if (*demo_p == DEMOMARKER) + if (*demobuf.p == DEMOMARKER) { // end of demo data stream G_CheckDemoStatus(); @@ -5587,7 +5584,7 @@ void G_ConsGhostTic(INT32 playernum) fixed_t syncleeway; boolean nightsfail = false; - if (!demo_p || !demo.deferstart) + if (!demobuf.p || !demo.deferstart) return; if (!(demoflags & DF_GHOST)) return; // No ghost data to use. @@ -5595,32 +5592,32 @@ void G_ConsGhostTic(INT32 playernum) testmo = players[playernum].mo; // Grab ghost data. - ziptic = READUINT8(demo_p); + ziptic = READUINT8(demobuf.p); if (ziptic & GZT_XYZ) { - oldghost[playernum].x = READFIXED(demo_p); - oldghost[playernum].y = READFIXED(demo_p); - oldghost[playernum].z = READFIXED(demo_p); + oldghost[playernum].x = READFIXED(demobuf.p); + oldghost[playernum].y = READFIXED(demobuf.p); + oldghost[playernum].z = READFIXED(demobuf.p); syncleeway = 0; } else { if (ziptic & GZT_MOMXY) { - oldghost[playernum].momx = READINT16(demo_p)<<8; - oldghost[playernum].momy = READINT16(demo_p)<<8; + oldghost[playernum].momx = READINT16(demobuf.p)<<8; + oldghost[playernum].momy = READINT16(demobuf.p)<<8; } if (ziptic & GZT_MOMZ) - oldghost[playernum].momz = READINT16(demo_p)<<8; + oldghost[playernum].momz = READINT16(demobuf.p)<<8; oldghost[playernum].x += oldghost[playernum].momx; oldghost[playernum].y += oldghost[playernum].momy; oldghost[playernum].z += oldghost[playernum].momz; syncleeway = FRACUNIT; } if (ziptic & GZT_ANGLE) - demo_p++; + demobuf.p++; if (ziptic & GZT_SPRITE) - demo_p++; + demobuf.p++; if(ziptic & GZT_NIGHTS) { if (!testmo || !testmo->player || !(testmo->player->pflags & PF_NIGHTSMODE) || !testmo->tracer) nightsfail = true; @@ -5630,14 +5627,14 @@ void G_ConsGhostTic(INT32 playernum) if (ziptic & GZT_EXTRA) { // But wait, there's more! - ziptic = READUINT8(demo_p); + ziptic = READUINT8(demobuf.p); if (ziptic & EZT_COLOR) - demo_p++; + demobuf.p++; if (ziptic & EZT_SCALE) - demo_p += sizeof(fixed_t); + demobuf.p += sizeof(fixed_t); if (ziptic & EZT_HIT) { // Resync mob damage. - UINT16 i, count = READUINT16(demo_p); + UINT16 i, count = READUINT16(demobuf.p); thinker_t *th; mobj_t *mobj; @@ -5649,13 +5646,13 @@ void G_ConsGhostTic(INT32 playernum) for (i = 0; i < count; i++) { - demo_p += 4; // reserved. - type = READUINT32(demo_p); - health = READUINT16(demo_p); - x = READFIXED(demo_p); - y = READFIXED(demo_p); - z = READFIXED(demo_p); - demo_p += sizeof(angle_t); // angle, unnecessary for cons. + demobuf.p += 4; // reserved. + type = READUINT32(demobuf.p); + health = READUINT16(demobuf.p); + x = READFIXED(demobuf.p); + y = READFIXED(demobuf.p); + z = READFIXED(demobuf.p); + demobuf.p += sizeof(angle_t); // angle, unnecessary for cons. mobj = NULL; for (th = thinkercap.next; th != &thinkercap; th = th->next) @@ -5677,12 +5674,12 @@ void G_ConsGhostTic(INT32 playernum) } } if (ziptic & EZT_SPRITE) - demo_p++; + demobuf.p++; if (ziptic & EZT_KART) { - ghostext[playernum].kartitem = READINT32(demo_p); - ghostext[playernum].kartamount = READINT32(demo_p); - ghostext[playernum].kartbumpers = READINT32(demo_p); + ghostext[playernum].kartitem = READINT32(demobuf.p); + ghostext[playernum].kartamount = READINT32(demobuf.p); + ghostext[playernum].kartbumpers = READINT32(demobuf.p); } } @@ -5742,7 +5739,7 @@ void G_ConsGhostTic(INT32 playernum) } } - if (*demo_p == DEMOMARKER) + if (*demobuf.p == DEMOMARKER) { // end of demo data stream G_CheckDemoStatus(); @@ -6155,7 +6152,7 @@ void G_ConfirmRewind(tic_t rewindtime) if (rewind) { - demo_p = demobuffer + rewind->demopos; + demobuf.p = demobuf.buffer + rewind->demopos; memcpy(oldcmd, rewind->oldcmd, sizeof (oldcmd)); memcpy(oldghost, rewind->oldghost, sizeof (oldghost)); paused = false; @@ -6303,10 +6300,10 @@ void G_WriteMetalTic(mobj_t *metal) UINT8 ziptic = 0; UINT8 *ziptic_p; - if (!demo_p) // demo_p will be NULL until the race start linedef executor is triggered! + if (!demobuf.p) // demobuf.p will be NULL until the race start linedef executor is triggered! return; - ziptic_p = demo_p++; // the ziptic, written at the end of this function + ziptic_p = demobuf.p++; // the ziptic, written at the end of this function #define MAXMOM (0xFFFF<<8) @@ -6318,9 +6315,9 @@ void G_WriteMetalTic(mobj_t *metal) oldmetal.x = metal->x; oldmetal.y = metal->y; oldmetal.z = metal->z; - WRITEFIXED(demo_p,oldmetal.x); - WRITEFIXED(demo_p,oldmetal.y); - WRITEFIXED(demo_p,oldmetal.z); + WRITEFIXED(demobuf.p,oldmetal.x); + WRITEFIXED(demobuf.p,oldmetal.y); + WRITEFIXED(demobuf.p,oldmetal.z); ziptic |= GZT_XYZ; } else @@ -6334,15 +6331,15 @@ void G_WriteMetalTic(mobj_t *metal) { oldmetal.momx = momx; oldmetal.momy = momy; - WRITEINT16(demo_p,momx); - WRITEINT16(demo_p,momy); + WRITEINT16(demobuf.p,momx); + WRITEINT16(demobuf.p,momy); ziptic |= GZT_MOMXY; } momx = (INT16)((metal->z-oldmetal.z)>>8); if (momx != oldmetal.momz) { oldmetal.momz = momx; - WRITEINT16(demo_p,momx); + WRITEINT16(demobuf.p,momx); ziptic |= GZT_MOMZ; } @@ -6362,7 +6359,7 @@ void G_WriteMetalTic(mobj_t *metal) if (metal->angle>>24 != oldmetal.angle) { oldmetal.angle = metal->angle>>24; - WRITEUINT8(demo_p,oldmetal.angle); + WRITEUINT8(demobuf.p,oldmetal.angle); ziptic |= GZT_ANGLE; } @@ -6375,16 +6372,16 @@ void G_WriteMetalTic(mobj_t *metal) if ((metal->eflags & MFE_VERTICALFLIP) != (oldmetal.eflags & MFE_VERTICALFLIP)) { if (!exttic_p) - exttic_p = demo_p++; + exttic_p = demobuf.p++; exttic |= EZT_FLIP; oldmetal.eflags ^= MFE_VERTICALFLIP; } if (metal->scale != oldmetal.scale) { if (!exttic_p) - exttic_p = demo_p++; + exttic_p = demobuf.p++; exttic |= EZT_SCALE; - WRITEFIXED(demo_p,metal->scale); + WRITEFIXED(demobuf.p,metal->scale); oldmetal.scale = metal->scale; } if (exttic_p) @@ -6398,7 +6395,7 @@ void G_WriteMetalTic(mobj_t *metal) // attention here for the ticcmd size! // latest demos with mouse aiming byte in ticcmd - if (demo_p >= demoend - 32) + if (demobuf.p >= demoend - 32) { G_StopMetalRecording(); // no more space return; @@ -6412,11 +6409,11 @@ void G_RecordDemo(const char *name) { INT32 maxsize; - demo_p = NULL; + demobuf.p = NULL; demo.recording = false; - if (demobuffer) - free(demobuffer); - demobuffer = NULL; + if (demobuf.buffer) + free(demobuf.buffer); + demobuf.buffer = NULL; demoend = NULL; if (cv_recordmultiplayerdemos.value) @@ -6428,10 +6425,10 @@ void G_RecordDemo(const char *name) maxsize = cv_maxdemosize.value*1024*1024; - demobuffer = malloc(maxsize); - demoend = demobuffer + maxsize; + demobuf.buffer = malloc(maxsize); + demoend = demobuf.buffer + maxsize; - if (demobuffer) + if (demobuf.buffer) demo.recording = true; else CONS_Alert(CONS_ERROR, "Failed to allocate demo buffer\n"); @@ -6442,14 +6439,14 @@ void G_RecordMetal(void) { INT32 maxsize; maxsize = cv_maxdemosize.value*1024*1024; - if (demobuffer) - free(demobuffer); - demo_p = NULL; + if (demobuf.buffer) + free(demobuf.buffer); + demobuf.p = NULL; metalrecording = false; - demobuffer = malloc(maxsize); - demoend = demobuffer + maxsize; + demobuf.buffer = malloc(maxsize); + demoend = demobuf.buffer + maxsize; - if (demobuffer) + if (demobuf.buffer) metalrecording = true; else CONS_Alert(CONS_ERROR, "Failed to allocate demo buffer\n"); @@ -6465,11 +6462,11 @@ void G_BeginRecording(void) UINT8 totalfiles; UINT8 *m; - if (demo_p) + if (demobuf.p) return; memset(name,0,sizeof(name)); - demo_p = demobuffer; + demobuf.p = demobuf.buffer; demoflags = DF_GHOST|(multiplayer ? DF_MULTIPLAYER : (modeattacking<important) { nameonly(( filename = va("%s", wadfiles[i]->filename) )); - WRITESTRINGL(demo_p, filename, MAX_WADPATH); - WRITEMEM(demo_p, wadfiles[i]->md5sum, 16); + WRITESTRINGL(demobuf.p, filename, MAX_WADPATH); + WRITEMEM(demobuf.p, wadfiles[i]->md5sum, 16); totalfiles++; } @@ -6526,63 +6523,63 @@ void G_BeginRecording(void) case ATTACKING_NONE: // 0 break; case ATTACKING_RECORD: // 1 - demotime_p = demo_p; - WRITEUINT32(demo_p,UINT32_MAX); // time - WRITEUINT32(demo_p,UINT32_MAX); // lap + demotime_p = demobuf.p; + WRITEUINT32(demobuf.p,UINT32_MAX); // time + WRITEUINT32(demobuf.p,UINT32_MAX); // lap break; default: // 3 break; } - WRITEUINT32(demo_p,P_GetInitSeed()); + WRITEUINT32(demobuf.p,P_GetInitSeed()); // Reserved for extrainfo location from start of file - demoinfo_p = demo_p; - WRITEUINT32(demo_p, 0); + demoinfo_p = demobuf.p; + WRITEUINT32(demobuf.p, 0); // Save netvars - CV_SaveNetVars(&demo_p, true); + CV_SaveNetVars(&demobuf.p, true); // Now store some info for each in-game player for (p = 0; p < MAXPLAYERS; p++) { if (playeringame[p]) { player = &players[p]; - WRITEUINT8(demo_p, p | (player->spectator ? DEMO_SPECTATOR : 0)); + WRITEUINT8(demobuf.p, p | (player->spectator ? DEMO_SPECTATOR : 0)); // Name memset(name, 0, 16); memcpy(name, player_names[p], 15); - M_Memcpy(demo_p,name,16); - demo_p += 16; + M_Memcpy(demobuf.p,name,16); + demobuf.p += 16; // Skin memset(name, 0, 16); strncpy(name, skins[player->skin].name, 16); - M_Memcpy(demo_p,name,16); - demo_p += 16; + M_Memcpy(demobuf.p,name,16); + demobuf.p += 16; // Color memset(name, 0, 16); strncpy(name, KartColor_Names[player->skincolor], 16); - M_Memcpy(demo_p,name,16); - demo_p += 16; + M_Memcpy(demobuf.p,name,16); + demobuf.p += 16; // Score, since Kart uses this to determine where you start on the map - WRITEUINT32(demo_p, player->score); + WRITEUINT32(demobuf.p, player->score); // Kart speed and weight - WRITEUINT8(demo_p, skins[player->skin].kartspeed); - WRITEUINT8(demo_p, skins[player->skin].kartweight); + WRITEUINT8(demobuf.p, skins[player->skin].kartspeed); + WRITEUINT8(demobuf.p, skins[player->skin].kartweight); } } - WRITEUINT8(demo_p, 0xFF); // Denote the end of the player listing + WRITEUINT8(demobuf.p, 0xFF); // Denote the end of the player listing // player lua vars, always saved even if empty... Unless it's record attack. if (demoflags & DF_LUAVARS) - LUA_Archive(&demo_p, false); + LUA_Archive(&demobuf.p, false); memset(&oldcmd,0,sizeof(oldcmd)); memset(&oldghost,0,sizeof(oldghost)); @@ -6611,21 +6608,21 @@ void G_BeginMetal(void) { mobj_t *mo = players[consoleplayer].mo; - if (demo_p) + if (demobuf.p) return; - demo_p = demobuffer; + demobuf.p = demobuf.buffer; // Write header. - M_Memcpy(demo_p, DEMOHEADER, 12); demo_p += 12; - WRITEUINT8(demo_p,VERSION); - WRITEUINT8(demo_p,SUBVERSION); - WRITEUINT16(demo_p,DEMOVERSION); + M_Memcpy(demobuf.p, DEMOHEADER, 12); demobuf.p += 12; + WRITEUINT8(demobuf.p,VERSION); + WRITEUINT8(demobuf.p,SUBVERSION); + WRITEUINT16(demobuf.p,DEMOVERSION); // demo checksum - demo_p += 16; + demobuf.p += 16; - M_Memcpy(demo_p, "METL", 4); demo_p += 4; + M_Memcpy(demobuf.p, "METL", 4); demobuf.p += 4; // Set up our memory. memset(&oldmetal,0,sizeof(oldmetal)); @@ -6641,33 +6638,33 @@ void G_WriteStanding(UINT8 ranking, char *name, INT32 skinnum, UINT8 color, UINT if (demoinfo_p && *(UINT32 *)demoinfo_p == 0) { - WRITEUINT8(demo_p, DEMOMARKER); // add the demo end marker - *(UINT32 *)demoinfo_p = demo_p - demobuffer; + WRITEUINT8(demobuf.p, DEMOMARKER); // add the demo end marker + *(UINT32 *)demoinfo_p = demobuf.p - demobuf.buffer; } - WRITEUINT8(demo_p, DW_STANDING); - WRITEUINT8(demo_p, ranking); + WRITEUINT8(demobuf.p, DW_STANDING); + WRITEUINT8(demobuf.p, ranking); // Name memset(temp, 0, 16); strncpy(temp, name, 16); - M_Memcpy(demo_p,temp,16); - demo_p += 16; + M_Memcpy(demobuf.p,temp,16); + demobuf.p += 16; // Skin memset(temp, 0, 16); strncpy(temp, skins[skinnum].name, 16); - M_Memcpy(demo_p,temp,16); - demo_p += 16; + M_Memcpy(demobuf.p,temp,16); + demobuf.p += 16; // Color memset(temp, 0, 16); strncpy(temp, KartColor_Names[color], 16); - M_Memcpy(demo_p,temp,16); - demo_p += 16; + M_Memcpy(demobuf.p,temp,16); + demobuf.p += 16; // Score/time/whatever - WRITEUINT32(demo_p, val); + WRITEUINT32(demobuf.p, val); } void G_SetDemoTime(UINT32 ptime, UINT32 plap) @@ -7233,7 +7230,7 @@ void G_DoPlayDemo(char *defdemoname) // No demo name means we're restarting the current demo if (defdemoname == NULL) { - demo_p = demobuffer; + demobuf.p = demobuf.buffer; pdemoname = ZZ_Alloc(1); // Easier than adding checks for this everywhere it's freed } else @@ -7252,7 +7249,7 @@ void G_DoPlayDemo(char *defdemoname) if (FIL_CheckExtension(defdemoname)) { //FIL_DefaultExtension(defdemoname, ".lmp"); - if (!FIL_ReadFile(defdemoname, &demobuffer)) + if (!FIL_ReadFile(defdemoname, &demobuf.buffer)) { snprintf(msg, 1024, M_GetText("Failed to read file '%s'.\n"), defdemoname); CONS_Alert(CONS_ERROR, "%s", msg); @@ -7260,7 +7257,7 @@ void G_DoPlayDemo(char *defdemoname) M_StartMessage(msg, NULL, MM_NOTHING); return; } - demo_p = demobuffer; + demobuf.p = demobuf.buffer; } // load demo resource from WAD else if ((l = W_CheckNumForName(defdemoname)) == LUMPERROR) @@ -7273,7 +7270,7 @@ void G_DoPlayDemo(char *defdemoname) } else // it's an internal demo { - demobuffer = demo_p = W_CacheLumpNum(l, PU_STATIC); + demobuf.buffer = demobuf.p = W_CacheLumpNum(l, PU_STATIC); #if defined(SKIPERRORS) && !defined(DEVELOP) skiperrors = true; // SRB2Kart: Don't print warnings for staff ghosts, since they'll inevitably happen when we make bugfixes/changes... #endif @@ -7283,29 +7280,29 @@ void G_DoPlayDemo(char *defdemoname) // read demo header gameaction = ga_nothing; demo.playback = true; - if (memcmp(demo_p, DEMOHEADER, 12)) + if (memcmp(demobuf.p, DEMOHEADER, 12)) { snprintf(msg, 1024, M_GetText("%s is not a SRB2Kart replay file.\n"), pdemoname); CONS_Alert(CONS_ERROR, "%s", msg); M_StartMessage(msg, NULL, MM_NOTHING); Z_Free(pdemoname); - Z_Free(demobuffer); - demobuffer = NULL; + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; demo.title = false; return; } - demo_p += 12; // DEMOHEADER + demobuf.p += 12; // DEMOHEADER - version = READUINT8(demo_p); - subversion = READUINT8(demo_p); - demo.version = READUINT16(demo_p); + version = READUINT8(demobuf.p); + subversion = READUINT8(demobuf.p); + demo.version = READUINT16(demobuf.p); switch(demo.version) { case DEMOVERSION: // latest always supported // demo title - M_Memcpy(demo.titlename, demo_p, 64); - demo_p += 64; + M_Memcpy(demo.titlename, demobuf.p, 64); + demobuf.p += 64; break; #ifdef DEMO_COMPAT_100 @@ -7318,30 +7315,30 @@ void G_DoPlayDemo(char *defdemoname) CONS_Alert(CONS_ERROR, "%s", msg); M_StartMessage(msg, NULL, MM_NOTHING); Z_Free(pdemoname); - Z_Free(demobuffer); - demobuffer = NULL; + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; demo.title = false; return; } - demo_p += 16; // demo checksum - if (memcmp(demo_p, "PLAY", 4)) + demobuf.p += 16; // demo checksum + if (memcmp(demobuf.p, "PLAY", 4)) { snprintf(msg, 1024, M_GetText("%s is the wrong type of recording and cannot be played.\n"), pdemoname); CONS_Alert(CONS_ERROR, "%s", msg); M_StartMessage(msg, NULL, MM_NOTHING); Z_Free(pdemoname); - Z_Free(demobuffer); - demobuffer = NULL; + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; demo.title = false; return; } - demo_p += 4; // "PLAY" - gamemap = READINT16(demo_p); - demo_p += 16; // mapmd5 + demobuf.p += 4; // "PLAY" + gamemap = READINT16(demobuf.p); + demobuf.p += 16; // mapmd5 - demoflags = READUINT8(demo_p); + demoflags = READUINT8(demobuf.p); #ifdef DEMO_COMPAT_100 if (demo.version == 0x0001) { @@ -7351,8 +7348,8 @@ void G_DoPlayDemo(char *defdemoname) CONS_Alert(CONS_ERROR, "%s", msg); M_StartMessage(msg, NULL, MM_NOTHING); Z_Free(pdemoname); - Z_Free(demobuffer); - demobuffer = NULL; + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; demo.title = false; return; @@ -7361,17 +7358,17 @@ void G_DoPlayDemo(char *defdemoname) else { #endif - gametype = READUINT8(demo_p); + gametype = READUINT8(demobuf.p); if (demo.title) // Titledemos should always play and ought to always be compatible with whatever wadlist is running. - G_SkipDemoExtraFiles(&demo_p); + G_SkipDemoExtraFiles(&demobuf.p); else if (demo.loadfiles) - G_LoadDemoExtraFiles(&demo_p); + G_LoadDemoExtraFiles(&demobuf.p); else if (demo.ignorefiles) - G_SkipDemoExtraFiles(&demo_p); + G_SkipDemoExtraFiles(&demobuf.p); else { - UINT8 error = G_CheckDemoExtraFiles(&demo_p, false); + UINT8 error = G_CheckDemoExtraFiles(&demobuf.p, false); if (error) { @@ -7412,8 +7409,8 @@ void G_DoPlayDemo(char *defdemoname) if (!CON_Ready()) // In the console they'll just see the notice there! No point pulling them out. M_StartMessage(msg, NULL, MM_NOTHING); Z_Free(pdemoname); - Z_Free(demobuffer); - demobuffer = NULL; + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; demo.title = false; return; @@ -7435,8 +7432,8 @@ void G_DoPlayDemo(char *defdemoname) case ATTACKING_NONE: // 0 break; case ATTACKING_RECORD: // 1 - hu_demotime = READUINT32(demo_p); - hu_demolap = READUINT32(demo_p); + hu_demotime = READUINT32(demobuf.p); + hu_demolap = READUINT32(demobuf.p); break; default: // 3 modeattacking = ATTACKING_NONE; @@ -7444,33 +7441,33 @@ void G_DoPlayDemo(char *defdemoname) } // Random seed - randseed = READUINT32(demo_p); + randseed = READUINT32(demobuf.p); #ifdef DEMO_COMPAT_100 if (demo.version != 0x0001) #endif - demo_p += 4; // Extrainfo location + demobuf.p += 4; // Extrainfo location #ifdef DEMO_COMPAT_100 if (demo.version == 0x0001) { // Player name - M_Memcpy(player_names[0],demo_p,16); - demo_p += 16; + M_Memcpy(player_names[0],demobuf.p,16); + demobuf.p += 16; // Skin - M_Memcpy(skin,demo_p,16); - demo_p += 16; + M_Memcpy(skin,demobuf.p,16); + demobuf.p += 16; // Color - M_Memcpy(color,demo_p,16); - demo_p += 16; + M_Memcpy(color,demobuf.p,16); + demobuf.p += 16; - demo_p += 5; // Backwards compat - some stats + demobuf.p += 5; // Backwards compat - some stats // SRB2kart - kartspeed[0] = READUINT8(demo_p); - kartweight[0] = READUINT8(demo_p); + kartspeed[0] = READUINT8(demobuf.p); + kartweight[0] = READUINT8(demobuf.p); // - demo_p += 9; // Backwards compat - more stats + demobuf.p += 9; // Backwards compat - more stats // Skin not loaded? if (!SetPlayerSkin(0, skin)) @@ -7479,8 +7476,8 @@ void G_DoPlayDemo(char *defdemoname) CONS_Alert(CONS_ERROR, "%s", msg); M_StartMessage(msg, NULL, MM_NOTHING); Z_Free(pdemoname); - Z_Free(demobuffer); - demobuffer = NULL; + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; demo.title = false; return; @@ -7493,8 +7490,8 @@ void G_DoPlayDemo(char *defdemoname) CONS_Alert(CONS_ERROR, "%s", msg); M_StartMessage(msg, NULL, MM_NOTHING); Z_Free(pdemoname); - Z_Free(demobuffer); - demobuffer = NULL; + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; demo.title = false; return; @@ -7509,17 +7506,17 @@ void G_DoPlayDemo(char *defdemoname) } // net var data - CV_LoadNetVars(&demo_p); + CV_LoadNetVars(&demobuf.p); // Sigh ... it's an empty demo. - if (*demo_p == DEMOMARKER) + if (*demobuf.p == DEMOMARKER) { snprintf(msg, 1024, M_GetText("%s contains no data to be played.\n"), pdemoname); CONS_Alert(CONS_ERROR, "%s", msg); M_StartMessage(msg, NULL, MM_NOTHING); Z_Free(pdemoname); - Z_Free(demobuffer); - demobuffer = NULL; + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; demo.title = false; return; @@ -7553,17 +7550,17 @@ void G_DoPlayDemo(char *defdemoname) #endif // net var data - CV_LoadNetVars(&demo_p); + CV_LoadNetVars(&demobuf.p); // Sigh ... it's an empty demo. - if (*demo_p == DEMOMARKER) + if (*demobuf.p == DEMOMARKER) { snprintf(msg, 1024, M_GetText("%s contains no data to be played.\n"), pdemoname); CONS_Alert(CONS_ERROR, "%s", msg); M_StartMessage(msg, NULL, MM_NOTHING); Z_Free(pdemoname); - Z_Free(demobuffer); - demobuffer = NULL; + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; demo.title = false; return; @@ -7598,7 +7595,7 @@ void G_DoPlayDemo(char *defdemoname) memset(playeringame,0,sizeof(playeringame)); // Load players that were in-game when the map started - p = READUINT8(demo_p); + p = READUINT8(demobuf.p); for (i = 1; i < MAXSPLITSCREENPLAYERS; i++) displayplayers[i] = INT32_MAX; @@ -7617,8 +7614,8 @@ void G_DoPlayDemo(char *defdemoname) CONS_Alert(CONS_ERROR, "%s", msg); M_StartMessage(msg, NULL, MM_NOTHING); Z_Free(pdemoname); - Z_Free(demobuffer); - demobuffer = NULL; + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; demo.title = false; return; @@ -7632,8 +7629,8 @@ void G_DoPlayDemo(char *defdemoname) CONS_Alert(CONS_ERROR, "%s", msg); M_StartMessage(msg, NULL, MM_NOTHING); Z_Free(pdemoname); - Z_Free(demobuffer); - demobuffer = NULL; + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; demo.title = false; return; @@ -7646,17 +7643,17 @@ void G_DoPlayDemo(char *defdemoname) players[p].spectator = spectator; // Name - M_Memcpy(player_names[p],demo_p,16); - demo_p += 16; + M_Memcpy(player_names[p],demobuf.p,16); + demobuf.p += 16; // Skin - M_Memcpy(skin,demo_p,16); - demo_p += 16; + M_Memcpy(skin,demobuf.p,16); + demobuf.p += 16; SetPlayerSkin(p, skin); // Color - M_Memcpy(color,demo_p,16); - demo_p += 16; + M_Memcpy(color,demobuf.p,16); + demobuf.p += 16; for (i = 0; i < MAXSKINCOLORS; i++) if (!stricmp(KartColor_Names[i],color)) // SRB2kart { @@ -7665,28 +7662,28 @@ void G_DoPlayDemo(char *defdemoname) } // Score, since Kart uses this to determine where you start on the map - players[p].score = READUINT32(demo_p); + players[p].score = READUINT32(demobuf.p); // Kart stats, temporarily - kartspeed[p] = READUINT8(demo_p); - kartweight[p] = READUINT8(demo_p); + kartspeed[p] = READUINT8(demobuf.p); + kartweight[p] = READUINT8(demobuf.p); if (stricmp(skins[players[p].skin].name, skin) != 0) FindClosestSkinForStats(p, kartspeed[p], kartweight[p]); // Look for the next player - p = READUINT8(demo_p); + p = READUINT8(demobuf.p); } -// end of player read (the 0xFF marker) -// so this is where we are to read our lua variables (if possible!) + // end of player read (the 0xFF marker) + // so this is where we are to read our lua variables (if possible!) if (demoflags & DF_LUAVARS) // again, used for compability, lua shit will be saved to replays regardless of if it's even been loaded { if (!gL) // No Lua state! ...I guess we'll just start one... LUA_ClearState(); // No modeattacking check, DF_LUAVARS won't be present here. - LUA_UnArchiveDemo(&demo_p); + LUA_UnArchive(&demobuf.p, false); } splitscreen = 0; @@ -8267,22 +8264,22 @@ void G_StopMetalDemo(void) ATTRNORETURN void FUNCNORETURN G_StopMetalRecording(void) { boolean saved = false; - if (demo_p) + if (demobuf.p) { - UINT8 *p = demobuffer+16; // checksum position + UINT8 *p = demobuf.buffer+16; // checksum position #ifdef NOMD5 UINT8 i; - WRITEUINT8(demo_p, DEMOMARKER); // add the demo end marker + WRITEUINT8(demobuf.p, DEMOMARKER); // add the demo end marker for (i = 0; i < 16; i++, p++) *p = P_RandomByte(); // This MD5 was chosen by fair dice roll and most likely < 50% correct. #else - WRITEUINT8(demo_p, DEMOMARKER); // add the demo end marker - md5_buffer((char *)p+16, demo_p - (p+16), (void *)p); // make a checksum of everything after the checksum in the file. + WRITEUINT8(demobuf.p, DEMOMARKER); // add the demo end marker + md5_buffer((char *)p+16, demobuf.p - (p+16), (void *)p); // make a checksum of everything after the checksum in the file. #endif - saved = FIL_WriteFile(va("%sMS.LMP", G_BuildMapName(gamemap)), demobuffer, demo_p - demobuffer); // finally output the file. + saved = FIL_WriteFile(va("%sMS.LMP", G_BuildMapName(gamemap)), demobuf.buffer, demobuf.p - demobuf.buffer); // finally output the file. } - free(demobuffer); - demobuffer = NULL; + free(demobuf.buffer); + demobuf.buffer = NULL; metalrecording = false; if (saved) I_Error("Saved to %sMS.LMP", G_BuildMapName(gamemap)); @@ -8293,13 +8290,8 @@ ATTRNORETURN void FUNCNORETURN G_StopMetalRecording(void) // called from stopdemo command, map command, and g_checkdemoStatus. void G_StopDemo(void) { - Z_Free(demobuffer); - demobuffer = NULL; - if (demo.playback) - { - CV_SetValue(&cv_director, directorstate); - directorstate = 0; - } + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; if (demo.title) modeattacking = false; @@ -8391,6 +8383,7 @@ boolean G_CheckDemoStatus(void) G_SaveDemo(); return true; } + demo.recording = false; return false; @@ -8398,7 +8391,7 @@ boolean G_CheckDemoStatus(void) void G_SaveDemo(void) { - UINT8 *p = demobuffer+16; // after version + UINT8 *p = demobuf.buffer+16; // after version UINT32 length; #ifdef NOMD5 UINT8 i; @@ -8407,10 +8400,10 @@ void G_SaveDemo(void) // Ensure extrainfo pointer is always available, even if no info is present. if (demoinfo_p && *(UINT32 *)demoinfo_p == 0) { - WRITEUINT8(demo_p, DEMOMARKER); // add the demo end marker - *(UINT32 *)demoinfo_p = demo_p - demobuffer; + WRITEUINT8(demobuf.p, DEMOMARKER); // add the demo end marker + *(UINT32 *)demoinfo_p = demobuf.p - demobuf.buffer; } - WRITEUINT8(demo_p, DW_END); // Mark end of demo extra data. + WRITEUINT8(demobuf.p, DW_END); // Mark end of demo extra data. M_Memcpy(p, demo.titlename, 64); // Write demo title here p += 64; @@ -8461,14 +8454,14 @@ void G_SaveDemo(void) *p = M_RandomByte(); // This MD5 was chosen by fair dice roll and most likely < 50% correct. #else // Make a checksum of everything after the checksum in the file up to the end of the standard data. Extrainfo is freely modifiable. - md5_buffer((char *)p+16, (demobuffer + length) - (p+16), p); + md5_buffer((char *)p+16, (demobuf.buffer + length) - (p+16), p); #endif - if (FIL_WriteFile(va(pandf, srb2home, demoname), demobuffer, demo_p - demobuffer)) // finally output the file. + if (FIL_WriteFile(va(pandf, srb2home, demoname), demobuf.buffer, demobuf.p - demobuf.buffer)) // finally output the file. demo.savemode = DSM_SAVED; - free(demobuffer); - demobuffer = NULL; + free(demobuf.buffer); + demobuf.buffer = NULL; demo.recording = false; if (modeattacking != ATTACKING_RECORD) @@ -8537,7 +8530,6 @@ boolean G_DemoTitleResponder(event_t *ev) void G_SetGamestate(gamestate_t newstate) { gamestate = newstate; - #ifdef HAVE_DISCORDRPC DRPC_UpdatePresence(); #endif @@ -8610,4 +8602,3 @@ INT32 G_TicsToMilliseconds(tic_t tics) { return (INT32)((tics%TICRATE) * (1000.00f/TICRATE)); } - diff --git a/src/g_game.h b/src/g_game.h index 14decd858..7fbf98a8c 100644 --- a/src/g_game.h +++ b/src/g_game.h @@ -17,6 +17,7 @@ #include "doomdef.h" #include "doomstat.h" #include "d_event.h" +#include "p_saveg.h" extern char gamedatafilename[64]; extern char timeattackfolder[64]; @@ -32,7 +33,7 @@ extern INT32 player_name_changes[MAXPLAYERS]; extern player_t players[MAXPLAYERS]; extern boolean playeringame[MAXPLAYERS]; -extern UINT8 *demo_p; +extern savebuffer_t demobuf; // ====================================== // DEMO playback/recording related stuff. diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 1aadea9a3..442c12f88 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -1480,7 +1480,7 @@ boolean LUAh_HurtMsg(player_t *player, mobj_t *inflictor, mobj_t *source) return hooked; } -void LUAh_NetArchiveHook(lua_CFunction archFunc) +void LUAh_NetArchiveHook(lua_CFunction archFunc, savebuffer_t *save) { hook_p hookp; int errorhandlerindex; @@ -1501,8 +1501,9 @@ void LUAh_NetArchiveHook(lua_CFunction archFunc) // tables becomes an upvalue of archFunc lua_pushvalue(gL, -3); - lua_pushcclosure(gL, archFunc, 1); - // stack: tables, archFunc + lua_pushlightuserdata(gL, save); + lua_pushcclosure(gL, archFunc, 2); + // stack: tables, savebuffer_t, archFunc for (hookp = roothook; hookp; hookp = hookp->next) { diff --git a/src/lua_script.c b/src/lua_script.c index aec9a3037..2440be7ab 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -40,8 +40,6 @@ static lua_State *mL = 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 @@ -889,9 +887,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; } @@ -1126,7 +1125,7 @@ static UINT8 UnArchiveValueDemo(UINT8 **p, int TABLESINDEX, char field[1024]) return 0; } -static void UnArchiveExtVars(UINT8 **p, void *pointer) +static void UnArchiveExtVars(UINT8 **p, void *pointer, boolean isdemo) { int TABLESINDEX; UINT16 field_count = READUINT16(*p); @@ -1145,40 +1144,23 @@ static void UnArchiveExtVars(UINT8 **p, void *pointer) TABLESINDEX = lua_gettop(gL); lua_createtable(gL, 0, field_count); // pointer's ext vars subtable - for (i = 0; i < field_count; i++) + if (isdemo) { - READSTRING(*p, field); - UnArchiveValue(p, TABLESINDEX); - lua_setfield(gL, -2, field); + for (i = 0; i < field_count; i++) + { + READSTRING(*p, field); + if (UnArchiveValueDemo(p, TABLESINDEX, field) != 3) // This will return 3 if we shouldn't set this field. + lua_setfield(gL, -2, field); + } } - - lua_getfield(gL, LUA_REGISTRYINDEX, LREG_EXTVARS); - I_Assert(lua_istable(gL, -1)); - lua_pushlightuserdata(gL, pointer); - lua_pushvalue(gL, -3); // pointer's ext vars subtable - lua_rawset(gL, -3); - lua_pop(gL, 2); // pop LREG_EXTVARS and pointer's subtable -} - -static void UnArchiveExtVarsDemo(UINT8 **p, void *pointer) -{ - int TABLESINDEX; - UINT16 field_count = READUINT16(*p); - UINT16 i; - char field[1024]; - - if (field_count == 0) - return; - I_Assert(gL != NULL); - - TABLESINDEX = lua_gettop(gL); - lua_createtable(gL, 0, field_count); // pointer's ext vars subtable - - for (i = 0; i < field_count; i++) + else { - READSTRING(*p, field); - if (UnArchiveValueDemo(p, TABLESINDEX, field) != 3) // This will return 3 if we shouldn't set this field. + for (i = 0; i < field_count; i++) + { + READSTRING(*p, field); + UnArchiveValue(p, TABLESINDEX); lua_setfield(gL, -2, field); + } } lua_getfield(gL, LUA_REGISTRYINDEX, LREG_EXTVARS); @@ -1192,13 +1174,14 @@ static void UnArchiveExtVarsDemo(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; } -static void UnArchiveTables(UINT8 **p) +static void UnArchiveTables(UINT8 **p, boolean isdemo) { int TABLESINDEX; UINT16 i, n; @@ -1209,67 +1192,60 @@ static void UnArchiveTables(UINT8 **p) TABLESINDEX = lua_gettop(gL); n = (UINT16)lua_objlen(gL, TABLESINDEX); - for (i = 1; i <= n; i++) + if (isdemo) { - lua_rawgeti(gL, TABLESINDEX, i); - while (true) + for (i = 1; i <= n; i++) { - if (UnArchiveValue(p, TABLESINDEX) == 1) // read key - break; - if (UnArchiveValue(p, TABLESINDEX) == 2) // read value - n++; - if (lua_isnil(gL, -2)) // if key is nil (if a function etc was accidentally saved) + lua_rawgeti(gL, TABLESINDEX, i); + while (true) { - CONS_Alert(CONS_ERROR, "A nil key in table %d was found! (Invalid key type or corrupted save?)\n", i); - lua_pop(gL, 2); // pop key and value instead of setting them in the table, to prevent Lua panic errors + UINT8 ret; + + ret = UnArchiveValueDemo(p, TABLESINDEX, NULL); + if (ret == 3) + lua_pushnil(gL); + else if (ret == 1) // read key + break; + + ret = UnArchiveValueDemo(p, TABLESINDEX, NULL); + + if (ret == 3) + lua_pushnil(gL); + else if (ret == 2) // read value + n++; + + if (lua_isnil(gL, -2)) // if key is nil (if a function etc was accidentally saved) + { + CONS_Alert(CONS_ERROR, "A nil key in table %d was found! (Invalid key type or corrupted save?)\n", i); + lua_pop(gL, 2); // pop key and value instead of setting them in the table, to prevent Lua panic errors + } + else + lua_rawset(gL, -3); } - else - lua_rawset(gL, -3); + lua_pop(gL, 1); } - lua_pop(gL, 1); } -} - -static void UnArchiveTablesDemo(UINT8 **p) -{ - int TABLESINDEX; - UINT16 i, n; - - if (!gL) - return; - - TABLESINDEX = lua_gettop(gL); - - n = (UINT16)lua_objlen(gL, TABLESINDEX); - for (i = 1; i <= n; i++) + else { - lua_rawgeti(gL, TABLESINDEX, i); - while (true) + for (i = 1; i <= n; i++) { - UINT8 ret; - - ret = UnArchiveValueDemo(p, TABLESINDEX, NULL); - if (ret == 3) - lua_pushnil(gL); - else if (ret == 1) // read key - break; - - ret = UnArchiveValueDemo(p, TABLESINDEX, NULL); - - if (ret == 3) - lua_pushnil(gL); - else if (ret == 2) // read value - n++; - - if (lua_isnil(gL, -2)) // if key is nil (if a function etc was accidentally saved) + lua_rawgeti(gL, TABLESINDEX, i); + while (true) { - CONS_Alert(CONS_ERROR, "A nil key in table %d was found! (Invalid key type or corrupted save?)\n", i); - lua_pop(gL, 2); // pop key and value instead of setting them in the table, to prevent Lua panic errors + if (UnArchiveValue(p, TABLESINDEX) == 1) // read key + break; + if (UnArchiveValue(p, TABLESINDEX) == 2) // read value + n++; + if (lua_isnil(gL, -2)) // if key is nil (if a function etc was accidentally saved) + { + CONS_Alert(CONS_ERROR, "A nil key in table %d was found! (Invalid key type or corrupted save?)\n", i); + lua_pop(gL, 2); // pop key and value instead of setting them in the table, to prevent Lua panic errors + } + else + lua_rawset(gL, -3); } - else - lua_rawset(gL, -3); + lua_pop(gL, 1); } - lua_pop(gL, 1); } } @@ -1281,7 +1257,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; @@ -1294,7 +1270,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) @@ -1308,22 +1284,21 @@ 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, &players[i], "player"); } } - 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; - LUAh_NetArchiveHook(NetArchive); // call the NetArchive hook in archive mode + LUAh_NetArchiveHook(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) +void LUA_UnArchive(savebuffer_t *save, boolean network) { UINT32 mobjnum; INT32 i; @@ -1336,47 +1311,23 @@ void LUA_UnArchive(UINT8 **p) { if (!playeringame[i] && i > 0) // same here, this is to synch dediservs properly. continue; - UnArchiveExtVars(p, &players[i]); + UnArchiveExtVars(&save->p, &players[i], !network); } - do { - mobjnum = READUINT32(*p); // read a mobjnum - for (th = thinkercap.next; th != &thinkercap; th = th->next) - { - if (th->function.acp1 != (actionf_p1)P_MobjThinker) - continue; - - if (((mobj_t *)th)->mobjnum != mobjnum) // find matching mobj - continue; - - UnArchiveExtVars(p, th); // apply variables - } - } while(mobjnum != UINT32_MAX); // repeat until end of mobjs marker. - - lua_save_p = p; - LUAh_NetArchiveHook(NetUnArchive); // call the NetArchive hook in unarchive mode - UnArchiveTables(p); - - if (gL) - lua_pop(gL, 1); // pop tables -} - -// this time make sure we read from demo_p -void LUA_UnArchiveDemo(UINT8 **p) -{ - INT32 i; - - if (gL) - lua_newtable(gL); // tables to be read - - for (i = 0; i < MAXPLAYERS; i++) + if (network == true) { - if (!playeringame[i] && i > 0) // same here, this is to synch dediservs properly. - continue; - UnArchiveExtVarsDemo(p, &players[i]); + do { + mobjnum = READUINT32(save->p); // read a mobjnum + for (th = thinkercap.next; th != &thinkercap; th = th->next) + if (th->function.acp1 == (actionf_p1)P_MobjThinker + && ((mobj_t *)th)->mobjnum == mobjnum) // find matching mobj + UnArchiveExtVars(&save->p, th, !network); // apply variables + } while(mobjnum != UINT32_MAX); // repeat until end of mobjs marker. + + LUAh_NetArchiveHook(NetUnArchive, save); // call the NetArchive hook in unarchive mode } - UnArchiveTablesDemo(p); + UnArchiveTables(&save->p, !network); if (gL) lua_pop(gL, 1); // pop tables diff --git a/src/lua_script.h b/src/lua_script.h index 7039603a5..f0b77e80e 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -13,6 +13,7 @@ #include "m_fixed.h" #include "doomtype.h" #include "d_player.h" +#include "p_saveg.h" #include "blua/lua.h" #include "blua/lualib.h" @@ -55,17 +56,14 @@ void LUA_InvalidateLevel(void); void LUA_InvalidateMapthings(void); void LUA_InvalidatePlayer(player_t *player); void LUA_Step(void); -void LUA_Archive(UINT8 **p, boolean network); -void LUA_UnArchive(UINT8 **p); - -// keeping this around since demos are kinda ass -void LUA_UnArchiveDemo(UINT8 **p); +void LUA_Archive(savebuffer_t *save, boolean network); +void LUA_UnArchive(savebuffer_t *save, boolean network); void Got_Luacmd(UINT8 **cp, INT32 playernum); // lua_consolelib.c void LUA_CVarChanged(const char *name); // lua_consolelib.c int Lua_optoption(lua_State *L, int narg, int def, int list_ref); int Lua_CreateFieldTable(lua_State *L, const char *const lst[]); -void LUAh_NetArchiveHook(lua_CFunction archFunc); +void LUAh_NetArchiveHook(lua_CFunction archFunc, savebuffer_t *save); // Console wrapper void COM_Lua_f(void); diff --git a/src/p_saveg.c b/src/p_saveg.c index 9e3e8f888..50247a852 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -3501,7 +3501,7 @@ void P_SaveNetGame(savebuffer_t *save, boolean resending) P_NetArchiveSpecials(save); } - LUA_Archive(&save->p, true); + LUA_Archive(save, true); WRITEUINT8(save->p, 0x1d); // consistency marker } @@ -3549,7 +3549,7 @@ boolean P_LoadNetGame(savebuffer_t *save, boolean reloading) P_FinishMobjs(); } - LUA_UnArchive(&save->p); + LUA_UnArchive(save); // This is stupid and hacky, but maybe it'll work! P_SetRandSeed(P_GetInitSeed());