merge FileData and ResourceData.

This commit is contained in:
Christoph Oelckers 2023-12-10 22:30:54 +01:00
commit fe106d9bfe
38 changed files with 77 additions and 118 deletions

View file

@ -91,7 +91,7 @@ enum
class FileReader;
// an opaque memory buffer to the file's content. Can either own the memory or just point to an external buffer.
class ResourceData
class FileData
{
void* memory;
size_t length;
@ -99,13 +99,13 @@ class ResourceData
public:
using value_type = uint8_t;
ResourceData() { memory = nullptr; length = 0; owned = true; }
FileData() { memory = nullptr; length = 0; owned = true; }
const void* data() const { return memory; }
size_t size() const { return length; }
const char* string() const { return (const char*)memory; }
const uint8_t* bytes() const { return (const uint8_t*)memory; }
ResourceData& operator = (const ResourceData& copy)
FileData& operator = (const FileData& copy)
{
if (owned && memory) free(memory);
length = copy.length;
@ -119,7 +119,7 @@ public:
return *this;
}
ResourceData& operator = (ResourceData&& copy) noexcept
FileData& operator = (FileData&& copy) noexcept
{
if (owned && memory) free(memory);
length = copy.length;
@ -131,13 +131,13 @@ public:
return *this;
}
ResourceData(const ResourceData& copy)
FileData(const FileData& copy)
{
memory = nullptr;
*this = copy;
}
~ResourceData()
~FileData()
{
if (owned && memory) free(memory);
}
@ -254,7 +254,7 @@ public:
bool OpenMemory(const void *mem, Size length); // read directly from the buffer
bool OpenMemoryArray(const void *mem, Size length); // read from a copy of the buffer.
bool OpenMemoryArray(std::vector<uint8_t>& data); // take the given array
bool OpenMemoryArray(ResourceData& data); // take the given array
bool OpenMemoryArray(FileData& data); // take the given array
bool OpenMemoryArray(std::function<bool(std::vector<uint8_t>&)> getter); // read contents to a buffer and return a reader to it
bool OpenDecompressor(FileReader &parent, Size length, int method, bool seekable, bool exceptions = false); // creates a decompressor stream. 'seekable' uses a buffered version so that the Seek and Tell methods can be used.
@ -273,9 +273,9 @@ public:
return mReader->Read(buffer, len);
}
ResourceData Read(size_t len)
FileData Read(size_t len)
{
ResourceData buffer;
FileData buffer;
if (len > 0)
{
Size length = mReader->Read(buffer.allocate(len), len);
@ -284,15 +284,15 @@ public:
return buffer;
}
ResourceData Read()
FileData Read()
{
return Read(GetLength());
}
ResourceData ReadPadded(size_t padding)
FileData ReadPadded(size_t padding)
{
auto len = GetLength();
ResourceData buffer;
FileData buffer;
if (len > 0)
{

View file

@ -22,42 +22,6 @@ union LumpShortName
};
// A lump in memory.
class FileData
{
public:
FileData() { lump = nullptr; }
const void *GetMem () { return lump->Cache; }
size_t GetSize () { return lump->LumpSize; }
const char* GetString () const { return (const char*)lump->Cache; }
const uint8_t* GetBytes() const { return (const uint8_t*)lump->Cache; }
FileData& operator = (const FileData& copy) = delete;
FileData(const FileData& copy)
{
lump = copy.lump;
lump->Lock();
}
~FileData()
{
if (lump) lump->Unlock();
}
private:
FileData(FResourceLump* nlump)
{
lump = nlump;
if (lump) lump->Lock();
}
FResourceLump* lump;
friend class FileSystem;
};
struct FolderEntry
{
const char *name;

View file

@ -249,7 +249,7 @@ public:
return l ? l->GetIndexNum() : 0;
}
ResourceData Read(int entry)
FileData Read(int entry)
{
auto fr = GetEntryReader(entry, false);
return fr.Read();

View file

@ -406,10 +406,10 @@ bool FileReader::OpenMemoryArray(std::vector<uint8_t>& data)
return true;
}
bool FileReader::OpenMemoryArray(ResourceData& data)
bool FileReader::OpenMemoryArray(FileData& data)
{
Close();
if (data.size() > 0) mReader = new MemoryArrayReader<ResourceData>(data);
if (data.size() > 0) mReader = new MemoryArrayReader<FileData>(data);
return true;
}

View file

@ -306,7 +306,6 @@ int FileSystem::AddFromBuffer(const char* name, char* data, int size, int id, in
if (rf)
{
Files.push_back(rf);
FResourceLump* lump = rf->GetLump(0);
FileInfo.resize(FileInfo.size() + 1);
FileSystem::LumpRecord* lump_p = &FileInfo.back();
lump_p->SetFromLump(rf, 0, (int)Files.size() - 1, stringpool);
@ -379,7 +378,6 @@ void FileSystem::AddFile (const char *filename, FileReader *filer, LumpFilterInf
Files.push_back(resfile);
for (int i = 0; i < resfile->EntryCount(); i++)
{
FResourceLump* lump = resfile->GetLump(i);
FileInfo.resize(FileInfo.size() + 1);
FileSystem::LumpRecord* lump_p = &FileInfo.back();
lump_p->SetFromLump(resfile, i, (int)Files.size() - 1, stringpool);
@ -1312,10 +1310,7 @@ FileData FileSystem::ReadFile (int lump)
{
throw FileSystemException("ReadFile: %u >= NumEntries", lump);
}
auto file = FileInfo[lump].resfile;
auto lumpp = file->GetLump(FileInfo[lump].resindex);
return FileData(lumpp);
return FileInfo[lump].resfile->Read(FileInfo[lump].resindex);
}
//==========================================================================