Skip to content

Commit

Permalink
rework some of the friend-dependent access to FResourceLump.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Dec 10, 2023
1 parent ebc808e commit df56fc6
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 47 deletions.
10 changes: 9 additions & 1 deletion src/common/filesystem/include/resourcefile.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ struct FResourceLump
class FResourceFile
{
public:
protected:
FileReader Reader;
const char* FileName;
protected:
uint32_t NumLumps;
char Hash[48];
StringPool* stringpool;
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -247,6 +248,13 @@ class FResourceFile
return l->GetRawData();
}

FileReader Destroy()
{
auto fr = std::move(Reader);
delete this;
return fr;
}


};

Expand Down
3 changes: 1 addition & 2 deletions src/common/filesystem/source/file_7z.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/common/filesystem/source/file_grp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/common/filesystem/source/file_lump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 1 addition & 3 deletions src/common/filesystem/source/file_pak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/common/filesystem/source/file_rff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/common/filesystem/source/file_ssi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 7 additions & 8 deletions src/common/filesystem/source/file_wad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ 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;
}
int FillCache() override
{
if(!Compressed)
{
const char * buffer = Owner->Reader.GetBuffer();
const char * buffer = Owner->GetContainerReader()->GetBuffer();

if (buffer != NULL)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/common/filesystem/source/file_whres.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 10 additions & 12 deletions src/common/filesystem/source/file_zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -501,17 +501,17 @@ 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<char*>(buffer) + Position;
RefCount = -1;
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;
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/common/filesystem/source/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -1507,7 +1507,7 @@ const char *FileSystem::GetResourceFileFullName (int rfnum) const noexcept
return nullptr;
}

return Files[rfnum]->FileName;
return Files[rfnum]->GetFileName();
}


Expand Down
14 changes: 7 additions & 7 deletions src/common/filesystem/source/resourcefile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

//==========================================================================
Expand All @@ -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)
{
Expand All @@ -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);
Expand Down

0 comments on commit df56fc6

Please sign in to comment.