From df56fc65e08932d4ade9c7580f827c3133744b37 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 10 Dec 2023 10:26:44 +0100 Subject: [PATCH] rework some of the friend-dependent access to FResourceLump. --- src/common/filesystem/include/resourcefile.h | 10 ++++++++- src/common/filesystem/source/file_7z.cpp | 3 +-- src/common/filesystem/source/file_grp.cpp | 4 +--- src/common/filesystem/source/file_lump.cpp | 3 +-- src/common/filesystem/source/file_pak.cpp | 4 +--- src/common/filesystem/source/file_rff.cpp | 4 +--- src/common/filesystem/source/file_ssi.cpp | 3 +-- src/common/filesystem/source/file_wad.cpp | 15 ++++++------- src/common/filesystem/source/file_whres.cpp | 3 +-- src/common/filesystem/source/file_zip.cpp | 22 +++++++++---------- src/common/filesystem/source/filesystem.cpp | 4 ++-- src/common/filesystem/source/resourcefile.cpp | 14 ++++++------ 12 files changed, 42 insertions(+), 47 deletions(-) diff --git a/src/common/filesystem/include/resourcefile.h b/src/common/filesystem/include/resourcefile.h index 6cc18939a04..c82fc630983 100644 --- a/src/common/filesystem/include/resourcefile.h +++ b/src/common/filesystem/include/resourcefile.h @@ -176,9 +176,9 @@ struct FResourceLump class FResourceFile { public: +protected: FileReader Reader; const char* FileName; -protected: uint32_t NumLumps; char Hash[48]; StringPool* stringpool; @@ -206,6 +206,7 @@ class FResourceFile virtual ~FResourceFile(); // If this FResourceFile represents a directory, the Reader object is not usable so don't return it. FileReader *GetContainerReader() { return Reader.isOpen()? &Reader : nullptr; } + const char* GetFileName() const { return FileName; } [[deprecated]] uint32_t LumpCount() const { return NumLumps; } uint32_t GetFirstEntry() const { return FirstLump; } void SetFirstLump(uint32_t f) { FirstLump = f; } @@ -247,6 +248,13 @@ class FResourceFile return l->GetRawData(); } + FileReader Destroy() + { + auto fr = std::move(Reader); + delete this; + return fr; + } + }; diff --git a/src/common/filesystem/source/file_7z.cpp b/src/common/filesystem/source/file_7z.cpp index 1be0d6a1c2d..0740ceca1a2 100644 --- a/src/common/filesystem/source/file_7z.cpp +++ b/src/common/filesystem/source/file_7z.cpp @@ -367,8 +367,7 @@ FResourceFile *Check7Z(const char *filename, FileReader &file, LumpFilterInfo* f auto rf = new F7ZFile(filename, file, sp); if (rf->Open(filter, Printf)) return rf; - file = std::move(rf->Reader); // to avoid destruction of reader - delete rf; + file = rf->Destroy(); } } return NULL; diff --git a/src/common/filesystem/source/file_grp.cpp b/src/common/filesystem/source/file_grp.cpp index b289b121d68..05299f94070 100644 --- a/src/common/filesystem/source/file_grp.cpp +++ b/src/common/filesystem/source/file_grp.cpp @@ -145,9 +145,7 @@ FResourceFile *CheckGRP(const char *filename, FileReader &file, LumpFilterInfo* { auto rf = new FGrpFile(filename, file, sp); if (rf->Open(filter)) return rf; - - file = std::move(rf->Reader); // to avoid destruction of reader - delete rf; + file = rf->Destroy(); } } return NULL; diff --git a/src/common/filesystem/source/file_lump.cpp b/src/common/filesystem/source/file_lump.cpp index ad4406a0f49..94da2af4fb3 100644 --- a/src/common/filesystem/source/file_lump.cpp +++ b/src/common/filesystem/source/file_lump.cpp @@ -89,8 +89,7 @@ FResourceFile *CheckLump(const char *filename, FileReader &file, LumpFilterInfo* // always succeeds auto rf = new FLumpFile(filename, file, sp); if (rf->Open(filter)) return rf; - file = std::move(rf->Reader); // to avoid destruction of reader - delete rf; + file = rf->Destroy(); return NULL; } diff --git a/src/common/filesystem/source/file_pak.cpp b/src/common/filesystem/source/file_pak.cpp index 4e3b3ef9310..12ab2f4e25c 100644 --- a/src/common/filesystem/source/file_pak.cpp +++ b/src/common/filesystem/source/file_pak.cpp @@ -138,9 +138,7 @@ FResourceFile *CheckPak(const char *filename, FileReader &file, LumpFilterInfo* { auto rf = new FPakFile(filename, file, sp); if (rf->Open(filter)) return rf; - - file = std::move(rf->Reader); // to avoid destruction of reader - delete rf; + file = rf->Destroy(); } } return NULL; diff --git a/src/common/filesystem/source/file_rff.cpp b/src/common/filesystem/source/file_rff.cpp index 1d3cf15bf04..dff7b69e9dc 100644 --- a/src/common/filesystem/source/file_rff.cpp +++ b/src/common/filesystem/source/file_rff.cpp @@ -252,9 +252,7 @@ FResourceFile *CheckRFF(const char *filename, FileReader &file, LumpFilterInfo* { auto rf = new FRFFFile(filename, file, sp); if (rf->Open(filter)) return rf; - - file = std::move(rf->Reader); // to avoid destruction of reader - delete rf; + file = rf->Destroy(); } } return NULL; diff --git a/src/common/filesystem/source/file_ssi.cpp b/src/common/filesystem/source/file_ssi.cpp index 4b68997f0fb..276a6dd8559 100644 --- a/src/common/filesystem/source/file_ssi.cpp +++ b/src/common/filesystem/source/file_ssi.cpp @@ -147,8 +147,7 @@ FResourceFile* CheckSSI(const char* filename, FileReader& file, LumpFilterInfo* } auto ssi = new FSSIFile(filename, file, sp); if (ssi->Open(version, numfiles, filter)) return ssi; - file = std::move(ssi->Reader); // to avoid destruction of reader - delete ssi; + file = ssi->Destroy(); } } return nullptr; diff --git a/src/common/filesystem/source/file_wad.cpp b/src/common/filesystem/source/file_wad.cpp index 13eb0b098a7..3451b7da812 100644 --- a/src/common/filesystem/source/file_wad.cpp +++ b/src/common/filesystem/source/file_wad.cpp @@ -76,8 +76,8 @@ class FWadFileLump : public FResourceLump { if(!Compressed) { - Owner->Reader.Seek(Position, FileReader::SeekSet); - return &Owner->Reader; + Owner->GetContainerReader()->Seek(Position, FileReader::SeekSet); + return Owner->GetContainerReader(); } return NULL; } @@ -85,7 +85,7 @@ class FWadFileLump : public FResourceLump { if(!Compressed) { - const char * buffer = Owner->Reader.GetBuffer(); + const char * buffer = Owner->GetContainerReader()->GetBuffer(); if (buffer != NULL) { @@ -96,20 +96,20 @@ class FWadFileLump : public FResourceLump } } - Owner->Reader.Seek(Position, FileReader::SeekSet); + Owner->GetContainerReader()->Seek(Position, FileReader::SeekSet); Cache = new char[LumpSize]; if(Compressed) { FileReader lzss; - if (lzss.OpenDecompressor(Owner->Reader, LumpSize, METHOD_LZSS, false, true)) + if (lzss.OpenDecompressor(*Owner->GetContainerReader(), LumpSize, METHOD_LZSS, false, true)) { lzss.Read(Cache, LumpSize); } } else { - auto read = Owner->Reader.Read(Cache, LumpSize); + auto read = Owner->GetContainerReader()->Read(Cache, LumpSize); if (read != LumpSize) { throw FileSystemException("only read %d of %d bytes", (int)read, (int)LumpSize); @@ -478,8 +478,7 @@ FResourceFile *CheckWad(const char *filename, FileReader &file, LumpFilterInfo* auto rf = new FWadFile(filename, file, sp); if (rf->Open(filter, Printf)) return rf; - file = std::move(rf->Reader); // to avoid destruction of reader - delete rf; + file = rf->Destroy(); } } return NULL; diff --git a/src/common/filesystem/source/file_whres.cpp b/src/common/filesystem/source/file_whres.cpp index cef800a0f3b..ffa8dd8a568 100644 --- a/src/common/filesystem/source/file_whres.cpp +++ b/src/common/filesystem/source/file_whres.cpp @@ -136,8 +136,7 @@ FResourceFile *CheckWHRes(const char *filename, FileReader &file, LumpFilterInfo } auto rf = new FWHResFile(filename, file, sp); if (rf->Open(filter)) return rf; - file = std::move(rf->Reader); // to avoid destruction of reader - delete rf; + file = rf->Destroy(); } return NULL; } diff --git a/src/common/filesystem/source/file_zip.cpp b/src/common/filesystem/source/file_zip.cpp index 155cbf45a60..352c7adc295 100644 --- a/src/common/filesystem/source/file_zip.cpp +++ b/src/common/filesystem/source/file_zip.cpp @@ -445,8 +445,8 @@ FCompressedBuffer FZipLump::GetRawData() { FCompressedBuffer cbuf = { (unsigned)LumpSize, (unsigned)CompressedSize, Method, GPFlags, CRC32, new char[CompressedSize] }; if (NeedFileStart) SetLumpAddress(); - Owner->Reader.Seek(Position, FileReader::SeekSet); - Owner->Reader.Read(cbuf.mBuffer, CompressedSize); + Owner->GetContainerReader()->Seek(Position, FileReader::SeekSet); + Owner->GetContainerReader()->Read(cbuf.mBuffer, CompressedSize); return cbuf; } @@ -464,8 +464,8 @@ void FZipLump::SetLumpAddress() FZipLocalFileHeader localHeader; int skiplen; - Owner->Reader.Seek(Position, FileReader::SeekSet); - Owner->Reader.Read(&localHeader, sizeof(localHeader)); + Owner->GetContainerReader()->Seek(Position, FileReader::SeekSet); + Owner->GetContainerReader()->Read(&localHeader, sizeof(localHeader)); skiplen = LittleShort(localHeader.NameLength) + LittleShort(localHeader.ExtraLength); Position += sizeof(localHeader) + skiplen; NeedFileStart = false; @@ -484,8 +484,8 @@ FileReader *FZipLump::GetReader() if (Method == METHOD_STORED) { if (NeedFileStart) SetLumpAddress(); - Owner->Reader.Seek(Position, FileReader::SeekSet); - return &Owner->Reader; + Owner->GetContainerReader()->Seek(Position, FileReader::SeekSet); + return Owner->GetContainerReader(); } else return NULL; } @@ -501,7 +501,7 @@ int FZipLump::FillCache() if (NeedFileStart) SetLumpAddress(); const char *buffer; - if (Method == METHOD_STORED && (buffer = Owner->Reader.GetBuffer()) != NULL) + if (Method == METHOD_STORED && (buffer = Owner->GetContainerReader()->GetBuffer()) != NULL) { // This is an in-memory file so the cache can point directly to the file's data. Cache = const_cast(buffer) + Position; @@ -509,9 +509,9 @@ int FZipLump::FillCache() return -1; } - Owner->Reader.Seek(Position, FileReader::SeekSet); + Owner->GetContainerReader()->Seek(Position, FileReader::SeekSet); Cache = new char[LumpSize]; - UncompressZipLump(Cache, Owner->Reader, Method, LumpSize, CompressedSize, GPFlags, true); + UncompressZipLump(Cache, *Owner->GetContainerReader(), Method, LumpSize, CompressedSize, GPFlags, true); RefCount = 1; return 1; } @@ -548,9 +548,7 @@ FResourceFile *CheckZip(const char *filename, FileReader &file, LumpFilterInfo* { auto rf = new FZipFile(filename, file, sp); if (rf->Open(filter, Printf)) return rf; - - file = std::move(rf->Reader); // to avoid destruction of reader - delete rf; + file = rf->Destroy(); } } return NULL; diff --git a/src/common/filesystem/source/filesystem.cpp b/src/common/filesystem/source/filesystem.cpp index 007756bcebc..7d964476df4 100644 --- a/src/common/filesystem/source/filesystem.cpp +++ b/src/common/filesystem/source/filesystem.cpp @@ -1441,7 +1441,7 @@ const char *FileSystem::GetResourceFileName (int rfnum) const noexcept return NULL; } - name = Files[rfnum]->FileName; + name = Files[rfnum]->GetFileName(); slash = strrchr (name, '/'); return (slash != nullptr && slash[1] != 0) ? slash+1 : name; } @@ -1507,7 +1507,7 @@ const char *FileSystem::GetResourceFileFullName (int rfnum) const noexcept return nullptr; } - return Files[rfnum]->FileName; + return Files[rfnum]->GetFileName(); } diff --git a/src/common/filesystem/source/resourcefile.cpp b/src/common/filesystem/source/resourcefile.cpp index 99f8d08380e..d21d996e3bf 100644 --- a/src/common/filesystem/source/resourcefile.cpp +++ b/src/common/filesystem/source/resourcefile.cpp @@ -157,7 +157,7 @@ static bool IsWadInFolder(const FResourceFile* const archive, const char* const return false; } - const auto dirName = ExtractBaseName(archive->FileName); + const auto dirName = ExtractBaseName(archive->GetFileName()); const auto fileName = ExtractBaseName(resPath, true); const std::string filePath = dirName + '/' + fileName; @@ -242,7 +242,7 @@ void *FResourceLump::Lock() catch (const FileSystemException& err) { // enrich the message with info about this lump. - throw FileSystemException("%s, file '%s': %s", getName(), Owner->FileName, err.what()); + throw FileSystemException("%s, file '%s': %s", getName(), Owner->GetFileName(), err.what()); } } return Cache; @@ -638,8 +638,8 @@ int FResourceFile::FindEntry(const char *name) FileReader *FUncompressedLump::GetReader() { - Owner->Reader.Seek(Position, FileReader::SeekSet); - return &Owner->Reader; + Owner->GetContainerReader()->Seek(Position, FileReader::SeekSet); + return Owner->GetContainerReader(); } //========================================================================== @@ -650,7 +650,7 @@ FileReader *FUncompressedLump::GetReader() int FUncompressedLump::FillCache() { - const char * buffer = Owner->Reader.GetBuffer(); + const char * buffer = Owner->GetContainerReader()->GetBuffer(); if (buffer != NULL) { @@ -660,10 +660,10 @@ int FUncompressedLump::FillCache() return -1; } - Owner->Reader.Seek(Position, FileReader::SeekSet); + Owner->GetContainerReader()->Seek(Position, FileReader::SeekSet); Cache = new char[LumpSize]; - auto read = Owner->Reader.Read(Cache, LumpSize); + auto read = Owner->GetContainerReader()->Read(Cache, LumpSize); if (read != LumpSize) { throw FileSystemException("only read %d of %d bytes", (int)read, (int)LumpSize);