- generate a hash identifier for each loaded resource file.

This is not useful by itself but can be used for adding new features later, e.g. mod-wide compatibility settings or file specific lump replacement or injection.
This commit is contained in:
Christoph Oelckers 2019-09-13 12:29:17 +02:00
parent bcef440511
commit 31aa855a51
8 changed files with 42 additions and 2 deletions

View file

@ -316,6 +316,7 @@ bool F7ZFile::Open(bool quiet)
if (!quiet && !batchrun) Printf(", %d lumps\n", NumLumps);
GenerateHash();
PostProcessArchive(&Lumps[0], sizeof(F7ZLump));
return true;
}

View file

@ -120,7 +120,7 @@ bool FGrpFile::Open(bool quiet)
Lumps[i].LumpNameSetup(fileinfo[i].NameWithZero);
}
if (!quiet && !batchrun) Printf(", %d lumps\n", NumLumps);
GenerateHash();
delete[] fileinfo;
return true;
}

View file

@ -113,7 +113,7 @@ bool FPakFile::Open(bool quiet)
Lumps[i].LumpSize = LittleLong(fileinfo[i].filelen);
Lumps[i].CheckEmbedded();
}
GenerateHash();
return true;
}

View file

@ -184,6 +184,7 @@ bool FRFFFile::Open(bool quiet)
}
}
delete[] lumps;
GenerateHash();
return true;
}

View file

@ -203,6 +203,7 @@ bool FWadFile::Open(bool quiet)
}
delete[] fileinfo;
GenerateHash(); // Do this before the lump processing below.
if (!quiet)
{

View file

@ -363,6 +363,7 @@ bool FZipFile::Open(bool quiet)
if (!quiet && !batchrun) Printf(TEXTCOLOR_NORMAL ", %d lumps\n", NumLumps);
GenerateHash();
PostProcessArchive(&Lumps[0], sizeof(FZipLump));
return true;
}

View file

@ -41,6 +41,7 @@
#include "gi.h"
#include "doomstat.h"
#include "doomtype.h"
#include "md5.h"
//==========================================================================
@ -353,6 +354,39 @@ int lumpcmp(const void * a, const void * b)
return rec1->FullName.CompareNoCase(rec2->FullName);
}
//==========================================================================
//
// FResourceFile :: GenerateHash
//
// Generates a hash identifier for use in file identification.
// Potential uses are mod-wide compatibility settings or localization add-ons.
// This only hashes the lump directory but not the actual content
//
//==========================================================================
void FResourceFile::GenerateHash()
{
// hash the lump directory after sorting
Hash.Format(("%08X-%04X-"), (unsigned)Reader.GetLength(), NumLumps);
MD5Context md5;
uint8_t digest[16];
for(uint32_t i = 0; i < NumLumps; i++)
{
auto lump = GetLump(i);
md5.Update((const uint8_t*)lump->Name, strlen(lump->Name) + 1); // +1 to hash the terminating 0 as well.
md5.Update((const uint8_t*)lump->FullName.GetChars(), lump->FullName.Len() + 1);
md5.Update((const uint8_t*)&lump->LumpSize, 4);
}
md5.Final(digest);
for (auto c : digest)
{
Hash.AppendFormat("%02X", c);
}
}
//==========================================================================
//
// FResourceFile :: PostProcessArchive

View file

@ -85,11 +85,13 @@ public:
FString FileName;
protected:
uint32_t NumLumps;
FString Hash;
FResourceFile(const char *filename);
FResourceFile(const char *filename, FileReader &r);
// for archives that can contain directories
void GenerateHash();
void PostProcessArchive(void *lumps, size_t lumpsize);
private: