diff --git a/source/blood/src/blood.cpp b/source/blood/src/blood.cpp index 4be3d78cf..675b9003d 100644 --- a/source/blood/src/blood.cpp +++ b/source/blood/src/blood.cpp @@ -1685,7 +1685,7 @@ static int parsedefinitions_game(scriptfile *pScript, int firstPass) break; } - if (fileName == NULL || check_file_exist(fileName)) + if (fileName == NULL || fileSystem.FileExists(fileName)) break; if (S_DefineMusic(musicID, fileName) == -1) @@ -1937,7 +1937,7 @@ static int parsedefinitions_game(scriptfile *pScript, int firstPass) break; } - if (fileName == NULL || check_file_exist(fileName)) + if (fileName == NULL || fileSystem.FileExists(fileName)) break; // maybe I should have just packed this into a sound_t and passed a reference... diff --git a/source/blood/src/d_menu.cpp b/source/blood/src/d_menu.cpp index 3444b1a06..2b81b6317 100644 --- a/source/blood/src/d_menu.cpp +++ b/source/blood/src/d_menu.cpp @@ -69,7 +69,7 @@ CGameMenuItemQAV::CGameMenuItemQAV(int a3, int a4, const char* name, bool widesc if (name) { // NBlood read this directly from the file system cache, but let's better store the data locally for robustness. - raw = kloadfile(name, 0); + raw = fileSystem.LoadFile(name, 0); if (raw.Size() != 0) { auto data = (QAV*)raw.Data(); diff --git a/source/blood/src/levels.cpp b/source/blood/src/levels.cpp index a91314575..f40f9d1fb 100644 --- a/source/blood/src/levels.cpp +++ b/source/blood/src/levels.cpp @@ -70,7 +70,7 @@ IniFile *BloodINI; void levelInitINI(const char *pzIni) { - if (!testkopen(pzIni, 0)) + if (!fileSystem.FileExists(pzIni)) ThrowError("Initialization: %s does not exist", pzIni); BloodINI = new IniFile(pzIni); Bstrncpy(BloodIniFile, pzIni, BMAX_PATH); diff --git a/source/build/include/cache1d.h b/source/build/include/cache1d.h index 161425025..3d7dc8691 100644 --- a/source/build/include/cache1d.h +++ b/source/build/include/cache1d.h @@ -27,31 +27,5 @@ inline FileReader fopenFileReader(const char* name, int where) return fr; } -inline bool testkopen(const char* name, int where) -{ - // todo: if backed by a single file, we must actually open it to make sure. - return fileSystem.FindFile(name) >= 0; -} - -inline TArray kloadfile(const char* name, int where) -{ - auto lump = fileSystem.FindFile(name); - if (lump < 0) return TArray(); - return fileSystem.GetFileData(lump); -} - -inline int32_t kfilesize(const char* name, int where) -{ - auto lump = fileSystem.FindFile(name); - if (lump < 0) return -1; - return fileSystem.FileLength(lump); -} - -// checks from path and in ZIPs, returns 1 if NOT found -inline int32_t check_file_exist(const char* fn) -{ - return fileSystem.FindFile(fn) >= 0; -} - #endif // cache1d_h_ diff --git a/source/build/src/defs.cpp b/source/build/src/defs.cpp index 7495fd5fd..f350dd231 100644 --- a/source/build/src/defs.cpp +++ b/source/build/src/defs.cpp @@ -355,7 +355,7 @@ static int32_t defsparser(scriptfile *script) if (scriptfile_getnumber(script,&fnoo)) break; //y-size if (scriptfile_getstring(script,&fn)) break; - if (check_file_exist(fn)) + if (fileSystem.FileExists(fn)) break; tileSetHightileReplacement(tile,pal,fn,-1.0,1.0,1.0,1.0,1.0,0); @@ -373,7 +373,7 @@ static int32_t defsparser(scriptfile *script) { if (scriptfile_getstring(script,&fn[i])) break; //grab the 6 faces - if (check_file_exist(fn[i])) + if (fileSystem.FileExists(fn[i])) happy = 0; } if (i < 6 || !happy) break; @@ -1098,7 +1098,7 @@ static int32_t defsparser(scriptfile *script) if (seenframe) { modelskin = ++lastmodelskin; } seenframe = 0; - if (check_file_exist(skinfn)) + if (fileSystem.FileExists(skinfn)) break; #ifdef USE_OPENGL @@ -1473,7 +1473,7 @@ static int32_t defsparser(scriptfile *script) break; } - if (check_file_exist(skinfn)) + if (fileSystem.FileExists(skinfn)) break; #ifdef USE_OPENGL @@ -1791,7 +1791,7 @@ static int32_t defsparser(scriptfile *script) { if (EDUKE32_PREDICT_FALSE(!fn[i])) initprintf("Error: skybox: missing '%s filename' near line %s:%d\n", skyfaces[i], script->filename, scriptfile_getlinum(script,skyboxtokptr)), happy = 0; // FIXME? - if (check_file_exist(fn[i])) + if (fileSystem.FileExists(fn[i])) happy = 0; } if (!happy) break; @@ -1846,7 +1846,7 @@ static int32_t defsparser(scriptfile *script) break; } - if (check_file_exist(fn)) + if (fileSystem.FileExists(fn)) break; } @@ -2101,7 +2101,7 @@ static int32_t defsparser(scriptfile *script) break; } - if (EDUKE32_PREDICT_FALSE(check_file_exist(fn))) + if (EDUKE32_PREDICT_FALSE(fileSystem.FileExists(fn))) break; if (xsiz > 0 && ysiz > 0) @@ -2164,7 +2164,7 @@ static int32_t defsparser(scriptfile *script) break; } - if (EDUKE32_PREDICT_FALSE(check_file_exist(fn))) + if (EDUKE32_PREDICT_FALSE(fileSystem.FileExists(fn))) break; switch (token) diff --git a/source/common/filesystem/filesystem.h b/source/common/filesystem/filesystem.h index 9bce7a0be..028716c44 100644 --- a/source/common/filesystem/filesystem.h +++ b/source/common/filesystem/filesystem.h @@ -95,12 +95,25 @@ public: int FindFile (const char *name, ELookupMode lookupmode = ELookupMode::FullName, int filenum = -1) const noexcept; int GetFile (const char *name, ELookupMode lookupmode = ELookupMode::FullName, int filenum = -1) const; // Like FindFile, but throws an exception when it cannot find what it looks for. + bool FileExists(const char* name) + { + return FindFile(name) >= 0; + } int FindFile (const FString &name, ELookupMode lookupmode = ELookupMode::FullName, int filenum = -1) const noexcept { return FindFile(name.GetChars(), lookupmode, filenum); } int GetFile (const FString &name, ELookupMode lookupmode = ELookupMode::FullName, int filenum = -1) const { return GetFile(name.GetChars(), lookupmode, filenum); } + bool FileExists(const FString & name) + { + return FindFile(name) >= 0; + } int FindFile (const std::string &name, ELookupMode lookupmode = ELookupMode::FullName, int filenum = -1) const noexcept { return FindFile(name.c_str(), lookupmode, filenum); } int GetFile (const std::string &name, ELookupMode lookupmode = ELookupMode::FullName, int filenum = -1) const { return GetFile(name.c_str(), lookupmode, filenum); } + bool FileExists(const std::string& name) + { + return FindFile(name) >= 0; + } + int FindResource (int resid, const char *type, int filenum = -1) const noexcept; int GetResource (int resid, const char *type, int filenum = -1) const; // Like FindFile, but throws an exception when it cannot find what it looks for. @@ -112,6 +125,15 @@ public: TArray GetFileData(int file, int pad = 0); // reads file into a writable buffer and optionally adds some padding at the end. (FileData isn't writable!) FileData ReadFile (int file); FileData ReadFile (const char *name) { return ReadFile (GetFile (name)); } + + inline TArray LoadFile(const char* name, int padding) + { + auto lump = FindFile(name); + if (lump < 0) return TArray(); + return GetFileData(lump, padding); + } + + const void *Lock(int lump); void Unlock(int lump, bool mayfree = false); diff --git a/source/common/fonts/singlelumpfont.cpp b/source/common/fonts/singlelumpfont.cpp index 2d5ce46c7..5b80a01e9 100644 --- a/source/common/fonts/singlelumpfont.cpp +++ b/source/common/fonts/singlelumpfont.cpp @@ -125,7 +125,7 @@ FSingleLumpFont::FSingleLumpFont (const char *name, const char * lump) FontName = name; - rawData = kloadfile(lump, 0); + rawData = fileSystem.LoadFile(lump, 0); auto& data = rawData; if (data[0] == 0xE1 && data[1] == 0xE6 && data[2] == 0xD5 && data[3] == 0x1A) diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index 990c1e04e..800b3a152 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -114,14 +114,14 @@ const char *G_DefaultRtsFile(void) return defaultrtsfilename[GAME_WW2GI]; else if (NAPALM) { - if (!testkopen(defaultrtsfilename[GAME_NAPALM],0) && testkopen(defaultrtsfilename[GAME_NAM],0)) + if (!fileSystem.FileExists(defaultrtsfilename[GAME_NAPALM]) && fileSystem.FileExists(defaultrtsfilename[GAME_NAM])) return defaultrtsfilename[GAME_NAM]; // NAM/NAPALM Sharing else return defaultrtsfilename[GAME_NAPALM]; } else if (NAM) { - if (!testkopen(defaultrtsfilename[GAME_NAM],0) && testkopen(defaultrtsfilename[GAME_NAPALM],0)) + if (!fileSystem.FileExists(defaultrtsfilename[GAME_NAM]) && fileSystem.FileExists(defaultrtsfilename[GAME_NAPALM])) return defaultrtsfilename[GAME_NAPALM]; // NAM/NAPALM Sharing else return defaultrtsfilename[GAME_NAM]; @@ -5065,7 +5065,7 @@ static int parsedefinitions_game(scriptfile *pScript, int firstPass) break; } - if (fileName == NULL || check_file_exist(fileName)) + if (fileName == NULL || fileSystem.FileExists(fileName)) break; if (S_DefineMusic(musicID, fileName) == -1) @@ -5213,7 +5213,7 @@ static int parsedefinitions_game(scriptfile *pScript, int firstPass) break; } - if (fileName == NULL || check_file_exist(fileName)) + if (fileName == NULL || fileSystem.FileExists(fileName)) break; // maybe I should have just packed this into a sound_t and passed a reference... @@ -5682,7 +5682,7 @@ static void G_Startup(void) Bcorrectfilename(boardfilename,0); - if (testkopen(boardfilename, 0)) + if (fileSystem.FileExists(boardfilename)) { initprintf("Using level: \"%s\".\n",boardfilename); } @@ -5875,7 +5875,7 @@ int GameInterface::app_main() g_Shareware = 1; else { - if (testkopen("DUKESW.BIN", 1)) // JBF 20030810 + if (fileSystem.FileExists("DUKESW.BIN")) // JBF 20030810 { g_Shareware = 1; } diff --git a/source/duke3d/src/network.cpp b/source/duke3d/src/network.cpp index 2c421971c..5fa854d4e 100644 --- a/source/duke3d/src/network.cpp +++ b/source/duke3d/src/network.cpp @@ -1986,7 +1986,7 @@ static void Net_ReceiveUserMapName(uint8_t *pbuf, int32_t packbufleng) Bcorrectfilename(boardfilename, 0); if (boardfilename[0] != 0) { - if (testkopen(boardfilename, 0)) + if (fileSystem.FileExists(boardfilename)) { Bmemset(boardfilename, 0, sizeof(boardfilename)); Net_SendUserMapName(); diff --git a/source/duke3d/src/osdcmds.cpp b/source/duke3d/src/osdcmds.cpp index 2505b2ca2..3bb926eac 100644 --- a/source/duke3d/src/osdcmds.cpp +++ b/source/duke3d/src/osdcmds.cpp @@ -124,7 +124,7 @@ static int osdcmd_map(osdcmdptr_t parm) maybe_append_ext(filename, sizeof(filename), parm->parms[0], ".map"); - if (!testkopen(filename,0)) + if (!fileSystem.FileExists(filename)) { OSD_Printf(OSD_ERROR "map: file \"%s\" not found.\n", filename); return OSDCMD_OK; diff --git a/source/duke3d/src/premap.cpp b/source/duke3d/src/premap.cpp index d9ead5965..324bcd1eb 100644 --- a/source/duke3d/src/premap.cpp +++ b/source/duke3d/src/premap.cpp @@ -1721,7 +1721,7 @@ void G_SetupFilenameBasedMusic(char *nameBuf, const char *fileName) { Bmemcpy(p+1, ext, Bstrlen(ext) + 1); - if (testkopen(nameBuf, 0)) + if (fileSystem.FileExists(nameBuf)) { realloc_copy(&g_mapInfo[USERMAPMUSICFAKESLOT].musicfn, nameBuf); return; diff --git a/source/duke3d/src/screens.cpp b/source/duke3d/src/screens.cpp index a21436a1f..844e91cdd 100644 --- a/source/duke3d/src/screens.cpp +++ b/source/duke3d/src/screens.cpp @@ -1303,7 +1303,7 @@ void gameDisplay3DRScreen() { Net_GetPackets(); - if (testkopen("3dr.ivf", 0) || testkopen("3dr.anm", 0)) + if (fileSystem.FileExists("3dr.ivf") || fileSystem.FileExists("3dr.anm")) { Anim_Play("3dr.anm"); G_FadePalette(0, 0, 0, 252); diff --git a/source/glbackend/glbackend.cpp b/source/glbackend/glbackend.cpp index a424567d8..ecad690b1 100644 --- a/source/glbackend/glbackend.cpp +++ b/source/glbackend/glbackend.cpp @@ -104,8 +104,8 @@ void GLInstance::Init(int ydim) ImGui_ImplOpenGL3_Init(); if (!ttf.Size()) { - //ttf = kloadfile("demolition/Capsmall_clean.ttf", 0); - ttf = kloadfile("demolition/Roboto-Regular.ttf", 0); + //ttf = fileSystem.LoadFile("demolition/Capsmall_clean.ttf", 0); + ttf = fileSystem.LoadFile("demolition/Roboto-Regular.ttf", 0); } if (ttf.Size()) io.Fonts->AddFontFromMemoryTTF(ttf.Data(), ttf.Size(), std::clamp(ydim / 40, 10, 30)); } diff --git a/source/rr/src/game.cpp b/source/rr/src/game.cpp index 633490c9b..bae9eabae 100644 --- a/source/rr/src/game.cpp +++ b/source/rr/src/game.cpp @@ -109,14 +109,14 @@ const char *G_DefaultRtsFile(void) return defaultrtsfilename[GAME_DUKE]; else if (NAPALM) { - if (!testkopen(defaultrtsfilename[GAME_NAPALM],0) && testkopen(defaultrtsfilename[GAME_NAM],0)) + if (!fileSystem.FileExists(defaultrtsfilename[GAME_NAPALM]) && fileSystem.FileExists(defaultrtsfilename[GAME_NAM])) return defaultrtsfilename[GAME_NAM]; // NAM/NAPALM Sharing else return defaultrtsfilename[GAME_NAPALM]; } else if (NAM) { - if (!testkopen(defaultrtsfilename[GAME_NAM],0) && testkopen(defaultrtsfilename[GAME_NAPALM],0)) + if (!fileSystem.FileExists(defaultrtsfilename[GAME_NAM]) && fileSystem.FileExists(defaultrtsfilename[GAME_NAPALM])) return defaultrtsfilename[GAME_NAPALM]; // NAM/NAPALM Sharing else return defaultrtsfilename[GAME_NAM]; @@ -6598,7 +6598,7 @@ static int parsedefinitions_game(scriptfile *pScript, int firstPass) break; } - if (fileName == NULL || check_file_exist(fileName)) + if (fileName == NULL || fileSystem.FileExists(fileName)) break; if (S_DefineMusic(musicID, fileName) == -1) @@ -6746,7 +6746,7 @@ static int parsedefinitions_game(scriptfile *pScript, int firstPass) break; } - if (fileName == NULL || check_file_exist(fileName)) + if (fileName == NULL || fileSystem.FileExists(fileName)) break; // maybe I should have just packed this into a sound_t and passed a reference... @@ -7119,7 +7119,7 @@ static void G_Startup(void) Bcorrectfilename(boardfilename,0); - if (testkopen(boardfilename, 0)) + if (fileSystem.FileExists(boardfilename)) { initprintf("Using level: \"%s\".\n",boardfilename); } @@ -7326,7 +7326,7 @@ int GameInterface::app_main() g_Shareware = 1; else { - if (testkopen("DUKESW.BIN",1)) // JBF 20030810 + if (fileSystem.FileExists("DUKESW.BIN")) // JBF 20030810 { g_Shareware = 1; } diff --git a/source/rr/src/net.cpp b/source/rr/src/net.cpp index 19154e116..8fcde4321 100644 --- a/source/rr/src/net.cpp +++ b/source/rr/src/net.cpp @@ -2594,7 +2594,7 @@ void Net_ParsePacket(uint8_t *packbuf, int packbufleng) Bcorrectfilename(boardfilename,0); if (boardfilename[0] != 0) { - if (testkopen(boardfilename,0)) + if (fileSystem.FileExists(boardfilename,0)) { Bmemset(boardfilename,0,sizeof(boardfilename)); Net_SendUserMapName(); @@ -3519,7 +3519,7 @@ void Net_ReceiveUserMapName(uint8_t *pbuf, int32_t packbufleng) Bcorrectfilename(boardfilename,0); if (boardfilename[0] != 0) { - if (testkopen(boardfilename,0)) + if (fileSystem.FileExists(boardfilename)) { Bmemset(boardfilename,0,sizeof(boardfilename)); Net_SendUserMapName(); diff --git a/source/rr/src/osdcmds.cpp b/source/rr/src/osdcmds.cpp index 9a9184f4f..c30c5cd63 100644 --- a/source/rr/src/osdcmds.cpp +++ b/source/rr/src/osdcmds.cpp @@ -157,7 +157,7 @@ static int osdcmd_map(osdcmdptr_t parm) maybe_append_ext(filename, sizeof(filename), parm->parms[0], ".map"); - if (!testkopen(filename,0)) + if (!fileSystem.FileExists(filename)) { OSD_Printf(OSD_ERROR "map: file \"%s\" not found.\n", filename); return OSDCMD_OK; diff --git a/source/rr/src/premap.cpp b/source/rr/src/premap.cpp index 2ef830756..0498c7a39 100644 --- a/source/rr/src/premap.cpp +++ b/source/rr/src/premap.cpp @@ -2280,7 +2280,7 @@ void G_SetupFilenameBasedMusic(char *nameBuf, const char *fileName, int levelNum { Bmemcpy(p+1, ext, Bstrlen(ext) + 1); - if (testkopen(nameBuf, 0)) + if (fileSystem.FileExists(nameBuf)) { realloc_copy(&g_mapInfo[levelNum].musicfn, nameBuf); return; diff --git a/source/rr/src/screens.cpp b/source/rr/src/screens.cpp index adc23ea27..626aa1c0b 100644 --- a/source/rr/src/screens.cpp +++ b/source/rr/src/screens.cpp @@ -1393,7 +1393,7 @@ void G_DisplayLogo(void) { Net_GetPackets(); - if (testkopen("3dr.ivf", 0) || testkopen("3dr.anm", 0)) + if (fileSystem.FileExists("3dr.ivf") || fileSystem.FileExists("3dr.anm")) { Anim_Play("3dr.anm"); G_FadePalette(0, 0, 0, 252); diff --git a/source/sw/src/anim.cpp b/source/sw/src/anim.cpp index f602fc08c..37fafd322 100644 --- a/source/sw/src/anim.cpp +++ b/source/sw/src/anim.cpp @@ -226,7 +226,7 @@ void AnimZilla(int frame, int numframes) } } -unsigned char *LoadAnm(short anim_num) +unsigned char *LoadAnm(short anim_num, int *lengthp) { int length; unsigned char *animbuf, *palptr; @@ -246,7 +246,7 @@ unsigned char *LoadAnm(short anim_num) auto handle = fileSystem.OpenFileReader(ANIMname[ANIMnum], 0); if (!handle.isOpen()) return NULL; - length = handle.GetLength(); + *lengthp = length = handle.GetLength(); buffer.Resize(length + sizeof(anim_t)); anm_ptr[anim_num] = (anim_t*)buffer.Data(); @@ -280,14 +280,10 @@ playanm(short anim_num) DSPRINTF(ds,"PlayAnm"); MONO_PRINT(ds); - animbuf = LoadAnm(anim_num); + animbuf = LoadAnm(anim_num, &length); if (!animbuf) return; - // [JM] Temporary, needed to get the file's length for ANIM_LoadAnim. !CHECKME! - length = kfilesize(ANIMname[ANIMnum], 0); - if (length == -1) return; - DSPRINTF(ds,"PlayAnm - Palette Stuff"); MONO_PRINT(ds); diff --git a/source/sw/src/anim.h b/source/sw/src/anim.h index c5bc4c410..b44ca1556 100644 --- a/source/sw/src/anim.h +++ b/source/sw/src/anim.h @@ -30,6 +30,6 @@ BEGIN_SW_NS #define ANIM_SUMO 2 #define ANIM_ZILLA 3 -unsigned char *LoadAnm(short anim_num); +unsigned char *LoadAnm(short anim_num, int *); void playanm(short anim_num); END_SW_NS diff --git a/source/sw/src/game.cpp b/source/sw/src/game.cpp index f3336fd9f..41a38c65d 100644 --- a/source/sw/src/game.cpp +++ b/source/sw/src/game.cpp @@ -1615,9 +1615,9 @@ void LogoLevel(void) MONO_PRINT(ds); // PreCache Anim - LoadAnm(0); + LoadAnm(0, &fin); - auto pal = kloadfile("3drealms.pal", 0); + auto pal = fileSystem.LoadFile("3drealms.pal", 0); if (pal.Size() >= 768) { @@ -2991,24 +2991,7 @@ char isShareware = FALSE; int DetectShareware(void) { -#define DOS_SCREEN_NAME_SW "SHADSW.BIN" -#define DOS_SCREEN_NAME_REG "SWREG.BIN" - - int h; - - if (testkopen(DOS_SCREEN_NAME_SW, 1)) - { - isShareware = TRUE; - return 0; - } - - if (testkopen(DOS_SCREEN_NAME_REG, 1)) - { - isShareware = FALSE; - return 0; - } - - return 1; // heavens knows what this is... + return (isShareware = !!(g_gameType & GAMEFLAG_SHAREWARE)); }