From 8d4ccf9a523559e552f6713ff521defa8ebf6a0e Mon Sep 17 00:00:00 2001 From: Fabian Greffrath Date: Sat, 3 Sep 2022 13:15:29 +0200 Subject: [PATCH] avoid demo lump name collisions (#712) * check if the demo file name gets truncated to a lump name that is already present * only apply to demos loaded from the command line * no need to copy string, lumpinfo[] isn't going to change anymore at this point * lumps called DEMO* are considered safe * correct order * Update w_wad.c * comments * reverse search order --- src/g_game.c | 3 +++ src/w_wad.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/w_wad.h | 1 + 3 files changed, 45 insertions(+) diff --git a/src/g_game.c b/src/g_game.c index ff9564c8..e0a2307b 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -3675,6 +3675,9 @@ void D_CheckNetPlaybackSkip(void); void G_DeferedPlayDemo(char* name) { + // [FG] avoid demo lump name collisions + W_DemoLumpNameCollision(&name); + defdemoname = name; gameaction = ga_playdemo; diff --git a/src/w_wad.c b/src/w_wad.c index 92855c07..4e74344f 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -555,6 +555,47 @@ boolean W_IsIWADLump (const int lump) lumpinfo[lump].wad_file == wadfiles[0]; } +// [FG] avoid demo lump name collisions +void W_DemoLumpNameCollision(char **name) +{ + const char *const safename = "DEMO1"; + char basename[9]; + int i, lump; + + ExtractFileBase(*name, basename); + + // [FG] lumps called DEMO* are considered safe + if (!strncasecmp(basename, safename, 4)) + { + return; + } + + lump = W_CheckNumForName(basename); + + if (lump >= 0) + { + for (i = lump - 1; i >= 0; i--) + { + if (!strncasecmp(lumpinfo[i].name, basename, 8)) + { + break; + } + } + + if (i >= 0) + { + fprintf(stderr, "Demo lump name collision detected with lump \'%.8s\' from %s.\n", + lumpinfo[i].name, W_WadNameForLump(i)); + + // [FG] the DEMO1 lump is almost certainly always a demo lump + M_StringCopy(lumpinfo[lump].name, safename, 8); + *name = lumpinfo[lump].name; + + W_InitLumpHash(); + } + } +} + void W_CloseFileDescriptors(void) { int i; diff --git a/src/w_wad.h b/src/w_wad.h index 855ac5f5..a8ce0eb3 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -121,6 +121,7 @@ extern void WritePredefinedLumpWad(const char *filename); // jff 5/6/98 // [FG] name of the WAD file that contains the lump const char *W_WadNameForLump (const int lump); boolean W_IsIWADLump (const int lump); +void W_DemoLumpNameCollision(char **name); void W_CloseFileDescriptors(void);