From 5398045f7de81728fa6e67772bf963d8d2d41994 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 19 Aug 2023 16:49:07 +0200 Subject: [PATCH] - got rid of FileSystem::GetFileData. Using FMemFile is better in all these places. --- src/common/audio/music/i_music.cpp | 7 +++-- src/common/engine/sc_man.cpp | 2 +- src/common/engine/stringtable.cpp | 26 +++++++++---------- src/common/engine/stringtable.h | 8 +++--- src/common/filesystem/filesystem.cpp | 30 ++-------------------- src/common/filesystem/filesystem.h | 10 ++------ src/common/textures/formats/qoitexture.cpp | 8 +++--- src/common/utility/zstring.h | 4 ++- src/d_iwad.cpp | 8 +++--- src/d_main.cpp | 4 +-- src/gamedata/xlat/parsecontext.cpp | 10 ++++---- src/gamedata/xlat/parsecontext.h | 2 +- src/playsim/fragglescript/t_prepro.cpp | 12 +++------ src/sound/s_doomsound.cpp | 3 ++- 14 files changed, 50 insertions(+), 84 deletions(-) diff --git a/src/common/audio/music/i_music.cpp b/src/common/audio/music/i_music.cpp index fd93959708..9a8ddfe81e 100644 --- a/src/common/audio/music/i_music.cpp +++ b/src/common/audio/music/i_music.cpp @@ -179,11 +179,10 @@ static void SetupGenMidi() Printf("No GENMIDI lump found. OPL playback not available.\n"); return; } - auto data = fileSystem.OpenFileReader(lump); + auto genmidi = fileSystem.ReadFile(lump); - auto genmidi = data.Read(); - if (genmidi.size() < 8 + 175 * 36 || memcmp(genmidi.data(), "#OPL_II#", 8)) return; - ZMusic_SetGenMidi(genmidi.data()+8); + if (genmidi.GetSize() < 8 + 175 * 36 || memcmp(genmidi.GetMem(), "#OPL_II#", 8)) return; + ZMusic_SetGenMidi((uint8_t*)genmidi.GetString().GetChars() + 8); } static void SetupWgOpn() diff --git a/src/common/engine/sc_man.cpp b/src/common/engine/sc_man.cpp index 98de9f0b18..bc50c79256 100644 --- a/src/common/engine/sc_man.cpp +++ b/src/common/engine/sc_man.cpp @@ -136,7 +136,7 @@ void FScanner::Open (const char *name) // // FScanner :: OpenFile // -// Loads a script from a file. Uses new/delete for memory allocation. +// Loads a script from a file. // //========================================================================== diff --git a/src/common/engine/stringtable.cpp b/src/common/engine/stringtable.cpp index 92819c144d..68cec6db88 100644 --- a/src/common/engine/stringtable.cpp +++ b/src/common/engine/stringtable.cpp @@ -60,10 +60,10 @@ void FStringTable::LoadStrings (const char *language) lastlump = 0; while ((lump = fileSystem.FindLump ("LANGUAGE", &lastlump)) != -1) { - auto lumpdata = fileSystem.GetFileData(lump); + auto lumpdata = fileSystem.ReadFile(lump); - if (!ParseLanguageCSV(lump, lumpdata)) - LoadLanguage (lump, lumpdata); + if (!ParseLanguageCSV(lump, lumpdata.GetString(), lumpdata.GetSize())) + LoadLanguage (lump, lumpdata.GetString(), lumpdata.GetSize()); } UpdateLanguage(language); allMacros.Clear(); @@ -77,9 +77,9 @@ void FStringTable::LoadStrings (const char *language) //========================================================================== -TArray> FStringTable::parseCSV(const TArray &buffer) +TArray> FStringTable::parseCSV(const char* buffer, size_t size) { - const size_t bufLength = buffer.Size(); + const size_t bufLength = size; TArray> data; TArray row; TArray cell; @@ -158,8 +158,8 @@ TArray> FStringTable::parseCSV(const TArray &buffer) bool FStringTable::readMacros(int lumpnum) { - auto lumpdata = fileSystem.GetFileData(lumpnum); - auto data = parseCSV(lumpdata); + auto lumpdata = fileSystem.ReadFile(lumpnum); + auto data = parseCSV(lumpdata.GetString(), lumpdata.GetSize()); for (unsigned i = 1; i < data.Size(); i++) { @@ -186,11 +186,11 @@ bool FStringTable::readMacros(int lumpnum) // //========================================================================== -bool FStringTable::ParseLanguageCSV(int lumpnum, const TArray &buffer) +bool FStringTable::ParseLanguageCSV(int lumpnum, const char* buffer, size_t size) { - if (buffer.Size() < 11) return false; - if (strnicmp((const char*)buffer.Data(), "default,", 8) && strnicmp((const char*)buffer.Data(), "identifier,", 11 )) return false; - auto data = parseCSV(buffer); + if (size < 11) return false; + if (strnicmp(buffer, "default,", 8) && strnicmp(buffer, "identifier,", 11 )) return false; + auto data = parseCSV(buffer, size); int labelcol = -1; int filtercol = -1; @@ -282,14 +282,14 @@ bool FStringTable::ParseLanguageCSV(int lumpnum, const TArray &buffer) // //========================================================================== -void FStringTable::LoadLanguage (int lumpnum, const TArray &buffer) +void FStringTable::LoadLanguage (int lumpnum, const char* buffer, size_t size) { bool errordone = false; TArray activeMaps; FScanner sc; bool hasDefaultEntry = false; - sc.OpenMem("LANGUAGE", buffer); + sc.OpenMem("LANGUAGE", buffer, (int)size); sc.SetCMode (true); while (sc.GetString ()) { diff --git a/src/common/engine/stringtable.h b/src/common/engine/stringtable.h index d75864303d..df1508ae86 100644 --- a/src/common/engine/stringtable.h +++ b/src/common/engine/stringtable.h @@ -44,6 +44,7 @@ #include +#include #include "basics.h" #include "zstring.h" #include "tarray.h" @@ -111,11 +112,10 @@ private: LangMap allStrings; TArray> currentLanguageSet; - void LoadLanguage (int lumpnum, const TArray &buffer); - TArray> parseCSV(const TArray &buffer); - bool ParseLanguageCSV(int lumpnum, const TArray &buffer); + void LoadLanguage (int lumpnum, const char* buffer, size_t size); + TArray> parseCSV(const char* buffer, size_t size); + bool ParseLanguageCSV(int lumpnum, const char* buffer, size_t size); - bool LoadLanguageFromSpreadsheet(int lumpnum, const TArray &buffer); bool readMacros(int lumpnum); void DeleteString(int langid, FName label); void DeleteForLabel(int lumpnum, FName label); diff --git a/src/common/filesystem/filesystem.cpp b/src/common/filesystem/filesystem.cpp index 7e69646328..796c319c91 100644 --- a/src/common/filesystem/filesystem.cpp +++ b/src/common/filesystem/filesystem.cpp @@ -1294,32 +1294,6 @@ unsigned FileSystem::GetFilesInFolder(const char *inpath, TArray &r return result.Size(); } -//========================================================================== -// -// GetFileData -// -// Loads the lump into a TArray and returns it. -// -//========================================================================== - -TArray FileSystem::GetFileData(int lump, int pad) -{ - if ((size_t)lump >= FileInfo.Size()) - return TArray(); - - auto lumpr = OpenFileReader(lump); - auto size = lumpr.GetLength(); - TArray data(size + pad, true); - auto numread = lumpr.Read(data.Data(), size); - - if (numread != size) - { - throw FileSystemException("GetFileData: only read %ld of %ld on lump %i\n", - numread, size, lump); - } - if (pad > 0) memset(&data[size], 0, pad); - return data; -} //========================================================================== // // W_ReadFile @@ -1352,7 +1326,7 @@ void FileSystem::ReadFile (int lump, void *dest) FileData FileSystem::ReadFile (int lump) { - return FileData(FString(ELumpNum(lump))); + return FileData(FString(*this, ELumpNum(lump))); } //========================================================================== @@ -1578,7 +1552,7 @@ FileData::~FileData () { } -FString::FString (ELumpNum lumpnum) +FString::FString (FileSystem& fileSystem, ELumpNum lumpnum) { auto lumpr = fileSystem.OpenFileReader ((int)lumpnum); auto size = lumpr.GetLength (); diff --git a/src/common/filesystem/filesystem.h b/src/common/filesystem/filesystem.h index b6ce6bad8d..97eece8023 100644 --- a/src/common/filesystem/filesystem.h +++ b/src/common/filesystem/filesystem.h @@ -125,16 +125,10 @@ public: inline int GetNumForFullName (const FString &name) { return GetNumForFullName(name.GetChars()); } void ReadFile (int lump, void *dest); - TArray GetFileData(int lump, int pad = 0); // reads lump into a writable buffer and optionally adds some padding at the end. (FileData isn't writable!) + // These should only be used if the file data really needs padding. FileData ReadFile (int lump); FileData ReadFile (const char *name) { return ReadFile (GetNumForName (name)); } - - inline TArray LoadFile(const char* name, int padding = 0) - { - auto lump = FindFile(name); - if (lump < 0) return TArray(); - return GetFileData(lump, padding); - } + FileData ReadFileFullName(const char* name) { return ReadFile(GetNumForFullName(name)); } FileReader OpenFileReader(int lump); // opens a reader that redirects to the containing file's one. FileReader ReopenFileReader(int lump, bool alwayscache = false); // opens an independent reader. diff --git a/src/common/textures/formats/qoitexture.cpp b/src/common/textures/formats/qoitexture.cpp index 223a0b70e1..abe6282230 100644 --- a/src/common/textures/formats/qoitexture.cpp +++ b/src/common/textures/formats/qoitexture.cpp @@ -140,15 +140,15 @@ int FQOITexture::CopyPixels(FBitmap *bmp, int conversion) constexpr auto QOI_COLOR_HASH = [](PalEntry C) { return (C.r * 3 + C.g * 5 + C.b * 7 + C.a * 11); }; - auto lump = fileSystem.OpenFileReader(SourceLump); - auto bytes = lump.Read(); - if (bytes.size() < 22) return 0; // error + auto lump = fileSystem.ReadFile(SourceLump); + if (lump.GetSize() < 22) return 0; // error PalEntry index[64] = {}; PalEntry pe = 0xff000000; size_t p = 14, run = 0; - size_t chunks_len = bytes.size() - 8; + size_t chunks_len = lump.GetSize() - 8; + auto bytes = (const uint8_t*)lump.GetMem(); for (int h = 0; h < Height; h++) { diff --git a/src/common/utility/zstring.h b/src/common/utility/zstring.h index 50e41d9c69..89ae13568e 100644 --- a/src/common/utility/zstring.h +++ b/src/common/utility/zstring.h @@ -117,6 +117,8 @@ enum ELumpNum { }; +class FileSystem; + class FString { public: @@ -146,7 +148,7 @@ public: FString (char head, const FString &tail); // Other constructors - FString (ELumpNum); // Create from a lump + FString (FileSystem&, ELumpNum); // Create from a lump ~FString (); diff --git a/src/d_iwad.cpp b/src/d_iwad.cpp index bfa67961a3..62e4e459b9 100644 --- a/src/d_iwad.cpp +++ b/src/d_iwad.cpp @@ -314,8 +314,8 @@ FIWadManager::FIWadManager(const char *firstfn, const char *optfn) int num = check.CheckNumForName("IWADINFO"); if (num >= 0) { - auto data = check.GetFileData(num); - ParseIWadInfo("IWADINFO", (const char*)data.Data(), data.Size()); + auto data = check.ReadFile(num); + ParseIWadInfo("IWADINFO", data.GetString(), (int)data.GetSize()); } } } @@ -399,8 +399,8 @@ int FIWadManager::CheckIWADInfo(const char* fn) { FIWADInfo result; - auto data = check.GetFileData(num); - ParseIWadInfo(fn, (const char*)data.Data(), data.Size(), &result); + auto data = check.ReadFile(num); + ParseIWadInfo(fn, data.GetString(), (int)data.GetSize(), &result); for (unsigned i = 0, count = mIWadInfos.Size(); i < count; ++i) { diff --git a/src/d_main.cpp b/src/d_main.cpp index d964671583..5660c4ed2b 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -1934,9 +1934,9 @@ static FString CheckGameInfo(TArray & pwads) if (num >= 0) { // Found one! - auto data = check.GetFileData(num); + auto data = check.ReadFile(num); auto wadname = check.GetResourceFileName(check.GetFileContainer(num)); - return ParseGameInfo(pwads, wadname, (const char*)data.Data(), data.Size()); + return ParseGameInfo(pwads, wadname, data.GetString(), (int)data.GetSize()); } } return ""; diff --git a/src/gamedata/xlat/parsecontext.cpp b/src/gamedata/xlat/parsecontext.cpp index cac663cfc3..1810af55dc 100644 --- a/src/gamedata/xlat/parsecontext.cpp +++ b/src/gamedata/xlat/parsecontext.cpp @@ -78,7 +78,7 @@ bool FParseContext::FindSym (char *sym, FParseSymbol **val) // // //========================================================================== -int FParseContext::GetToken (char *&sourcep, FParseToken *yylval) +int FParseContext::GetToken (const char *&sourcep, FParseToken *yylval) { char token[80]; int toksize; @@ -103,7 +103,7 @@ loop: c = *sourcep++; if (c == 'x' || c == 'X') { - yylval->val = (int)strtoll(sourcep, &sourcep, 16); + yylval->val = (int)strtoll(sourcep, (char**)&sourcep, 16); return TokenTrans[NUM]; } else @@ -118,7 +118,7 @@ loop: if (*endp == '.') { // It's a float - yylval->fval = strtod(sourcep, &sourcep); + yylval->fval = strtod(sourcep, (char**)& sourcep); return TokenTrans[FLOATVAL]; } else @@ -323,12 +323,12 @@ void FParseContext::ParseLump(const char *lumpname) } // Read the lump into a buffer and add a 0-terminator - auto lumpdata = fileSystem.GetFileData(lumpno, 1); + auto lumpdata = fileSystem.ReadFile(lumpno); SourceLine = 0; SourceFile = lumpname; - char *sourcep = (char*)lumpdata.Data(); + const char *sourcep = lumpdata.GetString(); while ( (tokentype = GetToken(sourcep, &token)) ) { // It is much easier to handle include statements outside the main parser. diff --git a/src/gamedata/xlat/parsecontext.h b/src/gamedata/xlat/parsecontext.h index 48a3425474..82c345f782 100644 --- a/src/gamedata/xlat/parsecontext.h +++ b/src/gamedata/xlat/parsecontext.h @@ -146,7 +146,7 @@ struct FParseContext void AddSym (char *sym, int val); bool FindSym (char *sym, FParseSymbol **val); virtual bool FindToken (char *tok, int *type) = 0; - int GetToken (char *&sourcep, FParseToken *yylval); + int GetToken (const char *&sourcep, FParseToken *yylval); int PrintError (const char *s); void ParseLump(const char *lumpname); }; diff --git a/src/playsim/fragglescript/t_prepro.cpp b/src/playsim/fragglescript/t_prepro.cpp index 409faa1320..495092581f 100644 --- a/src/playsim/fragglescript/t_prepro.cpp +++ b/src/playsim/fragglescript/t_prepro.cpp @@ -410,7 +410,6 @@ void DFsScript::Preprocess(FLevelLocals *Level) void DFsScript::ParseInclude(FLevelLocals *Level, char *lumpname) { int lumpnum; - char *lump; if((lumpnum = fileSystem.CheckNumForName(lumpname)) == -1) { @@ -419,21 +418,18 @@ void DFsScript::ParseInclude(FLevelLocals *Level, char *lumpname) } int lumplen=fileSystem.FileLength(lumpnum); - lump=new char[lumplen+10]; - fileSystem.ReadFile(lumpnum,lump); + TArray lump(lumplen + 10); + fileSystem.ReadFile(lumpnum,lump.Data()); lump[lumplen]=0; // preprocess the include // we assume that it does not include sections or labels or // other nasty things - ProcessFindChar(lump, 0); + ProcessFindChar(lump.Data(), 0); // now parse the lump FParser parse(Level, this); - parse.Run(lump, lump, lump+lumplen); - - // free the lump - delete[] lump; + parse.Run(lump.Data(), lump.Data(), lump.Data() + lumplen); } diff --git a/src/sound/s_doomsound.cpp b/src/sound/s_doomsound.cpp index 6a67fa446e..9f000f4c7d 100644 --- a/src/sound/s_doomsound.cpp +++ b/src/sound/s_doomsound.cpp @@ -237,7 +237,8 @@ void S_Init() TArray curve; if (curvelump >= 0) { - curve = fileSystem.GetFileData(curvelump); + curve.Resize(fileSystem.FileLength(curvelump)); + fileSystem.ReadFile(curvelump, curve.Data()); } soundEngine->Init(curve); }