From 22e65cd5e75ef791c23933eb72c319032d0fdbc9 Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 29 Dec 2024 15:58:43 +0100 Subject: [PATCH 1/5] fixup conflicts --- src/g_game.c | 41 +++++++++++++++++++---------------------- src/lua_hooklib.c | 33 +++++++++++++++++++++++++++++++++ src/lua_script.c | 9 +++++---- src/p_saveg.c | 6 +++--- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/src/g_game.c b/src/g_game.c index 9bee4a399..708083939 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -5995,7 +5995,7 @@ void G_RecordDemo(const char *name) { INT32 maxsize; - demo_p = NULL; + demobuf.p = NULL; G_ResetDemoRecording(); demoend = NULL; @@ -6008,8 +6008,8 @@ void G_RecordDemo(const char *name) maxsize = cv_maxdemosize.value*1024*1024; - demobuffer = Z_Malloc(maxsize, PU_STATIC, NULL); - demoend = demobuffer + maxsize; + demobuf.buffer = Z_Malloc(maxsize, PU_STATIC, NULL); + demoend = demobuf.buffer + maxsize; if (demobuf.buffer) demo.recording = true; @@ -6022,12 +6022,12 @@ void G_RecordMetal(void) { INT32 maxsize; maxsize = cv_maxdemosize.value*1024*1024; - if (demobuffer) - Z_Free(demobuffer); - demo_p = NULL; + if (demobuf.buffer) + Z_Free(demobuf.buffer); + demobuf.p = NULL; metalrecording = false; - demobuffer = Z_Malloc(maxsize, PU_STATIC, NULL); - demoend = demobuffer + maxsize; + demobuf.buffer = Z_Malloc(maxsize, PU_STATIC, NULL); + demoend = demobuf.buffer + maxsize; if (demobuf.buffer) metalrecording = true; @@ -6124,7 +6124,7 @@ void G_BeginRecording(void) break; } - WRITEUINT32(demobuf.p,P_GetInitSeed()); + WRITEUINT32(demobuf.p, P_GetInitSeed()); // Reserved for extrainfo location from start of file demoinfo_p = demobuf.p; @@ -7824,8 +7824,8 @@ ATTRNORETURN void FUNCNORETURN G_StopMetalRecording(void) #endif saved = FIL_WriteFile(va("%sMS.LMP", G_BuildMapName(gamemap)), demobuf.buffer, demobuf.p - demobuf.buffer); // finally output the file. } - Z_Free(demobuffer); - demobuffer = NULL; + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; metalrecording = false; if (saved) I_Error("Saved to %sMS.LMP", G_BuildMapName(gamemap)); @@ -7934,10 +7934,7 @@ boolean G_CheckDemoStatus(void) if (modeattacking || demo.savemode != DSM_NOTSAVING) { - if (demobuf.p) - { - G_SaveDemo(); - } + G_SaveDemo(); return true; } @@ -7948,9 +7945,9 @@ boolean G_CheckDemoStatus(void) void G_ResetDemoRecording(void) { - if (demobuffer) - Z_Free(demobuffer); - demobuffer = NULL; + if (demobuf.buffer) + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.recording = false; } @@ -7958,16 +7955,16 @@ static void G_ResetDemoPlayback(char *pdemoname) { if (pdemoname) Z_Free(pdemoname); - if (demobuffer) - Z_Free(demobuffer); - demobuffer = NULL; + if (demobuf.buffer) + Z_Free(demobuf.buffer); + demobuf.buffer = NULL; demo.playback = false; demo.title = false; } void G_SaveDemo(void) { - if (!demo_p) + if (!demobuf.p) { CONS_Alert(CONS_ERROR, "Failed to save Demo. No Demo pointer exists!\n"); // reset the demo buffer diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 9276e61aa..0a8ee9cae 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -934,6 +934,7 @@ int LUA_HookHurtMsg(player_t *player, mobj_t *inflictor, mobj_t *source) return hook.status; } +#if 0 void LUA_HookNetArchive(lua_CFunction archFunc) { const hook_t * map = &hookIds[HOOK(NetVars)]; @@ -964,6 +965,38 @@ void LUA_HookNetArchive(lua_CFunction archFunc) // stack: tables } } +#endif + +void LUA_HookNetArchive(lua_CFunction archFunc, savebuffer_t *save) // +{ + const hook_t * map = &hookIds[HOOK(NetVars)]; + Hook_State hook; + /* this is a remarkable case where the stack isn't reset */ + if (map->numHooks > 0) + { + // stack: tables + I_Assert(lua_gettop(gL) > 0); + I_Assert(lua_istable(gL, -1)); + + push_error_handler(); + lua_insert(gL, EINDEX); + + begin_hook_values(&hook); + + // tables becomes an upvalue of archFunc + lua_pushvalue(gL, -1); + lua_pushlightuserdata(gL, save); + lua_pushcclosure(gL, archFunc, 2); + // stack: tables, savebuffer_t, archFunc + + init_hook_call(&hook, 0, res_none); + call_mapped(&hook, map); + + lua_pop(gL, 1); // pop archFunc + lua_remove(gL, EINDEX); // pop error handler + // stack: tables + } +} void LUA_HookPlayerQuit(player_t *plr, int reason) { diff --git a/src/lua_script.c b/src/lua_script.c index cb28cc906..6f439479e 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -1309,11 +1309,11 @@ void LUA_Archive(savebuffer_t *save, boolean network) } WRITEUINT32(save->p, UINT32_MAX); // end of mobjs marker, replaces mobjnum. - LUAh_NetArchiveHook(NetArchive, save); // call the NetArchive hook in archive mode + LUA_HookNetArchive(NetArchive, save); // call the NetArchive hook in archive mode + //LUA_HookNetArchive(NetArchive); // call the NetArchive hook in archive mode } - LUA_HookNetArchive(NetArchive); // call the NetArchive hook in archive mode - ArchiveTables(); + ArchiveTables(&save->p); if (gL) lua_pop(gL, 1); // pop tables @@ -1350,7 +1350,8 @@ void LUA_UnArchive(savebuffer_t *save, boolean network) } } while(mobjnum != UINT32_MAX); // repeat until end of mobjs marker. - LUAh_NetArchiveHook(NetUnArchive, save); // call the NetArchive hook in unarchive mode + LUA_HookNetArchive(NetUnArchive, save); // call the NetArchive hook in unarchive mode + //LUA_HookNetArchive(NetUnArchive); // call the NetArchive hook in unarchive mode } UnArchiveTables(&save->p, network); diff --git a/src/p_saveg.c b/src/p_saveg.c index 4b79bc886..898a53d68 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -296,7 +296,7 @@ static void P_NetArchivePlayers(savebuffer_t *save, boolean resending) // // P_NetUnArchivePlayers // -static void P_NetUnArchivePlayers(boolean reloading) +static void P_NetUnArchivePlayers(savebuffer_t *save, boolean reloading) { INT32 i, j; UINT16 flags; @@ -307,7 +307,7 @@ static void P_NetUnArchivePlayers(boolean reloading) for (i = 0; i < MAXPLAYERS; i++) { if (reloading) - adminplayers[i] = (INT32)READSINT8(save_p); + adminplayers[i] = (INT32)READSINT8(save->p); // Do NOT memset player struct to 0 // other areas may initialize data elsewhere @@ -318,7 +318,7 @@ static void P_NetUnArchivePlayers(boolean reloading) // NOTE: sending tics should (hopefully) no longer be necessary if (reloading) - READSTRINGN(save_p, player_names[i], MAXPLAYERNAME); + READSTRINGN(save->p, player_names[i], MAXPLAYERNAME); players[i].aiming = READANGLE(save->p); players[i].awayviewaiming = READANGLE(save->p); From 8e10bcc7398b6c1c95fa482ee4430e0361d81020 Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 29 Dec 2024 15:59:10 +0100 Subject: [PATCH 2/5] add a bunch of extra checks into demo stuff buffer may be NULL in some cases should fix dedis crashing when replay recording is disabled --- src/g_game.c | 46 +++++++++++++++++++++++++++++++++++++--------- src/p_setup.c | 5 +++-- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/g_game.c b/src/g_game.c index 708083939..e48c53577 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -4782,6 +4782,7 @@ void G_WriteDemoTiccmd(ticcmd_t *cmd, INT32 playernum) if (!demobuf.p) return; + ziptic_p = demobuf.p++; // the ziptic, written at the end of this function if (cmd->forwardmove != oldcmd[playernum].forwardmove) @@ -4910,10 +4911,14 @@ void G_GhostAddHit(INT32 playernum, mobj_t *victim) void G_WriteAllGhostTics(void) { + if (!demobuf.p) + return; + UINT8 *save_demo_p = demobuf.p; #define CHECKSPACE(num) if (demobuf.p+(num) > demoend) { demobuf.p = save_demo_p; G_CheckDemoStatus(); return; } INT32 i, counter = leveltime; + for (i = 0; i < MAXPLAYERS; i++) { if (!playeringame[i] || players[i].spectator) @@ -4947,11 +4952,12 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) UINT8 sprite; UINT8 frame; + if (!demobuf.p) + return; + UINT8 *save_demo_p = demobuf.p; #define CHECKSPACE(num) if (demobuf.p+(num) > demoend) { demobuf.p = save_demo_p; G_CheckDemoStatus(); return; } - if (!demobuf.p) - return; if (!(demoflags & DF_GHOST)) return; // No ghost data to write. @@ -5144,6 +5150,9 @@ void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) void G_ConsAllGhostTics(void) { + if (!demobuf.p) + return; + UINT8 p = READUINT8(demobuf.p); while (p != 0xFF) @@ -5204,7 +5213,8 @@ void G_ConsGhostTic(INT32 playernum) demobuf.p++; if (ziptic & GZT_SPRITE) demobuf.p++; - if(ziptic & GZT_NIGHTS) { + if (ziptic & GZT_NIGHTS) + { if (!testmo || !testmo->player || !(testmo->player->pflags & PF_NIGHTSMODE) || !testmo->tracer) nightsfail = true; else @@ -6045,8 +6055,12 @@ void G_BeginRecording(void) UINT8 totalfiles; UINT8 *m; - if (demobuf.p) + if (!cv_recordmultiplayerdemos.value) return; + + if (demobuf.p || demobuf.buffer == NULL) + return; + memset(name,0,sizeof(name)); demobuf.p = demobuf.buffer; @@ -6056,7 +6070,7 @@ void G_BeginRecording(void) demoflags |= DF_ENCORE; if (!modeattacking && gL) // Ghosts don't read luavars, and you shouldn't ever need to save Lua in replays, you doof! - // SERIOUSLY THOUGH WHY WOULD YOU LOAD HOSTMOD AND RECORD A GHOST WITH IT !???? + // SERIOUSLY THOUGH WHY WOULD YOU LOAD HOSTMOD AND RECORD A GHOST WITH IT !???? demoflags |= DF_LUAVARS; // Setup header. @@ -6201,7 +6215,7 @@ void G_BeginMetal(void) { mobj_t *mo = players[consoleplayer].mo; - if (demobuf.p) + if (demobuf.p || demobuf.buffer == NULL) return; demobuf.p = demobuf.buffer; @@ -6229,7 +6243,10 @@ void G_WriteStanding(UINT8 ranking, char *name, INT32 skinnum, UINT8 color, UINT { char temp[17]; - if (demoinfo_p && *(UINT32 *)demoinfo_p == 0) + if (!demobuf.p) + return; + + if (demoinfo_p && *(UINT32 *)demoinfo_p == 0 && demobuf.buffer != NULL) { WRITEUINT8(demobuf.p, DEMOMARKER); // add the demo end marker *(UINT32 *)demoinfo_p = demobuf.p - demobuf.buffer; @@ -6815,6 +6832,9 @@ void G_DoPlayDemo(char *defdemoname) boolean spectator; UINT8 slots[MAXPLAYERS], kartspeed[MAXPLAYERS], kartweight[MAXPLAYERS], numslots = 0; + if (demobuf.buffer == NULL) + return; + G_InitDemoRewind(); skin[16] = '\0'; @@ -7810,7 +7830,7 @@ void G_StopMetalDemo(void) ATTRNORETURN void FUNCNORETURN G_StopMetalRecording(void) { boolean saved = false; - if (demobuf.p) + if (demobuf.p && demobuf.buffer != NULL) { UINT8 *p = demobuf.buffer+16; // checksum position #ifdef NOMD5 @@ -7972,7 +7992,15 @@ void G_SaveDemo(void) return; } - UINT8 *p = demobuffer+16; // after version + if (demobuf.buffer == NULL) + { + CONS_Alert(CONS_ERROR, "Failed to save Demo. No Demo buffer allocated!\n"); + // reset the demo buffer + G_ResetDemoRecording(); + return; + } + + UINT8 *p = demobuf.buffer+16; // after version UINT32 length; #ifdef NOMD5 UINT8 i; diff --git a/src/p_setup.c b/src/p_setup.c index 280c91935..0e7f89dcf 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -2908,8 +2908,9 @@ boolean P_SetupLevel(boolean skipprecip, boolean reloadinggamestate) : mapheaderinfo[gamemap - 1]->numlaps); // Start recording replay in multiplayer with a temp filename - //Ensure dedis only record a replay if there is a player at the start of the map, otherwise we get invalid replays! - if (!demo.playback && multiplayer && D_NumPlayers()) { + // Ensure dedis only record a replay if there is a player at the start of the map, otherwise we get invalid replays! + if (!demo.playback && multiplayer && D_NumPlayers()) + { static char buf[256]; sprintf(buf, "replay"PATHSEP"online"PATHSEP"%d-%s", (int) (time(NULL)), G_BuildMapName(gamemap)); From a15615a2299835dda888daefaf4bf8d01b1f0cac Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 29 Dec 2024 16:01:35 +0100 Subject: [PATCH 3/5] add missing include --- src/sdl/ogl_sdl.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sdl/ogl_sdl.c b/src/sdl/ogl_sdl.c index 65fae4dd4..9093f9a18 100644 --- a/src/sdl/ogl_sdl.c +++ b/src/sdl/ogl_sdl.c @@ -45,6 +45,8 @@ #include "../m_argv.h" #include "../i_video.h" +#include "../f_finale.h" + #ifdef DEBUG_TO_FILE #include #if defined (_WIN32) && !defined (__CYGWIN__) From eceb671ea801cb1c9816979fefd696d8b08a6ce5 Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 29 Dec 2024 16:01:35 +0100 Subject: [PATCH 4/5] add missing include --- src/sdl/ogl_sdl.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sdl/ogl_sdl.c b/src/sdl/ogl_sdl.c index 65fae4dd4..9093f9a18 100644 --- a/src/sdl/ogl_sdl.c +++ b/src/sdl/ogl_sdl.c @@ -45,6 +45,8 @@ #include "../m_argv.h" #include "../i_video.h" +#include "../f_finale.h" + #ifdef DEBUG_TO_FILE #include #if defined (_WIN32) && !defined (__CYGWIN__) From 66f89fb4adbcb7117aa95abe4bbad6216758382e Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 29 Dec 2024 16:03:56 +0100 Subject: [PATCH 5/5] oops --- src/lua_baselib.c | 4 ++-- src/lua_script.h | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 6bca10cbe..c621bd256 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -1829,7 +1829,7 @@ static int lib_sStartSoundAtVolume(lua_State *L) return LUA_ErrInvalid(L, "player_t"); } if (!player || P_IsLocalPlayer(player)) - S_StartSoundAtVolume(origin, sound_id, volume); + S_StartSoundAtVolume(origin, sound_id, volume); return 0; } @@ -1931,7 +1931,7 @@ static int lib_sChangeMusic(lua_State *L) music_flags = (UINT16)((music_num & 0x7FFF0000) >> 16); else #endif - music_flags = (UINT16)luaL_optinteger(L, 4, 0); + music_flags = (UINT16)luaL_optinteger(L, 4, 0); position = (UINT32)luaL_optinteger(L, 5, 0); prefadems = (UINT32)luaL_optinteger(L, 6, 0); diff --git a/src/lua_script.h b/src/lua_script.h index cf17ccbfa..9b8c65e20 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -63,7 +63,8 @@ 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 LUA_HookNetArchive(lua_CFunction archFunc); +void LUA_HookNetArchive(lua_CFunction archFunc, savebuffer_t *save); +//void LUA_HookNetArchive(lua_CFunction archFunc); // Console wrapper void COM_Lua_f(void);