Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make server resource file checksumming code memory-efficient #3924

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Client/mods/deathmatch/logic/CResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,7 @@ void CResource::Load()
else if (pResourceFile->IsAutoDownload())
{
// Check the file contents
if (CChecksum::GenerateChecksumFromFileUnsafe(pResourceFile->GetName()) == pResourceFile->GetServerChecksum())
{
}
else
if (CChecksum::GenerateChecksumFromFileUnsafe(pResourceFile->GetName()) != pResourceFile->GetServerChecksum())
{
HandleDownloadedFileTrouble(pResourceFile, false);
}
Expand Down
17 changes: 9 additions & 8 deletions Server/mods/deathmatch/logic/CResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,14 @@ std::future<SString> CResource::GenerateChecksumForFile(CResourceFile* pResource
if (!GetFilePath(pResourceFile->GetName(), strPath))
return SString();

std::vector<char> buffer;
FileLoad(strPath, buffer);
uint uiFileSize = buffer.size();
const char* pFileContents = uiFileSize ? buffer.data() : "";
CChecksum Checksum = CChecksum::GenerateChecksumFromBuffer(pFileContents, uiFileSize);
pResourceFile->SetLastChecksum(Checksum);
pResourceFile->SetLastFileSize(uiFileSize);
auto checksumOrError = CChecksum::GenerateChecksumFromFile(strPath);
if (std::holds_alternative<std::string>(checksumOrError))
{
return SString(std::get<std::string>(checksumOrError));
}

pResourceFile->SetLastChecksum(std::get<CChecksum>(checksumOrError));
pResourceFile->SetLastFileSizeHint(FileSize(strPath));

// Check if file is blocked
char szHashResult[33];
Expand Down Expand Up @@ -547,7 +548,7 @@ std::future<SString> CResource::GenerateChecksumForFile(CResourceFile* pResource

if (pResourceFile->GetLastChecksum() != cachedChecksum)
{
if (!FileSave(strCachedFilePath, pFileContents, uiFileSize))
if (!FileCopy(strPath, strCachedFilePath))
{
return SString("Could not copy '%s' to '%s'\n", *strPath, *strCachedFilePath);
}
Expand Down
6 changes: 3 additions & 3 deletions Server/mods/deathmatch/logic/CResourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CResourceFile
eResourceType m_type;
class CLuaMain* m_pVM;
CChecksum m_checksum; // Checksum last time this was loaded, generated by GenerateChecksum()
uint m_uiFileSize;
uint m_uiFileSizeHint;
map<string, string> m_attributeMap; // Map of attributes from the meta.xml file

public:
Expand All @@ -65,9 +65,9 @@ class CResourceFile

CChecksum GetLastChecksum() { return m_checksum; }
void SetLastChecksum(CChecksum checksum) { m_checksum = checksum; }
void SetLastFileSize(uint uiFileSize) { m_uiFileSize = uiFileSize; }
void SetLastFileSizeHint(uint uiFileSizeHint) { m_uiFileSizeHint = uiFileSizeHint; }

double GetApproxSize() { return m_uiFileSize; } // Only used by download counters
uint GetSizeHint() { return m_uiFileSizeHint; } // Only used by download counters
string GetMetaFileAttribute(const string& key) { return m_attributeMap[key]; }
SString GetCachedPathFilename(bool bForceClientCachePath = false);
};
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ bool CResourceStartPacket::Write(NetBitStreamInterface& BitStream) const
CChecksum checksum = (*iter)->GetLastChecksum();
BitStream.Write(checksum.ulCRC);
BitStream.Write((const char*)checksum.md5.data, sizeof(checksum.md5.data));
BitStream.Write((*iter)->GetApproxSize());
BitStream.Write((double)(*iter)->GetSizeHint()); // Has to be double for bitstream format compatibility
if ((*iter)->GetType() == CResourceScriptItem::RESOURCE_FILE_TYPE_CLIENT_FILE)
{
CResourceClientFileItem* pRCFItem = reinterpret_cast<CResourceClientFileItem*>(*iter);
Expand Down
Loading