- 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.

# Conflicts:
#	src/resourcefiles/resourcefile.cpp

# Conflicts:
#	src/resourcefiles/resourcefile.cpp
This commit is contained in:
Christoph Oelckers 2019-09-13 12:29:17 +02:00 committed by drfrag
parent b5bb5fa0bd
commit 8980ff688c
8 changed files with 42 additions and 2 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -42,6 +42,7 @@
#include "gi.h" #include "gi.h"
#include "doomstat.h" #include "doomstat.h"
#include "w_zip.h" #include "w_zip.h"
#include "md5.h"
//========================================================================== //==========================================================================
@ -347,6 +348,39 @@ int lumpcmp(const void * a, const void * b)
return rec1->FullName.CompareNoCase(rec2->FullName); 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 // FResourceFile :: PostProcessArchive

View file

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