Skip to content

Commit

Permalink
fix #5980
Browse files Browse the repository at this point in the history
  • Loading branch information
rt committed May 14, 2018
1 parent ac4afcb commit 9ec2f05
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
17 changes: 14 additions & 3 deletions rts/Game/GameSetup.cpp
Expand Up @@ -528,17 +528,28 @@ bool CGameSetup::Init(const std::string& buf)
return false;

{
// Used by dedicated server only
// read script-provided hashes for dedicated server
const std::string mapHashHexStr = file.SGetValueDef("", "GAME\\MapHash");
const std::string modHashHexStr = file.SGetValueDef("", "GAME\\ModHash");

LOG_L(L_INFO, "[GameSetup::%s]\n\tmapHashStr=\"%s\"\n\tmodHashStr=\"%s\"", __func__, mapHashHexStr.c_str(), modHashHexStr.c_str());

sha512::hex_digest mapHashHex;
sha512::hex_digest modHashHex;
sha512::raw_digest mapHashRaw;
sha512::raw_digest modHashRaw;

std::copy(mapHashHexStr.begin(), mapHashHexStr.end(), mapHashHex.data());
std::copy(modHashHexStr.begin(), modHashHexStr.end(), modHashHex.data());
// zero-fill; {map,mod}HashHexStr might be empty or invalid SHA digests
mapHashHex.fill(0);
modHashHex.fill(0);
mapHashRaw.fill(0);
modHashRaw.fill(0);

if (mapHashHexStr.size() == (sha512::SHA_LEN * 2))
std::copy(mapHashHexStr.begin(), mapHashHexStr.end(), mapHashHex.data());
if (modHashHexStr.size() == (sha512::SHA_LEN * 2))
std::copy(modHashHexStr.begin(), modHashHexStr.end(), modHashHex.data());

sha512::read_digest(mapHashHex, mapHashRaw);
sha512::read_digest(modHashHex, modHashRaw);
std::memcpy(dsMapHash, mapHashRaw.data(), sizeof(dsMapHash));
Expand Down
53 changes: 31 additions & 22 deletions rts/builds/dedicated/main.cpp
Expand Up @@ -166,35 +166,44 @@ int main(int argc, char* argv[])
rng.Seed(randSeed);
dsGameData->SetRandomSeed(rng.NextInt());

{
sha512::raw_digest dsMapChecksum;
sha512::raw_digest dsModChecksum;
sha512::hex_digest dsMapChecksumHex;
sha512::hex_digest dsModChecksumHex;

sha512::raw_digest dsMapChecksum;
sha512::raw_digest dsModChecksum;
std::memcpy(dsMapChecksum.data(), &dsGameSetup->dsMapHash[0], sizeof(dsGameSetup->dsMapHash));
std::memcpy(dsModChecksum.data(), &dsGameSetup->dsModHash[0], sizeof(dsGameSetup->dsModHash));
sha512::dump_digest(dsMapChecksum, dsMapChecksumHex);
sha512::dump_digest(dsModChecksum, dsModChecksumHex);

std::memcpy(dsMapChecksum.data(), &dsGameSetup->dsMapHash[0], sizeof(dsGameSetup->dsMapHash));
std::memcpy(dsModChecksum.data(), &dsGameSetup->dsModHash[0], sizeof(dsGameSetup->dsModHash));
LOG("[script-checksums]\n\tmap=%s\n\tmod=%s", dsMapChecksumHex.data(), dsModChecksumHex.data());

// use script-provided hashes if they exist; these
// are only used for some client-side sanity checks
if (std::find_if(dsMapChecksum.begin(), dsMapChecksum.end(), [](uint8_t byte) { return (byte != 0); }) != dsMapChecksum.end()) {
dsGameData->SetMapChecksum(dsMapChecksum.data());
dsGameSetup->LoadStartPositions(false); // reduced mode
} else {
dsGameData->SetMapChecksum(&archiveScanner->GetArchiveCompleteChecksumBytes(dsGameSetup->mapName)[0]);
// use script-provided hashes if any byte is non-zero; these
// are only used by some client-side (pregame) sanity checks
const auto hashPred = [](uint8_t byte) { return (byte != 0); };

CFileHandler f("maps/" + dsGameSetup->mapName);
if (!f.FileExists())
vfsHandler->AddArchiveWithDeps(dsGameSetup->mapName, false);
if (std::find_if(dsMapChecksum.begin(), dsMapChecksum.end(), hashPred) != dsMapChecksum.end()) {
dsGameData->SetMapChecksum(dsMapChecksum.data());
dsGameSetup->LoadStartPositions(false); // reduced mode
} else {
dsGameData->SetMapChecksum(&archiveScanner->GetArchiveCompleteChecksumBytes(dsGameSetup->mapName)[0]);

dsGameSetup->LoadStartPositions(); // full mode
}
CFileHandler f("maps/" + dsGameSetup->mapName);
if (!f.FileExists())
vfsHandler->AddArchiveWithDeps(dsGameSetup->mapName, false);

if (std::find_if(dsModChecksum.begin(), dsModChecksum.end(), [](uint8_t byte) { return (byte != 0); }) != dsModChecksum.end()) {
dsGameData->SetModChecksum(dsModChecksum.data());
} else {
const std::string& modArchive = archiveScanner->ArchiveFromName(dsGameSetup->modName);
const sha512::raw_digest& modCheckSum = archiveScanner->GetArchiveCompleteChecksumBytes(modArchive);
dsGameSetup->LoadStartPositions(); // full mode
}

dsGameData->SetModChecksum(&modCheckSum[0]);
if (std::find_if(dsModChecksum.begin(), dsModChecksum.end(), hashPred) != dsModChecksum.end()) {
dsGameData->SetModChecksum(dsModChecksum.data());
} else {
const std::string& modArchive = archiveScanner->ArchiveFromName(dsGameSetup->modName);
const sha512::raw_digest& modCheckSum = archiveScanner->GetArchiveCompleteChecksumBytes(modArchive);

dsGameData->SetModChecksum(&modCheckSum[0]);
}
}

LOG("starting server...");
Expand Down

0 comments on commit 9ec2f05

Please sign in to comment.