- renamed the caching functions of FResourceLump.

This commit is contained in:
Christoph Oelckers 2020-04-11 13:19:59 +02:00
parent 510fc2d549
commit d12a9bb77a
4 changed files with 16 additions and 19 deletions

View file

@ -1840,7 +1840,7 @@ void G_DoLoadGame ()
SaveVersion = 0; SaveVersion = 0;
void *data = info->CacheLump(); void *data = info->Lock();
FSerializer arc(nullptr); FSerializer arc(nullptr);
if (!arc.OpenReader((const char *)data, info->LumpSize)) if (!arc.OpenReader((const char *)data, info->LumpSize))
{ {
@ -1916,7 +1916,7 @@ void G_DoLoadGame ()
return; return;
} }
data = info->CacheLump(); data = info->Lock();
if (!arc.OpenReader((const char *)data, info->LumpSize)) if (!arc.OpenReader((const char *)data, info->LumpSize))
{ {
LoadGameError("TXT_SGINFOERR"); LoadGameError("TXT_SGINFOERR");

View file

@ -58,13 +58,13 @@ public:
FLumpReader(FResourceLump *src) FLumpReader(FResourceLump *src)
: MemoryReader(NULL, src->LumpSize), source(src) : MemoryReader(NULL, src->LumpSize), source(src)
{ {
src->CacheLump(); src->Lock();
bufptr = src->Cache; bufptr = src->Cache;
} }
~FLumpReader() ~FLumpReader()
{ {
source->ReleaseCache(); source->Unlock();
} }
}; };
@ -163,9 +163,9 @@ void FResourceLump::CheckEmbedded()
FCompressedBuffer FResourceLump::GetRawData() FCompressedBuffer FResourceLump::GetRawData()
{ {
FCompressedBuffer cbuf = { (unsigned)LumpSize, (unsigned)LumpSize, METHOD_STORED, 0, 0, new char[LumpSize] }; FCompressedBuffer cbuf = { (unsigned)LumpSize, (unsigned)LumpSize, METHOD_STORED, 0, 0, new char[LumpSize] };
memcpy(cbuf.mBuffer, CacheLump(), LumpSize); memcpy(cbuf.mBuffer, Lock(), LumpSize);
Unlock();
cbuf.mCRC32 = crc32(0, (uint8_t*)cbuf.mBuffer, LumpSize); cbuf.mCRC32 = crc32(0, (uint8_t*)cbuf.mBuffer, LumpSize);
ReleaseCache();
return cbuf; return cbuf;
} }
@ -198,7 +198,7 @@ FileReader FResourceLump::NewReader()
// //
//========================================================================== //==========================================================================
void *FResourceLump::CacheLump() void *FResourceLump::Lock()
{ {
if (Cache != NULL) if (Cache != NULL)
{ {
@ -217,7 +217,7 @@ void *FResourceLump::CacheLump()
// //
//========================================================================== //==========================================================================
int FResourceLump::ReleaseCache() int FResourceLump::Unlock()
{ {
if (LumpSize > 0 && RefCount > 0) if (LumpSize > 0 && RefCount > 0)
{ {

View file

@ -102,9 +102,11 @@ public:
void CheckEmbedded(); void CheckEmbedded();
virtual FCompressedBuffer GetRawData(); virtual FCompressedBuffer GetRawData();
void *CacheLump(); void *Lock(); // validates the cache and increases the refcount.
int ReleaseCache(); int Unlock(); // decreases the refcount and frees the buffer
unsigned Size() const{ return LumpSize; }
int LockCount() const { return RefCount; }
const char* getName() { return FullName.GetChars(); } const char* getName() { return FullName.GetChars(); }
protected: protected:

View file

@ -170,7 +170,7 @@ void FSavegameManager::ReadSaveStrings()
// I_FindName only returns the file's name and not its full path // I_FindName only returns the file's name and not its full path
FString filepath = G_BuildSaveName(I_FindName(&c_file), -1); FString filepath = G_BuildSaveName(I_FindName(&c_file), -1);
FResourceFile *savegame = FResourceFile::OpenResourceFile(filepath, true, true); std::unique_ptr<FResourceFile> savegame(FResourceFile::OpenResourceFile(filepath, true, true));
if (savegame != nullptr) if (savegame != nullptr)
{ {
bool oldVer = false; bool oldVer = false;
@ -179,10 +179,9 @@ void FSavegameManager::ReadSaveStrings()
if (info == nullptr) if (info == nullptr)
{ {
// savegame info not found. This is not a savegame so leave it alone. // savegame info not found. This is not a savegame so leave it alone.
delete savegame;
continue; continue;
} }
void *data = info->CacheLump(); void *data = info->Lock();
FSerializer arc(nullptr); FSerializer arc(nullptr);
if (arc.OpenReader((const char *)data, info->LumpSize)) if (arc.OpenReader((const char *)data, info->LumpSize))
{ {
@ -197,7 +196,6 @@ void FSavegameManager::ReadSaveStrings()
{ {
// different engine or newer version: // different engine or newer version:
// not our business. Leave it alone. // not our business. Leave it alone.
delete savegame;
continue; continue;
} }
@ -213,7 +211,6 @@ void FSavegameManager::ReadSaveStrings()
else else
{ {
// different game. Skip this. // different game. Skip this.
delete savegame;
continue; continue;
} }
@ -223,7 +220,6 @@ void FSavegameManager::ReadSaveStrings()
node->bMissingWads = missing; node->bMissingWads = missing;
node->SaveTitle = title; node->SaveTitle = title;
InsertSaveNode(node); InsertSaveNode(node);
delete savegame;
} }
} }
@ -472,7 +468,7 @@ unsigned FSavegameManager::ExtractSaveData(int index)
// this should not happen because the file has already been verified. // this should not happen because the file has already been verified.
return index; return index;
} }
void *data = info->CacheLump(); void *data = info->Lock();
FSerializer arc(nullptr); FSerializer arc(nullptr);
if (arc.OpenReader((const char *)data, info->LumpSize)) if (arc.OpenReader((const char *)data, info->LumpSize))
{ {
@ -494,10 +490,9 @@ unsigned FSavegameManager::ExtractSaveData(int index)
picreader.OpenMemoryArray([=](TArray<uint8_t> &array) picreader.OpenMemoryArray([=](TArray<uint8_t> &array)
{ {
auto cache = pic->CacheLump(); auto cache = pic->Lock();
array.Resize(pic->LumpSize); array.Resize(pic->LumpSize);
memcpy(&array[0], cache, pic->LumpSize); memcpy(&array[0], cache, pic->LumpSize);
pic->ReleaseCache();
return true; return true;
}); });
PNGHandle *png = M_VerifyPNG(picreader); PNGHandle *png = M_VerifyPNG(picreader);