From 927f30e51fbdbdaa68835378afc5973718611969 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Tue, 24 Jun 2025 00:56:45 +0200 Subject: [PATCH 01/18] Aww yeah, string buffers baby! --- src/hardware/hw_main.c | 15 +---- src/k_brightmap.c | 10 +--- src/k_terrain.c | 13 +---- src/lua_script.c | 16 +---- src/p_setup.c | 16 ++--- src/r_picformats.c | 2 +- src/r_skins.c | 4 +- src/r_textures.c | 2 +- src/r_things.cpp | 13 +++-- src/s_sound.c | 2 +- src/strbuf.h | 5 ++ src/w_wad.c | 129 +++++++++++++++++------------------------ src/w_wad.h | 11 ++-- 13 files changed, 92 insertions(+), 146 deletions(-) diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index 609c96e50..472f1c1fd 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -6552,19 +6552,6 @@ void HWR_DrawScreenFinalTexture(int width, int height) HWD.pfnDrawScreenFinalTexture(width, height); } -static inline UINT16 HWR_FindShaderDefs(UINT16 wadnum) -{ - UINT16 i; - lumpinfo_t *lump_p; - - lump_p = wadfiles[wadnum]->lumpinfo; - for (i = 0; i < wadfiles[wadnum]->numlumps; i++, lump_p++) - if (memcmp(lump_p->name, "SHADERS", 7) == 0) - return i; - - return INT16_MAX; -} - boolean HWR_CompileShaders(void) { return HWD.pfnCompileShaders(); @@ -6603,7 +6590,7 @@ void HWR_LoadCustomShadersFromFile(UINT16 wadnum, boolean PK3) int shadertype = 0; int i; - lump = HWR_FindShaderDefs(wadnum); + lump = W_CheckNumForNamePwad("SHADERS", wadnum, 0); if (lump == INT16_MAX) return; diff --git a/src/k_brightmap.c b/src/k_brightmap.c index cd213ae5c..edb9f95a0 100644 --- a/src/k_brightmap.c +++ b/src/k_brightmap.c @@ -190,15 +190,9 @@ void K_InitBrightmapsPwad(INT32 wadNum) size_t size = W_LumpLengthPwad(wadNum, lumpNum); char *datacopy; - size_t nameLength = strlen(wadfiles[wadNum]->filename) + 1 + strlen(lump_p->fullname); // length of file name, '|', and lump name - char *name = malloc(nameLength + 1); - - sprintf(name, "%s|%s", wadfiles[wadNum]->filename, lump_p->fullname); - name[nameLength] = '\0'; - size = W_LumpLengthPwad(wadNum, lumpNum); - CONS_Printf(M_GetText("Loading BRIGHT from %s\n"), name); + CONS_Printf(M_GetText("Loading BRIGHT from %s|%s\n"), wadfiles[wadNum]->filename, strbuf_get(lumpnamebuf, lump_p->fullname)); datacopy = (char *)Z_Malloc((size+1)*sizeof(char),PU_STATIC,NULL); memmove(datacopy,data,size); @@ -209,8 +203,6 @@ void K_InitBrightmapsPwad(INT32 wadNum) K_BRIGHTLumpParser(datacopy, size); Z_Free(datacopy); - - free(name); } lumpNum = W_CheckNumForNamePwad("BRIGHT", (UINT16)wadNum, lumpNum + 1); diff --git a/src/k_terrain.c b/src/k_terrain.c index 91837a81b..b786ace31 100644 --- a/src/k_terrain.c +++ b/src/k_terrain.c @@ -2271,7 +2271,7 @@ boolean K_InitTerrainPwad(UINT16 wadNum) { UINT8 *data; - if (memcmp(lump_p->name, "TERRAIN", 8) != 0) + if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname), "TERRAIN", 8) != 0) { continue; } @@ -2288,17 +2288,10 @@ boolean K_InitTerrainPwad(UINT16 wadNum) size_t size = W_LumpLengthPwad(wadNum, lumpNum); char *datacopy; - size_t nameLength = strlen(wadfiles[wadNum]->filename) + 1 + strlen(lump_p->fullname); // length of file name, '|', and lump name - char *name = malloc(nameLength + 1); - - sprintf(name, "%s|%s", wadfiles[wadNum]->filename, lump_p->fullname); - name[nameLength] = '\0'; - size = W_LumpLengthPwad(wadNum, lumpNum); - CONS_Printf(M_GetText("Loading TERRAIN from %s\n"), name); + CONS_Printf(M_GetText("Loading TERRAIN from %s|%s\n"), wadfiles[wadNum]->filename, strbuf_get(lumpnamebuf, lump_p->fullname)); terrainLoaded = true; - datacopy = (char *)Z_Malloc((size+1)*sizeof(char),PU_STATIC,NULL); memmove(datacopy,data,size); @@ -2309,8 +2302,6 @@ boolean K_InitTerrainPwad(UINT16 wadNum) K_TERRAINLumpParser(datacopy, size); Z_Free(datacopy); - - free(name); } } diff --git a/src/lua_script.c b/src/lua_script.c index d77ab74a2..f8b2846f5 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -654,7 +654,6 @@ void LUA_LoadLump(UINT16 wad, UINT16 lump, boolean noresults) { MYFILE f; char *name; - size_t len; if (M_CheckParm("-nolua")) { @@ -667,21 +666,10 @@ void LUA_LoadLump(UINT16 wad, UINT16 lump, boolean noresults) W_ReadLumpPwad(wad, lump, f.data); f.curpos = f.data; - len = strlen(wadfiles[wad]->filename); // length of file name - if (wadfiles[wad]->type == RET_LUA) - { - name = malloc(len+1); - strcpy(name, wadfiles[wad]->filename); - } + name = xva("%s", wadfiles[wad]->filename); else // If it's not a .lua file, copy the lump name in too. - { - lumpinfo_t *lump_p = &wadfiles[wad]->lumpinfo[lump]; - len += 1 + strlen(lump_p->fullname); // length of file name, '|', and lump name - name = malloc(len+1); - sprintf(name, "%s|%s", wadfiles[wad]->filename, lump_p->fullname); - name[len] = '\0'; - } + name = xva("%s|%s", wadfiles[wad]->filename, strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[lump].fullname)); lua_compatmode = wadfiles[wad]->compatmode; diff --git a/src/p_setup.c b/src/p_setup.c index 061c50939..bf31a7ccf 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -9038,11 +9038,11 @@ void P_LoadSoundsRange(UINT16 wadnum, UINT16 first, UINT16 num) // Let's check whether it's replacing an existing sound or it's a brand new one. for (j = 1; j < NUMSFX; j++) { - if (S_sfx[j].name && !strnicmp(S_sfx[j].name, lumpinfo->name + 2, 6)) + if (S_sfx[j].name && !strnicmp(S_sfx[j].name, strbuf_get(lumpnamebuf, lumpinfo->longname) + 2, 6)) { // the sound will be reloaded when needed, // since sfx->data will be NULL - CONS_Debug(DBG_SETUP, "Sound %.8s replaced\n", lumpinfo->name); + CONS_Debug(DBG_SETUP, "Sound %.8s replaced\n", strbuf_get(lumpnamebuf, lumpinfo->longname)); I_FreeSfx(&S_sfx[j]); break; // there shouldn't be two sounds with the same name, so stop looking @@ -9053,6 +9053,7 @@ void P_LoadSoundsRange(UINT16 wadnum, UINT16 first, UINT16 num) // Auxiliary function for PK3 loading - looks for music and music replacements. // NOTE: does nothing but print debug messages. The code is handled somewhere else. +/* void P_LoadMusicsRange(UINT16 wadnum, UINT16 first, UINT16 num) { lumpinfo_t *lumpinfo = wadfiles[wadnum]->lumpinfo + first; @@ -9071,18 +9072,19 @@ void P_LoadMusicsRange(UINT16 wadnum, UINT16 first, UINT16 num) } return; } +*/ // Auxiliary function - input a folder name and gives us the resource markers positions. static lumpinfo_t* FindFolder(const char *folName, UINT16 *start, UINT16 *end, lumpinfo_t *lumpinfo, UINT16 *pnumlumps, size_t *pi) { UINT16 numlumps = *pnumlumps; size_t i = *pi; - if (!stricmp(lumpinfo->fullname, folName)) + if (!stricmp(strbuf_get(lumpnamebuf, lumpinfo->fullname), folName)) { lumpinfo++; *start = ++i; for (; i < numlumps; i++, lumpinfo++) - if (strnicmp(lumpinfo->fullname, folName, strlen(folName))) + if (strnicmp(strbuf_get(lumpnamebuf, lumpinfo->fullname), folName, strlen(folName))) break; lumpinfo--; *end = i-- - *start; @@ -9316,8 +9318,8 @@ UINT16 P_PartialAddWadFile(const char *wadfilename, wadcompat_t compat) // P_LoadDehackRange(wadnum, socPos, socNum); if (sfxNum) // Sounds. TODO: Function currently only updates already existing sounds, the rest is handled somewhere else. P_LoadSoundsRange(wadnum, sfxPos, sfxNum); - if (musNum) // Music. TODO: Useless function right now. - P_LoadMusicsRange(wadnum, musPos, musNum); +// if (musNum) // Music. TODO: Useless function right now. +// P_LoadMusicsRange(wadnum, musPos, musNum); // if (sprNum) // Sprites. // R_LoadSpritsRange(wadnum, sprPos, sprNum); // if (texNum) // Textures. TODO: R_LoadTextures() does the folder positioning once again. New function maybe? @@ -9329,7 +9331,7 @@ UINT16 P_PartialAddWadFile(const char *wadfilename, wadcompat_t compat) lumpinfo = wadfiles[wadnum]->lumpinfo; for (i = 0; i < numlumps; i++, lumpinfo++) { - name = lumpinfo->name; + name = strbuf_get(lumpnamebuf, lumpinfo->longname); if (name[0] == 'D') { if (name[1] == 'S') diff --git a/src/r_picformats.c b/src/r_picformats.c index db33c33d6..bb131a25d 100644 --- a/src/r_picformats.c +++ b/src/r_picformats.c @@ -1860,7 +1860,7 @@ void R_LoadSpriteInfoLumps(UINT16 wadnum, UINT16 numlumps) for (i = 0; i < numlumps; i++, lumpinfo++) { - name = lumpinfo->name; + name = strbuf_get(lumpnamebuf, lumpinfo->longname); // Load SPRTINFO and SPR_ lumps as SpriteInfo if (!memcmp(name, "SPRTINFO", 8) || !memcmp(name, "SPR_", 4)) R_ParseSPRTINFOLump(wadnum, i); diff --git a/src/r_skins.c b/src/r_skins.c index 15f2034a7..e02fec575 100644 --- a/src/r_skins.c +++ b/src/r_skins.c @@ -362,7 +362,7 @@ static UINT16 W_CheckForSkinMarkerInPwad(UINT16 wadid, UINT16 startlump) { lump_p = wadfiles[wadid]->lumpinfo + startlump; for (i = startlump; i < wadfiles[wadid]->numlumps; i++, lump_p++) - if (memcmp(lump_p->name,S_SKIN,6)==0) + if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname),S_SKIN,6)==0) return i; } return INT16_MAX; // not found @@ -393,7 +393,7 @@ static UINT16 W_CheckForPatchSkinMarkerInPwad(UINT16 wadid, UINT16 startlump) { lump_p = wadfiles[wadid]->lumpinfo + startlump; for (i = startlump; i < wadfiles[wadid]->numlumps; i++, lump_p++) - if (memcmp(lump_p->name,P_SKIN,6)==0) + if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname),P_SKIN,6)==0) return i; } return INT16_MAX; // not found diff --git a/src/r_textures.c b/src/r_textures.c index d8c57b49d..9cf14c778 100644 --- a/src/r_textures.c +++ b/src/r_textures.c @@ -287,7 +287,7 @@ static boolean R_CheckTextureLumpLength(texture_t *texture, size_t patch) texture->name, sizeu1(offsetof(softwarepatch_t, columnofs)), sizeu2(lumplength), - wadfiles[wadnum]->lumpinfo[lumpnum].fullname + strbuf_get(lumpnamebuf, wadfiles[wadnum]->lumpinfo[lumpnum].fullname) ); return false; diff --git a/src/r_things.cpp b/src/r_things.cpp index 682768d2d..66e1674e6 100644 --- a/src/r_things.cpp +++ b/src/r_things.cpp @@ -329,7 +329,8 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 for (l = startlump; l < endlump; l++) { - if (memcmp(lumpinfo[l].name,sprname,4)) + const char *longname = strbuf_get(lumpnamebuf, lumpinfo[l].longname); + if (memcmp(longname,sprname,4)) continue; { @@ -339,8 +340,8 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 boolean isPNG = false; #endif - frame = R_Char2Frame(lumpinfo[l].name[4], compat); - rotation = R_Char2Rotation(lumpinfo[l].name[5]); + frame = R_Char2Frame(longname[4], compat); + rotation = R_Char2Rotation(longname[5]); if (frame >= 64 || rotation == 255) // Give an actual NAME error -_-... { @@ -391,10 +392,10 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 R_InstallSpriteLump(wadnum, l, numspritelumps, frame, rotation, 0); - if (lumpinfo[l].name[6]) + if (longname[6]) { - frame = R_Char2Frame(lumpinfo[l].name[6], compat); - rotation = R_Char2Rotation(lumpinfo[l].name[7]); + frame = R_Char2Frame(longname[6], compat); + rotation = R_Char2Rotation(longname[7]); if (frame >= 64 || rotation == 255) // Give an actual NAME error -_-... { diff --git a/src/s_sound.c b/src/s_sound.c index 306e7bac3..332657f36 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1392,7 +1392,7 @@ MusicDefError CONS_Alert(level, va("%%s|%%s: %s (line %%d)\n", description), wad->filename, - lump->fullname, + strbuf_get(lumpnamebuf, lump->fullname), field, line ); diff --git a/src/strbuf.h b/src/strbuf.h index 85bef9860..0a17a55cb 100644 --- a/src/strbuf.h +++ b/src/strbuf.h @@ -9,6 +9,9 @@ /// \file strbuf.h /// \brief string buffer library, for making chunks of many small strings +#ifndef __STRBUF_H__ +#define __STRBUF_H__ + #ifdef __cplusplus extern "C" { #endif @@ -36,3 +39,5 @@ FUNCINLINE static ATTRINLINE char *strbuf_get(strbuf_t *strbuf, UINT32 ofs) #ifdef __cplusplus } #endif + +#endif diff --git a/src/w_wad.c b/src/w_wad.c index 2d669bd37..bbe7a1d22 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -101,6 +101,8 @@ typedef struct lumpnum_cache_s static lumpnum_cache_t lumpnumcache[LUMPNUMCACHESIZE]; static UINT16 lumpnumcacheindex = 0; +strbuf_t *lumpnamebuf; + //=========================================================================== // GLOBALS //=========================================================================== @@ -120,18 +122,11 @@ void W_Shutdown(void) fclose(wad->handle); Z_Free(wad->filename); - while (wad->numlumps--) - { - Z_Free(wad->lumpinfo[wad->numlumps].longname); - if (wad->lumpinfo[wad->numlumps].fullname != wad->lumpinfo[wad->numlumps].longname) - { - Z_Free(wad->lumpinfo[wad->numlumps].fullname); - } - } - Z_Free(wad->lumpinfo); Z_Free(wad); } + + Z_Free(lumpnamebuf); } //=========================================================================== @@ -257,14 +252,8 @@ static inline void W_LoadDehackedLumpsPK3(UINT16 wadnum, boolean mainfile) for(; posStart < posEnd; posStart++) { - lumpinfo_t *lump_p = &wadfiles[wadnum]->lumpinfo[posStart]; - size_t length = strlen(wadfiles[wadnum]->filename) + 1 + strlen(lump_p->fullname); // length of file name, '|', and lump name - char *name = malloc(length + 1); - sprintf(name, "%s|%s", wadfiles[wadnum]->filename, lump_p->fullname); - name[length] = '\0'; - CONS_Printf(M_GetText("Loading SOC from %s\n"), name); + CONS_Printf(M_GetText("Loading SOC from %s|%s\n"), wadfiles[wadnum]->filename, strbuf_get(lumpnamebuf, wadfiles[wadnum]->lumpinfo[posStart].fullname)); DEH_LoadDehackedLumpPwad(wadnum, posStart, mainfile); - free(name); } } } @@ -278,30 +267,24 @@ static inline void W_LoadDehackedLumps(UINT16 wadnum, boolean mainfile) { lumpinfo_t *lump_p = wadfiles[wadnum]->lumpinfo; for (lump = 0; lump < wadfiles[wadnum]->numlumps; lump++, lump_p++) - if (memcmp(lump_p->name,"LUA_",4)==0) + if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname),"LUA_",4)==0) LUA_LoadLump(wadnum, lump, true); } { lumpinfo_t *lump_p = wadfiles[wadnum]->lumpinfo; for (lump = 0; lump < wadfiles[wadnum]->numlumps; lump++, lump_p++) - if (memcmp(lump_p->name,"SOC_",4)==0) // Check for generic SOC lump + if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname),"SOC_",4)==0) // Check for generic SOC lump { // shameless copy+paste of code from LUA_LoadLump - size_t length = strlen(wadfiles[wadnum]->filename) + 1 + strlen(lump_p->fullname); // length of file name, '|', and lump name - char *name = malloc(length + 1); - sprintf(name, "%s|%s", wadfiles[wadnum]->filename, lump_p->fullname); - name[length] = '\0'; - - CONS_Printf(M_GetText("Loading SOC from %s\n"), name); + CONS_Printf(M_GetText("Loading SOC from %s|%s\n"), wadfiles[wadnum]->filename, strbuf_get(lumpnamebuf, lump_p->fullname)); DEH_LoadDehackedLumpPwad(wadnum, lump, mainfile); - free(name); } - else if (memcmp(lump_p->name,"MAINCFG",8)==0) // Check for MAINCFG + else if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname),"MAINCFG",8)==0) // Check for MAINCFG { CONS_Printf(M_GetText("Loading main config from %s\n"), wadfiles[wadnum]->filename); DEH_LoadDehackedLumpPwad(wadnum, lump, mainfile); } - else if (memcmp(lump_p->name,"OBJCTCFG",8)==0) // Check for OBJCTCFG + else if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname),"OBJCTCFG",9)==0) // Check for OBJCTCFG { CONS_Printf(M_GetText("Loading object config from %s\n"), wadfiles[wadnum]->filename); DEH_LoadDehackedLumpPwad(wadnum, lump, mainfile); @@ -455,19 +438,10 @@ static lumpinfo_t* ResGetLumpsStandalone (FILE* handle, UINT16* numlumps, const fseek(handle, 0, SEEK_END); lumpinfo->size = ftell(handle); fseek(handle, 0, SEEK_SET); - strcpy(lumpinfo->name, lumpname); + + lumpinfo->longname = lumpinfo->fullname = strbuf_append(&lumpnamebuf, lumpname); lumpinfo->hash = quickncasehash(lumpname, 8); - // Allocate the lump's long name. - lumpinfo->longname = Z_Malloc(9 * sizeof(char), PU_STATIC, NULL); - strcpy(lumpinfo->longname, lumpname); - lumpinfo->longname[8] = '\0'; - - // Allocate the lump's full name. - lumpinfo->fullname = Z_Malloc(9 * sizeof(char), PU_STATIC, NULL); - strcpy(lumpinfo->fullname, lumpname); - lumpinfo->fullname[8] = '\0'; - *numlumps = 1; return lumpinfo; } @@ -553,9 +527,6 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen else lump_p->compression = CM_NOCOMPRESSION; - memset(lump_p->name, 0x00, 9); - strncpy(lump_p->name, fileinfo->name, 8); - if (WADNAMECHECK(fileinfo->name)) { size_t namelen; @@ -601,26 +572,28 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen namelen = strlen(trimname); // Allocate the lump's long and full name (save on memory). - lump_p->longname = lump_p->fullname = Z_Calloc(namelen * sizeof(char), PU_STATIC, NULL); - strncpy(lump_p->longname, trimname, namelen); - lump_p->longname[namelen-1] = '\0'; + char *name = strdup(trimname); + name[namelen-1] = '\0'; + lump_p->longname = lump_p->fullname = strbuf_append(&lumpnamebuf, name); - CONS_Debug(DBG_SETUP, "WADNAME handling:\n -- path %s\n -- interpreted lumpname %s\n", filename, lump_p->longname); + CONS_Debug(DBG_SETUP, "WADNAME handling:\n -- path %s\n -- interpreted lumpname %s\n", filename, trimname); // Grab the hash from the first part - lump_p->hash = quickncasehash(lump_p->longname, 8); + lump_p->hash = quickncasehash(name, 8); + free(name); wadnamelump = i | (numwadfiles << 16); } else { - // Set up true hash - lump_p->hash = quickncasehash(lump_p->name, 8); - // Allocate the lump's long and full name (save on memory). - lump_p->longname = lump_p->fullname = Z_Malloc(9 * sizeof(char), PU_STATIC, NULL); - strncpy(lump_p->longname, fileinfo->name, 8); - lump_p->longname[8] = '\0'; + char name[9]; + strncpy(name, fileinfo->name, 8); + name[8] = '\0'; + lump_p->longname = lump_p->fullname = strbuf_append(&lumpnamebuf, name); + + // Set up true hash + lump_p->hash = quickncasehash(name, 8); } } free(fileinfov); @@ -784,15 +757,15 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) if ((dotpos = strrchr(trimname, '.')) == 0) dotpos = fullname + strlen(fullname); // Watch for files without extension. - memset(lump_p->name, '\0', 9); // Making sure they're initialized to 0. Is it necessary? - strncpy(lump_p->name, trimname, min(8, dotpos - trimname)); - lump_p->hash = quickncasehash(lump_p->name, 8); + char backup = trimname[dotpos - trimname]; + trimname[dotpos - trimname] = '\0'; + lump_p->longname = strbuf_append(&lumpnamebuf, trimname); + char name[8]; + strncpy(name, trimname, 8); + lump_p->hash = quickncasehash(name, 8); + trimname[dotpos - trimname] = backup; - lump_p->longname = Z_Calloc(dotpos - trimname + 1, PU_STATIC, NULL); - strlcpy(lump_p->longname, trimname, dotpos - trimname + 1); - - lump_p->fullname = Z_Calloc(zentry.namelen + 1, PU_STATIC, NULL); - strncpy(lump_p->fullname, fullname, zentry.namelen); + lump_p->fullname = strbuf_append(&lumpnamebuf, fullname); switch(zentry.compression) { @@ -830,7 +803,7 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) // skip and ignore comments/extra fields if ((fseek(handle, lump_p->position, SEEK_SET) != 0) || (fread(&zlentry, 1, sizeof(zlentry_t), handle) < sizeof(zlentry_t))) { - CONS_Alert(CONS_ERROR, "Local headers for lump %s are corrupt\n", lump_p->fullname); + CONS_Alert(CONS_ERROR, "Local headers for lump %s are corrupt\n", strbuf_get(lumpnamebuf, lump_p->fullname)); Z_Free(lumpinfo); return NULL; } @@ -1064,6 +1037,10 @@ INT32 W_InitMultipleFiles(char **filenames, boolean addons) INT32 rc = 1; INT32 overallrc = 1; + // this is the first W_ function called so put this here I guess + if (lumpnamebuf == NULL) + lumpnamebuf = strbuf_alloc(); + // will be realloced as lumps are added for (; *filenames; filenames++) { @@ -1105,7 +1082,7 @@ const char *W_CheckNameForNumPwad(UINT16 wad, UINT16 lump) if (lump >= wadfiles[wad]->numlumps || !TestValidLump(wad, 0)) return NULL; - return wadfiles[wad]->lumpinfo[lump].name; + return strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[lump].longname); } const char *W_CheckNameForNum(lumpnum_t lumpnum) @@ -1118,7 +1095,7 @@ static const char *W_CheckFullNameForNumPwad(UINT16 wad, UINT16 lump) if (lump >= wadfiles[wad]->numlumps || !TestValidLump(wad, 0)) return NULL; - return wadfiles[wad]->lumpinfo[lump].fullname; + return strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[lump].fullname); } static const char *W_CheckFullNameForNum(lumpnum_t lumpnum) @@ -1170,7 +1147,7 @@ UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 st continue; // Not the name? (always use longname, even in wads, to accomodate WADNAME) - if (strcasecmp(name, (wadfiles[wad]->lumpinfo + i)->longname)) + if (strcasecmp(name, strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[i].longname))) continue; // Not a header? @@ -1196,7 +1173,7 @@ UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 st continue; // Not the name? - if (strcasecmp(name, (wadfiles[wad]->lumpinfo + i)->longname)) + if (strcasecmp(name, strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[i].longname))) continue; #if 0 @@ -1242,7 +1219,7 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) { lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) - if (lump_p->hash == hash && !strncmp(lump_p->name, uname, sizeof(uname) - 1)) + if (lump_p->hash == hash && !strncmp(strbuf_get(lumpnamebuf, lump_p->longname), uname, sizeof(uname) - 1)) return i; } @@ -1281,7 +1258,7 @@ UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) { if (lump_p->hash != hash) continue; - if (strcmp(lump_p->longname, uname)) + if (strcmp(strbuf_get(lumpnamebuf, lump_p->longname), uname)) continue; return i; } @@ -1320,7 +1297,7 @@ UINT16 W_CheckNumForLongNamePwadNoUpper(const char *name, UINT16 wad, UINT16 sta { if (lump_p->hash != hash) continue; - if (strcmp(lump_p->longname, name)) + if (strcmp(strbuf_get(lumpnamebuf, lump_p->longname), name)) continue; return i; } @@ -1350,10 +1327,10 @@ UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlum name_length = strlen(name); for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) { - if (strnicmp(name, lump_p->fullname, name_length) == 0) + if (strnicmp(name, strbuf_get(lumpnamebuf, lump_p->fullname), name_length) == 0) { /* SLADE is special and puts a single directory entry. Skip that. */ - if (strlen(lump_p->fullname) == name_length) + if (strlen(strbuf_get(lumpnamebuf, lump_p->fullname)) == name_length) i++; break; } @@ -1370,7 +1347,7 @@ UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump) lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) { - if (strnicmp(name, lump_p->fullname, strlen(name))) + if (strnicmp(name, strbuf_get(lumpnamebuf, lump_p->fullname), strlen(name))) break; } return i; @@ -1384,7 +1361,7 @@ UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump) lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) { - if (!strnicmp(name, lump_p->fullname, strlen(name))) + if (!strnicmp(name, strbuf_get(lumpnamebuf, lump_p->fullname), strlen(name))) { return i; } @@ -1659,7 +1636,7 @@ UINT8 W_LumpExists(const char *name) { lumpinfo_t *lump_p = wadfiles[i]->lumpinfo; for (j = 0; j < wadfiles[i]->numlumps; ++j, ++lump_p) - if (fastcmp(lump_p->longname, name)) + if (fastcmp(strbuf_get(lumpnamebuf, lump_p->longname), name)) return true; } return false; @@ -1690,7 +1667,7 @@ boolean W_IsLumpWad(lumpnum_t lumpnum) { if (wadfiles[WADFILENUM(lumpnum)]->type == RET_PK3) { - const char *lumpfullName = (wadfiles[WADFILENUM(lumpnum)]->lumpinfo + LUMPNUM(lumpnum))->fullname; + const char *lumpfullName = strbuf_get(lumpnamebuf, (wadfiles[WADFILENUM(lumpnum)]->lumpinfo + LUMPNUM(lumpnum))->fullname); if (strlen(lumpfullName) < 4) return false; // can't possibly be a WAD can it? @@ -1708,7 +1685,7 @@ boolean W_IsLumpFolder(UINT16 wad, UINT16 lump) { if (wadfiles[wad]->type == RET_PK3) { - const char *name = wadfiles[wad]->lumpinfo[lump].fullname; + const char *name = strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[lump].fullname); return (name[strlen(name)-1] == '/'); // folders end in '/' } @@ -1751,7 +1728,7 @@ boolean W_NeedPaletteRemapPwad(UINT16 wad, UINT16 lump, boolean ignorecache) // ugh, palette textures... // never do remapping on them; PaletteTextureHack handles that unsigned num; - const char *name = (wadfiles[wad]->lumpinfo + lump)->longname; + const char *name = strbuf_get(lumpnamebuf, (wadfiles[wad]->lumpinfo + lump)->longname); if (sscanf(name, "~%03u", &num) && num < 256) return false; else diff --git a/src/w_wad.h b/src/w_wad.h index 6449027e3..4c37d989f 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -18,6 +18,8 @@ #include "hardware/hw_data.h" #endif +#include "strbuf.h" + #ifdef __cplusplus extern "C" { #endif @@ -66,11 +68,10 @@ struct lumpinfo_t { unsigned long position; // filelump_t filepos unsigned long disksize; // filelump_t size - char name[9]; // filelump_t name[] e.g. "LongEntr" - UINT32 hash; - char *longname; // e.g. "LongEntryName" - char *fullname; // e.g. "Folder/Subfolder/LongEntryName.extension" size_t size; // real (uncompressed) size + UINT32 hash; + UINT32 longname; // e.g. "LongEntryName" + UINT32 fullname; // e.g. "Folder/Subfolder/LongEntryName.extension" compmethod compression; // lump compression method }; @@ -106,6 +107,8 @@ void* vres_GetPatch(virtlump_t *vlump, INT32 tag); #define lumpcache_t void * +extern strbuf_t *lumpnamebuf; + // Resource type of the WAD. Yeah, I know this sounds dumb, but I'll leave it like this until I clean up the code further. typedef enum restype { From 4ffd4621e428943ffddabc9411b2408452f5ce82 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Wed, 25 Jun 2025 00:20:15 +0200 Subject: [PATCH 02/18] Let's clean this shit up properly lumpinfo_t is now private. Wanna iterate thru lumps? Use the W_* functions --- src/info.c | 6 +- src/k_brightmap.c | 15 ++- src/k_brightmap.h | 4 +- src/k_terrain.c | 16 +-- src/lua_script.c | 2 +- src/p_setup.c | 148 ++++++-------------------- src/p_setup.h | 2 - src/r_picformats.c | 19 ++-- src/r_picformats.h | 2 +- src/r_skins.c | 52 +-------- src/r_textures.c | 2 +- src/r_things.cpp | 10 +- src/s_sound.c | 3 +- src/strbuf.h | 5 - src/typedef.h | 3 +- src/w_wad.c | 257 ++++++++++++++++++++++++--------------------- src/w_wad.h | 55 ++-------- 17 files changed, 221 insertions(+), 380 deletions(-) diff --git a/src/info.c b/src/info.c index 58a6b161f..12b5466b8 100644 --- a/src/info.c +++ b/src/info.c @@ -158,7 +158,7 @@ void P_ResetData(INT32 flags) if (!init) { - lumpnum = W_CheckNumForLongNamePwadNoUpper("states", MAINWAD_MAIN, 0); + lumpnum = W_CheckNumForLongNamePwad("STATES", MAINWAD_MAIN, 0); DEH_LoadDehackedLumpPwad(MAINWAD_MAIN, lumpnum, true); } } @@ -183,7 +183,7 @@ void P_ResetData(INT32 flags) if (!init) { - lumpnum = W_CheckNumForLongNamePwadNoUpper("mobjs", MAINWAD_MAIN, 0); + lumpnum = W_CheckNumForLongNamePwad("MOBJS", MAINWAD_MAIN, 0); DEH_LoadDehackedLumpPwad(MAINWAD_MAIN, lumpnum, true); } } @@ -211,7 +211,7 @@ void P_ResetData(INT32 flags) if (!init) { - lumpnum = W_CheckNumForLongNamePwadNoUpper("skincolors", MAINWAD_MAIN, 0); + lumpnum = W_CheckNumForLongNamePwad("SKINCOLORS", MAINWAD_MAIN, 0); DEH_LoadDehackedLumpPwad(MAINWAD_MAIN, lumpnum, true); } } diff --git a/src/k_brightmap.c b/src/k_brightmap.c index edb9f95a0..b67052f62 100644 --- a/src/k_brightmap.c +++ b/src/k_brightmap.c @@ -166,11 +166,11 @@ static boolean K_BRIGHTLumpParser(char *data, size_t size) } /*-------------------------------------------------- - void K_InitBrightmapsPwad(INT32 wadNum) + void K_InitBrightmapsPwad(UINT16 wadNum) See header file for description. --------------------------------------------------*/ -void K_InitBrightmapsPwad(INT32 wadNum) +void K_InitBrightmapsPwad(UINT16 wadNum) { UINT16 lumpNum; size_t i; @@ -180,19 +180,18 @@ void K_InitBrightmapsPwad(INT32 wadNum) // Find BRIGHT lump in the WAD lumpNum = W_CheckNumForNamePwad("BRIGHT", wadNum, 0); - while (lumpNum != INT16_MAX) + for (; lumpNum != INT16_MAX; lumpNum = W_CheckNumForNamePwad("BRIGHT", wadNum, lumpNum + 1)) { - UINT8 *data = (UINT8 *)W_CacheLumpNumPwad(wadNum, lumpNum, PU_CACHE); + UINT8 *data = (UINT8 *)W_CacheLumpNumPwad(wadNum, lumpNum, PU_STATIC); if (data != NULL) { - lumpinfo_t *lump_p = &wadfiles[wadNum]->lumpinfo[lumpNum]; size_t size = W_LumpLengthPwad(wadNum, lumpNum); char *datacopy; size = W_LumpLengthPwad(wadNum, lumpNum); - CONS_Printf(M_GetText("Loading BRIGHT from %s|%s\n"), wadfiles[wadNum]->filename, strbuf_get(lumpnamebuf, lump_p->fullname)); + CONS_Printf(M_GetText("Loading BRIGHT from %s|%s\n"), wadfiles[wadNum]->filename, W_CheckFullNameForNumPwad(wadNum, lumpNum)); datacopy = (char *)Z_Malloc((size+1)*sizeof(char),PU_STATIC,NULL); memmove(datacopy,data,size); @@ -204,8 +203,6 @@ void K_InitBrightmapsPwad(INT32 wadNum) Z_Free(datacopy); } - - lumpNum = W_CheckNumForNamePwad("BRIGHT", (UINT16)wadNum, lumpNum + 1); } if (maxBrightmapStorage == 0) @@ -248,7 +245,7 @@ void K_InitBrightmapsPwad(INT32 wadNum) --------------------------------------------------*/ void K_InitBrightmaps(void) { - INT32 wadNum; + UINT16 wadNum; for (wadNum = 0; wadNum < numwadfiles; wadNum++) { diff --git a/src/k_brightmap.h b/src/k_brightmap.h index 9726c792a..c1bed36d2 100644 --- a/src/k_brightmap.h +++ b/src/k_brightmap.h @@ -35,12 +35,12 @@ struct brightmapStorage_t }; /*-------------------------------------------------- - void K_InitBrightmapsPwad(INT32 wadNum); + void K_InitBrightmapsPwad(UINT16 wadNum); Finds all BRIGHT lumps for one WAD/PK3 and processes them. --------------------------------------------------*/ -void K_InitBrightmapsPwad(INT32 wadNum); +void K_InitBrightmapsPwad(UINT16 wadNum); /*-------------------------------------------------- void K_InitBrightmaps(void); diff --git a/src/k_terrain.c b/src/k_terrain.c index b786ace31..a42cec062 100644 --- a/src/k_terrain.c +++ b/src/k_terrain.c @@ -2261,22 +2261,16 @@ static boolean K_TERRAINLumpParser(char *data, size_t size) boolean K_InitTerrainPwad(UINT16 wadNum) { UINT16 lumpNum; - lumpinfo_t *lump_p = wadfiles[wadNum]->lumpinfo; boolean terrainLoaded = false; + lumpNum = W_CheckNumForNamePwad("TERRAIN", wadNum, 0); + // Iterate through all lumps and compare the name individually. // In PK3 files, you can potentially have multiple TERRAIN differentiated by // their file extension. - for (lumpNum = 0; lumpNum < wadfiles[wadNum]->numlumps; lumpNum++, lump_p++) + for (; lumpNum != INT16_MAX; lumpNum = W_CheckNumForNamePwad("TERRAIN", wadNum, lumpNum + 1)) { - UINT8 *data; - - if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname), "TERRAIN", 8) != 0) - { - continue; - } - - data = (UINT8 *)W_CacheLumpNumPwad(wadNum, lumpNum, PU_STATIC); + UINT8 *data = (UINT8 *)W_CacheLumpNumPwad(wadNum, lumpNum, PU_STATIC); // If that didn't exist, we have nothing to do here. if (data == NULL) @@ -2290,7 +2284,7 @@ boolean K_InitTerrainPwad(UINT16 wadNum) size = W_LumpLengthPwad(wadNum, lumpNum); - CONS_Printf(M_GetText("Loading TERRAIN from %s|%s\n"), wadfiles[wadNum]->filename, strbuf_get(lumpnamebuf, lump_p->fullname)); + CONS_Printf(M_GetText("Loading TERRAIN from %s|%s\n"), wadfiles[wadNum]->filename, W_CheckFullNameForNumPwad(wadNum, lumpNum)); terrainLoaded = true; datacopy = (char *)Z_Malloc((size+1)*sizeof(char),PU_STATIC,NULL); diff --git a/src/lua_script.c b/src/lua_script.c index f8b2846f5..8c31fd95a 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -669,7 +669,7 @@ void LUA_LoadLump(UINT16 wad, UINT16 lump, boolean noresults) if (wadfiles[wad]->type == RET_LUA) name = xva("%s", wadfiles[wad]->filename); else // If it's not a .lua file, copy the lump name in too. - name = xva("%s|%s", wadfiles[wad]->filename, strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[lump].fullname)); + name = xva("%s|%s", wadfiles[wad]->filename, W_CheckFullNameForNumPwad(wad, lump)); lua_compatmode = wadfiles[wad]->compatmode; diff --git a/src/p_setup.c b/src/p_setup.c index bf31a7ccf..a99751b95 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -9029,22 +9029,26 @@ boolean P_RunSOC(const char *socfilename) // Auxiliary function for PK3 loading - looks for sound replacements. // NOTE: it does not really add any new sound entry or anything. -void P_LoadSoundsRange(UINT16 wadnum, UINT16 first, UINT16 num) +static void P_LoadSoundsRange(UINT16 wadnum, UINT16 first, UINT16 end, size_t *sreplaces) { - size_t j; - lumpinfo_t *lumpinfo = wadfiles[wadnum]->lumpinfo + first; - for (; num > 0; num--, lumpinfo++) + sfxenum_t j; + UINT16 i; + for (i = first; i < end; i++) { // Let's check whether it's replacing an existing sound or it's a brand new one. + const char *name = W_CheckNameForNumPwad(wadnum, i); + if (name[0] != 'D' || name[1] != 'S') + continue; for (j = 1; j < NUMSFX; j++) { - if (S_sfx[j].name && !strnicmp(S_sfx[j].name, strbuf_get(lumpnamebuf, lumpinfo->longname) + 2, 6)) + if (S_sfx[j].name && !strnicmp(S_sfx[j].name, name + 2, 6)) { // the sound will be reloaded when needed, // since sfx->data will be NULL - CONS_Debug(DBG_SETUP, "Sound %.8s replaced\n", strbuf_get(lumpnamebuf, lumpinfo->longname)); + CONS_Debug(DBG_SETUP, "Sound %.8s replaced\n", name); I_FreeSfx(&S_sfx[j]); + *sreplaces += 1; break; // there shouldn't be two sounds with the same name, so stop looking } } @@ -9053,46 +9057,23 @@ void P_LoadSoundsRange(UINT16 wadnum, UINT16 first, UINT16 num) // Auxiliary function for PK3 loading - looks for music and music replacements. // NOTE: does nothing but print debug messages. The code is handled somewhere else. -/* -void P_LoadMusicsRange(UINT16 wadnum, UINT16 first, UINT16 num) +static void P_LoadMusicsRange(UINT16 wadnum, UINT16 first, UINT16 end, size_t *digmreplaces, size_t *mreplaces) { - lumpinfo_t *lumpinfo = wadfiles[wadnum]->lumpinfo + first; - char *name; - for (; num > 0; num--, lumpinfo++) + UINT16 i; + for (i = first; i < end; i++) { - name = lumpinfo->name; + const char *name = W_CheckNameForNumPwad(wadnum, i); if (name[0] == 'O' && name[1] == '_') { CONS_Debug(DBG_SETUP, "Music %.8s replaced\n", name); + *digmreplaces += 1; } else if (name[0] == 'D' && name[1] == '_') { - CONS_Debug(DBG_SETUP, "Music %.8s replaced\n", name); + CONS_Debug(DBG_SETUP, "Music %.8s ignored\n", name); + *mreplaces += 1; } } - return; -} -*/ - -// Auxiliary function - input a folder name and gives us the resource markers positions. -static lumpinfo_t* FindFolder(const char *folName, UINT16 *start, UINT16 *end, lumpinfo_t *lumpinfo, UINT16 *pnumlumps, size_t *pi) -{ - UINT16 numlumps = *pnumlumps; - size_t i = *pi; - if (!stricmp(strbuf_get(lumpnamebuf, lumpinfo->fullname), folName)) - { - lumpinfo++; - *start = ++i; - for (; i < numlumps; i++, lumpinfo++) - if (strnicmp(strbuf_get(lumpnamebuf, lumpinfo->fullname), folName, strlen(folName))) - break; - lumpinfo--; - *end = i-- - *start; - *pi = i; - *pnumlumps = numlumps; - return lumpinfo; - } - return lumpinfo; } lumpnum_t wadnamelump = LUMPERROR; @@ -9254,23 +9235,14 @@ boolean P_AddWadFile(const char *wadfilename, wadcompat_t compat) // UINT16 P_PartialAddWadFile(const char *wadfilename, wadcompat_t compat) { - size_t i, j, sreplaces = 0, mreplaces = 0, digmreplaces = 0; + size_t sreplaces = 0, mreplaces = 0, digmreplaces = 0; UINT16 numlumps, wadnum; - char *name; - lumpinfo_t *lumpinfo; // Vars to help us with the position start and amount of each resource type. // Useful for PK3s since they use folders. // WADs use markers for some resources, but others such as sounds are checked lump-by-lump anyway. -// UINT16 luaPos, luaNum = 0; -// UINT16 socPos, socNum = 0; - UINT16 sfxPos = 0, sfxNum = 0; - UINT16 musPos = 0, musNum = 0; -// UINT16 sprPos, sprNum = 0; - UINT16 texPos = 0, texNum = 0; -// UINT16 patPos, patNum = 0; -// UINT16 flaPos, flaNum = 0; -// UINT16 mapPos, mapNum = 0; + UINT16 sfxPos, sfxEnd; + UINT16 musPos, musEnd; // Init file. if ((numlumps = W_InitFile(wadfilename, false, false, compat)) == INT16_MAX) @@ -9291,80 +9263,24 @@ UINT16 P_PartialAddWadFile(const char *wadfilename, wadcompat_t compat) } partadd_stage = 0; - switch(wadfiles[wadnum]->type) { case RET_PK3: - // Look for the lumps that act as resource delimitation markers. - lumpinfo = wadfiles[wadnum]->lumpinfo; - for (i = 0; i < numlumps; i++, lumpinfo++) - { -// lumpinfo = FindFolder("Lua/", &luaPos, &luaNum, lumpinfo, &numlumps, &i); -// lumpinfo = FindFolder("SOC/", &socPos, &socNum, lumpinfo, &numlumps, &i); - lumpinfo = FindFolder("Sounds/", &sfxPos, &sfxNum, lumpinfo, &numlumps, &i); - lumpinfo = FindFolder("Music/", &musPos, &musNum, lumpinfo, &numlumps, &i); -// lumpinfo = FindFolder("Sprites/", &sprPos, &sprNum, lumpinfo, &numlumps, &i); - lumpinfo = FindFolder("Textures/", &texPos, &texNum, lumpinfo, &numlumps, &i); -// lumpinfo = FindFolder("Patches/", &patPos, &patNum, lumpinfo, &numlumps, &i); -// lumpinfo = FindFolder("Flats/", &flaPos, &flaNum, lumpinfo, &numlumps, &i); -// lumpinfo = FindFolder("Maps/", &mapPos, &mapNum, lumpinfo, &numlumps, &i); - } - - // Update the detected resources. - // Note: ALWAYS load Lua scripts first, SOCs right after, and the remaining resources afterwards. -// if (luaNum) // Lua scripts. -// P_LoadLuaScrRange(wadnum, luaPos, luaNum); -// if (socNum) // SOCs. -// P_LoadDehackRange(wadnum, socPos, socNum); - if (sfxNum) // Sounds. TODO: Function currently only updates already existing sounds, the rest is handled somewhere else. - P_LoadSoundsRange(wadnum, sfxPos, sfxNum); -// if (musNum) // Music. TODO: Useless function right now. -// P_LoadMusicsRange(wadnum, musPos, musNum); -// if (sprNum) // Sprites. -// R_LoadSpritsRange(wadnum, sprPos, sprNum); -// if (texNum) // Textures. TODO: R_LoadTextures() does the folder positioning once again. New function maybe? -// R_LoadTextures(); -// if (mapNum) // Maps. TODO: Actually implement the map WAD loading code, lulz. -// P_LoadWadMapRange(wadnum, mapPos, mapNum); + sfxPos = W_CheckNumForFolderStartPK3("Sounds/", wadnum, 0); + sfxEnd = W_CheckNumForFolderEndPK3("Sounds/", wadnum, sfxPos); + musPos = W_CheckNumForFolderStartPK3("Music/", wadnum, 0); + musEnd = W_CheckNumForFolderEndPK3("Music/", wadnum, musPos); break; + default: - lumpinfo = wadfiles[wadnum]->lumpinfo; - for (i = 0; i < numlumps; i++, lumpinfo++) - { - name = strbuf_get(lumpnamebuf, lumpinfo->longname); - if (name[0] == 'D') - { - if (name[1] == 'S') - { - for (j = 1; j < NUMSFX; j++) - { - if (S_sfx[j].name && !strnicmp(S_sfx[j].name, name + 2, 6)) - { - // the sound will be reloaded when needed, - // since sfx->data will be NULL - CONS_Debug(DBG_SETUP, "Sound %.8s replaced\n", name); - - I_FreeSfx(&S_sfx[j]); - - sreplaces++; - break; // there shouldn't be two sounds with the same name, so stop looking - } - } - } - else if (name[1] == '_') - { - CONS_Debug(DBG_SETUP, "Music %.8s ignored\n", name); - mreplaces++; - } - } - else if (name[0] == 'O' && name[1] == '_') - { - CONS_Debug(DBG_SETUP, "Music %.8s replaced\n", name); - digmreplaces++; - } - } + sfxPos = musPos = 0; + sfxEnd = musEnd = numlumps; break; } + + P_LoadSoundsRange(wadnum, sfxPos, sfxEnd, &sreplaces); + P_LoadMusicsRange(wadnum, musPos, musEnd, &digmreplaces, &mreplaces); + if (!devparm && sreplaces) CONS_Printf(M_GetText("%s sounds replaced\n"), sizeu1(sreplaces)); if (!devparm && mreplaces) @@ -9411,7 +9327,7 @@ UINT16 P_PartialAddWadFile(const char *wadfilename, wadcompat_t compat) // // extra sprite/skin data // - R_LoadSpriteInfoLumps(wadnum, numlumps); + R_LoadSpriteInfoLumps(wadnum); refreshdirmenu &= ~REFRESHDIR_GAMEDATA; // Under usual circumstances we'd wait for REFRESHDIR_ flags to disappear the next frame, but this one's a bit too dangerous for that... partadd_stage = 0; diff --git a/src/p_setup.h b/src/p_setup.h index c7c438d3d..a5c7e5878 100644 --- a/src/p_setup.h +++ b/src/p_setup.h @@ -115,8 +115,6 @@ SINT8 P_PartialAddGetStage(void); extern UINT16 partadd_earliestfile; boolean P_RunSOC(const char *socfilename); -void P_LoadSoundsRange(UINT16 wadnum, UINT16 first, UINT16 num); -void P_LoadMusicsRange(UINT16 wadnum, UINT16 first, UINT16 num); //void P_WriteThings(void); void P_UpdateSegLightOffset(seg_t *li); boolean P_ApplyLightOffset(UINT8 baselightnum, const sector_t *sector); diff --git a/src/r_picformats.c b/src/r_picformats.c index bb131a25d..e067a4496 100644 --- a/src/r_picformats.c +++ b/src/r_picformats.c @@ -1852,17 +1852,18 @@ void R_ParseSPRTINFOLump(UINT16 wadNum, UINT16 lumpNum) // // Load and read every SPRTINFO lump from the specified file. // -void R_LoadSpriteInfoLumps(UINT16 wadnum, UINT16 numlumps) +void R_LoadSpriteInfoLumps(UINT16 wadnum) { - lumpinfo_t *lumpinfo = wadfiles[wadnum]->lumpinfo; - UINT16 i; - char *name; +#define NUMPREFIXES 2 + static lumpprefixes_t sprtinfoprfx[NUMPREFIXES] = { + { "SPRTINFO", 8 }, + { "SPR_", 4 }, + }; - for (i = 0; i < numlumps; i++, lumpinfo++) + UINT16 i = W_CheckNumForNameMultiPrefixPwad(&sprtinfoprfx, NUMPREFIXES, wadnum, 0); + for (; i != INT16_MAX; i = W_CheckNumForNameMultiPrefixPwad(&sprtinfoprfx, NUMPREFIXES, wadnum, i + 1)) { - name = strbuf_get(lumpnamebuf, lumpinfo->longname); - // Load SPRTINFO and SPR_ lumps as SpriteInfo - if (!memcmp(name, "SPRTINFO", 8) || !memcmp(name, "SPR_", 4)) - R_ParseSPRTINFOLump(wadnum, i); + R_ParseSPRTINFOLump(wadnum, i); } +#undef NUMPREFIXES } diff --git a/src/r_picformats.h b/src/r_picformats.h index eb40b790a..e984c4ab5 100644 --- a/src/r_picformats.h +++ b/src/r_picformats.h @@ -129,7 +129,7 @@ boolean Picture_PNGDimensions(UINT8 *png, INT32 *width, INT32 *height, INT16 *to // SpriteInfo extern spriteinfo_t spriteinfo[NUMSPRITES]; -void R_LoadSpriteInfoLumps(UINT16 wadnum, UINT16 numlumps); +void R_LoadSpriteInfoLumps(UINT16 wadnum); void R_ParseSPRTINFOLump(UINT16 wadNum, UINT16 lumpNum); #ifdef __cplusplus diff --git a/src/r_skins.c b/src/r_skins.c index e02fec575..017f729ba 100644 --- a/src/r_skins.c +++ b/src/r_skins.c @@ -159,7 +159,7 @@ void R_InitSkins(void) R_AddSkins((UINT16)i); if (!wadfiles[i]->compatmode) R_PatchSkins((UINT16)i); - R_LoadSpriteInfoLumps(i, wadfiles[i]->numlumps); + R_LoadSpriteInfoLumps(i); } ST_ReloadSkinFaceGraphics(); } @@ -347,27 +347,6 @@ void SetPlayerSkinByNum(INT32 playernum, INT32 skinnum) // Add skins from a pwad, each skin preceded by 'S_SKIN' marker // -// Does the same is in w_wad, but check only for -// the first 6 characters (this is so we can have S_SKIN1, S_SKIN2.. -// for wad editors that don't like multiple resources of the same name) -// -static UINT16 W_CheckForSkinMarkerInPwad(UINT16 wadid, UINT16 startlump) -{ - UINT16 i; - const char *S_SKIN = "S_SKIN"; - lumpinfo_t *lump_p; - - // scan forward, start at - if (startlump < wadfiles[wadid]->numlumps) - { - lump_p = wadfiles[wadid]->lumpinfo + startlump; - for (i = startlump; i < wadfiles[wadid]->numlumps; i++, lump_p++) - if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname),S_SKIN,6)==0) - return i; - } - return INT16_MAX; // not found -} - // turn _ into spaces and . into katana dot #define SYMBOLCONVERT(name) for (value = name; *value; value++)\ {\ @@ -378,27 +357,6 @@ static UINT16 W_CheckForSkinMarkerInPwad(UINT16 wadid, UINT16 startlump) // Patch skins from a pwad, each skin preceded by 'P_SKIN' marker // -// Does the same is in w_wad, but check only for -// the first 6 characters (this is so we can have P_SKIN1, P_SKIN2.. -// for wad editors that don't like multiple resources of the same name) -// -static UINT16 W_CheckForPatchSkinMarkerInPwad(UINT16 wadid, UINT16 startlump) -{ - UINT16 i; - const char *P_SKIN = "P_SKIN"; - lumpinfo_t *lump_p; - - // scan forward, start at - if (startlump < wadfiles[wadid]->numlumps) - { - lump_p = wadfiles[wadid]->lumpinfo + startlump; - for (i = startlump; i < wadfiles[wadid]->numlumps; i++, lump_p++) - if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname),P_SKIN,6)==0) - return i; - } - return INT16_MAX; // not found -} - #define NUMKARTFRAMES 19 #define S(f, s) { f - 'A', SPR2_##s }, static UINT8 KART_TO_SPR2[][2] = { @@ -442,9 +400,9 @@ static void R_LoadSkinSprites(UINT16 wadnum, UINT16 *lump, UINT16 *lastlump, ski // old wadding practices die hard -- stop at S_SKIN (or P_SKIN) or S_START if they come before S_END. newlastlump = W_FindNextEmptyInPwad(wadnum,*lump); if (newlastlump < *lastlump) *lastlump = newlastlump; - newlastlump = W_CheckForSkinMarkerInPwad(wadnum,*lump); + newlastlump = W_CheckNumForNamePrefixPwad("S_SKIN", 6, wadnum, *lump); if (newlastlump < *lastlump) *lastlump = newlastlump; - newlastlump = W_CheckForPatchSkinMarkerInPwad(wadnum,*lump); + newlastlump = W_CheckNumForNamePrefixPwad("P_SKIN", 6, wadnum, *lump); if (newlastlump < *lastlump) *lastlump = newlastlump; newlastlump = W_CheckNumForNamePwad("S_START",wadnum,*lump); if (newlastlump < *lastlump) *lastlump = newlastlump; @@ -690,7 +648,7 @@ void R_AddSkins(UINT16 wadnum) // search for all skin markers in pwad // - while ((lump = W_CheckForSkinMarkerInPwad(wadnum, lastlump)) != INT16_MAX) + while ((lump = W_CheckNumForNamePrefixPwad("S_SKIN", 6, wadnum, lastlump)) != INT16_MAX) { // advance by default lastlump = lump + 1; @@ -862,7 +820,7 @@ void R_PatchSkins(UINT16 wadnum) // search for all skin patch markers in pwad // - while ((lump = W_CheckForPatchSkinMarkerInPwad(wadnum, lastlump)) != INT16_MAX) + while ((lump = W_CheckNumForNamePrefixPwad("P_SKIN", 6, wadnum, lastlump)) != INT16_MAX) { INT32 skinnum = 0; diff --git a/src/r_textures.c b/src/r_textures.c index 9cf14c778..6cee4242d 100644 --- a/src/r_textures.c +++ b/src/r_textures.c @@ -287,7 +287,7 @@ static boolean R_CheckTextureLumpLength(texture_t *texture, size_t patch) texture->name, sizeu1(offsetof(softwarepatch_t, columnofs)), sizeu2(lumplength), - strbuf_get(lumpnamebuf, wadfiles[wadnum]->lumpinfo[lumpnum].fullname) + W_CheckFullNameForNumPwad(wadnum, lumpnum) ); return false; diff --git a/src/r_things.cpp b/src/r_things.cpp index 66e1674e6..9232d9bcc 100644 --- a/src/r_things.cpp +++ b/src/r_things.cpp @@ -301,7 +301,6 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 UINT16 l; UINT8 frame; UINT8 rotation; - lumpinfo_t *lumpinfo; softwarepatch_t patch; UINT16 numadded = 0; boolean compat = wadfiles[wadnum]->compatmode; @@ -323,17 +322,14 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 // scan the lumps, // filling in the frames for whatever is found - lumpinfo = wadfiles[wadnum]->lumpinfo; if (endlump > wadfiles[wadnum]->numlumps) endlump = wadfiles[wadnum]->numlumps; - for (l = startlump; l < endlump; l++) + l = W_CheckNumForNamePrefixPwad(sprname, 4, wadnum, startlump); + for (; l != INT16_MAX && l < endlump; l = W_CheckNumForNamePrefixPwad(sprname, 4, wadnum, l+1)) { - const char *longname = strbuf_get(lumpnamebuf, lumpinfo[l].longname); - if (memcmp(longname,sprname,4)) - continue; - { + const char *longname = W_CheckNameForNumPwad(wadnum, l); INT32 width, height; INT16 topoffset, leftoffset; #ifndef NO_PNG_LUMPS diff --git a/src/s_sound.c b/src/s_sound.c index 332657f36..471ac6e43 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1387,12 +1387,11 @@ MusicDefError int line ){ const wadfile_t * wad = wadfiles[WADFILENUM (lumpnum)]; - const lumpinfo_t * lump = &wad->lumpinfo[LUMPNUM (lumpnum)]; CONS_Alert(level, va("%%s|%%s: %s (line %%d)\n", description), wad->filename, - strbuf_get(lumpnamebuf, lump->fullname), + W_CheckFullNameForNum(lumpnum), field, line ); diff --git a/src/strbuf.h b/src/strbuf.h index 0a17a55cb..85bef9860 100644 --- a/src/strbuf.h +++ b/src/strbuf.h @@ -9,9 +9,6 @@ /// \file strbuf.h /// \brief string buffer library, for making chunks of many small strings -#ifndef __STRBUF_H__ -#define __STRBUF_H__ - #ifdef __cplusplus extern "C" { #endif @@ -39,5 +36,3 @@ FUNCINLINE static ATTRINLINE char *strbuf_get(strbuf_t *strbuf, UINT32 ofs) #ifdef __cplusplus } #endif - -#endif diff --git a/src/typedef.h b/src/typedef.h index 51612d998..2f00c700f 100644 --- a/src/typedef.h +++ b/src/typedef.h @@ -407,9 +407,8 @@ TYPEDEF (colorlookup_t); TYPEDEF (cliprect_t); // w_wad.h -TYPEDEF (filelump_t); -TYPEDEF (wadinfo_t); TYPEDEF (lumpinfo_t); +TYPEDEF (lumpprefixes_t); TYPEDEF (virtlump_t); TYPEDEF (virtres_t); TYPEDEF (wadfile_t); diff --git a/src/w_wad.c b/src/w_wad.c index bbe7a1d22..9c3dd0201 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -76,10 +76,96 @@ #include "console.h" #endif +#include "strbuf.h" + #ifndef O_BINARY #define O_BINARY 0 #endif +#if defined(_MSC_VER) +#pragma pack(1) +#endif +// header of a wad file +typedef struct wadinfo_s +{ + char identification[4]; // should be "IWAD" or "PWAD" + UINT32 numlumps; // how many resources + UINT32 infotableofs; // the 'directory' of resources +} ATTRPACK wadinfo_t; + +// a raw entry of the wad directory +typedef struct filelump_s +{ + UINT32 filepos; // file offset of the resource + UINT32 size; // size of the resource + char name[8]; // name of the resource +} ATTRPACK filelump_t; + +// PKZip central directory +typedef struct zend_s +{ + char signature[4]; + UINT16 diskpos; + UINT16 cdirdisk; + UINT16 diskentries; + UINT16 entries; + UINT32 cdirsize; + UINT32 cdiroffset; + UINT16 commentlen; +} ATTRPACK zend_t; + +// PKZip central header +typedef struct zentry_s +{ + char signature[4]; + UINT16 version; + UINT16 versionneeded; + UINT16 flags; + UINT16 compression; + UINT16 modtime; + UINT16 moddate; + UINT32 CRC32; + UINT32 compsize; + UINT32 size; + UINT16 namelen; + UINT16 xtralen; + UINT16 commlen; + UINT16 diskstart; + UINT16 attrint; + UINT32 attrext; + UINT32 offset; +} ATTRPACK zentry_t; + +// PKZip local header +typedef struct zlentry_s +{ + char signature[4]; + UINT16 versionneeded; + UINT16 flags; + UINT16 compression; + UINT16 modtime; + UINT16 moddate; + UINT32 CRC32; + UINT32 compsize; + UINT32 size; + UINT16 namelen; + UINT16 xtralen; +} ATTRPACK zlentry_t; +#if defined(_MSC_VER) +#pragma pack() +#endif + +// a memory entry of the wad directory +struct lumpinfo_t +{ + unsigned long position; // filelump_t filepos + unsigned long disksize; // filelump_t size + size_t size; // real (uncompressed) size + UINT32 hash; + UINT32 longname; // e.g. "LongEntryName" + UINT32 fullname; // e.g. "Folder/Subfolder/LongEntryName.extension" + compmethod compression; // lump compression method +}; typedef struct { @@ -101,7 +187,7 @@ typedef struct lumpnum_cache_s static lumpnum_cache_t lumpnumcache[LUMPNUMCACHESIZE]; static UINT16 lumpnumcacheindex = 0; -strbuf_t *lumpnamebuf; +static strbuf_t *lumpnamebuf; //=========================================================================== // GLOBALS @@ -252,7 +338,7 @@ static inline void W_LoadDehackedLumpsPK3(UINT16 wadnum, boolean mainfile) for(; posStart < posEnd; posStart++) { - CONS_Printf(M_GetText("Loading SOC from %s|%s\n"), wadfiles[wadnum]->filename, strbuf_get(lumpnamebuf, wadfiles[wadnum]->lumpinfo[posStart].fullname)); + CONS_Printf(M_GetText("Loading SOC from %s|%s\n"), wadfiles[wadnum]->filename, W_CheckFullNameForNumPwad(wadnum, posStart)); DEH_LoadDehackedLumpPwad(wadnum, posStart, mainfile); } } @@ -264,32 +350,24 @@ static inline void W_LoadDehackedLumps(UINT16 wadnum, boolean mainfile) UINT16 lump; // Find Lua scripts before SOCs to allow new A_Actions in SOC editing. - { - lumpinfo_t *lump_p = wadfiles[wadnum]->lumpinfo; - for (lump = 0; lump < wadfiles[wadnum]->numlumps; lump++, lump_p++) - if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname),"LUA_",4)==0) - LUA_LoadLump(wadnum, lump, true); - } + lump = W_CheckNumForNamePrefixPwad("LUA_", 4, wadnum, 0); + for (; lump != INT16_MAX; lump = W_CheckNumForNamePrefixPwad("LUA_", 4, wadnum, lump + 1)) + LUA_LoadLump(wadnum, lump, true); - { - lumpinfo_t *lump_p = wadfiles[wadnum]->lumpinfo; - for (lump = 0; lump < wadfiles[wadnum]->numlumps; lump++, lump_p++) - if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname),"SOC_",4)==0) // Check for generic SOC lump - { // shameless copy+paste of code from LUA_LoadLump - CONS_Printf(M_GetText("Loading SOC from %s|%s\n"), wadfiles[wadnum]->filename, strbuf_get(lumpnamebuf, lump_p->fullname)); - DEH_LoadDehackedLumpPwad(wadnum, lump, mainfile); - } - else if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname),"MAINCFG",8)==0) // Check for MAINCFG - { - CONS_Printf(M_GetText("Loading main config from %s\n"), wadfiles[wadnum]->filename); - DEH_LoadDehackedLumpPwad(wadnum, lump, mainfile); - } - else if (memcmp(strbuf_get(lumpnamebuf, lump_p->longname),"OBJCTCFG",9)==0) // Check for OBJCTCFG - { - CONS_Printf(M_GetText("Loading object config from %s\n"), wadfiles[wadnum]->filename); - DEH_LoadDehackedLumpPwad(wadnum, lump, mainfile); - } +#define NUMPREFIXES 3 + static lumpprefixes_t socprfx[NUMPREFIXES] = { + { "SOC_", 4 }, + { "MAINCFG", 8 }, + { "OBJCTCFG", 9 }, + }; + + lump = W_CheckNumForNameMultiPrefixPwad(socprfx, NUMPREFIXES, wadnum, 0); + for (; lump != INT16_MAX; lump = W_CheckNumForNameMultiPrefixPwad(socprfx, NUMPREFIXES, wadnum, lump+1)) + { // shameless copy+paste of code from LUA_LoadLump + CONS_Printf(M_GetText("Loading SOC from %s|%s\n"), wadfiles[wadnum]->filename, W_CheckFullNameForNumPwad(wadnum, lump)); + DEH_LoadDehackedLumpPwad(wadnum, lump, mainfile); } +#undef NUMPREFIXES } static inline boolean CheckCompatFilename(const char *filename) @@ -576,7 +654,7 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen name[namelen-1] = '\0'; lump_p->longname = lump_p->fullname = strbuf_append(&lumpnamebuf, name); - CONS_Debug(DBG_SETUP, "WADNAME handling:\n -- path %s\n -- interpreted lumpname %s\n", filename, trimname); + CONS_Debug(DBG_SETUP, "WADNAME handling:\n -- path %s\n -- interpreted lumpname %s\n", filename, name); // Grab the hash from the first part lump_p->hash = quickncasehash(name, 8); @@ -626,60 +704,6 @@ static boolean ResFindSignature (FILE* handle, char endPat[], UINT32 startpos) return false; } -#if defined(_MSC_VER) -#pragma pack(1) -#endif -typedef struct zend_s -{ - char signature[4]; - UINT16 diskpos; - UINT16 cdirdisk; - UINT16 diskentries; - UINT16 entries; - UINT32 cdirsize; - UINT32 cdiroffset; - UINT16 commentlen; -} ATTRPACK zend_t; - -typedef struct zentry_s -{ - char signature[4]; - UINT16 version; - UINT16 versionneeded; - UINT16 flags; - UINT16 compression; - UINT16 modtime; - UINT16 moddate; - UINT32 CRC32; - UINT32 compsize; - UINT32 size; - UINT16 namelen; - UINT16 xtralen; - UINT16 commlen; - UINT16 diskstart; - UINT16 attrint; - UINT32 attrext; - UINT32 offset; -} ATTRPACK zentry_t; - -typedef struct zlentry_s -{ - char signature[4]; - UINT16 versionneeded; - UINT16 flags; - UINT16 compression; - UINT16 modtime; - UINT16 moddate; - UINT32 CRC32; - UINT32 compsize; - UINT32 size; - UINT16 namelen; - UINT16 xtralen; -} ATTRPACK zlentry_t; -#if defined(_MSC_VER) -#pragma pack() -#endif - /** Create a lumpinfo_t array for a PKZip file. */ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) @@ -1090,7 +1114,7 @@ const char *W_CheckNameForNum(lumpnum_t lumpnum) return W_CheckNameForNumPwad(WADFILENUM(lumpnum),LUMPNUM(lumpnum)); } -static const char *W_CheckFullNameForNumPwad(UINT16 wad, UINT16 lump) +const char *W_CheckFullNameForNumPwad(UINT16 wad, UINT16 lump) { if (lump >= wadfiles[wad]->numlumps || !TestValidLump(wad, 0)) return NULL; @@ -1098,7 +1122,7 @@ static const char *W_CheckFullNameForNumPwad(UINT16 wad, UINT16 lump) return strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[lump].fullname); } -static const char *W_CheckFullNameForNum(lumpnum_t lumpnum) +const char *W_CheckFullNameForNum(lumpnum_t lumpnum) { return W_CheckFullNameForNumPwad(WADFILENUM(lumpnum),LUMPNUM(lumpnum)); } @@ -1147,7 +1171,7 @@ UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 st continue; // Not the name? (always use longname, even in wads, to accomodate WADNAME) - if (strcasecmp(name, strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[i].longname))) + if (strcasecmp(name, W_CheckNameForNumPwad(wad, i))) continue; // Not a header? @@ -1173,7 +1197,7 @@ UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 st continue; // Not the name? - if (strcasecmp(name, strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[i].longname))) + if (strcasecmp(name, W_CheckNameForNumPwad(wad, i))) continue; #if 0 @@ -1269,43 +1293,42 @@ UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) return INT16_MAX; } -// -// Like W_CheckNumForLongNamePwad, but doesn't uppercase the search string AAAAAAAAAAAAA -// -UINT16 W_CheckNumForLongNamePwadNoUpper(const char *name, UINT16 wad, UINT16 startlump) +// same as W_CheckNumForNamePwad, but only checks for a name PREFIX +UINT16 W_CheckNumForNamePrefixPwad(const char *name, size_t namelen, UINT16 wad, UINT16 startlump) { UINT16 i; - char uname[8+1]; - UINT32 hash; + lumpinfo_t *lump_p; - if (!TestValidLump(wad,0)) + if (!TestValidLump(wad, startlump)) return INT16_MAX; - strncpy(uname, name, sizeof uname); - strupr(uname); - hash = quickncasehash(uname, 8); // Not a mistake, legacy system for short lumpnames - - // - // scan forward - // start at 'startlump', useful parameter when there are multiple - // resources with the same name - // - if (startlump < wadfiles[wad]->numlumps) - { - lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; - for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) - { - if (lump_p->hash != hash) - continue; - if (strcmp(strbuf_get(lumpnamebuf, lump_p->longname), name)) - continue; + // scan forward, start at + lump_p = wadfiles[wad]->lumpinfo + startlump; + for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) + if (!strncmp(strbuf_get(lumpnamebuf, lump_p->longname), name, namelen)) return i; - } - } + return INT16_MAX; // not found +} - // not found. - return INT16_MAX; +// same as W_CheckNumForNamePrefixPwad, but check for MULTIPLE PREFIXES!!!!!! +UINT16 W_CheckNumForNameMultiPrefixPwad(const lumpprefixes_t *prefixes, size_t numprefixes, UINT16 wad, UINT16 startlump) +{ + UINT16 i; + size_t j; + lumpinfo_t *lump_p; + + if (!TestValidLump(wad, startlump)) + return INT16_MAX; + + // scan forward, start at + lump_p = wadfiles[wad]->lumpinfo + startlump; + for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) + for (j = 0; j < numprefixes; j++) + if (!strncmp(strbuf_get(lumpnamebuf, lump_p->longname), prefixes[j].prefix, prefixes[j].len)) + return i; + + return INT16_MAX; // not found } UINT16 @@ -1667,7 +1690,7 @@ boolean W_IsLumpWad(lumpnum_t lumpnum) { if (wadfiles[WADFILENUM(lumpnum)]->type == RET_PK3) { - const char *lumpfullName = strbuf_get(lumpnamebuf, (wadfiles[WADFILENUM(lumpnum)]->lumpinfo + LUMPNUM(lumpnum))->fullname); + const char *lumpfullName = W_CheckFullNameForNum(lumpnum); if (strlen(lumpfullName) < 4) return false; // can't possibly be a WAD can it? @@ -1685,7 +1708,7 @@ boolean W_IsLumpFolder(UINT16 wad, UINT16 lump) { if (wadfiles[wad]->type == RET_PK3) { - const char *name = strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[lump].fullname); + const char *name = W_CheckFullNameForNumPwad(wad, lump); return (name[strlen(name)-1] == '/'); // folders end in '/' } @@ -1728,7 +1751,7 @@ boolean W_NeedPaletteRemapPwad(UINT16 wad, UINT16 lump, boolean ignorecache) // ugh, palette textures... // never do remapping on them; PaletteTextureHack handles that unsigned num; - const char *name = strbuf_get(lumpnamebuf, (wadfiles[wad]->lumpinfo + lump)->longname); + const char *name = W_CheckNameForNumPwad(wad, lump); if (sscanf(name, "~%03u", &num) && num < 256) return false; else diff --git a/src/w_wad.h b/src/w_wad.h index 4c37d989f..51ea2c1f1 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -18,40 +18,10 @@ #include "hardware/hw_data.h" #endif -#include "strbuf.h" - #ifdef __cplusplus extern "C" { #endif -// a raw entry of the wad directory -// NOTE: This sits here and not in w_wad.c because p_setup.c makes use of it to load map WADs inside PK3s. -#if defined(_MSC_VER) -#pragma pack(1) -#endif -struct filelump_t -{ - UINT32 filepos; // file offset of the resource - UINT32 size; // size of the resource - char name[8]; // name of the resource -} ATTRPACK; -#if defined(_MSC_VER) -#pragma pack() -#endif - - -// ============================================================== -// WAD FILE STRUCTURE DEFINITIONS -// ============================================================== - -// header of a wad file -struct wadinfo_t -{ - char identification[4]; // should be "IWAD" or "PWAD" - UINT32 numlumps; // how many resources - UINT32 infotableofs; // the 'directory' of resources -}; - // Available compression methods for lumps. typedef enum { @@ -63,18 +33,6 @@ typedef enum CM_UNSUPPORTED } compmethod; -// a memory entry of the wad directory -struct lumpinfo_t -{ - unsigned long position; // filelump_t filepos - unsigned long disksize; // filelump_t size - size_t size; // real (uncompressed) size - UINT32 hash; - UINT32 longname; // e.g. "LongEntryName" - UINT32 fullname; // e.g. "Folder/Subfolder/LongEntryName.extension" - compmethod compression; // lump compression method -}; - // ========================================================================= // 'VIRTUAL' RESOURCES // ========================================================================= @@ -107,8 +65,6 @@ void* vres_GetPatch(virtlump_t *vlump, INT32 tag); #define lumpcache_t void * -extern strbuf_t *lumpnamebuf; - // Resource type of the WAD. Yeah, I know this sounds dumb, but I'll leave it like this until I clean up the code further. typedef enum restype { @@ -142,6 +98,12 @@ typedef enum wadcompat WC_OFF, } wadcompat_t; +struct lumpprefixes_t +{ + const char *prefix; + size_t len; +}; + #define WADFILENUM(lumpnum) (UINT16)((lumpnum)>>16) // wad flumpnum>>16) // wad file number in upper word #define LUMPNUM(lumpnum) (UINT16)((lumpnum)&0xFFFF) // lump number for this pwad @@ -166,13 +128,16 @@ INT32 W_InitMultipleFiles(char **filenames, boolean addons); const char *W_CheckNameForNumPwad(UINT16 wad, UINT16 lump); const char *W_CheckNameForNum(lumpnum_t lumpnum); +const char *W_CheckFullNameForNumPwad(UINT16 wad, UINT16 lump); +const char *W_CheckFullNameForNum(lumpnum_t lumpnum); UINT16 W_FindNextEmptyInPwad(UINT16 wad, UINT16 startlump); // checks only in one pwad UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump); // checks only in one pwad UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump); -UINT16 W_CheckNumForLongNamePwadNoUpper(const char *name, UINT16 wad, UINT16 startlump); +UINT16 W_CheckNumForNamePrefixPwad(const char *name, size_t namelen, UINT16 wad, UINT16 startlump); +UINT16 W_CheckNumForNameMultiPrefixPwad(const lumpprefixes_t *prefixes, size_t numprefixes, UINT16 wad, UINT16 startlump); /* Find the first lump after F_START for instance. */ UINT16 W_CheckNumForMarkerStartPwad(const char *name, UINT16 wad, UINT16 startlump); From 4a3ae30984d50cbc9dfe4788fd4fdc05e104314c Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Wed, 25 Jun 2025 02:09:28 +0200 Subject: [PATCH 03/18] Switch to xxHash for hashing lump data And shrink lumpinfo_t to a nice, round 32 bytes. We don't support 64-bit file sizes anyway --- src/r_picformats.c | 4 ++-- src/w_wad.c | 50 +++++++++++++++++++++++++--------------------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/r_picformats.c b/src/r_picformats.c index e067a4496..85833b81a 100644 --- a/src/r_picformats.c +++ b/src/r_picformats.c @@ -1860,8 +1860,8 @@ void R_LoadSpriteInfoLumps(UINT16 wadnum) { "SPR_", 4 }, }; - UINT16 i = W_CheckNumForNameMultiPrefixPwad(&sprtinfoprfx, NUMPREFIXES, wadnum, 0); - for (; i != INT16_MAX; i = W_CheckNumForNameMultiPrefixPwad(&sprtinfoprfx, NUMPREFIXES, wadnum, i + 1)) + UINT16 i = W_CheckNumForNameMultiPrefixPwad(sprtinfoprfx, NUMPREFIXES, wadnum, 0); + for (; i != INT16_MAX; i = W_CheckNumForNameMultiPrefixPwad(sprtinfoprfx, NUMPREFIXES, wadnum, i + 1)) { R_ParseSPRTINFOLump(wadnum, i); } diff --git a/src/w_wad.c b/src/w_wad.c index 9c3dd0201..f5f8a882d 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -158,13 +158,13 @@ typedef struct zlentry_s // a memory entry of the wad directory struct lumpinfo_t { - unsigned long position; // filelump_t filepos - unsigned long disksize; // filelump_t size - size_t size; // real (uncompressed) size - UINT32 hash; - UINT32 longname; // e.g. "LongEntryName" - UINT32 fullname; // e.g. "Folder/Subfolder/LongEntryName.extension" + UINT32 hash; // hash for longname in ALL CAPS + UINT32 longname; // e.g. "LongEntryName" + UINT32 fullname; // e.g. "Folder/Subfolder/LongEntryName.extension" + UINT32 position; // filelump_t filepos + UINT32 disksize; // filelump_t size compmethod compression; // lump compression method + size_t size; // real (uncompressed) size }; typedef struct @@ -180,7 +180,7 @@ typedef struct typedef struct lumpnum_cache_s { char lumpname[32]; - UINT32 lumphash; + UINT32 lumphash; // hash for lumpname WITHOUT all caps lumpnum_t lumpnum; } lumpnum_cache_t; @@ -518,7 +518,10 @@ static lumpinfo_t* ResGetLumpsStandalone (FILE* handle, UINT16* numlumps, const fseek(handle, 0, SEEK_SET); lumpinfo->longname = lumpinfo->fullname = strbuf_append(&lumpnamebuf, lumpname); - lumpinfo->hash = quickncasehash(lumpname, 8); + char *name = strdup(lumpname); + strupr(name); + lumpinfo->hash = HASH32(lumpname, strlen(name)); + free(name); *numlumps = 1; return lumpinfo; @@ -657,7 +660,8 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen CONS_Debug(DBG_SETUP, "WADNAME handling:\n -- path %s\n -- interpreted lumpname %s\n", filename, name); // Grab the hash from the first part - lump_p->hash = quickncasehash(name, 8); + strupr(name); + lump_p->hash = HASH32(name, strlen(name)); free(name); wadnamelump = i | (numwadfiles << 16); @@ -671,7 +675,8 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen lump_p->longname = lump_p->fullname = strbuf_append(&lumpnamebuf, name); // Set up true hash - lump_p->hash = quickncasehash(name, 8); + strupr(name); + lump_p->hash = HASH32(name, strlen(name)); } } free(fileinfov); @@ -781,15 +786,11 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) if ((dotpos = strrchr(trimname, '.')) == 0) dotpos = fullname + strlen(fullname); // Watch for files without extension. - char backup = trimname[dotpos - trimname]; + lump_p->fullname = strbuf_append(&lumpnamebuf, fullname); trimname[dotpos - trimname] = '\0'; lump_p->longname = strbuf_append(&lumpnamebuf, trimname); - char name[8]; - strncpy(name, trimname, 8); - lump_p->hash = quickncasehash(name, 8); - trimname[dotpos - trimname] = backup; - - lump_p->fullname = strbuf_append(&lumpnamebuf, fullname); + strupr(trimname); + lump_p->hash = HASH32(trimname, strlen(trimname)); switch(zentry.compression) { @@ -1232,7 +1233,7 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) strlcpy(uname, name, sizeof uname); strupr(uname); - hash = quickncasehash(uname, 8); + hash = HASH32(uname, strlen(uname)); // // scan forward @@ -1268,7 +1269,7 @@ UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) strlcpy(uname, name, sizeof uname); strupr(uname); - hash = quickncasehash(uname, 8); // Not a mistake, legacy system for short lumpnames + hash = HASH32(uname, strlen(uname)); // // scan forward @@ -1400,7 +1401,7 @@ UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump) lumpnum_t W_CheckNumForName(const char *name) { INT32 i; - UINT32 hash = name ? quickncasehash(name, 8) : 0; + UINT32 hash = name ? HASH32(name, strlen(name)) : 0; lumpnum_t check = INT16_MAX; if (name == NULL) @@ -1452,7 +1453,7 @@ lumpnum_t W_CheckNumForName(const char *name) lumpnum_t W_CheckNumForLongName(const char *name) { INT32 i; - UINT32 hash = name ? quickncasehash(name, 8) : 0; + UINT32 hash = name ? HASH32(name, strlen(name)) : 0; lumpnum_t check = INT16_MAX; if (name == NULL) @@ -1502,7 +1503,7 @@ lumpnum_t W_CheckNumForLongName(const char *name) lumpnum_t W_CheckNumForMap(const char *name) { lumpnum_t check = INT16_MAX; - UINT32 uhash, hash = quickncasehash(name, LUMPNUMCACHENAME); + UINT32 uhash, hash = HASH32(name, min(strlen(name), LUMPNUMCACHENAME)); INT32 i; // Check the lumpnumcache first. Loop backwards so that we check @@ -1517,7 +1518,10 @@ lumpnum_t W_CheckNumForMap(const char *name) } } - uhash = quickncasehash(name, 8); // Not a mistake, legacy system for short lumpnames + char *uname = strdup(name); + strupr(uname); + uhash = HASH32(name, strlen(uname)); + free(uname); for (i = numwadfiles - 1; i >= 0; i--) { From 684ded764732f3e97140518299521dd01d852a3a Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Wed, 25 Jun 2025 14:49:10 +0200 Subject: [PATCH 04/18] Formally deprecate the shortname functions and make TestValidLump actually work --- src/hardware/hw_main.c | 2 +- src/k_brightmap.c | 4 +- src/k_terrain.c | 4 +- src/m_menu.c | 2 +- src/p_spec.c | 4 +- src/r_data.c | 4 +- src/r_skins.c | 4 +- src/r_textures.c | 30 +++--- src/r_things.cpp | 11 +- src/s_sound.c | 2 +- src/w_wad.c | 227 ++++++++++++----------------------------- src/w_wad.h | 4 - 12 files changed, 99 insertions(+), 199 deletions(-) diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index 472f1c1fd..d6795f3bc 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -6590,7 +6590,7 @@ void HWR_LoadCustomShadersFromFile(UINT16 wadnum, boolean PK3) int shadertype = 0; int i; - lump = W_CheckNumForNamePwad("SHADERS", wadnum, 0); + lump = W_CheckNumForLongNamePwad("SHADERS", wadnum, 0); if (lump == INT16_MAX) return; diff --git a/src/k_brightmap.c b/src/k_brightmap.c index b67052f62..19f90f02a 100644 --- a/src/k_brightmap.c +++ b/src/k_brightmap.c @@ -178,9 +178,9 @@ void K_InitBrightmapsPwad(UINT16 wadNum) I_Assert(brightmapStorage == NULL); // Find BRIGHT lump in the WAD - lumpNum = W_CheckNumForNamePwad("BRIGHT", wadNum, 0); + lumpNum = W_CheckNumForLongNamePwad("BRIGHT", wadNum, 0); - for (; lumpNum != INT16_MAX; lumpNum = W_CheckNumForNamePwad("BRIGHT", wadNum, lumpNum + 1)) + for (; lumpNum != INT16_MAX; lumpNum = W_CheckNumForLongNamePwad("BRIGHT", wadNum, lumpNum + 1)) { UINT8 *data = (UINT8 *)W_CacheLumpNumPwad(wadNum, lumpNum, PU_STATIC); diff --git a/src/k_terrain.c b/src/k_terrain.c index a42cec062..0a33d948b 100644 --- a/src/k_terrain.c +++ b/src/k_terrain.c @@ -2263,12 +2263,12 @@ boolean K_InitTerrainPwad(UINT16 wadNum) UINT16 lumpNum; boolean terrainLoaded = false; - lumpNum = W_CheckNumForNamePwad("TERRAIN", wadNum, 0); + lumpNum = W_CheckNumForLongNamePwad("TERRAIN", wadNum, 0); // Iterate through all lumps and compare the name individually. // In PK3 files, you can potentially have multiple TERRAIN differentiated by // their file extension. - for (; lumpNum != INT16_MAX; lumpNum = W_CheckNumForNamePwad("TERRAIN", wadNum, lumpNum + 1)) + for (; lumpNum != INT16_MAX; lumpNum = W_CheckNumForLongNamePwad("TERRAIN", wadNum, lumpNum + 1)) { UINT8 *data = (UINT8 *)W_CacheLumpNumPwad(wadNum, lumpNum, PU_STATIC); diff --git a/src/m_menu.c b/src/m_menu.c index f8e9ed333..abefb937a 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -375,7 +375,7 @@ consvar_t cv_dummymenuplayer = CVAR_INIT ("dummymenuplayer", "P1", CV_HIDEN|CV_C consvar_t cv_dummyteam = CVAR_INIT ("dummyteam", "Spectator", CV_HIDEN, dummyteam_cons_t, NULL); consvar_t cv_dummyspectate = CVAR_INIT ("dummyspectate", "Spectator", CV_HIDEN, dummyspectate_cons_t, NULL); consvar_t cv_dummyscramble = CVAR_INIT ("dummyscramble", "Random", CV_HIDEN, dummyscramble_cons_t, NULL); -consvar_t cv_dummystaff = CVAR_INIT ("dummystaff", "0", CV_HIDEN|CV_CALL, dummystaff_cons_t, Dummystaff_OnChange); +consvar_t cv_dummystaff = CVAR_INIT ("dummystaff", "0", CV_HIDEN|CV_CALL|CV_NOINIT, dummystaff_cons_t, Dummystaff_OnChange); consvar_t cv_dummyattackingrings = CVAR_INIT ("dummyattackingrings", "Off", CV_HIDEN|CV_CALL|CV_NOINIT, CV_OnOff, Nextmap_OnChange); consvar_t cv_dummyattackingstacking = CVAR_INIT ("dummyattackingstacking", "Off", CV_HIDEN|CV_CALL|CV_NOINIT, CV_OnOff, Nextmap_OnChange); diff --git a/src/p_spec.c b/src/p_spec.c index 10c83bb20..ee2fdf436 100644 --- a/src/p_spec.c +++ b/src/p_spec.c @@ -152,13 +152,13 @@ void P_InitPicAnims(void) UINT16 animdefsLumpNum; // Find ANIMDEFS lump in the WAD - animdefsLumpNum = W_CheckNumForNamePwad("ANIMDEFS", w, 0); + animdefsLumpNum = W_CheckNumForLongNamePwad("ANIMDEFS", w, 0); while (animdefsLumpNum != INT16_MAX) { animdeftempflats = ((partadd_earliestfile == UINT16_MAX) || partadd_earliestfile == w); P_ParseANIMDEFSLump(w, animdefsLumpNum); - animdefsLumpNum = W_CheckNumForNamePwad("ANIMDEFS", (UINT16)w, animdefsLumpNum + 1); + animdefsLumpNum = W_CheckNumForLongNamePwad("ANIMDEFS", (UINT16)w, animdefsLumpNum + 1); } } diff --git a/src/r_data.c b/src/r_data.c index 2864ebf69..3c50c59d1 100644 --- a/src/r_data.c +++ b/src/r_data.c @@ -240,11 +240,11 @@ static void R_InitExtraColormaps(void) for (cfile = clump = 0; cfile < numwadfiles; cfile++, clump = 0) { - startnum = W_CheckNumForNamePwad("C_START", cfile, clump); + startnum = W_CheckNumForLongNamePwad("C_START", cfile, clump); if (startnum == INT16_MAX) continue; - endnum = W_CheckNumForNamePwad("C_END", cfile, clump); + endnum = W_CheckNumForLongNamePwad("C_END", cfile, clump); if (endnum == INT16_MAX) I_Error("R_InitExtraColormaps: C_START without C_END\n"); diff --git a/src/r_skins.c b/src/r_skins.c index 017f729ba..2c72e0f30 100644 --- a/src/r_skins.c +++ b/src/r_skins.c @@ -395,7 +395,7 @@ static void R_LoadSkinSprites(UINT16 wadnum, UINT16 *lump, UINT16 *lastlump, ski UINT8 sprite2; *lump += 1; // start after S_SKIN - *lastlump = W_CheckNumForNamePwad("S_END",wadnum,*lump); // stop at S_END + *lastlump = W_CheckNumForLongNamePwad("S_END", wadnum, *lump); // stop at S_END // old wadding practices die hard -- stop at S_SKIN (or P_SKIN) or S_START if they come before S_END. newlastlump = W_FindNextEmptyInPwad(wadnum,*lump); @@ -404,7 +404,7 @@ static void R_LoadSkinSprites(UINT16 wadnum, UINT16 *lump, UINT16 *lastlump, ski if (newlastlump < *lastlump) *lastlump = newlastlump; newlastlump = W_CheckNumForNamePrefixPwad("P_SKIN", 6, wadnum, *lump); if (newlastlump < *lastlump) *lastlump = newlastlump; - newlastlump = W_CheckNumForNamePwad("S_START",wadnum,*lump); + newlastlump = W_CheckNumForLongNamePwad("S_START", wadnum, *lump); if (newlastlump < *lastlump) *lastlump = newlastlump; /*// ...and let's handle super, too diff --git a/src/r_textures.c b/src/r_textures.c index 6cee4242d..371878e66 100644 --- a/src/r_textures.c +++ b/src/r_textures.c @@ -1133,8 +1133,9 @@ Rloadflats (INT32 i, INT32 w) } else { - texstart = W_CheckNumForMarkerStartPwad("F_START", (UINT16)w, 0); - texend = W_CheckNumForNamePwad("F_END", (UINT16)w, texstart); + texstart = W_CheckNumForLongNamePwad("F_START", (UINT16)w, 0); + if (texstart != INT16_MAX) texstart++; + texend = W_CheckNumForLongNamePwad("F_END", (UINT16)w, texstart); } if (!( texstart == INT16_MAX || texend == INT16_MAX )) @@ -1200,9 +1201,6 @@ Rloadflats (INT32 i, INT32 w) return i; } -#define TX_START "TX_START" -#define TX_END "TX_END" - static INT32 Rloadtextures (INT32 i, INT32 w) { @@ -1217,18 +1215,19 @@ Rloadtextures (INT32 i, INT32 w) { texstart = W_CheckNumForFolderStartPK3("textures/", (UINT16)w, 0); texend = W_CheckNumForFolderEndPK3("textures/", (UINT16)w, texstart); - texturesLumpPos = W_CheckNumForNamePwad("TEXTURES", (UINT16)w, 0); + texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", (UINT16)w, 0); while (texturesLumpPos != INT16_MAX) { R_ParseTEXTURESLump(w, texturesLumpPos, &i); - texturesLumpPos = W_CheckNumForNamePwad("TEXTURES", (UINT16)w, texturesLumpPos + 1); + texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", (UINT16)w, texturesLumpPos + 1); } } else { - texstart = W_CheckNumForMarkerStartPwad(TX_START, (UINT16)w, 0); - texend = W_CheckNumForNamePwad(TX_END, (UINT16)w, 0); - texturesLumpPos = W_CheckNumForNamePwad("TEXTURES", (UINT16)w, 0); + texstart = W_CheckNumForLongNamePwad("TX_START", (UINT16)w, 0); + if (texstart != INT16_MAX) texstart++; + texend = W_CheckNumForLongNamePwad("TX_END", (UINT16)w, 0); + texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", (UINT16)w, 0); if (texturesLumpPos != INT16_MAX) R_ParseTEXTURESLump(w, texturesLumpPos, &i); } @@ -1321,8 +1320,9 @@ count_range } else { - texstart = W_CheckNumForMarkerStartPwad(marker_start, wadnum, 0); - texend = W_CheckNumForNamePwad(marker_end, wadnum, texstart); + texstart = W_CheckNumForLongNamePwad(marker_start, wadnum, 0); + if (texstart != INT16_MAX) texstart++; + texend = W_CheckNumForLongNamePwad(marker_end, wadnum, texstart); } if (texstart != INT16_MAX && texend != INT16_MAX) @@ -1361,16 +1361,16 @@ static INT32 R_CountTextures(UINT16 wadnum) count += count_range("F_START", "F_END", "flats/", wadnum); // Count the textures from TEXTURES lumps - texturesLumpPos = W_CheckNumForNamePwad("TEXTURES", wadnum, 0); + texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", wadnum, 0); while (texturesLumpPos != INT16_MAX) { count += R_CountTexturesInTEXTURESLump(wadnum, texturesLumpPos); - texturesLumpPos = W_CheckNumForNamePwad("TEXTURES", wadnum, texturesLumpPos + 1); + texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", wadnum, texturesLumpPos + 1); } // Count single-patch textures - count += count_range(TX_START, TX_END, "textures/", wadnum); + count += count_range("TX_START", "TX_END", "textures/", wadnum); return count; } diff --git a/src/r_things.cpp b/src/r_things.cpp index 9232d9bcc..b255b4e86 100644 --- a/src/r_things.cpp +++ b/src/r_things.cpp @@ -503,13 +503,14 @@ void R_AddSpriteDefs(UINT16 wadnum) switch (wadfiles[wadnum]->type) { case RET_WAD: - start = W_CheckNumForMarkerStartPwad("S_START", wadnum, 0); + start = W_CheckNumForLongNamePwad("S_START", wadnum, 0); if (start == INT16_MAX) - start = W_CheckNumForMarkerStartPwad("SS_START", wadnum, 0); //deutex compatib. + start = W_CheckNumForLongNamePwad("SS_START", wadnum, 0); //deutex compatib. + if (start != INT16_MAX) start++; - end = W_CheckNumForNamePwad("S_END",wadnum,start); + end = W_CheckNumForLongNamePwad("S_END", wadnum, start); if (end == INT16_MAX) - end = W_CheckNumForNamePwad("SS_END",wadnum,start); //deutex compatib. + end = W_CheckNumForLongNamePwad("SS_END", wadnum, start); //deutex compatib. break; case RET_PK3: start = W_CheckNumForFolderStartPK3("Sprites/", wadnum, 0); @@ -522,7 +523,7 @@ void R_AddSpriteDefs(UINT16 wadnum) if (start == INT16_MAX) { // ignore skin wads (we don't want skin sprites interfering with vanilla sprites) - if (W_CheckNumForNamePwad("S_SKIN", wadnum, 0) != UINT16_MAX) + if (W_CheckNumForLongNamePwad("S_SKIN", wadnum, 0) != UINT16_MAX) return; start = 0; //let say S_START is lump 0 diff --git a/src/s_sound.c b/src/s_sound.c index 471ac6e43..ba5771ca5 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1646,7 +1646,7 @@ void S_LoadMusicDefs(UINT16 wad) UINT16 lump = 0; - while (( lump = W_CheckNumForNamePwad("MUSICDEF", wad, lump) ) != INT16_MAX) + while ((lump = W_CheckNumForLongNamePwad("MUSICDEF", wad, lump)) != INT16_MAX) { S_LoadMusicDefLump(wadnum | lump); diff --git a/src/w_wad.c b/src/w_wad.c index f5f8a882d..ec18d580a 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -155,6 +155,9 @@ typedef struct zlentry_s #pragma pack() #endif +// maximum length of longname +#define MAXLUMPNAME 256 + // a memory entry of the wad directory struct lumpinfo_t { @@ -179,7 +182,7 @@ typedef struct typedef struct lumpnum_cache_s { - char lumpname[32]; + char lumpname[LUMPNUMCACHENAME]; UINT32 lumphash; // hash for lumpname WITHOUT all caps lumpnum_t lumpnum; } lumpnum_cache_t; @@ -1085,26 +1088,20 @@ INT32 W_InitMultipleFiles(char **filenames, boolean addons) return overallrc; } -/** Make sure a lump number is valid. - * Compiles away to nothing if PARANOIA is not defined. - */ +// check for a valid lump number +// lump cannot be asserted because some callers pass INT16_MAX, +// and most W_ functions previously always passed 0 for lump... static boolean TestValidLump(UINT16 wad, UINT16 lump) { - I_Assert(wad < MAX_WADFILES); - if (!wadfiles[wad]) // make sure the wad file exists - return false; - - I_Assert(lump < wadfiles[wad]->numlumps); - if (lump >= wadfiles[wad]->numlumps) // make sure the lump exists - return false; - - return true; + I_Assert(wad < numwadfiles); + return lump < wadfiles[wad]->numlumps; } - +// NOTE: this actually returns the longname, but since nothing relies on +// the 8-char truncation, i thought i'd save some effort :^) const char *W_CheckNameForNumPwad(UINT16 wad, UINT16 lump) { - if (lump >= wadfiles[wad]->numlumps || !TestValidLump(wad, 0)) + if (!TestValidLump(wad, lump)) return NULL; return strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[lump].longname); @@ -1117,7 +1114,7 @@ const char *W_CheckNameForNum(lumpnum_t lumpnum) const char *W_CheckFullNameForNumPwad(UINT16 wad, UINT16 lump) { - if (lump >= wadfiles[wad]->numlumps || !TestValidLump(wad, 0)) + if (!TestValidLump(wad, lump)) return NULL; return strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[lump].fullname); @@ -1138,7 +1135,7 @@ UINT16 W_FindNextEmptyInPwad(UINT16 wad, UINT16 startlump) { UINT16 i; - if (!TestValidLump(wad,0)) + if (!TestValidLump(wad, startlump)) return INT16_MAX; // @@ -1222,34 +1219,16 @@ UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 st // // 'startlump' is the lump number to start the search // +// NOTE: only exists for compatibility with code requiring the 8-char truncation. +// use W_CheckNumForLongNamePwad in new code! +// if the old code passes a string literal, you can switch without issues +// UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) { - UINT16 i; - static char uname[8 + 1]; - UINT32 hash; - - if (!TestValidLump(wad,0)) - return INT16_MAX; - - strlcpy(uname, name, sizeof uname); - strupr(uname); - hash = HASH32(uname, strlen(uname)); - - // - // scan forward - // start at 'startlump', useful parameter when there are multiple - // resources with the same name - // - if (startlump < wadfiles[wad]->numlumps) - { - lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; - for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) - if (lump_p->hash == hash && !strncmp(strbuf_get(lumpnamebuf, lump_p->longname), uname, sizeof(uname) - 1)) - return i; - } - - // not found. - return INT16_MAX; + char shortname[8+1]; + strncpy(shortname, name, 8); + shortname[8] = '\0'; + return W_CheckNumForLongNamePwad(shortname, wad, startlump); } // @@ -1261,34 +1240,28 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) { UINT16 i; - static char uname[256 + 1]; + char uname[MAXLUMPNAME+1]; UINT32 hash; + size_t namelen = min(MAXLUMPNAME, strlen(name)); + lumpinfo_t *lump_p; - if (!TestValidLump(wad,0)) + if (!TestValidLump(wad, startlump)) return INT16_MAX; - strlcpy(uname, name, sizeof uname); + memcpy(uname, name, namelen); + uname[namelen] = '\0'; strupr(uname); - hash = HASH32(uname, strlen(uname)); + hash = HASH32(uname, namelen); // // scan forward // start at 'startlump', useful parameter when there are multiple // resources with the same name // - if (startlump < wadfiles[wad]->numlumps) - { - lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; - for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) - { - if (lump_p->hash != hash) - continue; - if (strcmp(strbuf_get(lumpnamebuf, lump_p->longname), uname)) - continue; + lump_p = wadfiles[wad]->lumpinfo + startlump; + for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) + if (lump_p->hash == hash && !strcmp(strbuf_get(lumpnamebuf, lump_p->longname), uname)) return i; - } - - } // not found. return INT16_MAX; @@ -1332,16 +1305,6 @@ UINT16 W_CheckNumForNameMultiPrefixPwad(const lumpprefixes_t *prefixes, size_t n return INT16_MAX; // not found } -UINT16 -W_CheckNumForMarkerStartPwad (const char *name, UINT16 wad, UINT16 startlump) -{ - UINT16 marker; - marker = W_CheckNumForNamePwad(name, wad, startlump); - if (marker != INT16_MAX) - marker++; // Do not count the first marker - return marker; -} - // Look for the first lump from a folder. UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlump) { @@ -1398,50 +1361,16 @@ UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump) // W_CheckNumForName // Returns LUMPERROR if name not found. // +// NOTE: only exists for compatibility with code requiring the 8-char truncation. +// use W_CheckNumForLongName in new code! +// if the old code passes a string literal, you can switch without issues +// lumpnum_t W_CheckNumForName(const char *name) { - INT32 i; - UINT32 hash = name ? HASH32(name, strlen(name)) : 0; - lumpnum_t check = INT16_MAX; - - if (name == NULL) - return LUMPERROR; - - if (!*name) // some doofus gave us an empty string? - return LUMPERROR; - - // Check the lumpnumcache first. Loop backwards so that we check - // most recent entries first - for (i = lumpnumcacheindex + LUMPNUMCACHESIZE; i > lumpnumcacheindex; i--) - { - if (!lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumpname[8] - && lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumphash == hash - && strncmp(lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumpname, name, 8) == 0) - { - lumpnumcacheindex = i & (LUMPNUMCACHESIZE - 1); - return lumpnumcache[lumpnumcacheindex].lumpnum; - } - } - - // scan wad files backwards so patch lump files take precedence - for (i = numwadfiles - 1; i >= 0; i--) - { - check = W_CheckNumForNamePwad(name,(UINT16)i,0); - if (check != INT16_MAX) - break; //found it - } - - if (check == INT16_MAX) return LUMPERROR; - else - { - // Update the cache. - lumpnumcacheindex = (lumpnumcacheindex + 1) & (LUMPNUMCACHESIZE - 1); - memset(lumpnumcache[lumpnumcacheindex].lumpname, '\0', 32); - strncpy(lumpnumcache[lumpnumcacheindex].lumpname, name, 8); - lumpnumcache[lumpnumcacheindex].lumpnum = (i<<16)+check; - - return lumpnumcache[lumpnumcacheindex].lumpnum; - } + char shortname[8+1]; + strncpy(shortname, name, 8); + shortname[8] = '\0'; + return W_CheckNumForLongName(shortname); } // @@ -1453,8 +1382,8 @@ lumpnum_t W_CheckNumForName(const char *name) lumpnum_t W_CheckNumForLongName(const char *name) { INT32 i; - UINT32 hash = name ? HASH32(name, strlen(name)) : 0; - lumpnum_t check = INT16_MAX; + UINT32 hash; + lumpnum_t check; if (name == NULL) return LUMPERROR; @@ -1464,38 +1393,42 @@ lumpnum_t W_CheckNumForLongName(const char *name) // Check the lumpnumcache first. Loop backwards so that we check // most recent entries first - for (i = lumpnumcacheindex + LUMPNUMCACHESIZE; i > lumpnumcacheindex; i--) + if (strlen(name) < LUMPNUMCACHENAME) { - if (strcmp(lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumpname, name) == 0) + hash = HASH32(name, strlen(name)); + for (i = lumpnumcacheindex + LUMPNUMCACHESIZE; i > lumpnumcacheindex; i--) { - lumpnumcacheindex = i & (LUMPNUMCACHESIZE - 1); - return lumpnumcache[lumpnumcacheindex].lumpnum; + if (lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumphash == hash + && !strcmp(lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumpname, name)) + { + lumpnumcacheindex = i & (LUMPNUMCACHESIZE - 1); + return lumpnumcache[lumpnumcacheindex].lumpnum; + } } } // scan wad files backwards so patch lump files take precedence for (i = numwadfiles - 1; i >= 0; i--) { - check = W_CheckNumForLongNamePwad(name,(UINT16)i,0); + check = W_CheckNumForLongNamePwad(name, i, 0); if (check != INT16_MAX) - break; //found it + break; // found it } - if (check == INT16_MAX) return LUMPERROR; - else + if (check == INT16_MAX) + return LUMPERROR; + + if (strlen(name) < LUMPNUMCACHENAME) { - if (strlen(name) < 32) - { - // Update the cache. - lumpnumcacheindex = (lumpnumcacheindex + 1) & (LUMPNUMCACHESIZE - 1); - memset(lumpnumcache[lumpnumcacheindex].lumpname, '\0', 32); - strlcpy(lumpnumcache[lumpnumcacheindex].lumpname, name, 32); - lumpnumcache[lumpnumcacheindex].lumpnum = (i << 16) + check; - lumpnumcache[lumpnumcacheindex].lumphash = hash; - } - - return (i << 16) + check; + // Update the cache. + lumpnumcacheindex = (lumpnumcacheindex + 1) & (LUMPNUMCACHESIZE - 1); + memset(lumpnumcache[lumpnumcacheindex].lumpname, '\0', LUMPNUMCACHENAME); + strlcpy(lumpnumcache[lumpnumcacheindex].lumpname, name, LUMPNUMCACHENAME); + lumpnumcache[lumpnumcacheindex].lumpnum = (i << 16) + check; + lumpnumcache[lumpnumcacheindex].lumphash = hash; } + + return (i << 16) + check; } // Look for valid map data through all added files in descendant order. @@ -1586,36 +1519,6 @@ lumpnum_t W_GetNumForLongName(const char *name) return i; } -// -// W_CheckNumForNameInBlock -// Checks only in blocks from blockstart lump to blockend lump -// -lumpnum_t W_CheckNumForNameInBlock(const char *name, const char *blockstart, const char *blockend) -{ - INT32 i; - lumpnum_t bsid, beid; - lumpnum_t check = INT16_MAX; - - // scan wad files backwards so patch lump files take precedence - for (i = numwadfiles - 1; i >= 0; i--) - { - if (wadfiles[i]->type == RET_WAD) - { - bsid = W_CheckNumForNamePwad(blockstart, (UINT16)i, 0); - if (bsid == INT16_MAX) - continue; // Start block doesn't exist? - beid = W_CheckNumForNamePwad(blockend, (UINT16)i, 0); - if (beid == INT16_MAX) - continue; // End block doesn't exist? - - check = W_CheckNumForNamePwad(name, (UINT16)i, bsid); - if (check < beid) - return (i<<16)+check; // found it, in our constraints - } - } - return LUMPERROR; -} - // // W_CheckNumForNameInFolder // Checks only in PK3s in the specified folder diff --git a/src/w_wad.h b/src/w_wad.h index 51ea2c1f1..ba3e432e1 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -139,9 +139,6 @@ UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) UINT16 W_CheckNumForNamePrefixPwad(const char *name, size_t namelen, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForNameMultiPrefixPwad(const lumpprefixes_t *prefixes, size_t numprefixes, UINT16 wad, UINT16 startlump); -/* Find the first lump after F_START for instance. */ -UINT16 W_CheckNumForMarkerStartPwad(const char *name, UINT16 wad, UINT16 startlump); - UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump); @@ -151,7 +148,6 @@ lumpnum_t W_CheckNumForName(const char *name); lumpnum_t W_CheckNumForLongName(const char *name); lumpnum_t W_GetNumForName(const char *name); // like W_CheckNumForName but I_Error on LUMPERROR lumpnum_t W_GetNumForLongName(const char *name); -lumpnum_t W_CheckNumForNameInBlock(const char *name, const char *blockstart, const char *blockend); lumpnum_t W_CheckNumForNameInFolder(const char *lump, const char *folder); UINT8 W_LumpExists(const char *name); // Lua uses this. From 7aae6775e652a38eec18ed87db2395e3ad10ba47 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Wed, 25 Jun 2025 17:39:29 +0200 Subject: [PATCH 05/18] Use LUMPERROR instead of INT16_MAX, and MAX_WADFILES instead of UINT16_MAX The 32768th lump of a file works now lol --- src/blua/lbaselib.c | 2 +- src/d_main.cpp | 2 +- src/doomtype.h | 2 +- src/hardware/hw_main.c | 4 +-- src/hu_stuff.c | 8 ++--- src/k_brightmap.c | 4 +-- src/k_terrain.c | 4 +-- src/p_setup.c | 12 ++++---- src/p_spec.c | 7 ++--- src/r_data.c | 8 ++--- src/r_picformats.c | 4 +-- src/r_skins.c | 6 ++-- src/r_textures.c | 23 ++++++--------- src/r_things.cpp | 19 +++++++----- src/s_sound.c | 8 ++--- src/w_wad.c | 66 ++++++++++++++++++++---------------------- src/w_wad.h | 2 +- 17 files changed, 83 insertions(+), 98 deletions(-) diff --git a/src/blua/lbaselib.c b/src/blua/lbaselib.c index 644565c28..9fda07d7b 100644 --- a/src/blua/lbaselib.c +++ b/src/blua/lbaselib.c @@ -279,7 +279,7 @@ static int luaB_dofile (lua_State *L) { snprintf(fullfilename, sizeof(fullfilename), "Lua/%s", filename); lumpnum = W_CheckNumForFullNamePK3(fullfilename, numwadfiles - 1, 0); - if (lumpnum == INT16_MAX) + if (lumpnum == LUMPERROR) luaL_error(L, "can't find script " LUA_QS, fullfilename); LUA_LoadLump(numwadfiles - 1, lumpnum, false); diff --git a/src/d_main.cpp b/src/d_main.cpp index d770133a7..467378456 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -1550,7 +1550,7 @@ void D_SRB2Main(void) } D_CleanFile(startuppwads); - partadd_earliestfile = UINT16_MAX; + partadd_earliestfile = MAX_WADFILES; CON_SetLoadingProgress(LOADED_PWAD); diff --git a/src/doomtype.h b/src/doomtype.h index 6cd483773..970c8b5fb 100644 --- a/src/doomtype.h +++ b/src/doomtype.h @@ -341,7 +341,7 @@ union FColorRGBA typedef union FColorRGBA RGBA_t; typedef UINT32 lumpnum_t; // 16 : 16 unsigned long (wad num: lump num) -#define LUMPERROR UINT32_MAX +#define LUMPERROR UINT16_MAX // unified LUMPERROR typedef UINT32 tic_t; #define INFTICS UINT32_MAX diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index d6795f3bc..2b9c19d6c 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -6591,7 +6591,7 @@ void HWR_LoadCustomShadersFromFile(UINT16 wadnum, boolean PK3) int i; lump = W_CheckNumForLongNamePwad("SHADERS", wadnum, 0); - if (lump == INT16_MAX) + if (lump == LUMPERROR) return; shaderdef = W_CacheLumpNumPwad(wadnum, lump, PU_CACHE); @@ -6671,7 +6671,7 @@ skip_lump: shader_lumpnum = W_CheckNumForNamePwad(shader_lumpname, wadnum, 0); } - if (shader_lumpnum == INT16_MAX) + if (shader_lumpnum == LUMPERROR) { CONS_Alert(CONS_ERROR, "HWR_LoadCustomShadersFromFile: Missing shader source %s (file %s, line %d)\n", shader_lumpname, wadfiles[wadnum]->filename, linenum); Z_Free(shader_lumpname); diff --git a/src/hu_stuff.c b/src/hu_stuff.c index 4cf04d528..fc71e8450 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -312,19 +312,19 @@ patch_t *HU_UpdateOrBlankPatch(patch_t **user, boolean required, const char *for va_list ap; char buffer[9]; - lumpnum_t lump = INT16_MAX; + lumpnum_t lump = LUMPERROR; patch_t *patch; va_start (ap, format); vsnprintf(buffer, sizeof buffer, format, ap); va_end (ap); - if (user && partadd_earliestfile != UINT16_MAX) + if (user && partadd_earliestfile != MAX_WADFILES) { UINT16 fileref = numwadfiles; - lump = INT16_MAX; + lump = LUMPERROR; - while ((lump == INT16_MAX) && ((--fileref) >= partadd_earliestfile)) + while ((lump == LUMPERROR) && ((--fileref) >= partadd_earliestfile)) { lump = W_CheckNumForNamePwad(buffer, fileref, 0); } diff --git a/src/k_brightmap.c b/src/k_brightmap.c index 19f90f02a..cb69e30d4 100644 --- a/src/k_brightmap.c +++ b/src/k_brightmap.c @@ -178,9 +178,7 @@ void K_InitBrightmapsPwad(UINT16 wadNum) I_Assert(brightmapStorage == NULL); // Find BRIGHT lump in the WAD - lumpNum = W_CheckNumForLongNamePwad("BRIGHT", wadNum, 0); - - for (; lumpNum != INT16_MAX; lumpNum = W_CheckNumForLongNamePwad("BRIGHT", wadNum, lumpNum + 1)) + for (lumpNum = 0; (lumpNum = W_CheckNumForLongNamePwad("BRIGHT", wadNum, lumpNum)) != LUMPERROR; lumpNum++) { UINT8 *data = (UINT8 *)W_CacheLumpNumPwad(wadNum, lumpNum, PU_STATIC); diff --git a/src/k_terrain.c b/src/k_terrain.c index 0a33d948b..50d82516f 100644 --- a/src/k_terrain.c +++ b/src/k_terrain.c @@ -2263,12 +2263,10 @@ boolean K_InitTerrainPwad(UINT16 wadNum) UINT16 lumpNum; boolean terrainLoaded = false; - lumpNum = W_CheckNumForLongNamePwad("TERRAIN", wadNum, 0); - // Iterate through all lumps and compare the name individually. // In PK3 files, you can potentially have multiple TERRAIN differentiated by // their file extension. - for (; lumpNum != INT16_MAX; lumpNum = W_CheckNumForLongNamePwad("TERRAIN", wadNum, lumpNum + 1)) + for (lumpNum = 0; (lumpNum = W_CheckNumForLongNamePwad("TERRAIN", wadNum, lumpNum)) != LUMPERROR; lumpNum++) { UINT8 *data = (UINT8 *)W_CacheLumpNumPwad(wadNum, lumpNum, PU_STATIC); diff --git a/src/p_setup.c b/src/p_setup.c index a99751b95..c8842c0eb 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -196,7 +196,7 @@ static SINT8 partadd_stage = -1; static boolean partadd_replacescurrentmap = false; static boolean partadd_terrainloaded = false; static boolean partadd_important = false; -UINT16 partadd_earliestfile = UINT16_MAX; +UINT16 partadd_earliestfile = MAX_WADFILES; // Maintain *ZOOM TUBE* waypoints // Renamed because SRB2Kart owns real waypoints. @@ -9096,7 +9096,7 @@ UINT8 P_InitMapData(boolean existingmapheaders) maplump = W_CheckNumForMap(name); // Doesn't exist? - if (maplump == INT16_MAX) + if (maplump == LUMPERROR) { #ifndef DEVELOP if (!existingmapheaders) @@ -9222,7 +9222,7 @@ boolean P_AddWadFile(const char *wadfilename, wadcompat_t compat) { UINT16 wadnum; - if ((wadnum = P_PartialAddWadFile(wadfilename, compat)) == UINT16_MAX) + if ((wadnum = P_PartialAddWadFile(wadfilename, compat)) == MAX_WADFILES) return false; P_MultiSetupWadFiles(true); @@ -9245,10 +9245,10 @@ UINT16 P_PartialAddWadFile(const char *wadfilename, wadcompat_t compat) UINT16 musPos, musEnd; // Init file. - if ((numlumps = W_InitFile(wadfilename, false, false, compat)) == INT16_MAX) + if ((numlumps = W_InitFile(wadfilename, false, false, compat)) == LUMPERROR) { refreshdirmenu |= REFRESHDIR_NOTLOADED; - return UINT16_MAX; + return MAX_WADFILES; } wadnum = (UINT16)(numwadfiles-1); @@ -9422,7 +9422,7 @@ boolean P_MultiSetupWadFiles(boolean fullsetup) partadd_important = false; partadd_replacescurrentmap = false; partadd_terrainloaded = false; - partadd_earliestfile = UINT16_MAX; + partadd_earliestfile = MAX_WADFILES; return true; } diff --git a/src/p_spec.c b/src/p_spec.c index ee2fdf436..88acc17a3 100644 --- a/src/p_spec.c +++ b/src/p_spec.c @@ -152,13 +152,10 @@ void P_InitPicAnims(void) UINT16 animdefsLumpNum; // Find ANIMDEFS lump in the WAD - animdefsLumpNum = W_CheckNumForLongNamePwad("ANIMDEFS", w, 0); - - while (animdefsLumpNum != INT16_MAX) + for (animdefsLumpNum = 0; (animdefsLumpNum = W_CheckNumForLongNamePwad("ANIMDEFS", w, animdefsLumpNum)) != LUMPERROR; animdefsLumpNum++) { - animdeftempflats = ((partadd_earliestfile == UINT16_MAX) || partadd_earliestfile == w); + animdeftempflats = ((partadd_earliestfile == MAX_WADFILES) || partadd_earliestfile == w); P_ParseANIMDEFSLump(w, animdefsLumpNum); - animdefsLumpNum = W_CheckNumForLongNamePwad("ANIMDEFS", (UINT16)w, animdefsLumpNum + 1); } } diff --git a/src/r_data.c b/src/r_data.c index 3c50c59d1..c9bf53611 100644 --- a/src/r_data.c +++ b/src/r_data.c @@ -221,10 +221,10 @@ static inline lumpnum_t R_CheckNumForNameList(const char *name, lumplist_t *list size_t i; UINT16 lump; - for (i = listsize - 1; i < INT16_MAX; i--) + for (i = listsize - 1; i != SIZE_MAX; i--) { lump = W_CheckNumForNamePwad(name, list[i].wadfile, list[i].firstlump); - if (lump == INT16_MAX || lump > (list[i].firstlump + list[i].numlumps)) + if (lump == LUMPERROR || lump > (list[i].firstlump + list[i].numlumps)) continue; else return (list[i].wadfile<<16)+lump; @@ -241,12 +241,12 @@ static void R_InitExtraColormaps(void) for (cfile = clump = 0; cfile < numwadfiles; cfile++, clump = 0) { startnum = W_CheckNumForLongNamePwad("C_START", cfile, clump); - if (startnum == INT16_MAX) + if (startnum == LUMPERROR) continue; endnum = W_CheckNumForLongNamePwad("C_END", cfile, clump); - if (endnum == INT16_MAX) + if (endnum == LUMPERROR) I_Error("R_InitExtraColormaps: C_START without C_END\n"); // This shouldn't be possible when you use the Pwad function, silly diff --git a/src/r_picformats.c b/src/r_picformats.c index 85833b81a..9216f2c39 100644 --- a/src/r_picformats.c +++ b/src/r_picformats.c @@ -1860,8 +1860,8 @@ void R_LoadSpriteInfoLumps(UINT16 wadnum) { "SPR_", 4 }, }; - UINT16 i = W_CheckNumForNameMultiPrefixPwad(sprtinfoprfx, NUMPREFIXES, wadnum, 0); - for (; i != INT16_MAX; i = W_CheckNumForNameMultiPrefixPwad(sprtinfoprfx, NUMPREFIXES, wadnum, i + 1)) + UINT16 i; + for (i = 0; (i = W_CheckNumForNameMultiPrefixPwad(sprtinfoprfx, NUMPREFIXES, wadnum, i)) != LUMPERROR; i++) { R_ParseSPRTINFOLump(wadnum, i); } diff --git a/src/r_skins.c b/src/r_skins.c index 2c72e0f30..86f6146ea 100644 --- a/src/r_skins.c +++ b/src/r_skins.c @@ -105,7 +105,7 @@ static void Sk_SetDefaultValue(skin_t *skin) snprintf(skin->name, sizeof skin->name, "skin %u", (UINT32)(skin-skins)); skin->name[sizeof skin->name - 1] = '\0'; - skin->wadnum = INT16_MAX; + skin->wadnum = MAX_WADFILES; skin->flags = 0; @@ -648,7 +648,7 @@ void R_AddSkins(UINT16 wadnum) // search for all skin markers in pwad // - while ((lump = W_CheckNumForNamePrefixPwad("S_SKIN", 6, wadnum, lastlump)) != INT16_MAX) + while ((lump = W_CheckNumForNamePrefixPwad("S_SKIN", 6, wadnum, lastlump)) != LUMPERROR) { // advance by default lastlump = lump + 1; @@ -820,7 +820,7 @@ void R_PatchSkins(UINT16 wadnum) // search for all skin patch markers in pwad // - while ((lump = W_CheckNumForNamePrefixPwad("P_SKIN", 6, wadnum, lastlump)) != INT16_MAX) + while ((lump = W_CheckNumForNamePrefixPwad("P_SKIN", 6, wadnum, lastlump)) != LUMPERROR) { INT32 skinnum = 0; diff --git a/src/r_textures.c b/src/r_textures.c index 371878e66..ec68362a5 100644 --- a/src/r_textures.c +++ b/src/r_textures.c @@ -1134,11 +1134,11 @@ Rloadflats (INT32 i, INT32 w) else { texstart = W_CheckNumForLongNamePwad("F_START", (UINT16)w, 0); - if (texstart != INT16_MAX) texstart++; + if (texstart != LUMPERROR) texstart++; texend = W_CheckNumForLongNamePwad("F_END", (UINT16)w, texstart); } - if (!( texstart == INT16_MAX || texend == INT16_MAX )) + if (!( texstart == LUMPERROR || texend == LUMPERROR )) { // Work through each lump between the markers in the WAD. for (j = 0; j < (texend - texstart); j++) @@ -1216,7 +1216,7 @@ Rloadtextures (INT32 i, INT32 w) texstart = W_CheckNumForFolderStartPK3("textures/", (UINT16)w, 0); texend = W_CheckNumForFolderEndPK3("textures/", (UINT16)w, texstart); texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", (UINT16)w, 0); - while (texturesLumpPos != INT16_MAX) + while (texturesLumpPos != LUMPERROR) { R_ParseTEXTURESLump(w, texturesLumpPos, &i); texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", (UINT16)w, texturesLumpPos + 1); @@ -1225,14 +1225,14 @@ Rloadtextures (INT32 i, INT32 w) else { texstart = W_CheckNumForLongNamePwad("TX_START", (UINT16)w, 0); - if (texstart != INT16_MAX) texstart++; + if (texstart != LUMPERROR) texstart++; texend = W_CheckNumForLongNamePwad("TX_END", (UINT16)w, 0); texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", (UINT16)w, 0); - if (texturesLumpPos != INT16_MAX) + if (texturesLumpPos != LUMPERROR) R_ParseTEXTURESLump(w, texturesLumpPos, &i); } - if (!( texstart == INT16_MAX || texend == INT16_MAX )) + if (!( texstart == LUMPERROR || texend == LUMPERROR )) { // Work through each lump between the markers in the WAD. for (j = 0; j < (texend - texstart); j++) @@ -1321,11 +1321,11 @@ count_range else { texstart = W_CheckNumForLongNamePwad(marker_start, wadnum, 0); - if (texstart != INT16_MAX) texstart++; + if (texstart != LUMPERROR) texstart++; texend = W_CheckNumForLongNamePwad(marker_end, wadnum, texstart); } - if (texstart != INT16_MAX && texend != INT16_MAX) + if (texstart != LUMPERROR && texend != LUMPERROR) { // PK3s have subfolders, so we can't just make a simple sum if (wadfiles[wadnum]->type == RET_PK3) @@ -1361,13 +1361,8 @@ static INT32 R_CountTextures(UINT16 wadnum) count += count_range("F_START", "F_END", "flats/", wadnum); // Count the textures from TEXTURES lumps - texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", wadnum, 0); - - while (texturesLumpPos != INT16_MAX) - { + for (texturesLumpPos = 0; (texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", wadnum, texturesLumpPos)) != LUMPERROR; texturesLumpPos++) count += R_CountTexturesInTEXTURESLump(wadnum, texturesLumpPos); - texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", wadnum, texturesLumpPos + 1); - } // Count single-patch textures count += count_range("TX_START", "TX_END", "textures/", wadnum); diff --git a/src/r_things.cpp b/src/r_things.cpp index b255b4e86..d1420cb4a 100644 --- a/src/r_things.cpp +++ b/src/r_things.cpp @@ -325,8 +325,7 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 if (endlump > wadfiles[wadnum]->numlumps) endlump = wadfiles[wadnum]->numlumps; - l = W_CheckNumForNamePrefixPwad(sprname, 4, wadnum, startlump); - for (; l != INT16_MAX && l < endlump; l = W_CheckNumForNamePrefixPwad(sprname, 4, wadnum, l+1)) + for (l = startlump; (l = W_CheckNumForNamePrefixPwad(sprname, 4, wadnum, l)) != LUMPERROR && l < endlump; l++) { { const char *longname = W_CheckNameForNumPwad(wadnum, l); @@ -504,12 +503,12 @@ void R_AddSpriteDefs(UINT16 wadnum) { case RET_WAD: start = W_CheckNumForLongNamePwad("S_START", wadnum, 0); - if (start == INT16_MAX) + if (start == LUMPERROR) start = W_CheckNumForLongNamePwad("SS_START", wadnum, 0); //deutex compatib. - if (start != INT16_MAX) start++; + if (start != LUMPERROR) start++; end = W_CheckNumForLongNamePwad("S_END", wadnum, start); - if (end == INT16_MAX) + if (end == LUMPERROR) end = W_CheckNumForLongNamePwad("SS_END", wadnum, start); //deutex compatib. break; case RET_PK3: @@ -520,16 +519,20 @@ void R_AddSpriteDefs(UINT16 wadnum) return; } - if (start == INT16_MAX) + if (start == LUMPERROR) { // ignore skin wads (we don't want skin sprites interfering with vanilla sprites) - if (W_CheckNumForLongNamePwad("S_SKIN", wadnum, 0) != UINT16_MAX) + // G: DON'T ignore skin wads. this check did nothing because it incorrectly checked for UINT16_MAX instead of INT16_MAX + // plus, this didn't exist in kart, so it might break old WADs... + /* + if (W_CheckNumForLongNamePwad("S_SKIN", wadnum, 0) != LUMPERROR) return; + */ start = 0; //let say S_START is lump 0 } - if (end == INT16_MAX || start >= end) + if (end == LUMPERROR || start >= end) { CONS_Debug(DBG_SETUP, "no sprites in pwad %d\n", wadnum); return; diff --git a/src/s_sound.c b/src/s_sound.c index ba5771ca5..f4f5079e1 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1644,14 +1644,10 @@ void S_LoadMusicDefs(UINT16 wad) { const lumpnum_t wadnum = wad << 16; - UINT16 lump = 0; + UINT16 lump; - while ((lump = W_CheckNumForLongNamePwad("MUSICDEF", wad, lump)) != INT16_MAX) - { + for (lump = 0; (lump = W_CheckNumForLongNamePwad("MUSICDEF", wad, lump)) != LUMPERROR; lump++) S_LoadMusicDefLump(wadnum | lump); - - lump++; - } } // diff --git a/src/w_wad.c b/src/w_wad.c index ec18d580a..f6a353321 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -319,14 +319,14 @@ static inline void W_LoadDehackedLumpsPK3(UINT16 wadnum, boolean mainfile) UINT16 posStart, posEnd; posStart = W_CheckNumForFullNamePK3("Init.lua", wadnum, 0); - if (posStart != INT16_MAX) + if (posStart != LUMPERROR) { LUA_LoadLump(wadnum, posStart, true); } else { posStart = W_CheckNumForFolderStartPK3("Lua/", wadnum, 0); - if (posStart != INT16_MAX) + if (posStart != LUMPERROR) { posEnd = W_CheckNumForFolderEndPK3("Lua/", wadnum, posStart); for (; posStart < posEnd; posStart++) @@ -335,7 +335,7 @@ static inline void W_LoadDehackedLumpsPK3(UINT16 wadnum, boolean mainfile) } posStart = W_CheckNumForFolderStartPK3("SOC/", wadnum, 0); - if (posStart != INT16_MAX) + if (posStart != LUMPERROR) { posEnd = W_CheckNumForFolderEndPK3("SOC/", wadnum, posStart); @@ -353,8 +353,7 @@ static inline void W_LoadDehackedLumps(UINT16 wadnum, boolean mainfile) UINT16 lump; // Find Lua scripts before SOCs to allow new A_Actions in SOC editing. - lump = W_CheckNumForNamePrefixPwad("LUA_", 4, wadnum, 0); - for (; lump != INT16_MAX; lump = W_CheckNumForNamePrefixPwad("LUA_", 4, wadnum, lump + 1)) + for (lump = 0; (lump = W_CheckNumForNamePrefixPwad("LUA_", 4, wadnum, lump)) != LUMPERROR; lump++) LUA_LoadLump(wadnum, lump, true); #define NUMPREFIXES 3 @@ -364,8 +363,7 @@ static inline void W_LoadDehackedLumps(UINT16 wadnum, boolean mainfile) { "OBJCTCFG", 9 }, }; - lump = W_CheckNumForNameMultiPrefixPwad(socprfx, NUMPREFIXES, wadnum, 0); - for (; lump != INT16_MAX; lump = W_CheckNumForNameMultiPrefixPwad(socprfx, NUMPREFIXES, wadnum, lump+1)) + for (lump = 0; (lump = W_CheckNumForNameMultiPrefixPwad(socprfx, NUMPREFIXES, wadnum, lump)) != LUMPERROR; lump++) { // shameless copy+paste of code from LUA_LoadLump CONS_Printf(M_GetText("Loading SOC from %s|%s\n"), wadfiles[wadnum]->filename, W_CheckFullNameForNumPwad(wadnum, lump)); DEH_LoadDehackedLumpPwad(wadnum, lump, mainfile); @@ -394,7 +392,7 @@ static inline boolean CheckCompatSkins(UINT16 wadnum) { #if 0 UINT16 skin = W_CheckNumForNamePwad("S_SKIN", wadnum, 0); - if (skin == INT16_MAX) + if (skin == LUMPERROR) return false; // valid kart skins can only have one sprname for all frames @@ -855,7 +853,7 @@ static UINT16 W_InitFileError (const char *filename, boolean exitworthy) } else CONS_Printf(M_GetText("Errors occurred while loading %s; not added.\n"), filename); - return INT16_MAX; + return LUMPERROR; } // Allocate a wadfile, setup the lumpinfo (directory) and @@ -912,7 +910,7 @@ UINT16 W_InitFile(const char *filename, boolean mainfile, boolean startup, wadco if (important == -1) { fclose(handle); - return INT16_MAX; + return LUMPERROR; } important = !important; @@ -1077,9 +1075,9 @@ INT32 W_InitMultipleFiles(char **filenames, boolean addons) //CONS_Debug(DBG_SETUP, "Loading %s\n", *filenames); rc = W_InitFile(*filenames, !addons, true, WC_AUTO); - if (rc == INT16_MAX) + if (rc == LUMPERROR) CONS_Printf(M_GetText("Errors occurred while loading %s; not added.\n"), *filenames); - overallrc &= (rc != INT16_MAX) ? 1 : 0; + overallrc &= (rc != LUMPERROR) ? 1 : 0; } if (!numwadfiles) @@ -1089,7 +1087,7 @@ INT32 W_InitMultipleFiles(char **filenames, boolean addons) } // check for a valid lump number -// lump cannot be asserted because some callers pass INT16_MAX, +// lump cannot be asserted because some callers pass LUMPERROR, // and most W_ functions previously always passed 0 for lump... static boolean TestValidLump(UINT16 wad, UINT16 lump) { @@ -1136,7 +1134,7 @@ UINT16 W_FindNextEmptyInPwad(UINT16 wad, UINT16 startlump) UINT16 i; if (!TestValidLump(wad, startlump)) - return INT16_MAX; + return LUMPERROR; // // scan forward @@ -1152,7 +1150,7 @@ UINT16 W_FindNextEmptyInPwad(UINT16 wad, UINT16 startlump) } // not found. - return INT16_MAX; + return LUMPERROR; } // Get a map marker for WADs, and a standalone WAD file lump inside PK3s. @@ -1183,7 +1181,7 @@ UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 st { i = W_CheckNumForFolderStartPK3("maps/", wad, startlump); - if (i != INT16_MAX) + if (i != LUMPERROR) { end = W_CheckNumForFolderEndPK3("maps/", wad, i); @@ -1209,7 +1207,7 @@ UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 st } } - return INT16_MAX; + return LUMPERROR; } // @@ -1246,7 +1244,7 @@ UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) lumpinfo_t *lump_p; if (!TestValidLump(wad, startlump)) - return INT16_MAX; + return LUMPERROR; memcpy(uname, name, namelen); uname[namelen] = '\0'; @@ -1264,7 +1262,7 @@ UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) return i; // not found. - return INT16_MAX; + return LUMPERROR; } // same as W_CheckNumForNamePwad, but only checks for a name PREFIX @@ -1274,7 +1272,7 @@ UINT16 W_CheckNumForNamePrefixPwad(const char *name, size_t namelen, UINT16 wad, lumpinfo_t *lump_p; if (!TestValidLump(wad, startlump)) - return INT16_MAX; + return LUMPERROR; // scan forward, start at lump_p = wadfiles[wad]->lumpinfo + startlump; @@ -1282,7 +1280,7 @@ UINT16 W_CheckNumForNamePrefixPwad(const char *name, size_t namelen, UINT16 wad, if (!strncmp(strbuf_get(lumpnamebuf, lump_p->longname), name, namelen)) return i; - return INT16_MAX; // not found + return LUMPERROR; // not found } // same as W_CheckNumForNamePrefixPwad, but check for MULTIPLE PREFIXES!!!!!! @@ -1293,7 +1291,7 @@ UINT16 W_CheckNumForNameMultiPrefixPwad(const lumpprefixes_t *prefixes, size_t n lumpinfo_t *lump_p; if (!TestValidLump(wad, startlump)) - return INT16_MAX; + return LUMPERROR; // scan forward, start at lump_p = wadfiles[wad]->lumpinfo + startlump; @@ -1302,7 +1300,7 @@ UINT16 W_CheckNumForNameMultiPrefixPwad(const lumpprefixes_t *prefixes, size_t n if (!strncmp(strbuf_get(lumpnamebuf, lump_p->longname), prefixes[j].prefix, prefixes[j].len)) return i; - return INT16_MAX; // not found + return LUMPERROR; // not found } // Look for the first lump from a folder. @@ -1341,7 +1339,7 @@ UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump) } // In a PK3 type of resource file, it looks for an entry with the specified full name. -// Returns lump position in PK3's lumpinfo, or INT16_MAX if not found. +// Returns lump position in PK3's lumpinfo, or LUMPERROR if not found. UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump) { INT32 i; @@ -1354,7 +1352,7 @@ UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump) } } // Not found at all? - return INT16_MAX; + return LUMPERROR; } // @@ -1411,11 +1409,11 @@ lumpnum_t W_CheckNumForLongName(const char *name) for (i = numwadfiles - 1; i >= 0; i--) { check = W_CheckNumForLongNamePwad(name, i, 0); - if (check != INT16_MAX) + if (check != LUMPERROR) break; // found it } - if (check == INT16_MAX) + if (check == LUMPERROR) return LUMPERROR; if (strlen(name) < LUMPNUMCACHENAME) @@ -1435,7 +1433,7 @@ lumpnum_t W_CheckNumForLongName(const char *name) // Get a map marker for WADs, and a standalone WAD file lump inside PK3s. lumpnum_t W_CheckNumForMap(const char *name) { - lumpnum_t check = INT16_MAX; + lumpnum_t check = LUMPERROR; UINT32 uhash, hash = HASH32(name, min(strlen(name), LUMPNUMCACHENAME)); INT32 i; @@ -1460,11 +1458,11 @@ lumpnum_t W_CheckNumForMap(const char *name) { check = W_CheckNumForMapPwad(name, uhash, (UINT16)i, 0); - if (check != INT16_MAX) + if (check != LUMPERROR) break; // found it } - if (check == INT16_MAX) + if (check == LUMPERROR) { return LUMPERROR; } @@ -1527,7 +1525,7 @@ lumpnum_t W_CheckNumForNameInFolder(const char *lump, const char *folder) { INT32 i; lumpnum_t fsid, feid; - lumpnum_t check = INT16_MAX; + lumpnum_t check = LUMPERROR; // scan wad files backwards so patch lump files take precedence for (i = numwadfiles - 1; i >= 0; i--) @@ -1535,13 +1533,13 @@ lumpnum_t W_CheckNumForNameInFolder(const char *lump, const char *folder) if (wadfiles[i]->type == RET_PK3) { fsid = W_CheckNumForFolderStartPK3(folder, (UINT16)i, 0); - if (fsid == INT16_MAX) + if (fsid == LUMPERROR) { continue; // Start doesn't exist? } feid = W_CheckNumForFolderEndPK3(folder, (UINT16)i, fsid); - if (feid == INT16_MAX) + if (feid == LUMPERROR) { continue; // End doesn't exist? } @@ -2158,7 +2156,7 @@ W_VerifyWAD (FILE *fp, lumpchecklist_t *checklist, boolean status) // read the header if (fread(&header, 1, sizeof header, fp) == sizeof header - && header.numlumps < INT16_MAX + && header.numlumps < UINT16_MAX && strncmp(header.identification, "ZWAD", 4) && strncmp(header.identification, "IWAD", 4) && strncmp(header.identification, "PWAD", 4) diff --git a/src/w_wad.h b/src/w_wad.h index ba3e432e1..f2b6b9993 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -116,7 +116,7 @@ void W_Shutdown(void); // Opens a WAD file. Returns the FILE * handle for the file, or NULL if not found or could not be opened FILE *W_OpenWadFile(const char **filename, boolean useerrors); -// Load and add a wadfile to the active wad files, returns numbers of lumps, INT16_MAX on error +// Load and add a wadfile to the active wad files, returns numbers of lumps, LUMPERROR on error UINT16 W_InitFile(const char *filename, boolean mainfile, boolean startup, wadcompat_t compat); boolean W_MakeFileHash(const char *filename, boolean openwad, UINT64 *ret); From 2ff383a248f16a557d5dd31df8035da2ca29e5f2 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Wed, 25 Jun 2025 22:13:32 +0200 Subject: [PATCH 06/18] Replace shortname functions with stubs, and some trivial caller replacements --- src/console.c | 9 ++-- src/d_main.cpp | 8 +-- src/f_finale.c | 26 ++++----- src/hardware/hw_light.c | 2 +- src/hardware/hw_main.c | 4 +- src/hu_stuff.c | 2 +- src/k_hud.c | 8 +-- src/m_menu.c | 116 ++++++++++++++++++++-------------------- src/r_data.c | 9 ++-- src/r_draw.cpp | 38 ++++++------- src/sdl/endtxt.c | 2 +- src/sdl/i_system.cpp | 2 +- src/st_stuff.c | 2 +- src/v_video.c | 2 +- src/w_wad.c | 71 ------------------------ src/w_wad.h | 19 +++++-- src/y_inter.c | 24 ++++----- 17 files changed, 142 insertions(+), 202 deletions(-) diff --git a/src/console.c b/src/console.c index ced99246e..855386b5c 100644 --- a/src/console.c +++ b/src/console.c @@ -299,7 +299,8 @@ void CON_SetupBackColormapEx(INT32 color, boolean prompt) { UINT16 i, palsum; UINT8 j, palindex; - UINT8 *pal = W_CacheLumpName(GetPalette(), PU_CACHE); + lumpnum_t lump = W_GetNumForName(GetPalette()); + UINT8 *pal = W_CacheLumpNum(lump, PU_CACHE); INT32 shift = 6; if (color == INT32_MAX) @@ -1758,12 +1759,12 @@ static void CON_DrawBackpic(void) // Get the lumpnum for CONSBACK, STARTUP (Only during game startup) or fallback into MISSING. if (con_startup) - piclump = W_CheckNumForName("STARTUP"); + piclump = W_CheckNumForLongName("STARTUP"); else - piclump = W_CheckNumForName("KARTKREW"); + piclump = W_CheckNumForLongName("KARTKREW"); if (piclump == LUMPERROR) - piclump = W_GetNumForName("MISSING"); + piclump = W_GetNumForLongName("MISSING"); // Cache the patch. con_backpic = W_CachePatchNum(piclump, PU_PATCH); diff --git a/src/d_main.cpp b/src/d_main.cpp index 467378456..ea834c848 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -295,7 +295,7 @@ static void D_RenderLevel(void) if (r_splitscreen == 2) // Draw over the fourth screen so you don't have to stare at a HOM :V { // V_DrawPatchFill, but for the fourth screen only - patch_t *pat = static_cast(W_CachePatchName("SRB2BACK", PU_CACHE)); + patch_t *pat = static_cast(W_CachePatchLongName("SRB2BACK", PU_CACHE)); INT32 dupz = (vid.dupx < vid.dupy ? vid.dupx : vid.dupy); INT32 x, y, pw = SHORT(pat->width) * dupz, ph = SHORT(pat->height) * dupz; @@ -632,7 +632,7 @@ static bool D_Display(void) py = 4; else py = viewwindowy + 4; - patch = static_cast(W_CachePatchName("M_PAUSE", PU_PATCH)); + patch = static_cast(W_CachePatchLongName("M_PAUSE", PU_PATCH)); V_DrawScaledPatch(viewwindowx + (BASEVIDWIDTH - patch->width)/2, py, 0, patch); } @@ -786,9 +786,9 @@ void D_SRB2Loop(void) /* Smells like a hack... Don't fade Sonic's ass into the title screen. */ if (gamestate != GS_TITLESCREEN) { - static lumpnum_t gstartuplumpnum = W_CheckNumForName("STARTUP"); + static lumpnum_t gstartuplumpnum = W_CheckNumForLongName("STARTUP"); if (gstartuplumpnum == LUMPERROR) - gstartuplumpnum = W_GetNumForName("MISSING"); + gstartuplumpnum = W_GetNumForLongName("MISSING"); V_DrawScaledPatch(0, 0, 0, W_CachePatchNum(gstartuplumpnum, PU_PATCH)); } #endif diff --git a/src/f_finale.c b/src/f_finale.c index 77ca2fb0c..26de402ad 100644 --- a/src/f_finale.c +++ b/src/f_finale.c @@ -232,8 +232,8 @@ static void F_TitleBGScroll(INT32 scrollspeed) patch_t *pat, *pat2; INT32 anim2 = 0; - pat = W_CachePatchName("TITLEBG1", PU_CACHE); - pat2 = W_CachePatchName("TITLEBG2", PU_CACHE); + pat = W_CachePatchLongName("TITLEBG1", PU_CACHE); + pat2 = W_CachePatchLongName("TITLEBG2", PU_CACHE); w = (vid.width / vid.dupx)<downloaded = false; - coronalumpnum = W_CheckNumForName("CORONA"); + coronalumpnum = W_CheckNumForLongName("CORONA"); } // -----------------+ diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index 2b9c19d6c..dbc08f032 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -3181,7 +3181,7 @@ static void HWR_DrawDropShadow(mobj_t *thing, fixed_t scale) if (!shadowpatch || !shadowpatch->hardware) { CONS_Debug(DBG_RENDER, "remaking shadowsprite\n"); - shadowpatch = (patch_t *)W_CachePatchName("DSHADOW", PU_SPRITE); + shadowpatch = (patch_t *)W_CachePatchLongName("DSHADOW", PU_SPRITE); } gpatch = shadowpatch; @@ -5986,7 +5986,7 @@ void HWR_RenderPlayerView(void) if (r_splitscreen == 2 && player == &players[displayplayers[2]]) // No black void please. { // V_DrawPatchFill, but for the fourth screen only - patch_t *gpatch = W_CachePatchName("SRB2BACK", PU_CACHE); + patch_t *gpatch = W_CachePatchLongName("SRB2BACK", PU_CACHE); INT32 dupz = (vid.dupx < vid.dupy ? vid.dupx : vid.dupy); INT32 x, y, pw = SHORT(gpatch->width) * dupz, ph = SHORT(gpatch->height) * dupz; diff --git a/src/hu_stuff.c b/src/hu_stuff.c index fc71e8450..b33ce0ae0 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -228,7 +228,7 @@ void HU_Init(void) // only allocate if not present, to save us a lot of headache if (missingpat == NULL) { - lumpnum_t missingnum = W_GetNumForName("MISSING"); + lumpnum_t missingnum = W_GetNumForLongName("MISSING"); if (missingnum == LUMPERROR) I_Error("HU_LoadGraphics: \"MISSING\" patch not present in resource files."); diff --git a/src/k_hud.c b/src/k_hud.c index 13e51fde8..2c348309d 100644 --- a/src/k_hud.c +++ b/src/k_hud.c @@ -1577,7 +1577,7 @@ void K_drawKartTimestamp(tic_t drawtime, INT32 TX, INT32 TY, INT16 emblemmap, UI V_DrawRightAlignedString(workx, worky, splitflags|V_6WIDTHSPACE, targettext); workx -= 67; - V_DrawSmallScaledPatch(workx + 4, worky, splitflags, W_CachePatchName("NEEDIT", PU_CACHE)); + V_DrawSmallScaledPatch(workx + 4, worky, splitflags, W_CachePatchLongName("NEEDIT", PU_CACHE)); break; @@ -2605,7 +2605,7 @@ static void K_drawKartBumpersOrKarma(void) { if (itembreaker) { - patch_t *item = W_CachePatchName("RNDMA0", PU_PATCH); + patch_t *item = W_CachePatchLongName("RNDMA0", PU_PATCH); UINT8 *itemcolormap = R_GetTranslationColormap(TC_BLINK, SKINCOLOR_BLACK, GTC_CACHE); if (!K_UseColorHud()) @@ -4929,8 +4929,8 @@ void K_drawKartHUD(void) snapflags = 0; } - V_DrawTinyScaledPatch(x-54, y, snapflags|V_SLIDEIN, W_CachePatchName("TTKBANNR", PU_CACHE)); - V_DrawTinyScaledPatch(x-54, y+25, snapflags|V_SLIDEIN, W_CachePatchName("TTKART", PU_CACHE)); + V_DrawTinyScaledPatch(x-54, y, snapflags|V_SLIDEIN, W_CachePatchLongName("TTKBANNR", PU_CACHE)); + V_DrawTinyScaledPatch(x-54, y+25, snapflags|V_SLIDEIN, W_CachePatchLongName("TTKART", PU_CACHE)); } else { diff --git a/src/m_menu.c b/src/m_menu.c index abefb937a..6f43bf1ad 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -2208,7 +2208,7 @@ static void M_DrawMapEmblems(INT32 mapnum, INT32 x, INT32 y) V_DrawSmallMappedPatch(x, y, 0, W_CachePatchName(M_GetEmblemPatch(emblem, false), PU_CACHE), R_GetTranslationColormap(TC_DEFAULT, M_GetEmblemColor(emblem), GTC_MENUCACHE)); else - V_DrawSmallScaledPatch(x, y, 0, W_CachePatchName("NEEDIT", PU_CACHE)); + V_DrawSmallScaledPatch(x, y, 0, W_CachePatchLongName("NEEDIT", PU_CACHE)); emblem = M_GetLevelEmblems(-1); x -= 8; @@ -2252,7 +2252,7 @@ static void M_DrawMenuTitle(void) static void M_DrawSliderCursor(INT16 x, INT16 y, INT32 vflags, consvar_t *cv, INT32 value) { fixed_t range = FixedDiv(value - cv->PossibleValue[0].value, cv->PossibleValue[1].value - cv->PossibleValue[0].value); - V_DrawFixedPatch((x - 4)*FRACUNIT + (SLIDER_RANGE+4)*range, y*FRACUNIT, FRACUNIT, vflags, W_CachePatchName("M_SLIDEC", PU_CACHE), NULL); + V_DrawFixedPatch((x - 4)*FRACUNIT + (SLIDER_RANGE+4)*range, y*FRACUNIT, FRACUNIT, vflags, W_CachePatchLongName("M_SLIDEC", PU_CACHE), NULL); } static void M_DrawRightString(menuitem_t *item, INT16 x, INT16 y, INT32 vflags, boolean selected) @@ -2297,14 +2297,14 @@ static void M_DrawRightString(menuitem_t *item, INT16 x, INT16 y, INT32 vflags, if (atoi(cv->defaultvalue) != cv->value) M_DrawSliderCursor(x, y, vflags, cv, atoi(cv->defaultvalue)); - p = W_CachePatchName("M_SLIDEL", PU_CACHE); + p = W_CachePatchLongName("M_SLIDEL", PU_CACHE); V_DrawScaledPatch(x - 8, y, vflags, p); - p = W_CachePatchName("M_SLIDEM", PU_CACHE); + p = W_CachePatchLongName("M_SLIDEM", PU_CACHE); for (i = 0; i < SLIDER_RANGE; i += 8) V_DrawScaledPatch (x + i, y, vflags, p); - p = W_CachePatchName("M_SLIDER", PU_CACHE); + p = W_CachePatchLongName("M_SLIDER", PU_CACHE); V_DrawScaledPatch(x + i, y, vflags, p); M_DrawSliderCursor(x, y, vflags, cv, cv->value); @@ -2414,7 +2414,7 @@ static void M_DrawRightString(menuitem_t *item, INT16 x, INT16 y, INT32 vflags, vflags &= ~(V_FLIP|V_PARAMMASK); const tic_t freq = TICRATE/2; if ((leveltime % freq) >= freq/2) - V_DrawScaledPatch(204, y - 1, vflags, W_CachePatchName("K_REQUE2", PU_CACHE)); + V_DrawScaledPatch(204, y - 1, vflags, W_CachePatchLongName("K_REQUE2", PU_CACHE)); } else if (item->patch) V_DrawRightAlignedString(BASEVIDWIDTH - x, y, (selected || (item->status & ITH_MASK) == ITH_HIGHLIGHT ? highlightflags : 0)|vflags, item->patch); @@ -2671,7 +2671,7 @@ void MD_DrawGenericMenu(void) // DRAW THE SKULL CURSOR if (M_ItemSelectable(¤tMenu->menuitems[itemOn])) - V_DrawScaledPatch(cursorx, cursory, 0, W_CachePatchName("M_CURSOR", PU_CACHE)); + V_DrawScaledPatch(cursorx, cursory, 0, W_CachePatchLongName("M_CURSOR", PU_CACHE)); x = currentMenu->x - 20 + currentMenu->cursoroffset; if (cliptop) @@ -3174,23 +3174,23 @@ INT32 MR_Addons(INT32 choice) W_UnlockCachedPatch(addonsp[i]); } - addonsp[EXT_FOLDER] = W_CachePatchName("M_FFLDR", PU_STATIC); - addonsp[EXT_UP] = W_CachePatchName("M_FBACK", PU_STATIC); - addonsp[EXT_NORESULTS] = W_CachePatchName("M_FNOPE", PU_STATIC); - addonsp[EXT_TXT] = W_CachePatchName("M_FTXT", PU_STATIC); - addonsp[EXT_CFG] = W_CachePatchName("M_FCFG", PU_STATIC); - addonsp[EXT_WAD] = W_CachePatchName("M_FWAD", PU_STATIC); + addonsp[EXT_FOLDER] = W_CachePatchLongName("M_FFLDR", PU_STATIC); + addonsp[EXT_UP] = W_CachePatchLongName("M_FBACK", PU_STATIC); + addonsp[EXT_NORESULTS] = W_CachePatchLongName("M_FNOPE", PU_STATIC); + addonsp[EXT_TXT] = W_CachePatchLongName("M_FTXT", PU_STATIC); + addonsp[EXT_CFG] = W_CachePatchLongName("M_FCFG", PU_STATIC); + addonsp[EXT_WAD] = W_CachePatchLongName("M_FWAD", PU_STATIC); #ifdef USE_KART - addonsp[EXT_KART] = W_CachePatchName("M_FKART", PU_STATIC); + addonsp[EXT_KART] = W_CachePatchLongName("M_FKART", PU_STATIC); #endif - addonsp[EXT_PK3] = W_CachePatchName("M_FPK3", PU_STATIC); - addonsp[EXT_SOC] = W_CachePatchName("M_FSOC", PU_STATIC); - addonsp[EXT_LUA] = W_CachePatchName("M_FLUA", PU_STATIC); - addonsp[NUM_EXT] = W_CachePatchName("M_FUNKN", PU_STATIC); - addonsp[NUM_EXT+1] = W_CachePatchName("M_FSEL", PU_STATIC); - addonsp[NUM_EXT+2] = W_CachePatchName("M_FLOAD", PU_STATIC); - addonsp[NUM_EXT+3] = W_CachePatchName("M_FSRCH", PU_STATIC); - addonsp[NUM_EXT+4] = W_CachePatchName("M_FSAVE", PU_STATIC); + addonsp[EXT_PK3] = W_CachePatchLongName("M_FPK3", PU_STATIC); + addonsp[EXT_SOC] = W_CachePatchLongName("M_FSOC", PU_STATIC); + addonsp[EXT_LUA] = W_CachePatchLongName("M_FLUA", PU_STATIC); + addonsp[NUM_EXT] = W_CachePatchLongName("M_FUNKN", PU_STATIC); + addonsp[NUM_EXT+1] = W_CachePatchLongName("M_FSEL", PU_STATIC); + addonsp[NUM_EXT+2] = W_CachePatchLongName("M_FLOAD", PU_STATIC); + addonsp[NUM_EXT+3] = W_CachePatchLongName("M_FSRCH", PU_STATIC); + addonsp[NUM_EXT+4] = W_CachePatchLongName("M_FSAVE", PU_STATIC); return true; } @@ -3885,7 +3885,7 @@ static void DrawReplayHutReplayInfo(void) { static angle_t rubyfloattime = 0; const fixed_t rubyheight = FINESINE(rubyfloattime>>ANGLETOFINESHIFT); - V_DrawFixedPatch((x+(w>>2))<>2))<>2))<>2))<x - 24, cursory, V_SNAPTOTOP|V_SNAPTOLEFT, - W_CachePatchName("M_CURSOR", PU_CACHE)); + W_CachePatchLongName("M_CURSOR", PU_CACHE)); // Now draw some replay info! V_DrawFill(10, 10, 300, 60, V_SNAPTOTOP|159); @@ -4105,7 +4105,7 @@ void MD_DrawReplayStartMenu(void) } else { - patch = W_CachePatchName("M_NORANK", PU_CACHE); + patch = W_CachePatchLongName("M_NORANK", PU_CACHE); colormap = R_GetTranslationColormap( TC_RAINBOW, demolist[dir_on[menudepthleft]].standings[i].color, @@ -4288,14 +4288,14 @@ void MD_DrawPlaybackMenu(void) else if (currentMenu->menuitems[i].patch && W_CheckNumForName(currentMenu->menuitems[i].patch) != LUMPERROR) icon = W_CachePatchName(currentMenu->menuitems[i].patch, PU_CACHE); else - icon = W_CachePatchName("PLAYRANK", PU_CACHE); // temp + icon = W_CachePatchLongName("PLAYRANK", PU_CACHE); // temp } else if (currentMenu->menuitems[i].status & IT_HIDDEN) continue; else if (currentMenu->menuitems[i].patch && W_CheckNumForName(currentMenu->menuitems[i].patch) != LUMPERROR) icon = W_CachePatchName(currentMenu->menuitems[i].patch, PU_CACHE); else - icon = W_CachePatchName("PLAYRANK", PU_CACHE); // temp + icon = W_CachePatchLongName("PLAYRANK", PU_CACHE); // temp if ((i == M_GetMenuIndex(MN_PLAYBACK, "FASTFORWARD") && cv_playbackspeed.value > 1) || (i == M_GetMenuIndex(MN_PLAYBACK, "REWIND") && demo.rewinding)) V_DrawMappedPatch(currentMenu->x + currentMenu->menuitems[i].x, currentMenu->y, transmap|V_SNAPTOTOP, icon, R_GetTranslationColormap(TC_RAINBOW, SKINCOLOR_JAWZ, GTC_MENUCACHE)); @@ -5234,7 +5234,7 @@ static void M_DrawStatsMaps(void) V_DrawRightAlignedString(BASEVIDWIDTH-16, 50, MENUCAPS|recommendedflags, "(complete)"); V_DrawString(32, 50, 0, va("x %d/%d", M_CountEmblems(), numemblems+numextraemblems)); - V_DrawSmallScaledPatch(20, 50, 0, W_CachePatchName("GOTITA", PU_STATIC)); + V_DrawSmallScaledPatch(20, 50, 0, W_CachePatchLongName("GOTITA", PU_STATIC)); if (location) V_DrawCharacter(10, y-(skullAnimCounter/5), @@ -5308,7 +5308,7 @@ static void M_DrawStatsMaps(void) V_DrawSmallMappedPatch(295, y, 0, W_CachePatchName(M_GetExtraEmblemPatch(exemblem, false), PU_CACHE), R_GetTranslationColormap(TC_DEFAULT, M_GetExtraEmblemColor(exemblem), GTC_MENUCACHE)); else - V_DrawSmallScaledPatch(295, y, 0, W_CachePatchName("NEEDIT", PU_CACHE)); + V_DrawSmallScaledPatch(295, y, 0, W_CachePatchLongName("NEEDIT", PU_CACHE)); V_DrawString(20, y, 0, va("%s", exemblem->description)); } @@ -6427,7 +6427,7 @@ static void M_DrawLevelSelectOnly(INT16 y, boolean selected, boolean leftfade, b { static angle_t rubyfloattime = 0; const fixed_t rubyheight = FINESINE(rubyfloattime>>ANGLETOFINESHIFT); - V_DrawFixedPatch((x+w/2)< 7 * FRACUNIT; cursorframe -= 7 * FRACUNIT) {} - V_DrawFixedPatch(x<> FRACBITS) + 1), PU_CACHE), NULL); + V_DrawFixedPatch(x<> FRACBITS) + 1), PU_CACHE), NULL); } x += incrwidth; @@ -6633,7 +6633,7 @@ void MD_DrawSetupMultiPlayerMenu(void) INT32 mx, my, st, flags = 0; spritedef_t *sprdef; spriteframe_t *sprframe; - patch_t *statdot = W_CachePatchName("K_SDOT0", PU_CACHE); + patch_t *statdot = W_CachePatchLongName("K_SDOT0", PU_CACHE); patch_t *patch; UINT8 frame; UINT8 speed; @@ -6663,13 +6663,13 @@ void MD_DrawSetupMultiPlayerMenu(void) speed = skins[cv_chooseskin.value].kartspeed; weight = skins[cv_chooseskin.value].kartweight; - statdot = W_CachePatchName("K_SDOT1", PU_CACHE); + statdot = W_CachePatchLongName("K_SDOT1", PU_CACHE); if (skullAnimCounter < 4) // SRB2Kart: we draw this dot later so that it's not covered if there's multiple skins with the same stats V_DrawFixedPatch(((BASEVIDWIDTH - mx - 80) + ((speed-1)*8))< 7 * FRACUNIT; cursorframe -= 7 * FRACUNIT) {} - cursor = W_CachePatchName(va("K_BHILI%d", (cursorframe >> FRACBITS) + 1), PU_CACHE); + cursor = W_CachePatchLongName(va("K_BHILI%d", (cursorframe >> FRACBITS) + 1), PU_CACHE); if (col < 0) col += numskins; @@ -7617,7 +7617,7 @@ void MD_DrawVideoMode(void) j = menudefs[MN_OP_VIDEOMODE].y + 14 + ((vidm_selected % vidm_column_size)*8); V_DrawScaledPatch(i - 8, j, 0, - W_CachePatchName("M_CURSOR", PU_CACHE)); + W_CachePatchLongName("M_CURSOR", PU_CACHE)); } // special menuitem key handler for video mode list @@ -7760,14 +7760,14 @@ void MD_DrawMonitorToggles(void) #ifdef ITEMTOGGLEBOTTOMRIGHT if (currentMenu->menuitems[thisitem].argument == 255) { - V_DrawScaledPatch(x, y, V_TRANSLUCENT, W_CachePatchName("K_ISBG", PU_CACHE)); + V_DrawScaledPatch(x, y, V_TRANSLUCENT, W_CachePatchLongName("K_ISBG", PU_CACHE)); continue; } #endif if (currentMenu->menuitems[thisitem].argument == 0) { - V_DrawScaledPatch(x, y, 0, W_CachePatchName("K_ISBG", PU_CACHE)); - V_DrawScaledPatch(x, y, 0, W_CachePatchName("K_ISTOGL", PU_CACHE)); + V_DrawScaledPatch(x, y, 0, W_CachePatchLongName("K_ISBG", PU_CACHE)); + V_DrawScaledPatch(x, y, 0, W_CachePatchLongName("K_ISTOGL", PU_CACHE)); continue; } @@ -7797,13 +7797,13 @@ void MD_DrawMonitorToggles(void) } if (cv->value) - V_DrawScaledPatch(x, y, 0, W_CachePatchName("K_ISBG", PU_CACHE)); + V_DrawScaledPatch(x, y, 0, W_CachePatchLongName("K_ISBG", PU_CACHE)); else - V_DrawScaledPatch(x, y, 0, W_CachePatchName("K_ISBGD", PU_CACHE)); + V_DrawScaledPatch(x, y, 0, W_CachePatchLongName("K_ISBGD", PU_CACHE)); if (drawnum != 0) { - V_DrawScaledPatch(x, y, 0, W_CachePatchName("K_ISMUL", PU_CACHE)); + V_DrawScaledPatch(x, y, 0, W_CachePatchLongName("K_ISMUL", PU_CACHE)); V_DrawScaledPatch(x, y, translucent, W_CachePatchName(K_GetItemPatch(currentMenu->menuitems[thisitem].argument, true), PU_CACHE)); V_DrawString(x+24, y+31, V_ALLOWLOWERCASE|translucent, va("x%d", drawnum)); } @@ -7821,7 +7821,7 @@ void MD_DrawMonitorToggles(void) #ifdef ITEMTOGGLEBOTTOMRIGHT if (currentMenu->menuitems[itemOn].argument == 255) { - V_DrawScaledPatch(onx-1, ony-2, V_TRANSLUCENT, W_CachePatchName("K_ITBG", PU_CACHE)); + V_DrawScaledPatch(onx-1, ony-2, V_TRANSLUCENT, W_CachePatchLongName("K_ITBG", PU_CACHE)); if (shitsfree) { INT32 trans = V_TRANSLUCENT; @@ -7829,15 +7829,15 @@ void MD_DrawMonitorToggles(void) trans = ((10-TICRATE)+shitsfree-1)<menuitems[itemOn].argument == 0) { - V_DrawScaledPatch(onx-1, ony-2, 0, W_CachePatchName("K_ITBG", PU_CACHE)); - V_DrawScaledPatch(onx-1, ony-2, 0, W_CachePatchName("K_ITTOGL", PU_CACHE)); + V_DrawScaledPatch(onx-1, ony-2, 0, W_CachePatchLongName("K_ITBG", PU_CACHE)); + V_DrawScaledPatch(onx-1, ony-2, 0, W_CachePatchLongName("K_ITTOGL", PU_CACHE)); } else { @@ -7855,15 +7855,15 @@ void MD_DrawMonitorToggles(void) } if (cv->value) - V_DrawScaledPatch(onx-1, ony-2, 0, W_CachePatchName("K_ITBG", PU_CACHE)); + V_DrawScaledPatch(onx-1, ony-2, 0, W_CachePatchLongName("K_ITBG", PU_CACHE)); else - V_DrawScaledPatch(onx-1, ony-2, 0, W_CachePatchName("K_ITBGD", PU_CACHE)); + V_DrawScaledPatch(onx-1, ony-2, 0, W_CachePatchLongName("K_ITBGD", PU_CACHE)); if (drawnum != 0) { - V_DrawScaledPatch(onx-1, ony-2, 0, W_CachePatchName("K_ITMUL", PU_CACHE)); + V_DrawScaledPatch(onx-1, ony-2, 0, W_CachePatchLongName("K_ITMUL", PU_CACHE)); V_DrawScaledPatch(onx-1, ony-2, translucent, W_CachePatchName(K_GetItemPatch(currentMenu->menuitems[itemOn].argument, false), PU_CACHE)); - V_DrawScaledPatch(onx+27, ony+39, translucent, W_CachePatchName("K_ITX", PU_CACHE)); + V_DrawScaledPatch(onx+27, ony+39, translucent, W_CachePatchLongName("K_ITX", PU_CACHE)); V_DrawKartString(onx+37, ony+34, translucent, va("%d", drawnum)); } else @@ -8353,7 +8353,7 @@ void M_QuitResponse(INT32 ch) while (ptime > I_GetTime()) { V_DrawFill(0, 0, BASEVIDWIDTH, BASEVIDHEIGHT, 31); - V_DrawSmallScaledPatch(0, 0, 0, W_CachePatchName("GAMEQUIT", PU_CACHE)); // Demo 3 Quit Screen Tails 06-16-2001 + V_DrawSmallScaledPatch(0, 0, 0, W_CachePatchLongName("GAMEQUIT", PU_CACHE)); // Demo 3 Quit Screen Tails 06-16-2001 I_FinishUpdate(); // Update the screen with the image Tails 06-19-2001 I_Sleep(cv_sleep.value); I_UpdateTime(cv_timescale.value); @@ -8428,12 +8428,12 @@ static void M_DrawSticker(INT32 x, INT32 y, INT32 width, INT32 flags, boolean is if (isSmall == true) { - stickerEnd = W_CachePatchName("K_STIKE2", PU_CACHE); + stickerEnd = W_CachePatchLongName("K_STIKE2", PU_CACHE); height = 6; } else { - stickerEnd = W_CachePatchName("K_STIKEN", PU_CACHE); + stickerEnd = W_CachePatchLongName("K_STIKEN", PU_CACHE); height = 11; } @@ -8460,12 +8460,12 @@ void MD_DrawDiscordRequests(void) if (confirmAccept == true) { colormap = R_GetTranslationColormap(TC_DEFAULT, SKINCOLOR_GREEN, GTC_MENUCACHE); - hand = W_CachePatchName("K_LAPH02", PU_CACHE); + hand = W_CachePatchLongName("K_LAPH02", PU_CACHE); } else { colormap = R_GetTranslationColormap(TC_DEFAULT, SKINCOLOR_RED, GTC_MENUCACHE); - hand = W_CachePatchName("K_LAPH03", PU_CACHE); + hand = W_CachePatchLongName("K_LAPH03", PU_CACHE); } slide = confirmLength - confirmDelay; @@ -8480,7 +8480,7 @@ void MD_DrawDiscordRequests(void) colormap = R_GetTranslationColormap(TC_DEFAULT, SKINCOLOR_GREY, GTC_MENUCACHE); } - V_DrawFixedPatch(56*FRACUNIT, 150*FRACUNIT, FRACUNIT, 0, W_CachePatchName("K_LAPE01", PU_CACHE), colormap); + V_DrawFixedPatch(56*FRACUNIT, 150*FRACUNIT, FRACUNIT, 0, W_CachePatchLongName("K_LAPE01", PU_CACHE), colormap); if (hand != NULL) { diff --git a/src/r_data.c b/src/r_data.c index c9bf53611..fc834a034 100644 --- a/src/r_data.c +++ b/src/r_data.c @@ -275,7 +275,7 @@ void R_InitColormaps(void) lumpnum_t lump; // Load in the light tables - lump = W_GetNumForName("COLORMAP"); + lump = W_GetNumForLongName("COLORMAP"); len = W_LumpLength(lump); if (len < COLORMAP_SIZE*2) // accomodate encore mode later len = COLORMAP_SIZE*2; @@ -295,13 +295,13 @@ void R_ReInitColormaps(UINT16 num, void *newencoremap, size_t encoremapsize, boo { char colormap[9] = "COLORMAP"; lumpnum_t lump; - const lumpnum_t basecolormaplump = W_GetNumForName(colormap); + const lumpnum_t basecolormaplump = W_GetNumForLongName(colormap); boolean remap = false; if (num > 0 && num <= 10000) snprintf(colormap, 8, "CLM%04u", num-1); // Load in the light tables, now 64k aligned for smokie... - lump = W_GetNumForName(colormap); + lump = W_GetNumForLongName(colormap); if (lump == LUMPERROR) lump = basecolormaplump; else @@ -1204,7 +1204,8 @@ static void R_Init8to16(void) UINT8 *palette; int i; - palette = W_CacheLumpName("PLAYPAL",PU_CACHE); + lumpnum_t lump = W_GetNumForLongName("PLAYPAL"); + palette = W_CacheLumpNum(lump, PU_CACHE); for (i = 0; i < 256; i++) { diff --git a/src/r_draw.cpp b/src/r_draw.cpp index a95384c57..841df4b6d 100644 --- a/src/r_draw.cpp +++ b/src/r_draw.cpp @@ -237,15 +237,15 @@ void R_InitTranslucencyTables(void) // optimised code (in other words, transtables pointer low word is 0) transtables = static_cast(Z_MallocAlign(NUMTRANSTABLES*0x10000, PU_STATIC, NULL, 16)); - W_ReadLump(W_GetNumForName("TRANS10"), transtables); - W_ReadLump(W_GetNumForName("TRANS20"), transtables+0x10000); - W_ReadLump(W_GetNumForName("TRANS30"), transtables+0x20000); - W_ReadLump(W_GetNumForName("TRANS40"), transtables+0x30000); - W_ReadLump(W_GetNumForName("TRANS50"), transtables+0x40000); - W_ReadLump(W_GetNumForName("TRANS60"), transtables+0x50000); - W_ReadLump(W_GetNumForName("TRANS70"), transtables+0x60000); - W_ReadLump(W_GetNumForName("TRANS80"), transtables+0x70000); - W_ReadLump(W_GetNumForName("TRANS90"), transtables+0x80000); + W_ReadLump(W_GetNumForLongName("TRANS10"), transtables); + W_ReadLump(W_GetNumForLongName("TRANS20"), transtables+0x10000); + W_ReadLump(W_GetNumForLongName("TRANS30"), transtables+0x20000); + W_ReadLump(W_GetNumForLongName("TRANS40"), transtables+0x30000); + W_ReadLump(W_GetNumForLongName("TRANS50"), transtables+0x40000); + W_ReadLump(W_GetNumForLongName("TRANS60"), transtables+0x50000); + W_ReadLump(W_GetNumForLongName("TRANS70"), transtables+0x60000); + W_ReadLump(W_GetNumForLongName("TRANS80"), transtables+0x70000); + W_ReadLump(W_GetNumForLongName("TRANS90"), transtables+0x80000); R_AllocateBlendTables(); R_GenerateBlendTables(); @@ -345,8 +345,8 @@ void R_InitPaletteRemap(void) { palremap = static_cast(Z_Malloc(256, PU_STATIC, NULL)); invremap = static_cast(Z_Malloc(256, PU_STATIC, NULL)); - W_ReadLump(W_GetNumForName("PALREMAP"), palremap); - W_ReadLump(W_GetNumForName("INVREMAP"), invremap); + W_ReadLump(W_GetNumForLongName("PALREMAP"), palremap); + W_ReadLump(W_GetNumForLongName("INVREMAP"), invremap); } UINT8 R_GetPaletteRemap(UINT8 color) @@ -512,14 +512,14 @@ lumpnum_t viewborderlump[8]; void R_InitViewBorder(void) { - viewborderlump[BRDR_T] = W_GetNumForName("brdr_t"); - viewborderlump[BRDR_B] = W_GetNumForName("brdr_b"); - viewborderlump[BRDR_L] = W_GetNumForName("brdr_l"); - viewborderlump[BRDR_R] = W_GetNumForName("brdr_r"); - viewborderlump[BRDR_TL] = W_GetNumForName("brdr_tl"); - viewborderlump[BRDR_BL] = W_GetNumForName("brdr_bl"); - viewborderlump[BRDR_TR] = W_GetNumForName("brdr_tr"); - viewborderlump[BRDR_BR] = W_GetNumForName("brdr_br"); + viewborderlump[BRDR_T] = W_GetNumForLongName("brdr_t"); + viewborderlump[BRDR_B] = W_GetNumForLongName("brdr_b"); + viewborderlump[BRDR_L] = W_GetNumForLongName("brdr_l"); + viewborderlump[BRDR_R] = W_GetNumForLongName("brdr_r"); + viewborderlump[BRDR_TL] = W_GetNumForLongName("brdr_tl"); + viewborderlump[BRDR_BL] = W_GetNumForLongName("brdr_bl"); + viewborderlump[BRDR_TR] = W_GetNumForLongName("brdr_tr"); + viewborderlump[BRDR_BR] = W_GetNumForLongName("brdr_br"); } #if 0 diff --git a/src/sdl/endtxt.c b/src/sdl/endtxt.c index 1e72ca9a8..cb7adc847 100644 --- a/src/sdl/endtxt.c +++ b/src/sdl/endtxt.c @@ -45,7 +45,7 @@ void ShowEndTxt(void) #endif UINT16 *ptext; void *data; - lumpnum_t endoomnum = W_GetNumForName("ENDOOM"); + lumpnum_t endoomnum = W_GetNumForLongName("ENDOOM"); //char *col; /* if the xterm has more then 80 columns we need to add nl's */ diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index 995edc626..bf0e8fba0 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -1919,7 +1919,7 @@ void I_Quit(void) I_ShutdownSystem(); SDL_Quit(); /* if option -noendtxt is set, don't print the text */ - if (!M_CheckParm("-noendtxt") && W_CheckNumForName("ENDOOM") != LUMPERROR) + if (!M_CheckParm("-noendtxt") && W_CheckNumForLongName("ENDOOM") != LUMPERROR) { printf("\r"); ShowEndTxt(); diff --git a/src/st_stuff.c b/src/st_stuff.c index 2e404b3e8..04ad728dc 100644 --- a/src/st_stuff.c +++ b/src/st_stuff.c @@ -200,7 +200,7 @@ void ST_LoadFaceGraphics(INT32 skinnum) } if (i < FACE_MAX) { - patch_t *missing = W_CachePatchName("MISSING", PU_HUDGFX); + patch_t *missing = W_CachePatchLongName("MISSING", PU_HUDGFX); while (i < FACE_MAX) { faceprefix[skinnum][i] = missing; diff --git a/src/v_video.c b/src/v_video.c index e4d39cd40..36a4df30a 100644 --- a/src/v_video.c +++ b/src/v_video.c @@ -994,7 +994,7 @@ void V_DrawContinueIcon(INT32 x, INT32 y, INT32 flags, INT32 skinnum, UINT16 ski { (void)skinnum; (void)skincolor; - V_DrawScaledPatch(x - 10, y - 14, flags, W_CachePatchName("CONTINS", PU_PATCH)); + V_DrawScaledPatch(x - 10, y - 14, flags, W_CachePatchLongName("CONTINS", PU_PATCH)); } // diff --git a/src/w_wad.c b/src/w_wad.c index f6a353321..fd6c603ad 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -1210,25 +1210,6 @@ UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 st return LUMPERROR; } -// -// Same as the original, but checks in one pwad only. -// wadid is a wad number -// (Used for sprites loading) -// -// 'startlump' is the lump number to start the search -// -// NOTE: only exists for compatibility with code requiring the 8-char truncation. -// use W_CheckNumForLongNamePwad in new code! -// if the old code passes a string literal, you can switch without issues -// -UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) -{ - char shortname[8+1]; - strncpy(shortname, name, 8); - shortname[8] = '\0'; - return W_CheckNumForLongNamePwad(shortname, wad, startlump); -} - // // Like W_CheckNumForNamePwad, but can find entries with long names // @@ -1355,22 +1336,6 @@ UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump) return LUMPERROR; } -// -// W_CheckNumForName -// Returns LUMPERROR if name not found. -// -// NOTE: only exists for compatibility with code requiring the 8-char truncation. -// use W_CheckNumForLongName in new code! -// if the old code passes a string literal, you can switch without issues -// -lumpnum_t W_CheckNumForName(const char *name) -{ - char shortname[8+1]; - strncpy(shortname, name, 8); - shortname[8] = '\0'; - return W_CheckNumForLongName(shortname); -} - // // Like W_CheckNumForName, but can find entries with long names // @@ -1482,23 +1447,6 @@ lumpnum_t W_CheckNumForMap(const char *name) } } -// -// W_GetNumForName -// -// Calls W_CheckNumForName, but bombs out if not found. -// -lumpnum_t W_GetNumForName(const char *name) -{ - lumpnum_t i; - - i = W_CheckNumForName(name); - - if (i == LUMPERROR) - I_Error("W_GetNumForName: %s not found!\n", name); - - return i; -} - // // Like W_GetNumForName, but can find entries with long names // @@ -1958,14 +1906,6 @@ boolean W_IsPatchCached(lumpnum_t lumpnum, void *ptr) return W_IsPatchCachedPWAD(WADFILENUM(lumpnum),LUMPNUM(lumpnum), ptr); } -// ========================================================================== -// W_CacheLumpName -// ========================================================================== -void *W_CacheLumpName(const char *name, INT32 tag) -{ - return W_CacheLumpNum(W_GetNumForName(name), tag); -} - // ========================================================================== // CACHING OF GRAPHIC PATCH RESOURCES // ========================================================================== @@ -2075,17 +2015,6 @@ void W_UnlockCachedPatch(void *patch) Z_Unlock(patch); } -void *W_CachePatchName(const char *name, INT32 tag) -{ - lumpnum_t num; - - num = W_CheckNumForName(name); - - if (num == LUMPERROR) - return missingpat; - return W_CachePatchNum(num, tag); -} - void *W_CachePatchLongName(const char *name, INT32 tag) { lumpnum_t num; diff --git a/src/w_wad.h b/src/w_wad.h index f2b6b9993..2f26147cf 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -133,8 +133,18 @@ const char *W_CheckFullNameForNum(lumpnum_t lumpnum); UINT16 W_FindNextEmptyInPwad(UINT16 wad, UINT16 startlump); // checks only in one pwad +// helper for old functions that use 8-char names +// careful, you might be dealing with a non-terminated string! +static inline const char *W_ShortName(const char *name) +{ + static char shortname[8+1]; + strncpy(shortname, name, 8); + shortname[8] = '\0'; + return shortname; +} + UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 startlump); -UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump); // checks only in one pwad +#define W_CheckNumForNamePwad(name, wad, start) W_CheckNumForLongNamePwad(W_ShortName(name), wad, start) UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForNamePrefixPwad(const char *name, size_t namelen, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForNameMultiPrefixPwad(const lumpprefixes_t *prefixes, size_t numprefixes, UINT16 wad, UINT16 startlump); @@ -144,9 +154,9 @@ UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlum UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump); lumpnum_t W_CheckNumForMap(const char *name); -lumpnum_t W_CheckNumForName(const char *name); +#define W_CheckNumForName(name) W_CheckNumForLongName(W_ShortName(name)) lumpnum_t W_CheckNumForLongName(const char *name); -lumpnum_t W_GetNumForName(const char *name); // like W_CheckNumForName but I_Error on LUMPERROR +#define W_GetNumForName(name) W_GetNumForLongName(W_ShortName(name)) lumpnum_t W_GetNumForLongName(const char *name); lumpnum_t W_CheckNumForNameInFolder(const char *lump, const char *folder); UINT8 W_LumpExists(const char *name); // Lua uses this. @@ -175,8 +185,7 @@ boolean W_IsPatchCached(lumpnum_t lump, void *ptr); boolean W_NeedPaletteRemapPwad(UINT16 wad, UINT16 lump, boolean ignorecache); boolean W_NeedPaletteRemap(lumpnum_t lump, boolean ignorecache); -void *W_CacheLumpName(const char *name, INT32 tag); -void *W_CachePatchName(const char *name, INT32 tag); +#define W_CachePatchName(name, tag) W_CachePatchLongName(W_ShortName(name), tag) void *W_CachePatchLongName(const char *name, INT32 tag); // Returns either a Software patch, or an OpenGL patch. diff --git a/src/y_inter.c b/src/y_inter.c index eb5fef9b0..e36f94a58 100644 --- a/src/y_inter.c +++ b/src/y_inter.c @@ -511,7 +511,7 @@ void Y_IntermissionDrawer(void) { // no y_buffer if (y_buffer == NULL) - V_DrawPatchFill(W_CachePatchName("SRB2BACK", PU_CACHE)); + V_DrawPatchFill(W_CachePatchLongName("SRB2BACK", PU_CACHE)); else { // Maybe the resolution changed? @@ -654,13 +654,13 @@ void Y_IntermissionDrawer(void) if (data.num[i] == whiteplayer) { UINT8 cursorframe = (intertic / 4) % 8; - V_DrawScaledPatch(x+16, y-4, 0, W_CachePatchName(va("K_CHILI%d", cursorframe+1), PU_CACHE)); + V_DrawScaledPatch(x+16, y-4, 0, W_CachePatchLongName(va("K_CHILI%d", cursorframe+1), PU_CACHE)); } if ((players[data.num[i]].pflags & PF_NOCONTEST) && players[data.num[i]].bot) { // RETIRED!! - V_DrawScaledPatch(x+12, y-7, 0, W_CachePatchName("K_NOBLNS", PU_CACHE)); + V_DrawScaledPatch(x+12, y-7, 0, W_CachePatchLongName("K_NOBLNS", PU_CACHE)); } STRBUFCPY(strtime, data.name[i]); @@ -1509,7 +1509,7 @@ void Y_VoteDrawer(void) if (!splitscreen && i == consoleplayer) { UINT8 cursorframe = (votetic / 4) % 8; - V_DrawScaledPatch(x+24, y+9, V_SNAPTOLEFT, W_CachePatchName(va("K_CHILI%d", cursorframe+1), PU_CACHE)); + V_DrawScaledPatch(x+24, y+9, V_SNAPTOLEFT, W_CachePatchLongName(va("K_CHILI%d", cursorframe+1), PU_CACHE)); } } @@ -1851,14 +1851,14 @@ void Y_StartVote(void) Y_AnimatedVoteScreenCheck(); - widebgpatch = W_CachePatchName((battlemode ? "BATTLSCW" : "INTERSCW"), PU_STATIC); - bgpatch = W_CachePatchName((battlemode ? "BATTLSCR" : "INTERSCR"), PU_STATIC); - cursor = W_CachePatchName("M_CURSOR", PU_STATIC); - cursor1 = W_CachePatchName("P1CURSOR", PU_STATIC); - cursor2 = W_CachePatchName("P2CURSOR", PU_STATIC); - cursor3 = W_CachePatchName("P3CURSOR", PU_STATIC); - cursor4 = W_CachePatchName("P4CURSOR", PU_STATIC); - rubyicon = W_CachePatchName("RUBYICON", PU_STATIC); + widebgpatch = W_CachePatchLongName((battlemode ? "BATTLSCW" : "INTERSCW"), PU_STATIC); + bgpatch = W_CachePatchLongName((battlemode ? "BATTLSCR" : "INTERSCR"), PU_STATIC); + cursor = W_CachePatchLongName("M_CURSOR", PU_STATIC); + cursor1 = W_CachePatchLongName("P1CURSOR", PU_STATIC); + cursor2 = W_CachePatchLongName("P2CURSOR", PU_STATIC); + cursor3 = W_CachePatchLongName("P3CURSOR", PU_STATIC); + cursor4 = W_CachePatchLongName("P4CURSOR", PU_STATIC); + rubyicon = W_CachePatchLongName("RUBYICON", PU_STATIC); timer = cv_votetime.value*TICRATE; pickedvote = -1; From e987c96651ef8c29fdbaf20422262dea13c4d6f7 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Wed, 25 Jun 2025 22:49:42 +0200 Subject: [PATCH 07/18] Sanitize textprompts and cutscenes --- src/deh_soc.c | 17 ++++++++++++----- src/doomstat.h | 8 +++++--- src/f_finale.c | 14 +++++++------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/deh_soc.c b/src/deh_soc.c index cfd80b952..ef22177d8 100644 --- a/src/deh_soc.c +++ b/src/deh_soc.c @@ -1353,7 +1353,8 @@ static void readcutscenescene(MYFILE *f, INT32 num, INT32 scenenum) if (fastcmp(word+4, "NAME")) { - strncpy(cutscenes[num]->scene[scenenum].picname[picid], word2, 8); + strncpy(cutscenes[num]->scene[scenenum].picname[picid], word2, SHORTNAMELEN); + cutscenes[num]->scene[scenenum].picname[picid][SHORTNAMELEN] = '\0'; } else if (fastcmp(word+4, "HIRES")) { @@ -1595,7 +1596,8 @@ static void readtextpromptpage(MYFILE *f, INT32 num, INT32 pagenum) for (picid = 0; picid < MAX_PROMPT_PICS; picid++) { - strncpy(textprompts[num]->page[pagenum].picname[picid], textprompts[num]->page[metapagenum].picname[picid], 8); + strncpy(textprompts[num]->page[pagenum].picname[picid], textprompts[num]->page[metapagenum].picname[picid], SHORTNAMELEN); + textprompts[num]->page[pagenum].picname[picid][SHORTNAMELEN] = '\0'; textprompts[num]->page[pagenum].pichires[picid] = textprompts[num]->page[metapagenum].pichires[picid]; textprompts[num]->page[pagenum].picduration[picid] = textprompts[num]->page[metapagenum].picduration[picid]; textprompts[num]->page[pagenum].xcoord[picid] = textprompts[num]->page[metapagenum].xcoord[picid]; @@ -1615,7 +1617,8 @@ static void readtextpromptpage(MYFILE *f, INT32 num, INT32 pagenum) if (fastcmp(word+4, "NAME")) { - strncpy(textprompts[num]->page[pagenum].picname[picid], word2, 8); + strncpy(textprompts[num]->page[pagenum].picname[picid], word2, SHORTNAMELEN); + textprompts[num]->page[pagenum].picname[picid][SHORTNAMELEN] = '\0'; } else if (fastcmp(word+4, "HIRES")) { @@ -1677,7 +1680,10 @@ static void readtextpromptpage(MYFILE *f, INT32 num, INT32 pagenum) *textprompts[num]->page[pagenum].name = '\0'; } else if (fastcmp(word, "ICON")) - strncpy(textprompts[num]->page[pagenum].iconname, word2, 8); + { + strncpy(textprompts[num]->page[pagenum].iconname, word2, SHORTNAMELEN); + textprompts[num]->page[pagenum].iconname[SHORTNAMELEN] = '\0'; + } else if (fastcmp(word, "ICONALIGN")) textprompts[num]->page[pagenum].rightside = (i || word2[0] == 'R'); else if (fastcmp(word, "ICONFLIP")) @@ -1745,7 +1751,8 @@ static void readtextpromptpage(MYFILE *f, INT32 num, INT32 pagenum) UINT8 metapagenum = usi - 1; strncpy(textprompts[num]->page[pagenum].name, textprompts[num]->page[metapagenum].name, 32); - strncpy(textprompts[num]->page[pagenum].iconname, textprompts[num]->page[metapagenum].iconname, 8); + strncpy(textprompts[num]->page[pagenum].iconname, textprompts[num]->page[metapagenum].iconname, SHORTNAMELEN); + textprompts[num]->page[pagenum].iconname[SHORTNAMELEN] = '\0'; textprompts[num]->page[pagenum].rightside = textprompts[num]->page[metapagenum].rightside; textprompts[num]->page[pagenum].iconflip = textprompts[num]->page[metapagenum].iconflip; textprompts[num]->page[pagenum].lines = textprompts[num]->page[metapagenum].lines; diff --git a/src/doomstat.h b/src/doomstat.h index 39a535a20..9f68b907f 100644 --- a/src/doomstat.h +++ b/src/doomstat.h @@ -204,10 +204,12 @@ extern UINT16 skincolor_redteam, skincolor_blueteam, skincolor_redring, skincolo extern boolean exitfadestarted; +#define SHORTNAMELEN 8 + struct scene_t { UINT8 numpics; - char picname[8][8]; + char picname[8][SHORTNAMELEN+1]; UINT8 pichires[8]; char *text; UINT16 xcoord[8]; @@ -251,7 +253,7 @@ struct textpage_t UINT8 picmode; // sequence mode after displaying last pic, 0 = persist, 1 = loop, 2 = destroy UINT8 pictoloop; // if picmode == loop, which pic to loop to? UINT8 pictostart; // initial pic number to show - char picname[MAX_PROMPT_PICS][8]; + char picname[MAX_PROMPT_PICS][SHORTNAMELEN+1]; UINT8 pichires[MAX_PROMPT_PICS]; UINT16 xcoord[MAX_PROMPT_PICS]; // gfx UINT16 ycoord[MAX_PROMPT_PICS]; // gfx @@ -263,7 +265,7 @@ struct textpage_t char tag[33]; // page tag char name[34]; // narrator name, extra char for color - char iconname[8]; // narrator icon lump + char iconname[SHORTNAMELEN+1]; // narrator icon lump boolean rightside; // narrator side, false = left, true = right boolean iconflip; // narrator flip icon horizontally UINT8 hidehud; // hide hud, 0 = show all, 1 = hide depending on prompt position (top/bottom), 2 = hide all diff --git a/src/f_finale.c b/src/f_finale.c index 26de402ad..f5f794916 100644 --- a/src/f_finale.c +++ b/src/f_finale.c @@ -1842,10 +1842,10 @@ void F_CutsceneDrawer(void) { if (cutscenes[cutnum]->scene[scenenum].pichires[picnum]) V_DrawSmallScaledPatch(picxpos, picypos, 0, - W_CachePatchName(cutscenes[cutnum]->scene[scenenum].picname[picnum], PU_PATCH_LOWPRIORITY)); + W_CachePatchLongName(cutscenes[cutnum]->scene[scenenum].picname[picnum], PU_PATCH_LOWPRIORITY)); else V_DrawScaledPatch(picxpos,picypos, 0, - W_CachePatchName(cutscenes[cutnum]->scene[scenenum].picname[picnum], PU_PATCH_LOWPRIORITY)); + W_CachePatchLongName(cutscenes[cutnum]->scene[scenenum].picname[picnum], PU_PATCH_LOWPRIORITY)); } if (dofadenow && rendermode != render_none) @@ -1928,7 +1928,7 @@ static void F_GetPageTextGeometry(UINT8 *pagelines, boolean *rightside, INT32 *b // reuse: // cutnum -> promptnum // scenenum -> pagenum - lumpnum_t iconlump = W_CheckNumForName(textprompts[cutnum]->page[scenenum].iconname); + lumpnum_t iconlump = W_CheckNumForLongName(textprompts[cutnum]->page[scenenum].iconname); *pagelines = textprompts[cutnum]->page[scenenum].lines ? textprompts[cutnum]->page[scenenum].lines : 4; *rightside = (iconlump != LUMPERROR && textprompts[cutnum]->page[scenenum].rightside); @@ -2303,7 +2303,7 @@ void F_TextPromptDrawer(void) if (!promptactive) return; - iconlump = W_CheckNumForName(textprompts[cutnum]->page[scenenum].iconname); + iconlump = W_CheckNumForLongName(textprompts[cutnum]->page[scenenum].iconname); F_GetPageTextGeometry(&pagelines, &rightside, &boxh, &texth, &texty, &namey, &chevrony, &textx, &textr); // Draw gfx first @@ -2311,10 +2311,10 @@ void F_TextPromptDrawer(void) { if (textprompts[cutnum]->page[scenenum].pichires[picnum]) V_DrawSmallScaledPatch(picxpos, picypos, 0, - W_CachePatchName(textprompts[cutnum]->page[scenenum].picname[picnum], PU_PATCH_LOWPRIORITY)); + W_CachePatchLongName(textprompts[cutnum]->page[scenenum].picname[picnum], PU_PATCH_LOWPRIORITY)); else V_DrawScaledPatch(picxpos,picypos, 0, - W_CachePatchName(textprompts[cutnum]->page[scenenum].picname[picnum], PU_PATCH_LOWPRIORITY)); + W_CachePatchLongName(textprompts[cutnum]->page[scenenum].picname[picnum], PU_PATCH_LOWPRIORITY)); } // Draw background @@ -2324,7 +2324,7 @@ void F_TextPromptDrawer(void) if (iconlump != LUMPERROR) { INT32 iconx, icony, scale, scaledsize; - patch = W_CachePatchName(textprompts[cutnum]->page[scenenum].iconname, PU_PATCH_LOWPRIORITY); + patch = W_CachePatchLongName(textprompts[cutnum]->page[scenenum].iconname, PU_PATCH_LOWPRIORITY); // scale and center if (patch->width > patch->height) From 5aaa72e10a6a263ff5ca25c265c9a159a0346ebd Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Wed, 25 Jun 2025 23:26:32 +0200 Subject: [PATCH 08/18] Sanitize bgname and ttname --- src/deh_soc.c | 6 ++-- src/f_finale.c | 74 ++++++++++++++++++++++++-------------------------- src/f_finale.h | 8 ++++-- src/m_menu.c | 8 +++--- src/m_menu.h | 6 ++-- 5 files changed, 52 insertions(+), 50 deletions(-) diff --git a/src/deh_soc.c b/src/deh_soc.c index ef22177d8..81e5f2c0b 100644 --- a/src/deh_soc.c +++ b/src/deh_soc.c @@ -2145,7 +2145,7 @@ void readmenu(MYFILE *f, INT32 num) if (fastcmp(word, "BACKGROUNDNAME")) { - strncpy(menudefs[num].bgname, word2, 8); + strlcpy(menudefs[num].bgname, word2, sizeof(menudefs[num].bgname)); titlechanged = true; } else if (fastcmp(word, "HIDEBACKGROUND")) @@ -2186,7 +2186,7 @@ void readmenu(MYFILE *f, INT32 num) } else if (fastcmp(word, "TITLEPICSNAME")) { - strncpy(menudefs[num].ttname, word2, 9); + strlcpy(menudefs[num].ttname, word2, sizeof(menudefs[num].ttname)); titlechanged = true; } else if (fastcmp(word, "TITLEPICSX")) @@ -3266,7 +3266,7 @@ void readmaincfg(MYFILE *f) } else if (fastcmp(word, "TITLEPICSNAME")) { - strncpy(ttname, word2, 9); + strlcpy(ttname, word2, sizeof(ttname)); titlechanged = true; } else if (fastcmp(word, "TITLEPICSX")) diff --git a/src/f_finale.c b/src/f_finale.c index f5f794916..90d0a9863 100644 --- a/src/f_finale.c +++ b/src/f_finale.c @@ -64,7 +64,7 @@ static INT32 menuanimtimer; // Title screen: background animation timing mobj_t *titlemapcameraref = NULL; // menu presentation state -char curbgname[9]; +char curbgname[SHORTNAMELEN+1]; SINT8 curfadevalue; INT32 curbgcolor; INT32 curbgxspeed; @@ -94,7 +94,7 @@ boolean curhidepics; ttmode_enum curttmode; UINT8 curttscale; // ttmode user vars -char curttname[9]; +char curttname[SHORTNAMELEN+1]; INT16 curttx; INT16 curtty; INT16 curttloop; @@ -1133,7 +1133,7 @@ void F_InitMenuPresValues(boolean title) } // Set defaults for presentation values - strncpy(curbgname, "TITLESKY", 9); + strcpy(curbgname, "TITLESKY"); curfadevalue = 16; curbgcolor = -1; curbgxspeed = titlescrollxspeed; @@ -1143,7 +1143,7 @@ void F_InitMenuPresValues(boolean title) curhidepics = hidetitlepics; curttmode = ttmode; curttscale = ttscale; - strncpy(curttname, ttname, 9); + strcpy(curttname, ttname); curttx = ttx; curtty = tty; curttloop = ttloop; @@ -1180,7 +1180,7 @@ void F_SkyScroll(INT32 scrollxspeed, INT32 scrollyspeed, const char *patchname) return; } - pat = W_CachePatchName(patchname, PU_PATCH_LOWPRIORITY); + pat = W_CachePatchLongName(patchname, PU_PATCH_LOWPRIORITY); if (scrollxspeed == 0 && scrollyspeed == 0) { @@ -1220,36 +1220,39 @@ void F_SkyScroll(INT32 scrollxspeed, INT32 scrollyspeed, const char *patchname) } } -#define LOADTTGFX(arr, name, maxf) \ -lumpnum = W_CheckNumForName(name); \ -if (lumpnum != LUMPERROR) \ -{ \ - arr[0] = W_CachePatchName(name, PU_PATCH_LOWPRIORITY); \ - arr[min(1, maxf-1)] = 0; \ -} \ -else if (strlen(name) <= 6) \ -{ \ - fixed_t cnt = strlen(name); \ - strncpy(lumpname, name, 7); \ - for (i = 0; i < maxf-1; i++) \ - { \ - sprintf(&lumpname[cnt], "%.2hu", (UINT16)(i+1)); \ - lumpname[8] = 0; \ - lumpnum = W_CheckNumForName(lumpname); \ - if (lumpnum != LUMPERROR) \ - arr[i] = W_CachePatchName(lumpname, PU_PATCH_LOWPRIORITY); \ - else \ - break; \ - } \ - arr[min(i, maxf-1)] = 0; \ -} \ -else \ - arr[0] = 0; +static void LoadTTGFX(patch_t **arr, const char *name, UINT16 maxf) +{ + UINT16 i; + lumpnum_t lumpnum; + char lumpname[SHORTNAMELEN+1]; + + lumpnum = W_CheckNumForLongName(name); + if (lumpnum != LUMPERROR) + { + arr[0] = W_CachePatchNum(lumpnum, PU_PATCH_LOWPRIORITY); + arr[min(1, maxf-1)] = NULL; + } + else if (strlen(name) <= 6) + { + fixed_t cnt = strlen(name); + strncpy(lumpname, name, 7); + for (i = 0; i < maxf-1; i++) + { + sprintf(&lumpname[cnt], "%.2hu", (UINT16)(i+1)); + lumpnum = W_CheckNumForLongName(lumpname); + if (lumpnum != LUMPERROR) + arr[i] = W_CachePatchNum(lumpnum, PU_PATCH_LOWPRIORITY); + else + break; + } + arr[min(i, maxf-1)] = NULL; + } + else + arr[0] = NULL; +} static void F_CacheTitleScreen(void) { - UINT16 i; - switch(curttmode) { case TTMODE_NONE: @@ -1263,13 +1266,8 @@ static void F_CacheTitleScreen(void) break; case TTMODE_USER: - { - lumpnum_t lumpnum; - char lumpname[9]; - - LOADTTGFX(ttuser, curttname, TTMAX_USER) + LoadTTGFX(ttuser, curttname, TTMAX_USER); break; - } } } diff --git a/src/f_finale.h b/src/f_finale.h index 8ad6cfb61..347001444 100644 --- a/src/f_finale.h +++ b/src/f_finale.h @@ -93,10 +93,12 @@ typedef enum #define TTMAX_ALACROIX 30 // max frames for SONIC typeface, plus one for NULL terminating entry #define TTMAX_USER 100 +#define SHORTNAMELEN 8 + extern ttmode_enum ttmode; extern UINT8 ttscale; // ttmode user vars -extern char ttname[9]; +extern char ttname[SHORTNAMELEN+1]; extern INT16 ttx; extern INT16 tty; extern INT16 ttloop; @@ -114,7 +116,7 @@ typedef enum // Current menu parameters extern mobj_t *titlemapcameraref; -extern char curbgname[9]; +extern char curbgname[SHORTNAMELEN+1]; extern SINT8 curfadevalue; extern INT32 curbgcolor; extern INT32 curbgxspeed; @@ -126,7 +128,7 @@ extern boolean curhidepics; extern ttmode_enum curttmode; extern UINT8 curttscale; // ttmode user vars -extern char curttname[9]; +extern char curttname[SHORTNAMELEN+1]; extern INT16 curttx; extern INT16 curtty; extern INT16 curttloop; diff --git a/src/m_menu.c b/src/m_menu.c index 6f43bf1ad..fc0f53e52 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -823,7 +823,7 @@ void M_SetMenuCurBackground(void) } else if (menudefs[menutype].bgname[0]) { - strncpy(curbgname, menudefs[menutype].bgname, 8); + strcpy(curbgname, menudefs[menutype].bgname); curbgxspeed = menudefs[menutype].titlescrollxspeed != INT32_MAX ? menudefs[menutype].titlescrollxspeed : titlescrollxspeed; curbgyspeed = menudefs[menutype].titlescrollyspeed != INT32_MAX ? menudefs[menutype].titlescrollyspeed : titlescrollyspeed; return; @@ -833,7 +833,7 @@ void M_SetMenuCurBackground(void) curbghide = true; else { - strncpy(curbgname, "TITLESKY", 9); + strcpy(curbgname, "TITLESKY"); curbgxspeed = titlescrollxspeed; curbgyspeed = titlescrollyspeed; } @@ -902,7 +902,7 @@ void M_SetMenuCurTitlePics(void) curhidepics = menudefs[menutype].hidetitlepics; curttmode = menudefs[menutype].ttmode; curttscale = (menudefs[menutype].ttscale != UINT8_MAX ? menudefs[menutype].ttscale : ttscale); - strncpy(curttname, menudefs[menutype].ttname, sizeof(curttname)-1); + strcpy(curttname, menudefs[menutype].ttname); curttx = (menudefs[menutype].ttx != INT16_MAX ? menudefs[menutype].ttx : ttx); curtty = (menudefs[menutype].tty != INT16_MAX ? menudefs[menutype].tty : tty); curttloop = (menudefs[menutype].ttloop != INT16_MAX ? menudefs[menutype].ttloop : ttloop); @@ -924,7 +924,7 @@ void M_SetMenuCurTitlePics(void) curhidepics = hidetitlepics; curttmode = ttmode; curttscale = ttscale; - strncpy(curttname, ttname, 9); + strcpy(curttname, ttname); curttx = ttx; curtty = tty; curttloop = ttloop; diff --git a/src/m_menu.h b/src/m_menu.h index 9e7be4d00..0b3c3faf3 100644 --- a/src/m_menu.h +++ b/src/m_menu.h @@ -190,6 +190,8 @@ struct menuitem_t INT16 x, y; // coordinates, see menuitemflags }; +#define SHORTNAMELEN 8 + struct menu_t { dehinfo_t info; @@ -208,7 +210,7 @@ struct menu_t // MENUPRES STUFF BELOW - char bgname[8]; // name for background gfx lump; lays over titlemap if this is set + char bgname[SHORTNAMELEN+1]; // name for background gfx lump; lays over titlemap if this is set SINT8 fadestrength; // darken background when displaying this menu, strength 0-31 or -1 for undefined INT32 bgcolor; // fill color, overrides bg name. -1 means follow bg name rules. INT32 titlescrollxspeed; // background gfx scroll per menu; inherits global setting @@ -218,7 +220,7 @@ struct menu_t SINT8 hidetitlepics; // hide title gfx per menu; -1 means undefined, inherits global setting ttmode_enum ttmode; // title wing animation mode; default TTMODE_KART UINT8 ttscale; // scale of title wing gfx (FRACUNIT / ttscale); -1 means undefined, inherits global setting - char ttname[9]; // lump name of title wing gfx. If name length is <= 6, engine will attempt to load numbered frames (TTNAMExx) + char ttname[SHORTNAMELEN+1]; // lump name of title wing gfx. If name length is <= 6, engine will attempt to load numbered frames (TTNAMExx) INT16 ttx; // X position of title wing INT16 tty; // Y position of title wing INT16 ttloop; // # frame to loop; -1 means dont loop From d37b987acbc9636a4220573a5e78d8470fa9fa96 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Wed, 25 Jun 2025 23:42:36 +0200 Subject: [PATCH 09/18] Remove W_CachePatchName, everything's safe Menu headerpics, menuitem patches and old-style map thumbnails/minimaps now support longnames --- src/f_finale.c | 2 +- src/k_hud.c | 4 ++-- src/m_menu.c | 20 ++++++++++---------- src/p_setup.c | 4 ++-- src/r_things.cpp | 2 +- src/w_wad.h | 1 - src/y_inter.c | 2 +- 7 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/f_finale.c b/src/f_finale.c index 90d0a9863..8f844a5f6 100644 --- a/src/f_finale.c +++ b/src/f_finale.c @@ -768,7 +768,7 @@ void F_CreditDrawer(void) sc = FRACUNIT; // quick hack so I don't have to add another field to credits_pics } - V_DrawFixedPatch(credits_pics[i].x<collected) { - emblempic[curemb] = W_CachePatchName(M_GetEmblemPatch(emblem, false), PU_CACHE); + emblempic[curemb] = W_CachePatchLongName(M_GetEmblemPatch(emblem, false), PU_CACHE); emblemcol[curemb] = R_GetTranslationColormap(TC_DEFAULT, M_GetEmblemColor(emblem), GTC_CACHE); if (++curemb == 3) break; diff --git a/src/m_menu.c b/src/m_menu.c index fc0f53e52..4306a94ca 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -2205,7 +2205,7 @@ static void M_DrawMapEmblems(INT32 mapnum, INT32 x, INT32 y) lasttype = curtype; if (emblem->collected) - V_DrawSmallMappedPatch(x, y, 0, W_CachePatchName(M_GetEmblemPatch(emblem, false), PU_CACHE), + V_DrawSmallMappedPatch(x, y, 0, W_CachePatchLongName(M_GetEmblemPatch(emblem, false), PU_CACHE), R_GetTranslationColormap(TC_DEFAULT, M_GetEmblemColor(emblem), GTC_MENUCACHE)); else V_DrawSmallScaledPatch(x, y, 0, W_CachePatchLongName("NEEDIT", PU_CACHE)); @@ -2219,7 +2219,7 @@ static void M_DrawMenuTitle(void) { if (currentMenu->headerpic) { - patch_t *p = W_CachePatchName(currentMenu->headerpic, PU_CACHE); + patch_t *p = W_CachePatchLongName(currentMenu->headerpic, PU_CACHE); if (p->height > 24) // title is larger than normal { @@ -2492,7 +2492,7 @@ static INT32 M_DrawMenuItem(menuitem_t *item, INT16 x, INT16 y, INT32 vflags, bo if (!item->patch) return 0; - p = W_CachePatchName(item->patch, PU_CACHE); + p = W_CachePatchLongName(item->patch, PU_CACHE); break; } } @@ -4286,14 +4286,14 @@ void MD_DrawPlaybackMenu(void) inactivemap = R_GetTranslationColormap(players[ply].skin, players[ply].skincolor, GTC_MENUCACHE); } else if (currentMenu->menuitems[i].patch && W_CheckNumForName(currentMenu->menuitems[i].patch) != LUMPERROR) - icon = W_CachePatchName(currentMenu->menuitems[i].patch, PU_CACHE); + icon = W_CachePatchLongName(currentMenu->menuitems[i].patch, PU_CACHE); else icon = W_CachePatchLongName("PLAYRANK", PU_CACHE); // temp } else if (currentMenu->menuitems[i].status & IT_HIDDEN) continue; else if (currentMenu->menuitems[i].patch && W_CheckNumForName(currentMenu->menuitems[i].patch) != LUMPERROR) - icon = W_CachePatchName(currentMenu->menuitems[i].patch, PU_CACHE); + icon = W_CachePatchLongName(currentMenu->menuitems[i].patch, PU_CACHE); else icon = W_CachePatchLongName("PLAYRANK", PU_CACHE); // temp @@ -5305,7 +5305,7 @@ static void M_DrawStatsMaps(void) exemblem = &extraemblems[i]; if (exemblem->collected) - V_DrawSmallMappedPatch(295, y, 0, W_CachePatchName(M_GetExtraEmblemPatch(exemblem, false), PU_CACHE), + V_DrawSmallMappedPatch(295, y, 0, W_CachePatchLongName(M_GetExtraEmblemPatch(exemblem, false), PU_CACHE), R_GetTranslationColormap(TC_DEFAULT, M_GetExtraEmblemColor(exemblem), GTC_MENUCACHE)); else V_DrawSmallScaledPatch(295, y, 0, W_CachePatchLongName("NEEDIT", PU_CACHE)); @@ -7804,11 +7804,11 @@ void MD_DrawMonitorToggles(void) if (drawnum != 0) { V_DrawScaledPatch(x, y, 0, W_CachePatchLongName("K_ISMUL", PU_CACHE)); - V_DrawScaledPatch(x, y, translucent, W_CachePatchName(K_GetItemPatch(currentMenu->menuitems[thisitem].argument, true), PU_CACHE)); + V_DrawScaledPatch(x, y, translucent, W_CachePatchLongName(K_GetItemPatch(currentMenu->menuitems[thisitem].argument, true), PU_CACHE)); V_DrawString(x+24, y+31, V_ALLOWLOWERCASE|translucent, va("x%d", drawnum)); } else - V_DrawScaledPatch(x, y, translucent, W_CachePatchName(K_GetItemPatch(currentMenu->menuitems[thisitem].argument, true), PU_CACHE)); + V_DrawScaledPatch(x, y, translucent, W_CachePatchLongName(K_GetItemPatch(currentMenu->menuitems[thisitem].argument, true), PU_CACHE)); y += spacing; } @@ -7862,12 +7862,12 @@ void MD_DrawMonitorToggles(void) if (drawnum != 0) { V_DrawScaledPatch(onx-1, ony-2, 0, W_CachePatchLongName("K_ITMUL", PU_CACHE)); - V_DrawScaledPatch(onx-1, ony-2, translucent, W_CachePatchName(K_GetItemPatch(currentMenu->menuitems[itemOn].argument, false), PU_CACHE)); + V_DrawScaledPatch(onx-1, ony-2, translucent, W_CachePatchLongName(K_GetItemPatch(currentMenu->menuitems[itemOn].argument, false), PU_CACHE)); V_DrawScaledPatch(onx+27, ony+39, translucent, W_CachePatchLongName("K_ITX", PU_CACHE)); V_DrawKartString(onx+37, ony+34, translucent, va("%d", drawnum)); } else - V_DrawScaledPatch(onx-1, ony-2, translucent, W_CachePatchName(K_GetItemPatch(currentMenu->menuitems[itemOn].argument, false), PU_CACHE)); + V_DrawScaledPatch(onx-1, ony-2, translucent, W_CachePatchLongName(K_GetItemPatch(currentMenu->menuitems[itemOn].argument, false), PU_CACHE)); } } diff --git a/src/p_setup.c b/src/p_setup.c index c8842c0eb..5019878b6 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -9191,7 +9191,7 @@ UINT8 P_InitMapData(boolean existingmapheaders) // okay... try finding them the old-fashioned way else { - oldPic = W_CachePatchName(va("%sP", name), PU_STATIC); + oldPic = W_CachePatchLongName(va("%sP", name), PU_STATIC); if (oldPic != missingpat) mapheaderinfo[i]->thumbnailPic = oldPic; } @@ -9202,7 +9202,7 @@ UINT8 P_InitMapData(boolean existingmapheaders) } else { - oldPic = W_CachePatchName(va("%sR", name), PU_STATIC); + oldPic = W_CachePatchLongName(va("%sR", name), PU_STATIC); if (oldPic != missingpat) mapheaderinfo[i]->minimapPic = oldPic; } diff --git a/src/r_things.cpp b/src/r_things.cpp index d1420cb4a..d143350c6 100644 --- a/src/r_things.cpp +++ b/src/r_things.cpp @@ -1473,7 +1473,7 @@ static void R_ProjectDropShadow( if (shadowpatch == NULL) { - shadowpatch = static_cast(W_CachePatchName("DSHADOW", PU_SPRITE)); + shadowpatch = static_cast(W_CachePatchLongName("DSHADOW", PU_SPRITE)); } patch = shadowpatch; diff --git a/src/w_wad.h b/src/w_wad.h index 2f26147cf..cdac87884 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -185,7 +185,6 @@ boolean W_IsPatchCached(lumpnum_t lump, void *ptr); boolean W_NeedPaletteRemapPwad(UINT16 wad, UINT16 lump, boolean ignorecache); boolean W_NeedPaletteRemap(lumpnum_t lump, boolean ignorecache); -#define W_CachePatchName(name, tag) W_CachePatchLongName(W_ShortName(name), tag) void *W_CachePatchLongName(const char *name, INT32 tag); // Returns either a Software patch, or an OpenGL patch. diff --git a/src/y_inter.c b/src/y_inter.c index e36f94a58..c8daa11bc 100644 --- a/src/y_inter.c +++ b/src/y_inter.c @@ -1154,7 +1154,7 @@ static inline void Y_DrawAnimatedVoteScreenPatch(boolean widePatch) currentAnimFrame = 0; } - patch_t *background = W_CachePatchName(va("%s%d", tempAnimPrefix, currentAnimFrame + 1), PU_CACHE); + patch_t *background = W_CachePatchLongName(va("%s%d", tempAnimPrefix, currentAnimFrame + 1), PU_CACHE); V_DrawScaledPatch(((vid.width/2) / vid.dupx) - (SHORT(background->width)/2), // Keep the width/height adjustments, for screens that are less wide than 320(?) (vid.height / vid.dupy) - SHORT(background->height), V_SNAPTOTOP|V_SNAPTOLEFT, background); From 146ac557ba554a1c8bb8844764c0f5eea4872b91 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Thu, 26 Jun 2025 00:18:20 +0200 Subject: [PATCH 10/18] Remove W_GetNumForName Still no longnames for sounds/music, but that's a story for another day --- src/console.c | 2 +- src/r_textures.c | 19 +++++-------------- src/s_sound.c | 13 ++++--------- src/v_video.c | 25 +++++++++---------------- src/v_video.h | 1 - src/w_wad.h | 1 - 6 files changed, 19 insertions(+), 42 deletions(-) diff --git a/src/console.c b/src/console.c index 855386b5c..91da5f27d 100644 --- a/src/console.c +++ b/src/console.c @@ -299,7 +299,7 @@ void CON_SetupBackColormapEx(INT32 color, boolean prompt) { UINT16 i, palsum; UINT8 j, palindex; - lumpnum_t lump = W_GetNumForName(GetPalette()); + lumpnum_t lump = W_GetNumForLongName(GetPalette()); UINT8 *pal = W_CacheLumpNum(lump, PU_CACHE); INT32 shift = 6; diff --git a/src/r_textures.c b/src/r_textures.c index ec68362a5..1c9f13759 100644 --- a/src/r_textures.c +++ b/src/r_textures.c @@ -1493,7 +1493,7 @@ static texpatch_t *R_ParsePatch(boolean actuallyLoadPatch) char *texturesToken; size_t texturesTokenLength; char *endPos; - char *patchName = NULL; + char patchName[SHORTNAMELEN+1]; INT16 patchXPos; INT16 patchYPos; UINT8 flip = 0; @@ -1509,19 +1509,13 @@ static texpatch_t *R_ParsePatch(boolean actuallyLoadPatch) I_Error("Error parsing TEXTURES lump: Unexpected end of file where patch name should be"); } texturesTokenLength = strlen(texturesToken); - if (texturesTokenLength>8) + if (texturesTokenLength >= sizeof(patchName)) { - I_Error("Error parsing TEXTURES lump: Patch name \"%s\" exceeds 8 characters",texturesToken); + I_Error("Error parsing TEXTURES lump: Patch name \"%s\" exceeds %zu characters", texturesToken, sizeof(patchName) - 1); } else { - if (patchName != NULL) - { - Z_Free(patchName); - } - patchName = (char *)Z_Malloc((texturesTokenLength+1)*sizeof(char),PU_STATIC,NULL); - M_Memcpy(patchName,texturesToken,texturesTokenLength*sizeof(char)); - patchName[texturesTokenLength] = '\0'; + strcpy(patchName, texturesToken); } // Comma 1 @@ -1660,7 +1654,7 @@ static texpatch_t *R_ParsePatch(boolean actuallyLoadPatch) if (actuallyLoadPatch == true) { // Check lump exists - patchLumpNum = W_GetNumForName(patchName); + patchLumpNum = W_GetNumForLongName(patchName); // If so, allocate memory for texpatch_t and fill 'er up resultPatch = (texpatch_t *)Z_Malloc(sizeof(texpatch_t),PU_STATIC,NULL); resultPatch->originx = patchXPos; @@ -1670,14 +1664,11 @@ static texpatch_t *R_ParsePatch(boolean actuallyLoadPatch) resultPatch->flip = flip; resultPatch->alpha = alpha; resultPatch->style = style; - // Clean up a little after ourselves - Z_Free(patchName); // Then return it return resultPatch; } else { - Z_Free(patchName); return NULL; } } diff --git a/src/s_sound.c b/src/s_sound.c index f4f5079e1..d615cbc28 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -313,22 +313,17 @@ static void SetChannelsNum(void) // lumpnum_t S_GetSfxLumpNum(sfxinfo_t *sfx) { - char namebuf[9]; lumpnum_t sfxlump; - sprintf(namebuf, "ds%s", sfx->name); - - sfxlump = W_CheckNumForName(namebuf); + sfxlump = W_CheckNumForLongName(va("DS%s", sfx->name)); if (sfxlump != LUMPERROR) return sfxlump; - strlcpy(namebuf, sfx->name, sizeof namebuf); - - sfxlump = W_CheckNumForName(namebuf); + sfxlump = W_CheckNumForLongName(sfx->name); if (sfxlump != LUMPERROR) return sfxlump; - return W_GetNumForName("dsthok"); + return W_GetNumForLongName("DSTHOK"); } // @@ -2186,7 +2181,7 @@ boolean S_RecallMusic(UINT16 status, boolean fromfirst) static lumpnum_t S_GetMusicLumpNum(const char *mname) { if (S_MusicExists(mname)) - return W_GetNumForName(va("o_%s", mname)); + return W_GetNumForLongName(va("O_%s", mname)); else return LUMPERROR; } diff --git a/src/v_video.c b/src/v_video.c index 36a4df30a..5cad2c959 100644 --- a/src/v_video.c +++ b/src/v_video.c @@ -313,7 +313,7 @@ UINT32 V_GammaCorrect(UINT32 input, double power) // keep a copy of the palette so that we can get the RGB value for a color index at any time. static void LoadPalette(const char *lumpname) { - lumpnum_t lumpnum = W_GetNumForName(lumpname); + lumpnum_t lumpnum = W_GetNumForLongName(lumpname); size_t i, palsize = W_LumpLength(lumpnum)/3; UINT8 *pal; @@ -408,22 +408,15 @@ void V_CubeApply(UINT8 *red, UINT8 *green, UINT8 *blue) *blue = (UINT8)(working[0][2]); } -const char *R_GetPalname(UINT16 num) -{ - static char palname[9]; - char newpal[9] = "PLAYPAL"; - - if (num > 0 && num <= 10000) - snprintf(newpal, 8, "PAL%04u", num-1); - - strncpy(palname, newpal, 8); - return palname; -} - const char *GetPalette(void) { - if (gamestate == GS_LEVEL) - return R_GetPalname((encoremode ? mapheaderinfo[gamemap-1]->encorepal : mapheaderinfo[gamemap-1]->palette)); + UINT16 num = encoremode ? mapheaderinfo[gamemap-1]->encorepal : mapheaderinfo[gamemap-1]->palette; + if (gamestate == GS_LEVEL && num > 0 && num <= 10000) + { + static char palname[SHORTNAMELEN+1]; + sprintf(palname, "PAL%04u", num-1); + return palname; + } return "PLAYPAL"; } @@ -1667,7 +1660,7 @@ void V_DrawCustomFadeScreen(const char *lump, UINT8 strength) lighttable_t *clm = NULL; if (lump != NULL) - lumpnum = W_GetNumForName(lump); + lumpnum = W_GetNumForLongName(lump); else return; diff --git a/src/v_video.h b/src/v_video.h index 1a17cd84c..5b82d3814 100644 --- a/src/v_video.h +++ b/src/v_video.h @@ -68,7 +68,6 @@ void V_SetPalette(INT32 palettenum); void V_SetPaletteLump(const char *pal); -const char *R_GetPalname(UINT16 num); const char *GetPalette(void); extern RGBA_t *pLocalPalette; diff --git a/src/w_wad.h b/src/w_wad.h index cdac87884..d87be6605 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -156,7 +156,6 @@ UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump) lumpnum_t W_CheckNumForMap(const char *name); #define W_CheckNumForName(name) W_CheckNumForLongName(W_ShortName(name)) lumpnum_t W_CheckNumForLongName(const char *name); -#define W_GetNumForName(name) W_GetNumForLongName(W_ShortName(name)) lumpnum_t W_GetNumForLongName(const char *name); lumpnum_t W_CheckNumForNameInFolder(const char *lump, const char *folder); UINT8 W_LumpExists(const char *name); // Lua uses this. From 24de6186537f5e3d729d30c2d7b4c779d465038a Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Thu, 26 Jun 2025 01:22:08 +0200 Subject: [PATCH 11/18] Remove W_CheckNumForName NOW long menuitem patches are supported lol Staff ghosts support long map names now The runsoc command and linedef special 415 support longnames now --- src/deh_soc.c | 8 +++----- src/f_finale.c | 2 +- src/f_wipe.c | 21 +++++++-------------- src/g_demo.c | 16 +++++++++------- src/g_demo.h | 4 ++-- src/hardware/hw_main.c | 4 ++-- src/hu_stuff.c | 6 +++--- src/m_menu.c | 6 +++--- src/p_setup.c | 10 ++++------ src/p_spec.c | 2 +- src/r_skins.c | 8 ++++---- src/r_skins.h | 2 +- src/r_things.cpp | 2 +- src/s_sound.c | 2 +- src/sdl/sdl_sound.c | 4 ++-- src/w_wad.h | 1 - 16 files changed, 44 insertions(+), 54 deletions(-) diff --git a/src/deh_soc.c b/src/deh_soc.c index 81e5f2c0b..1c47fbe4e 100644 --- a/src/deh_soc.c +++ b/src/deh_soc.c @@ -3146,13 +3146,11 @@ void readmaincfg(MYFILE *f) else { lumpnum_t lumpnum; - char newname[9]; + char newname[SHORTNAMELEN+1]; - strncpy(newname, word2, 8); + strlcpy(newname, word2, sizeof(newname)); - newname[8] = '\0'; - - lumpnum = W_CheckNumForName(newname); + lumpnum = W_CheckNumForLongName(newname); if (lumpnum == LUMPERROR || W_LumpLength(lumpnum) == 0) CONS_Debug(DBG_SETUP, "SOC Error: script lump %s not found/not valid.\n", newname); diff --git a/src/f_finale.c b/src/f_finale.c index 8f844a5f6..2fe8bd923 100644 --- a/src/f_finale.c +++ b/src/f_finale.c @@ -1616,7 +1616,7 @@ void F_TitleScreenTicker(boolean run) if (vLump == NULL) { - if (((W_CheckNumForName(va("%sS%02u", mapheaderinfo[mapnum]->lumpname, numstaff)))) != LUMPERROR) + if (((W_CheckNumForLongName(va("%sS%02u", mapheaderinfo[mapnum]->lumpname, numstaff)))) != LUMPERROR) sprintf(dname, "%sS%02u", mapheaderinfo[mapnum]->lumpname, numstaff); else { diff --git a/src/f_wipe.c b/src/f_wipe.c index 9146efcad..e0e702671 100644 --- a/src/f_wipe.c +++ b/src/f_wipe.c @@ -102,7 +102,7 @@ static fixed_t paldiv; * \return fademask_t for lump */ static fademask_t *F_GetFadeMask(UINT8 masknum, UINT8 scrnnum) { - static char lumpname[9] = "FADEmmss"; + static char lumpname[SHORTNAMELEN+1] = "FADEmmss"; static fademask_t fm = {NULL,0,0,0,0,0}; lumpnum_t lumpnum; UINT8 *lump, *mask; @@ -112,16 +112,9 @@ static fademask_t *F_GetFadeMask(UINT8 masknum, UINT8 scrnnum) { if (masknum > 99 || scrnnum > 99) goto freemask; - // SRB2Kart: This suddenly triggers ERRORMODE now - //sprintf(&lumpname[4], "%.2hu%.2hu", (UINT16)masknum, (UINT16)scrnnum); + sprintf(&lumpname[4], "%.2hu%.2hu", (UINT16)masknum, (UINT16)scrnnum); - lumpname[4] = '0'+(masknum/10); - lumpname[5] = '0'+(masknum%10); - - lumpname[6] = '0'+(scrnnum/10); - lumpname[7] = '0'+(scrnnum%10); - - lumpnum = W_CheckNumForName(lumpname); + lumpnum = W_CheckNumForLongName(lumpname); if (lumpnum == LUMPERROR) goto freemask; @@ -429,7 +422,7 @@ tic_t F_GetWipeLength(UINT8 wipetype) (void)wipetype; return 0; #else - static char lumpname[10] = "FADEmmss"; + static char lumpname[SHORTNAMELEN+1] = "FADEmmss"; lumpnum_t lumpnum; UINT8 wipeframe; @@ -440,7 +433,7 @@ tic_t F_GetWipeLength(UINT8 wipetype) { sprintf(&lumpname[4], "%.2hu%.2hu", (UINT16)wipetype, (UINT16)wipeframe); - lumpnum = W_CheckNumForName(lumpname); + lumpnum = W_CheckNumForLongName(lumpname); if (lumpnum == LUMPERROR) return --wipeframe; } @@ -456,7 +449,7 @@ boolean F_WipeExists(UINT8 wipetype) (void)wipetype; return false; #else - static char lumpname[10] = "FADEmm00"; + static char lumpname[SHORTNAMELEN+1] = "FADEmm00"; lumpnum_t lumpnum; if (wipetype > 99) @@ -464,7 +457,7 @@ boolean F_WipeExists(UINT8 wipetype) sprintf(&lumpname[4], "%.2hu00", (UINT16)wipetype); - lumpnum = W_CheckNumForName(lumpname); + lumpnum = W_CheckNumForLongName(lumpname); return !(lumpnum == LUMPERROR); #endif } diff --git a/src/g_demo.c b/src/g_demo.c index 38cc9894d..e7e024981 100644 --- a/src/g_demo.c +++ b/src/g_demo.c @@ -2868,11 +2868,12 @@ void G_DeferedPlayDemo(const char *name) #define SKIPERRORS -void G_DoPlayDemo(char *defdemoname) +void G_DoPlayDemo(const char *defdemoname) { UINT8 i, p; lumpnum_t l; - char skin[17],color[MAXCOLORNAME+1],follower[17],mapname[MAXMAPLUMPNAME],*n,*pdemoname; + char skin[17],color[MAXCOLORNAME+1],follower[17],mapname[MAXMAPLUMPNAME],*pdemoname; + const char *n; UINT8 version,subversion; UINT32 randseed; char msg[1024]; @@ -2927,7 +2928,7 @@ void G_DoPlayDemo(char *defdemoname) if (n == defdemoname) { // Raw lump. - if ((l = W_CheckNumForName(defdemoname)) == LUMPERROR) + if ((l = W_CheckNumForLongName(defdemoname)) == LUMPERROR) { snprintf(msg, 1024, M_GetText("Failed to read lump '%s'.\n"), defdemoname); CONS_Alert(CONS_ERROR, "%s", msg); @@ -3381,11 +3382,12 @@ void G_DoPlayDemo(char *defdemoname) demo.deferstart = true; } -void G_AddGhost(char *defdemoname) +void G_AddGhost(const char *defdemoname) { INT32 i; lumpnum_t l; - char name[17],skin[17],color[MAXCOLORNAME+1],*n,*pdemoname; + char name[17],skin[17],color[MAXCOLORNAME+1],*pdemoname; + const char *n; UINT64 demohash; demoghost *gh; UINT8 flags; @@ -3420,7 +3422,7 @@ void G_AddGhost(char *defdemoname) p = buffer; } // load demo resource from WAD - else if ((l = W_CheckNumForName(defdemoname)) == LUMPERROR) + else if ((l = W_CheckNumForLongName(defdemoname)) == LUMPERROR) { CONS_Alert(CONS_ERROR, M_GetText("Failed to read lump '%s'.\n"), defdemoname); Z_Free(pdemoname); @@ -3799,7 +3801,7 @@ void G_DoPlayMetal(void) // it's an internal demo // TODO: Use map header to determine lump name - if ((l = W_CheckNumForName(va("%sMS",G_BuildMapName(gamemap)))) == LUMPERROR) + if ((l = W_CheckNumForLongName(va("%sMS",G_BuildMapName(gamemap)))) == LUMPERROR) { CONS_Alert(CONS_WARNING, M_GetText("No bot recording for this map.\n")); return; diff --git a/src/g_demo.h b/src/g_demo.h index 7a6fa6e6d..776f9bb42 100644 --- a/src/g_demo.h +++ b/src/g_demo.h @@ -179,9 +179,9 @@ extern demoghost *ghosts; #define DFILE_ERROR_EXTRAFILES 0x05 // Extra files outside of the replay's file list are loaded. void G_DeferedPlayDemo(const char *demo); -void G_DoPlayDemo(char *defdemoname); +void G_DoPlayDemo(const char *defdemoname); void G_TimeDemo(const char *name); -void G_AddGhost(char *defdemoname); +void G_AddGhost(const char *defdemoname); void G_UpdateStaffGhostName(lumpnum_t l); void G_FreeGhosts(void); void G_DoneLevelLoad(void); diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index dbc08f032..6294508d6 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -6494,7 +6494,7 @@ static lumpnum_t wipelumpnum; // puts wipe lumpname in wipename[9] static boolean HWR_WipeCheck(UINT8 wipenum, UINT8 scrnnum) { - static char lumpname[9] = "FADEmmss"; + static char lumpname[SHORTNAMELEN+1] = "FADEmmss"; size_t lsize; // not a valid wipe number @@ -6506,7 +6506,7 @@ static boolean HWR_WipeCheck(UINT8 wipenum, UINT8 scrnnum) lumpname[5] = '0'+(wipenum%10); lumpname[6] = '0'+(scrnnum/10); lumpname[7] = '0'+(scrnnum%10); - wipelumpnum = W_CheckNumForName(lumpname); + wipelumpnum = W_CheckNumForLongName(lumpname); // again, shouldn't be here really if (wipelumpnum == LUMPERROR) diff --git a/src/hu_stuff.c b/src/hu_stuff.c index b33ce0ae0..ba8ec737f 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -310,7 +310,7 @@ void HU_Init(void) patch_t *HU_UpdateOrBlankPatch(patch_t **user, boolean required, const char *format, ...) { va_list ap; - char buffer[9]; + char buffer[SHORTNAMELEN+1]; lumpnum_t lump = LUMPERROR; patch_t *patch; @@ -326,7 +326,7 @@ patch_t *HU_UpdateOrBlankPatch(patch_t **user, boolean required, const char *for while ((lump == LUMPERROR) && ((--fileref) >= partadd_earliestfile)) { - lump = W_CheckNumForNamePwad(buffer, fileref, 0); + lump = W_CheckNumForLongNamePwad(buffer, fileref, 0); } /* no update in this wad */ @@ -339,7 +339,7 @@ patch_t *HU_UpdateOrBlankPatch(patch_t **user, boolean required, const char *for } else { - lump = W_CheckNumForName(buffer); + lump = W_CheckNumForLongName(buffer); if (lump == LUMPERROR) { diff --git a/src/m_menu.c b/src/m_menu.c index 4306a94ca..d529fe202 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -4285,14 +4285,14 @@ void MD_DrawPlaybackMenu(void) if (i != itemOn) inactivemap = R_GetTranslationColormap(players[ply].skin, players[ply].skincolor, GTC_MENUCACHE); } - else if (currentMenu->menuitems[i].patch && W_CheckNumForName(currentMenu->menuitems[i].patch) != LUMPERROR) + else if (currentMenu->menuitems[i].patch && W_CheckNumForLongName(currentMenu->menuitems[i].patch) != LUMPERROR) icon = W_CachePatchLongName(currentMenu->menuitems[i].patch, PU_CACHE); else icon = W_CachePatchLongName("PLAYRANK", PU_CACHE); // temp } else if (currentMenu->menuitems[i].status & IT_HIDDEN) continue; - else if (currentMenu->menuitems[i].patch && W_CheckNumForName(currentMenu->menuitems[i].patch) != LUMPERROR) + else if (currentMenu->menuitems[i].patch && W_CheckNumForLongName(currentMenu->menuitems[i].patch) != LUMPERROR) icon = W_CachePatchLongName(currentMenu->menuitems[i].patch, PU_CACHE); else icon = W_CachePatchLongName("PLAYRANK", PU_CACHE); // temp @@ -5697,7 +5697,7 @@ INT32 MR_ChooseTimeAttack(INT32 choice) INT32 MR_ReplayStaff(INT32 choice) { (void)choice; - lumpnum_t l = W_CheckNumForName(va("%sS%02u",G_BuildMapName(cv_nextmap.value),cv_dummystaff.value)); + lumpnum_t l = W_CheckNumForLongName(va("%sS%02u",G_BuildMapName(cv_nextmap.value),cv_dummystaff.value)); if (l == LUMPERROR) return false; diff --git a/src/p_setup.c b/src/p_setup.c index 5019878b6..08f5564b4 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -8104,13 +8104,11 @@ static void P_RunLevelScript(const char *scriptname) if (!(mapheaderinfo[gamemap-1]->levelflags & LF_SCRIPTISFILE)) { lumpnum_t lumpnum; - char newname[9]; + char newname[SHORTNAMELEN+1]; - strncpy(newname, scriptname, 8); + strlcpy(newname, scriptname, sizeof(newname)); - newname[8] = '\0'; - - lumpnum = W_CheckNumForName(newname); + lumpnum = W_CheckNumForLongName(newname); if (lumpnum == LUMPERROR || W_LumpLength(lumpnum) == 0) { @@ -9017,7 +9015,7 @@ boolean P_RunSOC(const char *socfilename) if (strstr(socfilename, ".soc") != NULL) return P_AddWadFile(socfilename, WC_AUTO); - lump = W_CheckNumForName(socfilename); + lump = W_CheckNumForLongName(socfilename); if (lump == LUMPERROR) return false; diff --git a/src/p_spec.c b/src/p_spec.c index 88acc17a3..f683aa89e 100644 --- a/src/p_spec.c +++ b/src/p_spec.c @@ -2975,7 +2975,7 @@ boolean P_ProcessSpecial(activator_t *activator, INT16 special, INT32 *args, cha case 415: // Run a script if (cv_runscripts.value) { - lumpnum_t lumpnum = W_CheckNumForName(stringargs[0]); + lumpnum_t lumpnum = W_CheckNumForLongName(stringargs[0]); if (lumpnum == LUMPERROR || W_LumpLength(lumpnum) == 0) CONS_Debug(DBG_SETUP, "Line type 415 Executor: script lump %s not found/not valid.\n", stringargs[0]); diff --git a/src/r_skins.c b/src/r_skins.c index 86f6146ea..cb6031ab1 100644 --- a/src/r_skins.c +++ b/src/r_skins.c @@ -110,9 +110,9 @@ static void Sk_SetDefaultValue(skin_t *skin) skin->flags = 0; strcpy(skin->realname, "Someone"); - strncpy(skin->facerank, "MISSING", 9); - strncpy(skin->facewant, "MISSING", 9); - strncpy(skin->facemmap, "MISSING", 9); + strcpy(skin->facerank, "MISSING"); + strcpy(skin->facewant, "MISSING"); + strcpy(skin->facemmap, "MISSING"); skin->starttranscolor = 96; skin->prefcolor = SKINCOLOR_GREEN; skin->supercolor = SKINCOLOR_SUPER1; @@ -586,7 +586,7 @@ static boolean R_ProcessPatchableFields(skin_t *skin, char *stoken, char *value) GETFLAG(OLDDEATH) #undef GETFLAG -#define GETPATCH(field) else if (compat && !stricmp(stoken, #field)) strncpy(skin->field, value, 8); +#define GETPATCH(field) else if (compat && !stricmp(stoken, #field)) strlcpy(skin->field, value, sizeof(skin->field)); GETPATCH(facerank) GETPATCH(facewant) GETPATCH(facemmap) diff --git a/src/r_skins.h b/src/r_skins.h index c7bef3c1a..1cae03d38 100644 --- a/src/r_skins.h +++ b/src/r_skins.h @@ -42,7 +42,7 @@ struct skin_t skinflags_t flags; char realname[SKINNAMESIZE+1]; // Display name for level completion. - char facerank[9], facewant[9], facemmap[9]; // Arbitrarily named patch lumps + char facerank[SHORTNAMELEN+1], facewant[SHORTNAMELEN+1], facemmap[SHORTNAMELEN+1]; // Arbitrarily named patch lumps // SRB2kart UINT8 kartspeed; diff --git a/src/r_things.cpp b/src/r_things.cpp index d143350c6..d9b73aa3a 100644 --- a/src/r_things.cpp +++ b/src/r_things.cpp @@ -271,7 +271,7 @@ void R_AddKartFaces(skin_t *skin) spritedef_t *sd = &skin->sprites[SPR2_XTRA]; for (size_t f = 0; f < NUMFACES; f++) { - lumpnum_t lumpnum = W_CheckNumForName(reinterpret_cast(skin) + KART_FACES[f]); // how do you do, fellow C++ers? + lumpnum_t lumpnum = W_CheckNumForLongName(reinterpret_cast(skin) + KART_FACES[f]); // how do you do, fellow C++ers? if (lumpnum == LUMPERROR) I_Error("R_AddKartFaces: missing patch %s for skin %s", reinterpret_cast(skin) + KART_FACES[f], skin->name); R_InstallSpriteLump(WADFILENUM(lumpnum), LUMPNUM(lumpnum), numspritelumps, f, 0, 0); diff --git a/src/s_sound.c b/src/s_sound.c index d615cbc28..7a75fb947 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1844,7 +1844,7 @@ boolean S_MusicInfo(char *mname, UINT16 *mflags, boolean *looping) boolean S_MusicExists(const char *mname) { - return W_CheckNumForName(va("O_%s", mname)) != LUMPERROR; + return W_CheckNumForLongName(va("O_%s", mname)) != LUMPERROR; } /// ------------------------ diff --git a/src/sdl/sdl_sound.c b/src/sdl/sdl_sound.c index 3b05bfa26..511520052 100644 --- a/src/sdl/sdl_sound.c +++ b/src/sdl/sdl_sound.c @@ -1537,7 +1537,7 @@ static void I_CleanupGME(void *userdata) static boolean I_StartGMESong(const char *musicname, boolean looping) { - char filename[9]; + char filename[SHORTNAMELEN+1]; void *data; lumpnum_t lumpnum; size_t lumplength; @@ -1552,7 +1552,7 @@ static boolean I_StartGMESong(const char *musicname, boolean looping) snprintf(filename, sizeof filename, "o_%s", musicname); - lumpnum = W_CheckNumForName(filename); + lumpnum = W_CheckNumForLongName(filename); if (lumpnum == LUMPERROR) { diff --git a/src/w_wad.h b/src/w_wad.h index d87be6605..53d6a90de 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -154,7 +154,6 @@ UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlum UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump); lumpnum_t W_CheckNumForMap(const char *name); -#define W_CheckNumForName(name) W_CheckNumForLongName(W_ShortName(name)) lumpnum_t W_CheckNumForLongName(const char *name); lumpnum_t W_GetNumForLongName(const char *name); lumpnum_t W_CheckNumForNameInFolder(const char *lump, const char *folder); From 91636209ffd02158970ab97b54e721c9b350ee20 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Thu, 26 Jun 2025 01:45:45 +0200 Subject: [PATCH 12/18] And finally... W_CheckNumForNamePwad --- src/hardware/hw_main.c | 14 +++++--------- src/w_wad.h | 11 ----------- 2 files changed, 5 insertions(+), 20 deletions(-) diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index 6294508d6..753510f4b 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -6658,23 +6658,19 @@ skip_lump: if (PK3) { - shader_lumpname = Z_Malloc(strlen(value) + 12, PU_STATIC, NULL); - strcpy(shader_lumpname, "Shaders/sh_"); - strcat(shader_lumpname, value); + shader_lumpname = xva("Shaders/sh_%s", value); shader_lumpnum = W_CheckNumForFullNamePK3(shader_lumpname, wadnum, 0); } else { - shader_lumpname = Z_Malloc(strlen(value) + 4, PU_STATIC, NULL); - strcpy(shader_lumpname, "SH_"); - strcat(shader_lumpname, value); - shader_lumpnum = W_CheckNumForNamePwad(shader_lumpname, wadnum, 0); + shader_lumpname = xva("SH_%.5s", value); + shader_lumpnum = W_CheckNumForLongNamePwad(shader_lumpname, wadnum, 0); } if (shader_lumpnum == LUMPERROR) { CONS_Alert(CONS_ERROR, "HWR_LoadCustomShadersFromFile: Missing shader source %s (file %s, line %d)\n", shader_lumpname, wadfiles[wadnum]->filename, linenum); - Z_Free(shader_lumpname); + free(shader_lumpname); continue; } @@ -6685,7 +6681,7 @@ skip_lump: HWD.pfnLoadCustomShader(shaderxlat[i].id, shader_source, shader_size, (shadertype == 2)); Z_Free(shader_source); - Z_Free(shader_lumpname); + free(shader_lumpname); } } diff --git a/src/w_wad.h b/src/w_wad.h index 53d6a90de..8f7491c16 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -133,18 +133,7 @@ const char *W_CheckFullNameForNum(lumpnum_t lumpnum); UINT16 W_FindNextEmptyInPwad(UINT16 wad, UINT16 startlump); // checks only in one pwad -// helper for old functions that use 8-char names -// careful, you might be dealing with a non-terminated string! -static inline const char *W_ShortName(const char *name) -{ - static char shortname[8+1]; - strncpy(shortname, name, 8); - shortname[8] = '\0'; - return shortname; -} - UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 startlump); -#define W_CheckNumForNamePwad(name, wad, start) W_CheckNumForLongNamePwad(W_ShortName(name), wad, start) UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForNamePrefixPwad(const char *name, size_t namelen, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForNameMultiPrefixPwad(const lumpprefixes_t *prefixes, size_t numprefixes, UINT16 wad, UINT16 startlump); From edae0b45ad99f603aaf2660e8cffab57d652e84a Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Thu, 26 Jun 2025 01:55:56 +0200 Subject: [PATCH 13/18] Do I have news for you! --- src/w_wad.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/w_wad.c b/src/w_wad.c index fd6c603ad..59bfab21b 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -1211,10 +1211,11 @@ UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 st } // -// Like W_CheckNumForNamePwad, but can find entries with long names +// Same as the original, but checks in one pwad only. +// wadid is a wad number +// (Used for sprites loading) // -// Should be the only version, but that's not possible until we fix -// all the instances of non null-terminated strings in the codebase... +// 'startlump' is the lump number to start the search // UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) { @@ -1337,10 +1338,8 @@ UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump) } // -// Like W_CheckNumForName, but can find entries with long names -// -// Should be the only version, but that's not possible until we fix -// all the instances of non null-terminated strings in the codebase... +// W_CheckNumForLongName +// Returns LUMPERROR if name not found. // lumpnum_t W_CheckNumForLongName(const char *name) { @@ -1448,10 +1447,9 @@ lumpnum_t W_CheckNumForMap(const char *name) } // -// Like W_GetNumForName, but can find entries with long names +// W_GetNumForLongName // -// Should be the only version, but that's not possible until we fix -// all the instances of non null-terminated strings in the codebase... +// Calls W_CheckNumForLongName, but bombs out if not found. // lumpnum_t W_GetNumForLongName(const char *name) { From 4c93f0eae7434a6438abc8363bd1d06c56ab3603 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Thu, 26 Jun 2025 21:28:45 +0200 Subject: [PATCH 14/18] Operation: Gotta Go Fast --- src/w_wad.c | 276 +++++++++++++++++++++++++--------------------------- src/w_wad.h | 13 +-- 2 files changed, 135 insertions(+), 154 deletions(-) diff --git a/src/w_wad.c b/src/w_wad.c index 59bfab21b..36807ca98 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -158,10 +158,20 @@ typedef struct zlentry_s // maximum length of longname #define MAXLUMPNAME 256 +// Available compression methods for lumps. +typedef enum +{ + CM_NOCOMPRESSION, +#ifdef HAVE_ZLIB + CM_DEFLATE, +#endif + CM_LZF, + CM_UNSUPPORTED +} compmethod; + // a memory entry of the wad directory struct lumpinfo_t { - UINT32 hash; // hash for longname in ALL CAPS UINT32 longname; // e.g. "LongEntryName" UINT32 fullname; // e.g. "Folder/Subfolder/LongEntryName.extension" UINT32 position; // filelump_t filepos @@ -178,12 +188,11 @@ typedef struct // Must be a power of two #define LUMPNUMCACHESIZE 64 -#define LUMPNUMCACHENAME 32 typedef struct lumpnum_cache_s { - char lumpname[LUMPNUMCACHENAME]; - UINT32 lumphash; // hash for lumpname WITHOUT all caps + UINT32 lumphash; + UINT32 lumpname; lumpnum_t lumpnum; } lumpnum_cache_t; @@ -519,10 +528,6 @@ static lumpinfo_t* ResGetLumpsStandalone (FILE* handle, UINT16* numlumps, const fseek(handle, 0, SEEK_SET); lumpinfo->longname = lumpinfo->fullname = strbuf_append(&lumpnamebuf, lumpname); - char *name = strdup(lumpname); - strupr(name); - lumpinfo->hash = HASH32(lumpname, strlen(name)); - free(name); *numlumps = 1; return lumpinfo; @@ -542,6 +547,8 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen filelump_t *fileinfo; void *fileinfov; + char shortname[9] = {0}; + // read the header if (fread(&header, 1, sizeof header, handle) < sizeof header) { @@ -653,6 +660,13 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen else namelen = strlen(trimname); + if (namelen > MAXLUMPNAME) + { + CONS_Alert(CONS_ERROR, "Lumpname length %zu is longer than the limit (%d)", namelen, MAXLUMPNAME); + free(fileinfov); + return NULL; + } + // Allocate the lump's long and full name (save on memory). char *name = strdup(trimname); name[namelen-1] = '\0'; @@ -660,9 +674,6 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen CONS_Debug(DBG_SETUP, "WADNAME handling:\n -- path %s\n -- interpreted lumpname %s\n", filename, name); - // Grab the hash from the first part - strupr(name); - lump_p->hash = HASH32(name, strlen(name)); free(name); wadnamelump = i | (numwadfiles << 16); @@ -670,14 +681,8 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen else { // Allocate the lump's long and full name (save on memory). - char name[9]; - strncpy(name, fileinfo->name, 8); - name[8] = '\0'; - lump_p->longname = lump_p->fullname = strbuf_append(&lumpnamebuf, name); - - // Set up true hash - strupr(name); - lump_p->hash = HASH32(name, strlen(name)); + memcpy(shortname, fileinfo->name, 8); + lump_p->longname = lump_p->fullname = strbuf_append(&lumpnamebuf, shortname); } } free(fileinfov); @@ -723,6 +728,8 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) lumpinfo_t *lump_p; size_t i; + char fullname[MAXLUMPNAME+1] = {0}; + char pat_central[] = {0x50, 0x4b, 0x01, 0x02, 0x00}; char pat_end[] = {0x50, 0x4b, 0x05, 0x06, 0x00}; @@ -748,7 +755,6 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) fseek(handle, zend.cdiroffset, SEEK_SET); for (i = 0; i < numlumps; i++, lump_p++) { - char* fullname; char* trimname; char* dotpos; @@ -769,12 +775,16 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) lump_p->disksize = zentry.compsize; lump_p->size = zentry.size; - fullname = malloc(zentry.namelen + 1); + if (zentry.namelen > MAXLUMPNAME) + { + CONS_Alert(CONS_ERROR, "Lumpname length %hu is longer than the limit (%d)", zentry.namelen, MAXLUMPNAME); + return NULL; + } + if (fgets(fullname, zentry.namelen + 1, handle) != fullname) { CONS_Alert(CONS_ERROR, "Unable to read lumpname (%s)\n", M_FileError(handle)); Z_Free(lumpinfo); - free(fullname); return NULL; } @@ -790,8 +800,6 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) lump_p->fullname = strbuf_append(&lumpnamebuf, fullname); trimname[dotpos - trimname] = '\0'; lump_p->longname = strbuf_append(&lumpnamebuf, trimname); - strupr(trimname); - lump_p->hash = HASH32(trimname, strlen(trimname)); switch(zentry.compression) { @@ -812,8 +820,6 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) break; } - free(fullname); - // skip and ignore comments/extra fields if (fseek(handle, zentry.xtralen + zentry.commlen, SEEK_CUR) != 0) { @@ -876,6 +882,7 @@ UINT16 W_InitFile(const char *filename, boolean mainfile, boolean startup, wadco UINT16 numlumps = 0; UINT64 filehash = 0; int important; + size_t i; if (!(refreshdirmenu & REFRESHDIR_ADDFILE)) refreshdirmenu = REFRESHDIR_NORMAL|REFRESHDIR_ADDFILE; // clean out cons_alerts that happened earlier @@ -924,7 +931,7 @@ UINT16 W_InitFile(const char *filename, boolean mainfile, boolean startup, wadco if (!W_MakeFileHash(filename, false, &filehash)) filehash = 0; - for (size_t i = 0; i < numwadfiles; i++) + for (i = 0; i < numwadfiles; i++) { if (wadfiles[i]->hash == filehash) { @@ -989,6 +996,20 @@ UINT16 W_InitFile(const char *filename, boolean mainfile, boolean startup, wadco Z_Calloc(numlumps * sizeof (*wadfile->lumpcache), PU_STATIC, &wadfile->lumpcache); Z_Calloc(numlumps * sizeof (*wadfile->patchcache), PU_STATIC, &wadfile->patchcache); + // + // hash the lump names + // + Z_Malloc(numlumps*sizeof(UINT32), PU_STATIC, &wadfile->lumphashes); + for (i = 0; i < numlumps; i++, lumpinfo++) + { + char uppername[MAXLUMPNAME+1]; + char *p = uppername; + const char *longname = strbuf_get(lumpnamebuf, lumpinfo->longname); + while (*longname) + *p++ = toupper(*longname++); + wadfile->lumphashes[i] = HASH32(uppername, p - uppername); + } + // // add the wadfile // @@ -1153,60 +1174,26 @@ UINT16 W_FindNextEmptyInPwad(UINT16 wad, UINT16 startlump) return LUMPERROR; } -// Get a map marker for WADs, and a standalone WAD file lump inside PK3s. -UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 startlump) +// the raw, low-level hot loop for finding lumps +// inc, cmp, je, cmp, jne... inc, cmp, je, cmp, jne... +static UINT16 LumpNum(const char *name, UINT16 wad, UINT16 startlump, UINT16 endlump, UINT32 hash) { - UINT16 i, end; - - if (wadfiles[wad]->type == RET_WAD) - { - for (i = startlump; i < wadfiles[wad]->numlumps; i++) - { - // Not the hash? - if ((wadfiles[wad]->lumpinfo + i)->hash != hash) - continue; - - // Not the name? (always use longname, even in wads, to accomodate WADNAME) - if (strcasecmp(name, W_CheckNameForNumPwad(wad, i))) - continue; - - // Not a header? - if (W_LumpLength(i | (wad << 16)) > 0) - continue; - + size_t i; + UINT32 *hashes = wadfiles[wad]->lumphashes; + for (i = startlump; i < endlump; i++) + if (hashes[i] == hash && !strcmp(name, W_CheckNameForNumPwad(wad, i))) return i; - } - } - else if (wadfiles[wad]->type == RET_PK3) - { - i = W_CheckNumForFolderStartPK3("maps/", wad, startlump); - - if (i != LUMPERROR) - { - end = W_CheckNumForFolderEndPK3("maps/", wad, i); - - // Now look for the specified map. - for (; i < end; i++) - { - // Not the hash? - if ((wadfiles[wad]->lumpinfo + i)->hash != hash) - continue; - - // Not the name? - if (strcasecmp(name, W_CheckNameForNumPwad(wad, i))) - continue; - -#if 0 - // Not a .wad? - if (!W_IsLumpWad(i | (wad << 16))) - continue; -#endif - - return i; - } - } - } + return LUMPERROR; +} +// also comes in case-insensitive flavor +static UINT16 LumpNumCase(const char *name, UINT16 wad, UINT16 startlump, UINT16 endlump, UINT32 hash) +{ + size_t i; + UINT32 *hashes = wadfiles[wad]->lumphashes; + for (i = startlump; i < endlump; i++) + if (hashes[i] == hash && !strcasecmp(name, W_CheckNameForNumPwad(wad, i))) + return i; return LUMPERROR; } @@ -1219,32 +1206,19 @@ UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 st // UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) { - UINT16 i; char uname[MAXLUMPNAME+1]; UINT32 hash; - size_t namelen = min(MAXLUMPNAME, strlen(name)); - lumpinfo_t *lump_p; + char *up = uname; if (!TestValidLump(wad, startlump)) return LUMPERROR; - memcpy(uname, name, namelen); - uname[namelen] = '\0'; - strupr(uname); - hash = HASH32(uname, namelen); + while (*name) + *up++ = toupper(*name++); + *up = '\0'; + hash = HASH32(uname, up - uname); - // - // scan forward - // start at 'startlump', useful parameter when there are multiple - // resources with the same name - // - lump_p = wadfiles[wad]->lumpinfo + startlump; - for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) - if (lump_p->hash == hash && !strcmp(strbuf_get(lumpnamebuf, lump_p->longname), uname)) - return i; - - // not found. - return LUMPERROR; + return LumpNum(uname, wad, startlump, wadfiles[wad]->numlumps, hash); } // same as W_CheckNumForNamePwad, but only checks for a name PREFIX @@ -1345,7 +1319,9 @@ lumpnum_t W_CheckNumForLongName(const char *name) { INT32 i; UINT32 hash; - lumpnum_t check; + UINT16 check; + char uname[MAXLUMPNAME+1]; + char *up = uname; if (name == NULL) return LUMPERROR; @@ -1353,26 +1329,27 @@ lumpnum_t W_CheckNumForLongName(const char *name) if (!*name) // some doofus gave us an empty string? return LUMPERROR; + while (*name) + *up++ = toupper(*name++); + *up = '\0'; + hash = HASH32(uname, up - uname); + // Check the lumpnumcache first. Loop backwards so that we check // most recent entries first - if (strlen(name) < LUMPNUMCACHENAME) + for (i = lumpnumcacheindex + LUMPNUMCACHESIZE; i > lumpnumcacheindex; i--) { - hash = HASH32(name, strlen(name)); - for (i = lumpnumcacheindex + LUMPNUMCACHESIZE; i > lumpnumcacheindex; i--) + if (lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumphash == hash + && !strcmp(strbuf_get(lumpnamebuf, lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumpname), uname)) { - if (lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumphash == hash - && !strcmp(lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumpname, name)) - { - lumpnumcacheindex = i & (LUMPNUMCACHESIZE - 1); - return lumpnumcache[lumpnumcacheindex].lumpnum; - } + lumpnumcacheindex = i & (LUMPNUMCACHESIZE - 1); + return lumpnumcache[lumpnumcacheindex].lumpnum; } } // scan wad files backwards so patch lump files take precedence for (i = numwadfiles - 1; i >= 0; i--) { - check = W_CheckNumForLongNamePwad(name, i, 0); + check = LumpNum(uname, i, 0, wadfiles[i]->numlumps, hash); if (check != LUMPERROR) break; // found it } @@ -1380,15 +1357,11 @@ lumpnum_t W_CheckNumForLongName(const char *name) if (check == LUMPERROR) return LUMPERROR; - if (strlen(name) < LUMPNUMCACHENAME) - { - // Update the cache. - lumpnumcacheindex = (lumpnumcacheindex + 1) & (LUMPNUMCACHESIZE - 1); - memset(lumpnumcache[lumpnumcacheindex].lumpname, '\0', LUMPNUMCACHENAME); - strlcpy(lumpnumcache[lumpnumcacheindex].lumpname, name, LUMPNUMCACHENAME); - lumpnumcache[lumpnumcacheindex].lumpnum = (i << 16) + check; - lumpnumcache[lumpnumcacheindex].lumphash = hash; - } + // Update the cache. + lumpnumcacheindex = (lumpnumcacheindex + 1) & (LUMPNUMCACHESIZE - 1); + lumpnumcache[lumpnumcacheindex].lumpname = wadfiles[i]->lumpinfo[check].longname; + lumpnumcache[lumpnumcacheindex].lumpnum = (i << 16) + check; + lumpnumcache[lumpnumcacheindex].lumphash = hash; return (i << 16) + check; } @@ -1397,53 +1370,72 @@ lumpnum_t W_CheckNumForLongName(const char *name) // Get a map marker for WADs, and a standalone WAD file lump inside PK3s. lumpnum_t W_CheckNumForMap(const char *name) { - lumpnum_t check = LUMPERROR; - UINT32 uhash, hash = HASH32(name, min(strlen(name), LUMPNUMCACHENAME)); INT32 i; + UINT32 hash; + UINT16 check; + char uname[MAXLUMPNAME+1]; + char *up = uname; + + while (*name) + *up++ = toupper(*name++); + *up = '\0'; + hash = HASH32(uname, up - uname); // Check the lumpnumcache first. Loop backwards so that we check // most recent entries first for (i = lumpnumcacheindex + LUMPNUMCACHESIZE; i > lumpnumcacheindex; i--) { if (lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumphash == hash - && strcasecmp(lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumpname, name) == 0) + && !strcmp(strbuf_get(lumpnamebuf, lumpnumcache[i & (LUMPNUMCACHESIZE - 1)].lumpname), uname)) { lumpnumcacheindex = i & (LUMPNUMCACHESIZE - 1); return lumpnumcache[lumpnumcacheindex].lumpnum; } } - char *uname = strdup(name); - strupr(uname); - uhash = HASH32(name, strlen(uname)); - free(uname); - for (i = numwadfiles - 1; i >= 0; i--) { - check = W_CheckNumForMapPwad(name, uhash, (UINT16)i, 0); + UINT16 start, end; + switch (wadfiles[i]->type) + { + case RET_PK3: + start = W_CheckNumForFolderStartPK3("Maps/", i, 0); + end = W_CheckNumForFolderEndPK3("Maps/", i, start); + break; + default: + start = 0; + end = wadfiles[i]->numlumps; + break; + } - if (check != LUMPERROR) + if (start == LUMPERROR || end == LUMPERROR) + continue; + + retry: + check = LumpNumCase(uname, i, start, end, hash); + + if (check == LUMPERROR) + continue; + + // valid map marker? + if (W_IsLumpWad(check | (i << 16)) || !W_LumpLengthPwad(i, check)) break; // found it + + // keep looking in this wad + start = check + 1; + goto retry; } if (check == LUMPERROR) - { return LUMPERROR; - } - else - { - if (strlen(name) < LUMPNUMCACHENAME) - { - // Update the cache. - lumpnumcacheindex = (lumpnumcacheindex + 1) & (LUMPNUMCACHESIZE - 1); - memset(lumpnumcache[lumpnumcacheindex].lumpname, '\0', LUMPNUMCACHENAME); - strlcpy(lumpnumcache[lumpnumcacheindex].lumpname, name, LUMPNUMCACHENAME); - lumpnumcache[lumpnumcacheindex].lumpnum = (i << 16) + check; - lumpnumcache[lumpnumcacheindex].lumphash = hash; - } - return (i << 16) + check; - } + // Update the cache. + lumpnumcacheindex = (lumpnumcacheindex + 1) & (LUMPNUMCACHESIZE - 1); + lumpnumcache[lumpnumcacheindex].lumpname = wadfiles[i]->lumpinfo[check].longname; + lumpnumcache[lumpnumcacheindex].lumpnum = (i << 16) + check; + lumpnumcache[lumpnumcacheindex].lumphash = hash; + + return (i << 16) + check; } // diff --git a/src/w_wad.h b/src/w_wad.h index 8f7491c16..f73d7d4cb 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -22,17 +22,6 @@ extern "C" { #endif -// Available compression methods for lumps. -typedef enum -{ - CM_NOCOMPRESSION, -#ifdef HAVE_ZLIB - CM_DEFLATE, -#endif - CM_LZF, - CM_UNSUPPORTED -} compmethod; - // ========================================================================= // 'VIRTUAL' RESOURCES // ========================================================================= @@ -79,6 +68,7 @@ struct wadfile_t { char *filename; restype_t type; + UINT32 *lumphashes; // hashes for every lump's fullname in ALL CAPS lumpinfo_t *lumpinfo; lumpcache_t *lumpcache; lumpcache_t *patchcache; @@ -133,7 +123,6 @@ const char *W_CheckFullNameForNum(lumpnum_t lumpnum); UINT16 W_FindNextEmptyInPwad(UINT16 wad, UINT16 startlump); // checks only in one pwad -UINT16 W_CheckNumForMapPwad(const char *name, UINT32 hash, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForNamePrefixPwad(const char *name, size_t namelen, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForNameMultiPrefixPwad(const lumpprefixes_t *prefixes, size_t numprefixes, UINT16 wad, UINT16 startlump); From 286dfc7a43c362aea14e07e76bb840e0599cb1bf Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Thu, 26 Jun 2025 22:43:52 +0200 Subject: [PATCH 15/18] Add strbuf_append_m to simplify lumpname reading --- src/strbuf.c | 24 ++++++++++++++++++++++++ src/strbuf.h | 1 + src/w_wad.c | 30 +++++++++++------------------- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/strbuf.c b/src/strbuf.c index 89987e377..35bdd3869 100644 --- a/src/strbuf.c +++ b/src/strbuf.c @@ -47,3 +47,27 @@ UINT32 strbuf_append(strbuf_t **strbuf, const char *str) strcpy(buf->buf - sizeof(UINT32) + oldsize, str); return oldsize; } + +// appends a fixed-size array of bytes to the buffer, and inserts a null terminator +// useful if you know the length of a string in advance, or want to truncate it to a certain length +// returns an offset to the newly appended bytes +UINT32 strbuf_append_m(strbuf_t **strbuf, const void *src, UINT32 len) +{ + strbuf_t *buf = *strbuf; + size_t addbytes = len + 1; + UINT32 oldsize = buf->size; + + buf->size += addbytes; + if ((oldsize % STRBUF_BLOCKSIZE) + addbytes >= STRBUF_BLOCKSIZE) { + UINT32 newsize = buf->size + STRBUF_BLOCKSIZE - (buf->size % STRBUF_BLOCKSIZE); + if (newsize <= oldsize) + I_Error("String buffer size wrapped around!?"); + buf = *strbuf = Z_Realloc(buf, newsize, PU_STATIC, NULL); + if (!buf) + I_Error("Failed to allocate string buffer"); + } + + memcpy(buf->buf - sizeof(UINT32) + oldsize, src, len); + buf->buf[-sizeof(UINT32) + oldsize + len] = '\0'; + return oldsize; +} diff --git a/src/strbuf.h b/src/strbuf.h index 85bef9860..f805ae65d 100644 --- a/src/strbuf.h +++ b/src/strbuf.h @@ -22,6 +22,7 @@ struct strbuf_t strbuf_t *strbuf_alloc(void); UINT32 strbuf_append(strbuf_t **strbuf, const char *str); +UINT32 strbuf_append_m(strbuf_t **strbuf, const void *src, UINT32 len); // returns the string at the given offset FUNCINLINE static ATTRINLINE char *strbuf_get(strbuf_t *strbuf, UINT32 ofs) diff --git a/src/w_wad.c b/src/w_wad.c index 36807ca98..2eff63e77 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -547,8 +547,6 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen filelump_t *fileinfo; void *fileinfov; - char shortname[9] = {0}; - // read the header if (fread(&header, 1, sizeof header, handle) < sizeof header) { @@ -649,14 +647,14 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen } #endif // Strip away file address - if (trimname != 0) + if (trimname != NULL) trimname++; else trimname = filename; // Care taken for root files. // First stop, not last, to permit RR_GREENHILLS.beta3.wad - if ((dotpos = strchr(trimname, '.')) != 0) - namelen = (dotpos + 1 - trimname); + if ((dotpos = strchr(trimname, '.')) != NULL) + namelen = dotpos - trimname; else namelen = strlen(trimname); @@ -664,26 +662,20 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen { CONS_Alert(CONS_ERROR, "Lumpname length %zu is longer than the limit (%d)", namelen, MAXLUMPNAME); free(fileinfov); + Z_Free(lumpinfo); return NULL; } // Allocate the lump's long and full name (save on memory). - char *name = strdup(trimname); - name[namelen-1] = '\0'; - lump_p->longname = lump_p->fullname = strbuf_append(&lumpnamebuf, name); + lump_p->longname = lump_p->fullname = strbuf_append_m(&lumpnamebuf, trimname, namelen); - CONS_Debug(DBG_SETUP, "WADNAME handling:\n -- path %s\n -- interpreted lumpname %s\n", filename, name); - - free(name); + CONS_Debug(DBG_SETUP, "WADNAME handling:\n -- path %s\n -- interpreted lumpname %s\n", filename, strbuf_get(lumpnamebuf, lump_p->longname)); wadnamelump = i | (numwadfiles << 16); } else - { // Allocate the lump's long and full name (save on memory). - memcpy(shortname, fileinfo->name, 8); - lump_p->longname = lump_p->fullname = strbuf_append(&lumpnamebuf, shortname); - } + lump_p->longname = lump_p->fullname = strbuf_append_m(&lumpnamebuf, fileinfo->name, strnlen(fileinfo->name, 8)); } free(fileinfov); *nlmp = numlumps; @@ -778,6 +770,7 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) if (zentry.namelen > MAXLUMPNAME) { CONS_Alert(CONS_ERROR, "Lumpname length %hu is longer than the limit (%d)", zentry.namelen, MAXLUMPNAME); + Z_Free(lumpinfo); return NULL; } @@ -789,17 +782,16 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) } // Strip away file address and extension for the 8char name. - if ((trimname = strrchr(fullname, '/')) != 0) + if ((trimname = strrchr(fullname, '/')) != NULL) trimname++; else trimname = fullname; // Care taken for root files. - if ((dotpos = strrchr(trimname, '.')) == 0) + if ((dotpos = strrchr(trimname, '.')) == NULL) dotpos = fullname + strlen(fullname); // Watch for files without extension. lump_p->fullname = strbuf_append(&lumpnamebuf, fullname); - trimname[dotpos - trimname] = '\0'; - lump_p->longname = strbuf_append(&lumpnamebuf, trimname); + lump_p->longname = strbuf_append_m(&lumpnamebuf, trimname, dotpos - trimname); switch(zentry.compression) { From be3592684379ef553b2c4ae2e0e233e85e98b5c9 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Fri, 27 Jun 2025 16:32:25 +0200 Subject: [PATCH 16/18] Add W_CheckNumForMarkers, clean up lump validation Fixes many cases of invalid startlumps, so validation is now actually done only when PARANOIA is defined The W_CheckNumFor* functions allow startlump == numlumps, to make iteration less frustrating --- src/p_setup.c | 43 +++---- src/r_skins.c | 4 +- src/r_textures.c | 298 +++++++++++++++++++++-------------------------- src/r_things.cpp | 43 ++----- src/w_wad.c | 217 +++++++++++++++------------------- src/w_wad.h | 4 +- 6 files changed, 253 insertions(+), 356 deletions(-) diff --git a/src/p_setup.c b/src/p_setup.c index 08f5564b4..c650b7954 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -9027,11 +9027,14 @@ boolean P_RunSOC(const char *socfilename) // Auxiliary function for PK3 loading - looks for sound replacements. // NOTE: it does not really add any new sound entry or anything. -static void P_LoadSoundsRange(UINT16 wadnum, UINT16 first, UINT16 end, size_t *sreplaces) +static void P_LoadSoundsRange(UINT16 wadnum, size_t *sreplaces) { sfxenum_t j; - UINT16 i; - for (i = first; i < end; i++) + UINT16 i, end; + if (!W_CheckNumForMarkers((const char *[]){"Sounds/"}, 1, wadnum, &i, &end)) + return; + + for (; i < end; i++) { // Let's check whether it's replacing an existing sound or it's a brand new one. const char *name = W_CheckNameForNumPwad(wadnum, i); @@ -9055,10 +9058,13 @@ static void P_LoadSoundsRange(UINT16 wadnum, UINT16 first, UINT16 end, size_t *s // Auxiliary function for PK3 loading - looks for music and music replacements. // NOTE: does nothing but print debug messages. The code is handled somewhere else. -static void P_LoadMusicsRange(UINT16 wadnum, UINT16 first, UINT16 end, size_t *digmreplaces, size_t *mreplaces) +static void P_LoadMusicsRange(UINT16 wadnum, size_t *digmreplaces, size_t *mreplaces) { - UINT16 i; - for (i = first; i < end; i++) + UINT16 i, end; + if (!W_CheckNumForMarkers((const char *[]){"Music/"}, 1, wadnum, &i, &end)) + return; + + for (; i < end; i++) { const char *name = W_CheckNameForNumPwad(wadnum, i); if (name[0] == 'O' && name[1] == '_') @@ -9236,12 +9242,6 @@ UINT16 P_PartialAddWadFile(const char *wadfilename, wadcompat_t compat) size_t sreplaces = 0, mreplaces = 0, digmreplaces = 0; UINT16 numlumps, wadnum; - // Vars to help us with the position start and amount of each resource type. - // Useful for PK3s since they use folders. - // WADs use markers for some resources, but others such as sounds are checked lump-by-lump anyway. - UINT16 sfxPos, sfxEnd; - UINT16 musPos, musEnd; - // Init file. if ((numlumps = W_InitFile(wadfilename, false, false, compat)) == LUMPERROR) { @@ -9261,23 +9261,8 @@ UINT16 P_PartialAddWadFile(const char *wadfilename, wadcompat_t compat) } partadd_stage = 0; - switch(wadfiles[wadnum]->type) - { - case RET_PK3: - sfxPos = W_CheckNumForFolderStartPK3("Sounds/", wadnum, 0); - sfxEnd = W_CheckNumForFolderEndPK3("Sounds/", wadnum, sfxPos); - musPos = W_CheckNumForFolderStartPK3("Music/", wadnum, 0); - musEnd = W_CheckNumForFolderEndPK3("Music/", wadnum, musPos); - break; - - default: - sfxPos = musPos = 0; - sfxEnd = musEnd = numlumps; - break; - } - - P_LoadSoundsRange(wadnum, sfxPos, sfxEnd, &sreplaces); - P_LoadMusicsRange(wadnum, musPos, musEnd, &digmreplaces, &mreplaces); + P_LoadSoundsRange(wadnum, &sreplaces); + P_LoadMusicsRange(wadnum, &digmreplaces, &mreplaces); if (!devparm && sreplaces) CONS_Printf(M_GetText("%s sounds replaced\n"), sizeu1(sreplaces)); diff --git a/src/r_skins.c b/src/r_skins.c index cb6031ab1..ff05f1655 100644 --- a/src/r_skins.c +++ b/src/r_skins.c @@ -648,7 +648,7 @@ void R_AddSkins(UINT16 wadnum) // search for all skin markers in pwad // - while ((lump = W_CheckNumForNamePrefixPwad("S_SKIN", 6, wadnum, lastlump)) != LUMPERROR) + while (lastlump != LUMPERROR && (lump = W_CheckNumForNamePrefixPwad("S_SKIN", 6, wadnum, lastlump)) != LUMPERROR) { // advance by default lastlump = lump + 1; @@ -820,7 +820,7 @@ void R_PatchSkins(UINT16 wadnum) // search for all skin patch markers in pwad // - while ((lump = W_CheckNumForNamePrefixPwad("P_SKIN", 6, wadnum, lastlump)) != LUMPERROR) + while (lastlump != LUMPERROR && (lump = W_CheckNumForNamePrefixPwad("P_SKIN", 6, wadnum, lastlump)) != LUMPERROR) { INT32 skinnum = 0; diff --git a/src/r_textures.c b/src/r_textures.c index 1c9f13759..1d1020f5a 100644 --- a/src/r_textures.c +++ b/src/r_textures.c @@ -1117,6 +1117,11 @@ void R_FlushTextureCache(void) int R_CountTexturesInTEXTURESLump(UINT16 wadNum, UINT16 lumpNum); void R_ParseTEXTURESLump(UINT16 wadNum, UINT16 lumpNum, INT32 *index); +static const char *flatmarkers[] = { + "Flats/", + "F_START", "F_END", +}; + static INT32 Rloadflats (INT32 i, INT32 w) { @@ -1126,81 +1131,74 @@ Rloadflats (INT32 i, INT32 w) texpatch_t *patch; UINT8 header[PNG_HEADER_SIZE]; - if (wadfiles[w]->type == RET_PK3) - { - texstart = W_CheckNumForFolderStartPK3("flats/", (UINT16)w, 0); - texend = W_CheckNumForFolderEndPK3("flats/", (UINT16)w, texstart); - } - else - { - texstart = W_CheckNumForLongNamePwad("F_START", (UINT16)w, 0); - if (texstart != LUMPERROR) texstart++; - texend = W_CheckNumForLongNamePwad("F_END", (UINT16)w, texstart); - } + if (!W_CheckNumForMarkers(flatmarkers, sizeof(flatmarkers)/sizeof(*flatmarkers), (UINT16)w, &texstart, &texend)) + return i; - if (!( texstart == LUMPERROR || texend == LUMPERROR )) + // Work through each lump between the markers in the WAD. + for (j = 0; j < (texend - texstart); j++) { - // Work through each lump between the markers in the WAD. - for (j = 0; j < (texend - texstart); j++) + UINT16 wadnum = (UINT16)w; + lumpnum_t lumpnum = texstart + j; + + if (wadfiles[w]->type == RET_PK3) { - UINT16 wadnum = (UINT16)w; - lumpnum_t lumpnum = texstart + j; + if (W_IsLumpFolder(wadnum, lumpnum)) // Check if lump is a folder + continue; // If it is then SKIP IT + } - if (wadfiles[w]->type == RET_PK3) - { - if (W_IsLumpFolder(wadnum, lumpnum)) // Check if lump is a folder - continue; // If it is then SKIP IT - } + size_t lumplength = W_LumpLengthPwad(wadnum, lumpnum); + size_t flatsize = R_FlatDimensionsFromLumpSize(lumplength); - size_t lumplength = W_LumpLengthPwad(wadnum, lumpnum); - size_t flatsize = R_FlatDimensionsFromLumpSize(lumplength); + //CONS_Printf("\n\"%s\" is a flat, dimensions %d x %d",W_CheckNameForNumPwad((UINT16)w,texstart+j),flatsize,flatsize); + texture = textures[i] = Z_Calloc(sizeof(texture_t) + sizeof(texpatch_t), PU_STATIC, NULL); - //CONS_Printf("\n\"%s\" is a flat, dimensions %d x %d",W_CheckNameForNumPwad((UINT16)w,texstart+j),flatsize,flatsize); - texture = textures[i] = Z_Calloc(sizeof(texture_t) + sizeof(texpatch_t), PU_STATIC, NULL); - - // Set texture properties. - M_Memcpy(texture->name, W_CheckNameForNumPwad(wadnum, lumpnum), sizeof(texture->name)); - texture->hash = quickncasehash(texture->name, 8); + // Set texture properties. + M_Memcpy(texture->name, W_CheckNameForNumPwad(wadnum, lumpnum), sizeof(texture->name)); + texture->hash = quickncasehash(texture->name, 8); #ifndef NO_PNG_LUMPS - W_ReadLumpHeaderPwad(wadnum, lumpnum, header, sizeof header, 0); + W_ReadLumpHeaderPwad(wadnum, lumpnum, header, sizeof header, 0); - if (Picture_IsLumpPNG(header, lumplength)) - { - UINT8 *flatlump = W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE); - INT32 width, height; - Picture_PNGDimensions((UINT8 *)flatlump, &width, &height, NULL, NULL, lumplength); - texture->width = (INT16)width; - texture->height = (INT16)height; - Z_Free(flatlump); - } - else -#endif - texture->width = texture->height = flatsize; - - texture->type = TEXTURETYPE_FLAT; - texture->patchcount = 1; - texture->holes = false; - texture->flip = 0; - texture->terrainID = K_GetTerrainIDForTextureName(texture->name); - - // Allocate information for the texture's patches. - patch = &texture->patches[0]; - - patch->originx = patch->originy = 0; - patch->wad = (UINT16)w; - patch->lump = texstart + j; - patch->flip = 0; - - texturewidth[i] = texture->width; - textureheight[i] = texture->height << FRACBITS; - i++; + if (Picture_IsLumpPNG(header, lumplength)) + { + UINT8 *flatlump = W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE); + INT32 width, height; + Picture_PNGDimensions((UINT8 *)flatlump, &width, &height, NULL, NULL, lumplength); + texture->width = (INT16)width; + texture->height = (INT16)height; + Z_Free(flatlump); } + else +#endif + texture->width = texture->height = flatsize; + + texture->type = TEXTURETYPE_FLAT; + texture->patchcount = 1; + texture->holes = false; + texture->flip = 0; + texture->terrainID = K_GetTerrainIDForTextureName(texture->name); + + // Allocate information for the texture's patches. + patch = &texture->patches[0]; + + patch->originx = patch->originy = 0; + patch->wad = (UINT16)w; + patch->lump = texstart + j; + patch->flip = 0; + + texturewidth[i] = texture->width; + textureheight[i] = texture->height << FRACBITS; + i++; } return i; } +static const char *texturemarkers[] = { + "Textures/", + "TX_START", "TX_END", +}; + static INT32 Rloadtextures (INT32 i, INT32 w) { @@ -1211,136 +1209,108 @@ Rloadtextures (INT32 i, INT32 w) softwarepatch_t patchlump; // Get the lump numbers for the markers in the WAD, if they exist. + if (!W_CheckNumForMarkers(texturemarkers, sizeof(texturemarkers)/sizeof(*texturemarkers), (UINT16)w, &texstart, &texend)) + return i; + if (wadfiles[w]->type == RET_PK3) - { - texstart = W_CheckNumForFolderStartPK3("textures/", (UINT16)w, 0); - texend = W_CheckNumForFolderEndPK3("textures/", (UINT16)w, texstart); - texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", (UINT16)w, 0); - while (texturesLumpPos != LUMPERROR) - { + for (texturesLumpPos = 0; (texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", (UINT16)w, texturesLumpPos)) != LUMPERROR; texturesLumpPos++) R_ParseTEXTURESLump(w, texturesLumpPos, &i); - texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", (UINT16)w, texturesLumpPos + 1); - } - } else { - texstart = W_CheckNumForLongNamePwad("TX_START", (UINT16)w, 0); - if (texstart != LUMPERROR) texstart++; - texend = W_CheckNumForLongNamePwad("TX_END", (UINT16)w, 0); texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", (UINT16)w, 0); if (texturesLumpPos != LUMPERROR) R_ParseTEXTURESLump(w, texturesLumpPos, &i); } - if (!( texstart == LUMPERROR || texend == LUMPERROR )) + // Work through each lump between the markers in the WAD. + for (j = 0; j < (texend - texstart); j++) { - // Work through each lump between the markers in the WAD. - for (j = 0; j < (texend - texstart); j++) + UINT16 wadnum = (UINT16)w; + lumpnum_t lumpnum = texstart + j; +#ifndef NO_PNG_LUMPS + size_t lumplength; +#endif + + if (wadfiles[w]->type == RET_PK3) { - UINT16 wadnum = (UINT16)w; - lumpnum_t lumpnum = texstart + j; -#ifndef NO_PNG_LUMPS - size_t lumplength; -#endif - - if (wadfiles[w]->type == RET_PK3) - { - if (W_IsLumpFolder(wadnum, lumpnum)) // Check if lump is a folder - continue; // If it is then SKIP IT - } - - W_ReadLumpHeaderPwad(wadnum, lumpnum, &patchlump, PNG_HEADER_SIZE, 0); -#ifndef NO_PNG_LUMPS - lumplength = W_LumpLengthPwad(wadnum, lumpnum); -#endif - - //CONS_Printf("\n\"%s\" is a single patch, dimensions %d x %d",W_CheckNameForNumPwad((UINT16)w,texstart+j),patchlump->width, patchlump->height); - texture = textures[i] = Z_Calloc(sizeof(texture_t) + sizeof(texpatch_t), PU_STATIC, NULL); - - // Set texture properties. - M_Memcpy(texture->name, W_CheckNameForNumPwad(wadnum, lumpnum), sizeof(texture->name)); - texture->hash = quickncasehash(texture->name, 8); - -#ifndef NO_PNG_LUMPS - if (Picture_IsLumpPNG((UINT8 *)&patchlump, lumplength)) - { - UINT8 *png = W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE); - INT32 width, height; - Picture_PNGDimensions(png, &width, &height, NULL, NULL, lumplength); - texture->width = (INT16)width; - texture->height = (INT16)height; - Z_Free(png); - } - else -#endif - { - texture->width = SHORT(patchlump.width); - texture->height = SHORT(patchlump.height); - } - - texture->type = TEXTURETYPE_SINGLEPATCH; - texture->patchcount = 1; - texture->holes = false; - texture->flip = 0; - texture->terrainID = K_GetTerrainIDForTextureName(texture->name); - - // Allocate information for the texture's patches. - patch = &texture->patches[0]; - - patch->originx = patch->originy = 0; - patch->wad = (UINT16)w; - patch->lump = texstart + j; - patch->flip = 0; - - texturewidth[i] = texture->width; - textureheight[i] = texture->height << FRACBITS; - i++; + if (W_IsLumpFolder(wadnum, lumpnum)) // Check if lump is a folder + continue; // If it is then SKIP IT } + + W_ReadLumpHeaderPwad(wadnum, lumpnum, &patchlump, PNG_HEADER_SIZE, 0); +#ifndef NO_PNG_LUMPS + lumplength = W_LumpLengthPwad(wadnum, lumpnum); +#endif + + //CONS_Printf("\n\"%s\" is a single patch, dimensions %d x %d",W_CheckNameForNumPwad((UINT16)w,texstart+j),patchlump->width, patchlump->height); + texture = textures[i] = Z_Calloc(sizeof(texture_t) + sizeof(texpatch_t), PU_STATIC, NULL); + + // Set texture properties. + M_Memcpy(texture->name, W_CheckNameForNumPwad(wadnum, lumpnum), sizeof(texture->name)); + texture->hash = quickncasehash(texture->name, 8); + +#ifndef NO_PNG_LUMPS + if (Picture_IsLumpPNG((UINT8 *)&patchlump, lumplength)) + { + UINT8 *png = W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE); + INT32 width, height; + Picture_PNGDimensions(png, &width, &height, NULL, NULL, lumplength); + texture->width = (INT16)width; + texture->height = (INT16)height; + Z_Free(png); + } + else +#endif + { + texture->width = SHORT(patchlump.width); + texture->height = SHORT(patchlump.height); + } + + texture->type = TEXTURETYPE_SINGLEPATCH; + texture->patchcount = 1; + texture->holes = false; + texture->flip = 0; + texture->terrainID = K_GetTerrainIDForTextureName(texture->name); + + // Allocate information for the texture's patches. + patch = &texture->patches[0]; + + patch->originx = patch->originy = 0; + patch->wad = (UINT16)w; + patch->lump = texstart + j; + patch->flip = 0; + + texturewidth[i] = texture->width; + textureheight[i] = texture->height << FRACBITS; + i++; } return i; } -static INT32 -count_range -( const char * marker_start, - const char * marker_end, - const char * folder, - UINT16 wadnum) +static INT32 count_range(const char *markers[], size_t nummarkers, UINT16 wadnum) { UINT16 j; UINT16 texstart, texend; INT32 count = 0; // Count flats + if (!W_CheckNumForMarkers(markers, nummarkers, wadnum, &texstart, &texend)) + return count; + + // PK3s have subfolders, so we can't just make a simple sum if (wadfiles[wadnum]->type == RET_PK3) { - texstart = W_CheckNumForFolderStartPK3(folder, wadnum, 0); - texend = W_CheckNumForFolderEndPK3(folder, wadnum, texstart); - } - else - { - texstart = W_CheckNumForLongNamePwad(marker_start, wadnum, 0); - if (texstart != LUMPERROR) texstart++; - texend = W_CheckNumForLongNamePwad(marker_end, wadnum, texstart); - } - - if (texstart != LUMPERROR && texend != LUMPERROR) - { - // PK3s have subfolders, so we can't just make a simple sum - if (wadfiles[wadnum]->type == RET_PK3) + for (j = texstart; j < texend; j++) { - for (j = texstart; j < texend; j++) - { - if (!W_IsLumpFolder(wadnum, j)) // Check if lump is a folder; if not, then count it - count++; - } - } - else // Add all the textures between markers - { - count += (texend - texstart); + if (!W_IsLumpFolder(wadnum, j)) // Check if lump is a folder; if not, then count it + count++; } } + else // Add all the textures between markers + { + count += (texend - texstart); + } return count; } @@ -1358,14 +1328,14 @@ static INT32 R_CountTextures(UINT16 wadnum) // This system will allocate memory for all duplicate/patched textures even if it never uses them, // but the alternative is to spend a ton of time checking and re-checking all previous entries just to skip any potentially patched textures. - count += count_range("F_START", "F_END", "flats/", wadnum); + count += count_range(flatmarkers, sizeof(flatmarkers)/sizeof(*flatmarkers), wadnum); // Count the textures from TEXTURES lumps for (texturesLumpPos = 0; (texturesLumpPos = W_CheckNumForLongNamePwad("TEXTURES", wadnum, texturesLumpPos)) != LUMPERROR; texturesLumpPos++) count += R_CountTexturesInTEXTURESLump(wadnum, texturesLumpPos); // Count single-patch textures - count += count_range("TX_START", "TX_END", "textures/", wadnum); + count += count_range(texturemarkers, sizeof(texturemarkers)/sizeof(*texturemarkers), wadnum); return count; } diff --git a/src/r_things.cpp b/src/r_things.cpp index d9b73aa3a..9475a6f4a 100644 --- a/src/r_things.cpp +++ b/src/r_things.cpp @@ -489,6 +489,12 @@ boolean R_AddSingleSpriteDef(const char *sprname, spritedef_t *spritedef, UINT16 return true; } +static const char *spritemarkers[] = { + "Sprites/", + "S_START", "S_END", + "SS_START", "SS_END", // deutex compatib. +}; + // // Search for sprites replacements in a wad whose names are in namelist // @@ -498,47 +504,12 @@ void R_AddSpriteDefs(UINT16 wadnum) UINT16 start, end; char wadname[MAX_WADPATH]; - // Find the sprites section in this resource file. - switch (wadfiles[wadnum]->type) - { - case RET_WAD: - start = W_CheckNumForLongNamePwad("S_START", wadnum, 0); - if (start == LUMPERROR) - start = W_CheckNumForLongNamePwad("SS_START", wadnum, 0); //deutex compatib. - if (start != LUMPERROR) start++; - - end = W_CheckNumForLongNamePwad("S_END", wadnum, start); - if (end == LUMPERROR) - end = W_CheckNumForLongNamePwad("SS_END", wadnum, start); //deutex compatib. - break; - case RET_PK3: - start = W_CheckNumForFolderStartPK3("Sprites/", wadnum, 0); - end = W_CheckNumForFolderEndPK3("Sprites/", wadnum, start); - break; - default: - return; - } - - if (start == LUMPERROR) - { - // ignore skin wads (we don't want skin sprites interfering with vanilla sprites) - // G: DON'T ignore skin wads. this check did nothing because it incorrectly checked for UINT16_MAX instead of INT16_MAX - // plus, this didn't exist in kart, so it might break old WADs... - /* - if (W_CheckNumForLongNamePwad("S_SKIN", wadnum, 0) != LUMPERROR) - return; - */ - - start = 0; //let say S_START is lump 0 - } - - if (end == LUMPERROR || start >= end) + if (!W_CheckNumForMarkers(spritemarkers, sizeof(spritemarkers)/sizeof(*spritemarkers), wadnum, &start, &end)) { CONS_Debug(DBG_SETUP, "no sprites in pwad %d\n", wadnum); return; } - // // scan through lumps, for each sprite, find all the sprite frames // diff --git a/src/w_wad.c b/src/w_wad.c index 2eff63e77..b599c271d 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -332,23 +332,15 @@ static inline void W_LoadDehackedLumpsPK3(UINT16 wadnum, boolean mainfile) { LUA_LoadLump(wadnum, posStart, true); } - else + else if (W_CheckNumForMarkers((const char *[]){"Lua/"}, 1, wadnum, &posStart, &posEnd)) { - posStart = W_CheckNumForFolderStartPK3("Lua/", wadnum, 0); - if (posStart != LUMPERROR) - { - posEnd = W_CheckNumForFolderEndPK3("Lua/", wadnum, posStart); - for (; posStart < posEnd; posStart++) - LUA_LoadLump(wadnum, posStart, true); - } + for (; posStart < posEnd; posStart++) + LUA_LoadLump(wadnum, posStart, true); } - posStart = W_CheckNumForFolderStartPK3("SOC/", wadnum, 0); - if (posStart != LUMPERROR) + if (W_CheckNumForMarkers((const char *[]){"SOC/"}, 1, wadnum, &posStart, &posEnd)) { - posEnd = W_CheckNumForFolderEndPK3("SOC/", wadnum, posStart); - - for(; posStart < posEnd; posStart++) + for (; posStart < posEnd; posStart++) { CONS_Printf(M_GetText("Loading SOC from %s|%s\n"), wadfiles[wadnum]->filename, W_CheckFullNameForNumPwad(wadnum, posStart)); DEH_LoadDehackedLumpPwad(wadnum, posStart, mainfile); @@ -1099,22 +1091,25 @@ INT32 W_InitMultipleFiles(char **filenames, boolean addons) return overallrc; } -// check for a valid lump number -// lump cannot be asserted because some callers pass LUMPERROR, -// and most W_ functions previously always passed 0 for lump... -static boolean TestValidLump(UINT16 wad, UINT16 lump) -{ - I_Assert(wad < numwadfiles); - return lump < wadfiles[wad]->numlumps; -} +#ifdef PARANOIA +#define TESTVALID(wad, lump) \ + if (wad >= numwadfiles || lump >= wadfiles[wad]->numlumps) \ + I_Error("Invalid lump number %04x:%04x", wad, lump); + +// allows one past the end of the lump array to make iteration less hellish +#define TESTVALIDEND(wad, lump) \ + if (wad >= numwadfiles || lump > wadfiles[wad]->numlumps) \ + I_Error("Invalid lump number %04x:%04x", wad, lump); +#else +#define TESTVALID(wad, lump) +#define TESTVALIDEND(wad, lump) +#endif // NOTE: this actually returns the longname, but since nothing relies on // the 8-char truncation, i thought i'd save some effort :^) const char *W_CheckNameForNumPwad(UINT16 wad, UINT16 lump) { - if (!TestValidLump(wad, lump)) - return NULL; - + TESTVALID(wad, lump) return strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[lump].longname); } @@ -1125,9 +1120,7 @@ const char *W_CheckNameForNum(lumpnum_t lumpnum) const char *W_CheckFullNameForNumPwad(UINT16 wad, UINT16 lump) { - if (!TestValidLump(wad, lump)) - return NULL; - + TESTVALID(wad, lump) return strbuf_get(lumpnamebuf, wadfiles[wad]->lumpinfo[lump].fullname); } @@ -1146,21 +1139,17 @@ UINT16 W_FindNextEmptyInPwad(UINT16 wad, UINT16 startlump) { UINT16 i; - if (!TestValidLump(wad, startlump)) - return LUMPERROR; + TESTVALIDEND(wad, startlump) // // scan forward // start at 'startlump', useful parameter when there are multiple // resources with the same name // - if (startlump < wadfiles[wad]->numlumps) - { - lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; - for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) - if (!lump_p->size) - return i; - } + lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; + for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) + if (!lump_p->size) + return i; // not found. return LUMPERROR; @@ -1202,8 +1191,7 @@ UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) UINT32 hash; char *up = uname; - if (!TestValidLump(wad, startlump)) - return LUMPERROR; + TESTVALIDEND(wad, startlump) while (*name) *up++ = toupper(*name++); @@ -1219,8 +1207,7 @@ UINT16 W_CheckNumForNamePrefixPwad(const char *name, size_t namelen, UINT16 wad, UINT16 i; lumpinfo_t *lump_p; - if (!TestValidLump(wad, startlump)) - return LUMPERROR; + TESTVALIDEND(wad, startlump) // scan forward, start at lump_p = wadfiles[wad]->lumpinfo + startlump; @@ -1238,8 +1225,7 @@ UINT16 W_CheckNumForNameMultiPrefixPwad(const lumpprefixes_t *prefixes, size_t n size_t j; lumpinfo_t *lump_p; - if (!TestValidLump(wad, startlump)) - return LUMPERROR; + TESTVALIDEND(wad, startlump) // scan forward, start at lump_p = wadfiles[wad]->lumpinfo + startlump; @@ -1251,39 +1237,50 @@ UINT16 W_CheckNumForNameMultiPrefixPwad(const lumpprefixes_t *prefixes, size_t n return LUMPERROR; // not found } -// Look for the first lump from a folder. -UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlump) +// finds the range [start:end) of lumps within the given PK3 folder or (optionally) WAD marker names +// if wadnum is a WAD but no markers are specified, yields 0 and wad->numlumps +// returns true and sets ostart/oend if lumps were found +// [0] = PK3 folder [1], [2] = WAD start/end markers [3], [4] = secondary WAD start/end markers etc... +boolean W_CheckNumForMarkers(const char *markers[], size_t nummarkers, UINT16 wad, UINT16 *ostart, UINT16 *oend) { - size_t name_length; - INT32 i; - lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; - name_length = strlen(name); - for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) + INT32 start, end; + if (wadfiles[wad]->type == RET_PK3) { - if (strnicmp(name, strbuf_get(lumpnamebuf, lump_p->fullname), name_length) == 0) + size_t name_length = strlen(markers[0]); + for (start = 0; start < wadfiles[wad]->numlumps; start++) + if (!strncasecmp(markers[0], W_CheckFullNameForNumPwad(wad, start), name_length)) + break; + if (start == wadfiles[wad]->numlumps) + return false; + for (end = start + 1; end < wadfiles[wad]->numlumps; end++) + if (strncasecmp(markers[0], W_CheckFullNameForNumPwad(wad, end), name_length)) + break; + } + else if (nummarkers == 1) + { + start = 0; + end = wadfiles[wad]->numlumps; + } + else + { + size_t i; + for (i = 1; i < nummarkers; i += 2) { - /* SLADE is special and puts a single directory entry. Skip that. */ - if (strlen(strbuf_get(lumpnamebuf, lump_p->fullname)) == name_length) - i++; + start = W_CheckNumForLongNamePwad(markers[i], wad, 0); + if (start == LUMPERROR || ++start == wadfiles[wad]->numlumps) + continue; + end = W_CheckNumForLongNamePwad(markers[i+1], wad, start); + if (end == LUMPERROR) + continue; break; } + if (i == nummarkers) + return false; } - return i; -} -// In a PK3 type of resource file, it looks for the next lumpinfo entry that doesn't share the specified pathfile. -// Useful for finding folder ends. -// Returns the position of the lumpinfo entry. -UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump) -{ - INT32 i; - lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; - for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) - { - if (strnicmp(name, strbuf_get(lumpnamebuf, lump_p->fullname), strlen(name))) - break; - } - return i; + *ostart = (UINT16)start; + *oend = (UINT16)end; + return true; } // In a PK3 type of resource file, it looks for an entry with the specified full name. @@ -1292,6 +1289,7 @@ UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump) { INT32 i; lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; + for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) { if (!strnicmp(name, strbuf_get(lumpnamebuf, lump_p->fullname), strlen(name))) @@ -1315,8 +1313,7 @@ lumpnum_t W_CheckNumForLongName(const char *name) char uname[MAXLUMPNAME+1]; char *up = uname; - if (name == NULL) - return LUMPERROR; + I_Assert(name != NULL); if (!*name) // some doofus gave us an empty string? return LUMPERROR; @@ -1388,19 +1385,7 @@ lumpnum_t W_CheckNumForMap(const char *name) for (i = numwadfiles - 1; i >= 0; i--) { UINT16 start, end; - switch (wadfiles[i]->type) - { - case RET_PK3: - start = W_CheckNumForFolderStartPK3("Maps/", i, 0); - end = W_CheckNumForFolderEndPK3("Maps/", i, start); - break; - default: - start = 0; - end = wadfiles[i]->numlumps; - break; - } - - if (start == LUMPERROR || end == LUMPERROR) + if (!W_CheckNumForMarkers((const char *[]){"Maps/"}, 1, i, &start, &end)) continue; retry: @@ -1454,31 +1439,22 @@ lumpnum_t W_GetNumForLongName(const char *name) lumpnum_t W_CheckNumForNameInFolder(const char *lump, const char *folder) { INT32 i; - lumpnum_t fsid, feid; + UINT16 fsid, feid; lumpnum_t check = LUMPERROR; // scan wad files backwards so patch lump files take precedence for (i = numwadfiles - 1; i >= 0; i--) { - if (wadfiles[i]->type == RET_PK3) + if (wadfiles[i]->type != RET_PK3) + continue; + + if (!W_CheckNumForMarkers(&folder, 1, i, &fsid, &feid)) + continue; + + check = W_CheckNumForLongNamePwad(lump, (UINT16)i, fsid); + if (check < feid) { - fsid = W_CheckNumForFolderStartPK3(folder, (UINT16)i, 0); - if (fsid == LUMPERROR) - { - continue; // Start doesn't exist? - } - - feid = W_CheckNumForFolderEndPK3(folder, (UINT16)i, fsid); - if (feid == LUMPERROR) - { - continue; // End doesn't exist? - } - - check = W_CheckNumForLongNamePwad(lump, (UINT16)i, fsid); - if (check < feid) - { - return (i<<16) + check; // found it, in our constraints - } + return (i<<16) + check; // found it, in our constraints } } @@ -1502,8 +1478,7 @@ UINT8 W_LumpExists(const char *name) size_t W_LumpLengthPwad(UINT16 wad, UINT16 lump) { - if (!TestValidLump(wad, lump)) - return 0; + TESTVALID(wad, lump) return wadfiles[wad]->lumpinfo[lump].size; } @@ -1617,8 +1592,7 @@ size_t W_ReadLumpHeaderPwad(UINT16 wad, UINT16 lump, void *dest, size_t size, si lumpinfo_t *l; FILE *handle; - if (!TestValidLump(wad,lump)) - return 0; + TESTVALID(wad, lump) lumpsize = wadfiles[wad]->lumpinfo[lump].size; // empty resource (usually markers like S_START, F_END ..) @@ -1782,8 +1756,7 @@ void *W_CacheLumpNumPwad(UINT16 wad, UINT16 lump, INT32 tag) { lumpcache_t *lumpcache; - if (!TestValidLump(wad,lump)) - return NULL; + TESTVALID(wad, lump) lumpcache = wadfiles[wad]->lumpcache; if (!lumpcache[lump]) @@ -1815,8 +1788,7 @@ void *W_CacheLumpNumForce(lumpnum_t lumpnum, INT32 tag) wad = WADFILENUM(lumpnum); lump = LUMPNUM(lumpnum); - if (!TestValidLump(wad,lump)) - return NULL; + TESTVALID(wad, lump) ptr = Z_Malloc(W_LumpLengthPwad(wad, lump), tag, NULL); W_ReadLumpHeaderPwad(wad, lump, ptr, 0, 0); // read the lump in full @@ -1835,8 +1807,7 @@ static inline boolean W_IsLumpCachedPWAD(UINT16 wad, UINT16 lump, void *ptr) { void *lcache; - if (!TestValidLump(wad, lump)) - return false; + TESTVALID(wad, lump) lcache = wadfiles[wad]->lumpcache[lump]; @@ -1867,8 +1838,7 @@ static inline boolean W_IsPatchCachedPWAD(UINT16 wad, UINT16 lump, void *ptr) { void *lcache; - if (!TestValidLump(wad, lump)) - return false; + TESTVALID(wad, lump) lcache = wadfiles[wad]->patchcache[lump]; @@ -1929,8 +1899,7 @@ void *W_CacheSoftwarePatchNumPwad(UINT16 wad, UINT16 lump, INT32 tag) { lumpcache_t *lumpcache = NULL; - if (!TestValidLump(wad, lump)) - return NULL; + TESTVALID(wad, lump) lumpcache = wadfiles[wad]->patchcache; @@ -1960,8 +1929,7 @@ void *W_CachePatchNumPwad(UINT16 wad, UINT16 lump, INT32 tag) { patch_t *patch; - if (!TestValidLump(wad, lump)) - return NULL; + TESTVALID(wad, lump) patch = W_CacheSoftwarePatchNumPwad(wad, lump, tag); @@ -2405,17 +2373,20 @@ virtres_t* vres_GetMap(lumpnum_t lumpnum) { // Count number of lumps until the end of resource OR up until next 0-length lump. lumpnum_t lumppos = lumpnum + 1; - lumpnum_t lastlump = wadfiles[WADFILENUM(lumpnum)]->numlumps; + UINT16 wad = WADFILENUM(lumpnum); + UINT16 lastlump = wadfiles[wad]->numlumps; + // or the end of directory, for PK3 files if (wadfiles[WADFILENUM(lumpnum)]->type == RET_PK3) { const char *fullname = W_CheckFullNameForNum(lumpnum); const char *dirend = strrchr(fullname, '/'); - char *dirname = malloc(dirend - fullname + 2); - strlcpy(dirname, fullname, dirend - fullname + 2); - lastlump = W_CheckNumForFolderEndPK3(dirname, WADFILENUM(lumpnum), lumpnum); - free(dirname); + size_t dirlen = dirend - fullname + 1; + for (lastlump = LUMPNUM(lumppos); lastlump < wadfiles[wad]->numlumps; lastlump++) + if (strnicmp(fullname, W_CheckFullNameForNumPwad(wad, lastlump), dirlen)) + break; } + for (i = LUMPNUM(lumppos); i < lastlump; i++, lumppos++, numlumps++) { if (W_LumpLength(lumppos) > 0) diff --git a/src/w_wad.h b/src/w_wad.h index f73d7d4cb..32436ba2f 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -127,9 +127,9 @@ UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) UINT16 W_CheckNumForNamePrefixPwad(const char *name, size_t namelen, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForNameMultiPrefixPwad(const lumpprefixes_t *prefixes, size_t numprefixes, UINT16 wad, UINT16 startlump); +boolean W_CheckNumForMarkers(const char *markers[], size_t nummarkers, UINT16 wad, UINT16 *ostart, UINT16 *oend); + UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump); -UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlump); -UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump); lumpnum_t W_CheckNumForMap(const char *name); lumpnum_t W_CheckNumForLongName(const char *name); From d6299d8394510657de8dbc4a8c7b8f0ff3d138f3 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Fri, 27 Jun 2025 18:01:32 +0200 Subject: [PATCH 17/18] Fix segfault when filename lacks extension and don't mix up fullname with longname --- src/w_wad.c | 6 +----- src/w_wad.h | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/w_wad.c b/src/w_wad.c index b599c271d..68d5a464d 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -382,11 +382,7 @@ static inline boolean CheckCompatFilename(const char *filename) static inline boolean CheckCompatExtension(const char *filename) { char *basename = strrchr(filename, '.'); - - if (!stricmp(basename+1, "KART")) - return true; - - return false; + return basename && !stricmp(basename+1, "KART"); } static inline boolean CheckCompatSkins(UINT16 wadnum) diff --git a/src/w_wad.h b/src/w_wad.h index 32436ba2f..e69c09bc3 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -68,7 +68,7 @@ struct wadfile_t { char *filename; restype_t type; - UINT32 *lumphashes; // hashes for every lump's fullname in ALL CAPS + UINT32 *lumphashes; // hashes for every lump's longname in ALL CAPS lumpinfo_t *lumpinfo; lumpcache_t *lumpcache; lumpcache_t *patchcache; From 76daf2f0e08ca93df0323a0906c9450d6eae2654 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Fri, 27 Jun 2025 23:33:59 +0200 Subject: [PATCH 18/18] Fix music names longer than 6 chars not being truncated anymore --- src/s_sound.c | 17 ++++++++--------- src/w_wad.c | 16 ++++++++++++++++ src/w_wad.h | 1 + 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/s_sound.c b/src/s_sound.c index 7a75fb947..37a232209 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1842,9 +1842,16 @@ boolean S_MusicInfo(char *mname, UINT16 *mflags, boolean *looping) return (boolean)mname[0]; } +static lumpnum_t S_GetMusicLumpNum(const char *mname) +{ + char *n = va("O_%s", mname); + strupr(n); // yep, you can do that + return W_CheckNumForNamePrefix(n, 8); +} + boolean S_MusicExists(const char *mname) { - return W_CheckNumForLongName(va("O_%s", mname)) != LUMPERROR; + return S_GetMusicLumpNum(mname) != LUMPERROR; } /// ------------------------ @@ -2178,14 +2185,6 @@ boolean S_RecallMusic(UINT16 status, boolean fromfirst) /// Music Playback /// ------------------------ -static lumpnum_t S_GetMusicLumpNum(const char *mname) -{ - if (S_MusicExists(mname)) - return W_GetNumForLongName(va("O_%s", mname)); - else - return LUMPERROR; -} - static boolean S_LoadMusic(const char *mname) { lumpnum_t mlumpnum; diff --git a/src/w_wad.c b/src/w_wad.c index 68d5a464d..f8cb1561c 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -1198,6 +1198,7 @@ UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) } // same as W_CheckNumForNamePwad, but only checks for a name PREFIX +// this does NOT uppercase the search string, do it yourself! UINT16 W_CheckNumForNamePrefixPwad(const char *name, size_t namelen, UINT16 wad, UINT16 startlump) { UINT16 i; @@ -1411,6 +1412,21 @@ lumpnum_t W_CheckNumForMap(const char *name) return (i << 16) + check; } +// again, no uppercasing. see pwad version +lumpnum_t W_CheckNumForNamePrefix(const char *name, size_t namelen) +{ + INT32 i; + UINT16 check; + + for (i = numwadfiles - 1; i >= 0; i--) + { + check = W_CheckNumForNamePrefixPwad(name, namelen, i, 0); + if (check != LUMPERROR) + return (i << 16) + check; + } + return LUMPERROR; +} + // // W_GetNumForLongName // diff --git a/src/w_wad.h b/src/w_wad.h index e69c09bc3..fe309c14d 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -133,6 +133,7 @@ UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump); lumpnum_t W_CheckNumForMap(const char *name); lumpnum_t W_CheckNumForLongName(const char *name); +lumpnum_t W_CheckNumForNamePrefix(const char *name, size_t namelen); lumpnum_t W_GetNumForLongName(const char *name); lumpnum_t W_CheckNumForNameInFolder(const char *lump, const char *folder); UINT8 W_LumpExists(const char *name); // Lua uses this.