From ebc808e2a96214274832257c65267936fb66b854 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 10 Dec 2023 10:22:07 +0100 Subject: [PATCH] did some cleanup of the FResourceFile interface. * making all members protected (but adding friend overrides for the classes which still need it) * allowing to read data without retrieving the FResourceLump object. --- src/common/audio/music/i_soundfont.cpp | 10 +-- src/common/filesystem/include/fs_files.h | 2 +- src/common/filesystem/include/resourcefile.h | 67 +++++++++++++++++-- src/common/filesystem/source/filesystem.cpp | 2 +- src/common/filesystem/source/resourcefile.cpp | 9 ++- src/common/fonts/hexfont.cpp | 13 ++-- src/common/menu/savegamemanager.cpp | 25 ++----- src/g_game.cpp | 16 ++--- src/g_level.cpp | 36 +++++----- src/maploader/glnodes.cpp | 12 ++-- src/menu/loadsavemenu.cpp | 8 +-- src/p_openmap.cpp | 20 +++--- 12 files changed, 128 insertions(+), 92 deletions(-) diff --git a/src/common/audio/music/i_soundfont.cpp b/src/common/audio/music/i_soundfont.cpp index e1059fc12..cfc400e41 100644 --- a/src/common/audio/music/i_soundfont.cpp +++ b/src/common/audio/music/i_soundfont.cpp @@ -215,10 +215,10 @@ FileReader FZipPatReader::OpenFile(const char *name) FileReader fr; if (resf != nullptr) { - auto lump = resf->FindLump(name); - if (lump != nullptr) + auto lump = resf->FindEntry(name); + if (lump >= 0) { - return lump->NewReader(); + return resf->GetEntryReader(lump); } } fr.OpenFile(name); @@ -369,8 +369,8 @@ void FSoundFontManager::ProcessOneFile(const char* fn) { if (zip->LumpCount() > 1) // Anything with just one lump cannot possibly be a packed GUS patch set so skip it right away and simplify the lookup code { - auto zipl = zip->FindLump("timidity.cfg"); - if (zipl != nullptr) + auto zipl = zip->FindEntry("timidity.cfg"); + if (zipl >= 0) { // It seems like this is what we are looking for FSoundFontInfo sft = { fb, fbe, fn, SF_GUS }; diff --git a/src/common/filesystem/include/fs_files.h b/src/common/filesystem/include/fs_files.h index 900f7b3bb..ada77ca6e 100644 --- a/src/common/filesystem/include/fs_files.h +++ b/src/common/filesystem/include/fs_files.h @@ -4,7 +4,7 @@ ** **--------------------------------------------------------------------------- ** Copyright 1998-2008 Randy Heit -** Copyright 2005-2008 Christoph Oelckers +** Copyright 2005-2023 Christoph Oelckers ** All rights reserved. ** ** Redistribution and use in source and binary forms, with or without diff --git a/src/common/filesystem/include/resourcefile.h b/src/common/filesystem/include/resourcefile.h index 5c41225ca..6cc18939a 100644 --- a/src/common/filesystem/include/resourcefile.h +++ b/src/common/filesystem/include/resourcefile.h @@ -110,18 +110,34 @@ struct FCompressedBuffer struct FResourceLump { +protected: friend class FResourceFile; friend class FWadFile; // this still needs direct access. + friend class FileData; + friend class FileSystem; + friend class FLumpFile; + friend class FLumpReader; + friend class FGrpFile; + friend class F7ZFile; + friend class FSSIFile; + friend class FWHResFile; + friend class FZipFile; + friend class FPakFile; + friend class FRFFFile; + friend class FDirectory; + friend int lumpcmp(const void* a, const void* b); + int LumpSize; int RefCount; -protected: +//protected: const char* FullName; -public: +//public: uint8_t Flags; char * Cache; FResourceFile * Owner; +public: FResourceLump() { Cache = NULL; @@ -129,9 +145,12 @@ public: Flags = 0; RefCount = 0; FullName = ""; + LumpSize = 0; } - virtual ~FResourceLump(); + +protected: + virtual FileReader *GetReader(); virtual FileReader NewReader(); virtual int GetFileOffset() { return -1; } @@ -186,15 +205,49 @@ public: static FResourceFile *OpenDirectory(const char *filename, LumpFilterInfo* filter = nullptr, FileSystemMessageFunc Printf = nullptr, StringPool* sp = nullptr); virtual ~FResourceFile(); // If this FResourceFile represents a directory, the Reader object is not usable so don't return it. - FileReader *GetReader() { return Reader.isOpen()? &Reader : nullptr; } - uint32_t LumpCount() const { return NumLumps; } + FileReader *GetContainerReader() { return Reader.isOpen()? &Reader : nullptr; } + [[deprecated]] uint32_t LumpCount() const { return NumLumps; } uint32_t GetFirstEntry() const { return FirstLump; } void SetFirstLump(uint32_t f) { FirstLump = f; } const char* GetHash() const { return Hash; } - virtual FResourceLump *GetLump(int no) = 0; - FResourceLump *FindLump(const char *name); + + int EntryCount() const { return NumLumps; } + int FindEntry(const char* name); + + size_t Length(int entry) + { + auto l = GetLump(entry); + return l ? l->LumpSize : -1; + } + + FileReader GetEntryReader(int entry, bool newreader = true) + { + auto l = GetLump(entry); + return l ? l->NewReader() : FileReader(); + + } + + ResourceData Read(int entry) + { + auto fr = GetEntryReader(entry, false); + return fr.Read(); + } + + const char* getName(int entry) + { + auto l = GetLump(entry); + return l ? l->FullName : nullptr; + } + FCompressedBuffer GetRawData(int entry) + { + auto l = GetLump(entry); + if (!l) return {}; + return l->GetRawData(); + } + + }; diff --git a/src/common/filesystem/source/filesystem.cpp b/src/common/filesystem/source/filesystem.cpp index 0841ea68e..007756bce 100644 --- a/src/common/filesystem/source/filesystem.cpp +++ b/src/common/filesystem/source/filesystem.cpp @@ -1421,7 +1421,7 @@ FileReader *FileSystem::GetFileReader(int rfnum) return NULL; } - return Files[rfnum]->GetReader(); + return Files[rfnum]->GetContainerReader(); } //========================================================================== diff --git a/src/common/filesystem/source/resourcefile.cpp b/src/common/filesystem/source/resourcefile.cpp index 64c620c3b..99f8d0838 100644 --- a/src/common/filesystem/source/resourcefile.cpp +++ b/src/common/filesystem/source/resourcefile.cpp @@ -618,17 +618,16 @@ bool FResourceFile::FindPrefixRange(const char* filter, void *lumps, size_t lump // //========================================================================== -FResourceLump *FResourceFile::FindLump(const char *name) +int FResourceFile::FindEntry(const char *name) { for (unsigned i = 0; i < NumLumps; i++) { - FResourceLump *lump = GetLump(i); - if (!stricmp(name, lump->FullName)) + if (!stricmp(name, getName(i))) { - return lump; + return i; } } - return nullptr; + return -1; } //========================================================================== diff --git a/src/common/fonts/hexfont.cpp b/src/common/fonts/hexfont.cpp index 95b7999bf..d8aa4db0a 100644 --- a/src/common/fonts/hexfont.cpp +++ b/src/common/fonts/hexfont.cpp @@ -58,12 +58,12 @@ struct HexDataSource // //========================================================================== - void ParseDefinition(FileSys::FResourceLump* font) + void ParseDefinition(FResourceFile* resf, int index) { FScanner sc; - auto data = font->Lock(); - sc.OpenMem("newconsolefont.hex", (const char*)data, font->Size()); + auto data = resf->Read(index); + sc.OpenMem("newconsolefont.hex", (const char*)data.data(), data.size()); sc.SetCMode(true); glyphdata.Push(0); // ensure that index 0 can be used as 'not present'. while (sc.GetString()) @@ -97,7 +97,6 @@ struct HexDataSource lumb = i * 255 / 17; SmallPal[i] = PalEntry(255, lumb, lumb, lumb); } - font->Unlock(); } }; @@ -440,8 +439,8 @@ void LoadHexFont(const char* filename) { auto resf = FResourceFile::OpenResourceFile(filename); if (resf == nullptr) I_FatalError("Unable to open %s", filename); - auto hexfont = resf->FindLump("newconsolefont.hex"); - if (hexfont == nullptr) I_FatalError("Unable to find newconsolefont.hex in %s", filename); - hexdata.ParseDefinition(hexfont); + auto hexfont = resf->FindEntry("newconsolefont.hex"); + if (hexfont < 0) I_FatalError("Unable to find newconsolefont.hex in %s", filename); + hexdata.ParseDefinition(resf, hexfont); delete resf; } diff --git a/src/common/menu/savegamemanager.cpp b/src/common/menu/savegamemanager.cpp index 35cf6674d..942a47e73 100644 --- a/src/common/menu/savegamemanager.cpp +++ b/src/common/menu/savegamemanager.cpp @@ -297,37 +297,26 @@ unsigned FSavegameManagerBase::ExtractSaveData(int index) !node->bOldVersion && (resf = FResourceFile::OpenResourceFile(node->Filename.GetChars(), true)) != nullptr) { - auto info = resf->FindLump("info.json"); - if (info == nullptr) + auto info = resf->FindEntry("info.json"); + if (info < 0) { // this should not happen because the file has already been verified. return index; } - void* data = info->Lock(); + auto data = resf->Read(info); FSerializer arc; - if (!arc.OpenReader((const char*)data, info->LumpSize)) + if (!arc.OpenReader((const char*)data.data(), data.size())) { - info->Unlock(); return index; } - info->Unlock(); SaveCommentString = ExtractSaveComment(arc); - auto pic = resf->FindLump("savepic.png"); - if (pic != nullptr) + auto pic = resf->FindEntry("savepic.png"); + if (pic >= 0) { - FileReader picreader; - - picreader.OpenMemoryArray([=](std::vector &array) - { - auto cache = pic->Lock(); - array.resize(pic->LumpSize); - memcpy(&array[0], cache, pic->LumpSize); - pic->Unlock(); - return true; - }); + FileReader picreader = resf->GetEntryReader(pic); PNGHandle *png = M_VerifyPNG(picreader); if (png != nullptr) { diff --git a/src/g_game.cpp b/src/g_game.cpp index a0663ea9b..77be536de 100644 --- a/src/g_game.cpp +++ b/src/g_game.cpp @@ -2002,8 +2002,8 @@ void G_DoLoadGame () LoadGameError("TXT_COULDNOTREAD"); return; } - auto info = resfile->FindLump("info.json"); - if (info == nullptr) + auto info = resfile->FindEntry("info.json"); + if (info < 0) { LoadGameError("TXT_NOINFOJSON"); return; @@ -2011,9 +2011,9 @@ void G_DoLoadGame () SaveVersion = 0; - void *data = info->Lock(); + auto data = resfile->Read(info); FSerializer arc; - if (!arc.OpenReader((const char *)data, info->LumpSize)) + if (!arc.OpenReader((char*)data.data(), data.size())) { LoadGameError("TXT_FAILEDTOREADSG"); return; @@ -2080,15 +2080,15 @@ void G_DoLoadGame () // we are done with info.json. arc.Close(); - info = resfile->FindLump("globals.json"); - if (info == nullptr) + info = resfile->FindEntry("globals.json"); + if (info < 0) { LoadGameError("TXT_NOGLOBALSJSON"); return; } - data = info->Lock(); - if (!arc.OpenReader((const char *)data, info->LumpSize)) + data = resfile->Read(info); + if (!arc.OpenReader((char*)data.data(), data.size())) { LoadGameError("TXT_SGINFOERR"); return; diff --git a/src/g_level.cpp b/src/g_level.cpp index b4b27c873..ba54de8cc 100644 --- a/src/g_level.cpp +++ b/src/g_level.cpp @@ -2004,32 +2004,28 @@ void G_ReadSnapshots(FResourceFile *resf) G_ClearSnapshots(); - for (unsigned j = 0; j < resf->LumpCount(); j++) + for (unsigned j = 0; j < resf->EntryCount(); j++) { - auto resl = resf->GetLump(j); - if (resl != nullptr) + auto name = resf->getName(j); + auto ptr = strstr(name, ".map.json"); + if (ptr != nullptr) { - auto name = resl->getName(); - auto ptr = strstr(name, ".map.json"); + ptrdiff_t maplen = ptr - name; + FString mapname(name, (size_t)maplen); + i = FindLevelInfo(mapname.GetChars()); + if (i != nullptr) + { + i->Snapshot = resf->GetRawData(j); + } + } + else + { + auto ptr = strstr(name, ".mapd.json"); if (ptr != nullptr) { ptrdiff_t maplen = ptr - name; FString mapname(name, (size_t)maplen); - i = FindLevelInfo(mapname.GetChars()); - if (i != nullptr) - { - i->Snapshot = resl->GetRawData(); - } - } - else - { - auto ptr = strstr(name, ".mapd.json"); - if (ptr != nullptr) - { - ptrdiff_t maplen = ptr - name; - FString mapname(name, (size_t)maplen); - TheDefaultLevelInfo.Snapshot = resl->GetRawData(); - } + TheDefaultLevelInfo.Snapshot = resf->GetRawData(j); } } } diff --git a/src/maploader/glnodes.cpp b/src/maploader/glnodes.cpp index fec7143aa..6c97065a3 100644 --- a/src/maploader/glnodes.cpp +++ b/src/maploader/glnodes.cpp @@ -780,7 +780,7 @@ static int FindGLNodesInFile(FResourceFile * f, const char * label) FString glheader; bool mustcheck=false; - uint32_t numentries = f->LumpCount(); + uint32_t numentries = f->EntryCount(); glheader.Format("GL_%.8s", label); if (glheader.Len()>8) @@ -793,13 +793,13 @@ static int FindGLNodesInFile(FResourceFile * f, const char * label) { for(uint32_t i=0;iGetLump(i)->getName(), glheader.GetChars(), 8)) + if (!strnicmp(f->getName(i), glheader.GetChars(), 8)) { if (mustcheck) { char check[16]={0}; - auto fr = f->GetLump(i)->GetReader(); - fr->Read(check, 16); + auto fr = f->GetEntryReader(i, false); + fr.Read(check, 16); if (MatchHeader(label, check)) return i; } else return i; @@ -900,13 +900,13 @@ bool MapLoader::LoadGLNodes(MapData * map) result=true; for(unsigned i=0; i<4;i++) { - if (strnicmp(f_gwa->GetLump(li+i+1)->getName(), check[i], 8)) + if (strnicmp(f_gwa->getName(li + i + 1), check[i], 8)) { result=false; break; } else - gwalumps[i] = f_gwa->GetLump(li+i+1)->NewReader(); + gwalumps[i] = f_gwa->GetEntryReader(li + i + 1); } if (result) result = DoLoadGLNodes(gwalumps); } diff --git a/src/menu/loadsavemenu.cpp b/src/menu/loadsavemenu.cpp index 4202ba23e..3bf2a87c2 100644 --- a/src/menu/loadsavemenu.cpp +++ b/src/menu/loadsavemenu.cpp @@ -76,15 +76,15 @@ void FSavegameManager::ReadSaveStrings() { bool oldVer = false; bool missing = false; - auto info = savegame->FindLump("info.json"); - if (info == nullptr) + auto info = savegame->FindEntry("info.json"); + if (info < 0) { // savegame info not found. This is not a savegame so leave it alone. continue; } - void *data = info->Lock(); + auto data = savegame->Read(info); FSerializer arc; - if (arc.OpenReader((const char *)data, info->LumpSize)) + if (arc.OpenReader((const char*)data.data(), data.size())) { int savever = 0; arc("Save Version", savever); diff --git a/src/p_openmap.cpp b/src/p_openmap.cpp index e744755ba..722ae18f9 100644 --- a/src/p_openmap.cpp +++ b/src/p_openmap.cpp @@ -126,7 +126,7 @@ MapData *P_OpenMapData(const char * mapname, bool justcheck) return NULL; } map->resource = FResourceFile::OpenResourceFile(mapname); - wadReader = map->resource->GetReader(); + wadReader = map->resource->GetContainerReader(); } else { @@ -265,7 +265,7 @@ MapData *P_OpenMapData(const char * mapname, bool justcheck) map->lumpnum = lump_wad; auto reader = fileSystem.ReopenFileReader(lump_wad); map->resource = FResourceFile::OpenResourceFile(fileSystem.GetFileFullName(lump_wad), reader, true); - wadReader = map->resource->GetReader(); + wadReader = map->resource->GetContainerReader(); } } uint32_t id; @@ -280,21 +280,21 @@ MapData *P_OpenMapData(const char * mapname, bool justcheck) char maplabel[9]=""; int index=0; - map->MapLumps[0].Reader = map->resource->GetLump(0)->NewReader(); - uppercopy(map->MapLumps[0].Name, map->resource->GetLump(0)->getName()); + map->MapLumps[0].Reader = map->resource->GetEntryReader(0); + uppercopy(map->MapLumps[0].Name, map->resource->getName(0)); - for(uint32_t i = 1; i < map->resource->LumpCount(); i++) + for(uint32_t i = 1; i < map->resource->EntryCount(); i++) { - const char* lumpname = map->resource->GetLump(i)->getName(); + const char* lumpname = map->resource->getName(i); if (i == 1 && !strnicmp(lumpname, "TEXTMAP", 8)) { map->isText = true; - map->MapLumps[ML_TEXTMAP].Reader = map->resource->GetLump(i)->NewReader(); + map->MapLumps[ML_TEXTMAP].Reader = map->resource->GetEntryReader(i); strncpy(map->MapLumps[ML_TEXTMAP].Name, lumpname, 8); for(int i = 2;; i++) { - lumpname = map->resource->GetLump(i)->getName(); + lumpname = map->resource->getName(i); if (!strnicmp(lumpname, "ZNODES",8)) { index = ML_GLZNODES; @@ -326,7 +326,7 @@ MapData *P_OpenMapData(const char * mapname, bool justcheck) return map; } else continue; - map->MapLumps[index].Reader = map->resource->GetLump(i)->NewReader(); + map->MapLumps[index].Reader = map->resource->GetEntryReader(i); strncpy(map->MapLumps[index].Name, lumpname, 8); } } @@ -358,7 +358,7 @@ MapData *P_OpenMapData(const char * mapname, bool justcheck) maplabel[8]=0; } - map->MapLumps[index].Reader = map->resource->GetLump(i)->NewReader(); + map->MapLumps[index].Reader = map->resource->GetEntryReader(i); strncpy(map->MapLumps[index].Name, lumpname, 8); } }