Skip to content

Commit

Permalink
did some cleanup of the FResourceFile interface.
Browse files Browse the repository at this point in the history
* making all members protected (but adding friend overrides for the classes which still need it)
* allowing to read data without retrieving the FResourceLump object.
  • Loading branch information
coelckers committed Dec 10, 2023
1 parent fc84579 commit ebc808e
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 92 deletions.
10 changes: 5 additions & 5 deletions src/common/audio/music/i_soundfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion src/common/filesystem/include/fs_files.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 60 additions & 7 deletions src/common/filesystem/include/resourcefile.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,47 @@ 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;
Owner = NULL;
Flags = 0;
RefCount = 0;
FullName = "";
LumpSize = 0;
}

virtual ~FResourceLump();

protected:

virtual FileReader *GetReader();
virtual FileReader NewReader();
virtual int GetFileOffset() { return -1; }
Expand Down Expand Up @@ -186,15 +205,49 @@ class FResourceFile
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();
}


};


Expand Down
2 changes: 1 addition & 1 deletion src/common/filesystem/source/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ FileReader *FileSystem::GetFileReader(int rfnum)
return NULL;
}

return Files[rfnum]->GetReader();
return Files[rfnum]->GetContainerReader();
}

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

//==========================================================================
Expand Down
13 changes: 6 additions & 7 deletions src/common/fonts/hexfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -97,7 +97,6 @@ struct HexDataSource
lumb = i * 255 / 17;
SmallPal[i] = PalEntry(255, lumb, lumb, lumb);
}
font->Unlock();
}
};

Expand Down Expand Up @@ -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;
}
25 changes: 7 additions & 18 deletions src/common/menu/savegamemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> &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)
{
Expand Down
16 changes: 8 additions & 8 deletions src/g_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2002,18 +2002,18 @@ 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;
}

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;
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 16 additions & 20 deletions src/g_level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/maploader/glnodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -793,13 +793,13 @@ static int FindGLNodesInFile(FResourceFile * f, const char * label)
{
for(uint32_t i=0;i<numentries-4;i++)
{
if (!strnicmp(f->GetLump(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;
Expand Down Expand Up @@ -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);
}
Expand Down
Loading

0 comments on commit ebc808e

Please sign in to comment.