Remove code for CRC in most places, except where needed for net compat

This should path the way to remove it altogether in the future.
This commit is contained in:
heinrich5991 2020-10-10 22:06:47 +02:00
parent 812c5c17b9
commit 6ff53358d3
8 changed files with 52 additions and 65 deletions

View file

@ -1165,8 +1165,8 @@ const char *CClient::LoadMap(const char *pName, const char *pFilename, SHA256_DI
return s_aErrorMsg;
}
// get the crc of the map
if(m_pMap->Crc() != WantedCrc)
// Only check CRC if we don't have the secure SHA256.
if(!pWantedSha256 && m_pMap->Crc() != WantedCrc)
{
str_format(s_aErrorMsg, sizeof(s_aErrorMsg), "map differs from the server. %08x != %08x", m_pMap->Crc(), WantedCrc);
m_pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "client", s_aErrorMsg);
@ -1196,15 +1196,17 @@ const char *CClient::LoadMapSearch(const char *pMapName, SHA256_DIGEST *pWantedS
char aWanted[256];
char aWantedSha256[SHA256_MAXSTRSIZE];
aWanted[0] = 0;
if(pWantedSha256)
{
sha256_str(*pWantedSha256, aWantedSha256, sizeof(aWantedSha256));
str_format(aWanted, sizeof(aWanted), " sha256=%s", aWantedSha256);
str_format(aWanted, sizeof(aWanted), "sha256=%s", aWantedSha256);
}
else
{
str_format(aWanted, sizeof(aWanted), "crc=%08x", WantedCrc);
}
str_format(aBuf, sizeof(aBuf), "loading map, map=%s wanted%s crc=%08x", pMapName, aWanted, WantedCrc);
str_format(aBuf, sizeof(aBuf), "loading map, map=%s wanted %s", pMapName, aWanted);
m_pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "client", aBuf);
SetState(IClient::STATE_LOADING);
@ -1217,14 +1219,15 @@ const char *CClient::LoadMapSearch(const char *pMapName, SHA256_DIGEST *pWantedS
// try the downloaded maps
if(pWantedSha256)
{
str_format(aBuf, sizeof(aBuf), "downloadedmaps/%s_%08x_%s.map", pMapName, WantedCrc, aWantedSha256);
str_format(aBuf, sizeof(aBuf), "downloadedmaps/%s_%s.map", pMapName, aWantedSha256);
}
else
{
str_format(aBuf, sizeof(aBuf), "downloadedmaps/%s_%08x.map", pMapName, WantedCrc);
pError = LoadMap(pMapName, aBuf, pWantedSha256, WantedCrc);
if(!pError)
return pError;
}
// try the downloaded maps folder without appending the sha256
str_format(aBuf, sizeof(aBuf), "downloadedmaps/%s_%08x.map", pMapName, WantedCrc);
pError = LoadMap(pMapName, aBuf, pWantedSha256, WantedCrc);
if(!pError)
return pError;
@ -1566,28 +1569,23 @@ bool CClient::ShouldSendChatTimeoutCodeHeuristic()
static void FormatMapDownloadFilename(const char *pName, SHA256_DIGEST *pSha256, int Crc, bool Temp, char *pBuffer, int BufferSize)
{
char aSha256[SHA256_MAXSTRSIZE + 1];
aSha256[0] = 0;
char aHash[SHA256_MAXSTRSIZE];
if(pSha256)
{
aSha256[0] = '_';
sha256_str(*pSha256, aSha256 + 1, sizeof(aSha256) - 1);
sha256_str(*pSha256, aHash, sizeof(aHash));
}
else
{
str_format(aHash, sizeof(aHash), "%08x", Crc);
}
if(Temp)
{
str_format(pBuffer, BufferSize, "%s_%08x%s.map.%d.tmp",
pName,
Crc,
aSha256,
pid());
str_format(pBuffer, BufferSize, "%s_%s.map.%d.tmp", pName, aHash, pid());
}
else
{
str_format(pBuffer, BufferSize, "%s_%08x%s.map",
pName,
Crc,
aSha256);
str_format(pBuffer, BufferSize, "%s_%s.map", pName, aHash);
}
}

View file

@ -2265,8 +2265,6 @@ int CServer::LoadMap(const char *pMapName)
sha256_str(m_aCurrentMapSha256[SIX], aSha256, sizeof(aSha256));
str_format(aBufMsg, sizeof(aBufMsg), "%s sha256 is %s", aBuf, aSha256);
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "server", aBufMsg);
str_format(aBufMsg, sizeof(aBufMsg), "%s crc is %08x", aBuf, m_aCurrentMapCrc[SIX]);
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "server", aBufMsg);
str_copy(m_aCurrentMap, pMapName, sizeof(m_aCurrentMap));
@ -2304,8 +2302,6 @@ int CServer::LoadMap(const char *pMapName)
sha256_str(m_aCurrentMapSha256[SIXUP], aSha256, sizeof(aSha256));
str_format(aBufMsg, sizeof(aBufMsg), "%s sha256 is %s", aBuf, aSha256);
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "sixup", aBufMsg);
str_format(aBufMsg, sizeof(aBufMsg), "%s crc is %08x", aBuf, m_aCurrentMapCrc[SIXUP]);
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "sixup", aBufMsg);
}
}

View file

@ -84,15 +84,13 @@ int CDemoRecorder::Start(class IStorage *pStorage, class IConsole *pConsole, con
// try the downloaded maps
if(pSha256)
{
str_format(aMapFilename, sizeof(aMapFilename), "downloadedmaps/%s_%08x_%s.map", pMap, Crc, aSha256);
MapFile = pStorage->OpenFile(aMapFilename, IOFLAG_READ, IStorage::TYPE_ALL);
str_format(aMapFilename, sizeof(aMapFilename), "downloadedmaps/%s_%s.map", pMap, aSha256);
}
if(!MapFile)
else
{
// try the downloaded maps without sha
str_format(aMapFilename, sizeof(aMapFilename), "downloadedmaps/%s_%08x.map", pMap, Crc);
MapFile = pStorage->OpenFile(aMapFilename, IOFLAG_READ, IStorage::TYPE_ALL);
}
MapFile = pStorage->OpenFile(aMapFilename, IOFLAG_READ, IStorage::TYPE_ALL);
if(!MapFile)
{
// try the normal maps folder
@ -849,7 +847,7 @@ bool CDemoPlayer::ExtractMap(class IStorage *pStorage)
// construct name
char aSha[SHA256_MAXSTRSIZE], aMapFilename[128];
sha256_str(Sha256, aSha, sizeof(aSha));
str_format(aMapFilename, sizeof(aMapFilename), "downloadedmaps/%s_%08x_%s.map", m_Info.m_Header.m_aMapName, m_MapInfo.m_Crc, aSha);
str_format(aMapFilename, sizeof(aMapFilename), "downloadedmaps/%s_%s.map", m_Info.m_Header.m_aMapName, aSha);
// save map
IOHANDLE MapFile = pStorage->OpenFile(aMapFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE);

View file

@ -958,22 +958,21 @@ void CMenus::RenderDemoList(CUIRect MainView)
Labels.HSplitTop(5.0f, 0, &Labels);
Labels.HSplitTop(20.0f, &Left, &Labels);
Left.VSplitLeft(150.0f, &Left, &Right);
UI()->DoLabelScaled(&Left, Localize("Crc:"), 14.0f, -1);
str_format(aBuf, sizeof(aBuf), "%08x", m_lDemos[m_DemolistSelectedIndex].m_MapInfo.m_Crc);
UI()->DoLabelScaled(&Right, aBuf, 14.0f, -1);
Labels.HSplitTop(5.0f, 0, &Labels);
Labels.HSplitTop(20.0f, &Left, &Labels);
if(m_lDemos[m_DemolistSelectedIndex].m_MapInfo.m_Sha256 != SHA256_ZEROED)
{
Left.VSplitLeft(150.0f, &Left, &Right);
UI()->DoLabelScaled(&Left, "SHA256:", 14.0f, -1);
char aSha[SHA256_MAXSTRSIZE];
sha256_str(m_lDemos[m_DemolistSelectedIndex].m_MapInfo.m_Sha256, aSha, sizeof(aSha) / 2);
UI()->DoLabelScaled(&Right, aSha, Right.w > 235 ? 14.0f : 11.0f, -1);
Labels.HSplitTop(5.0f, 0, &Labels);
Labels.HSplitTop(20.0f, &Left, &Labels);
}
else
{
UI()->DoLabelScaled(&Left, Localize("Crc:"), 14.0f, -1);
str_format(aBuf, sizeof(aBuf), "%08x", m_lDemos[m_DemolistSelectedIndex].m_MapInfo.m_Crc);
UI()->DoLabelScaled(&Right, aBuf, 14.0f, -1);
}
Labels.HSplitTop(5.0f, 0, &Labels);
Labels.HSplitTop(20.0f, &Left, &Labels);
Left.VSplitLeft(150.0f, &Left, &Right);
UI()->DoLabelScaled(&Left, Localize("Netversion:"), 14.0f, -1);

View file

@ -7,13 +7,11 @@ struct CMapDescription
const char *m_pName;
int m_Size;
SHA256_DIGEST m_Sha256;
int m_Crc;
bool operator==(const CMapDescription &Other) const
{
return str_comp(m_pName, Other.m_pName) == 0 &&
m_Size == Other.m_Size &&
m_Crc == Other.m_Crc;
m_Size == Other.m_Size;
}
};
@ -45,11 +43,11 @@ static SHA256_DIGEST s(const char *pSha256)
static CMapBugsInternal MAP_BUGS[] =
{
{{"Binary", 2022597, s("65b410e197fd2298ec270e89a84b762f6739d1d18089529f8ef6cf2104d3d600"), 0x0ae3a3d5}, BugToFlag(BUG_GRENADE_DOUBLEEXPLOSION)}};
{{"Binary", 2022597, s("65b410e197fd2298ec270e89a84b762f6739d1d18089529f8ef6cf2104d3d600")}, BugToFlag(BUG_GRENADE_DOUBLEEXPLOSION)}};
CMapBugs GetMapBugs(const char *pName, int Size, SHA256_DIGEST Sha256, int Crc)
CMapBugs GetMapBugs(const char *pName, int Size, SHA256_DIGEST Sha256)
{
CMapDescription Map = {pName, Size, Sha256, Crc};
CMapDescription Map = {pName, Size, Sha256};
CMapBugs Result;
Result.m_Extra = 0;
for(unsigned int i = 0; i < sizeof(MAP_BUGS) / sizeof(MAP_BUGS[0]); i++)
@ -122,10 +120,9 @@ void CMapBugs::Dump() const
{
char aSha256[SHA256_MAXSTRSIZE];
sha256_str(pInternal->m_Map.m_Sha256, aSha256, sizeof(aSha256));
dbg_msg("mapbugs", "map='%s' map_size=%d map_sha256=%s map_crc=%08x",
dbg_msg("mapbugs", "map='%s' map_size=%d map_sha256=%s",
pInternal->m_Map.m_pName,
pInternal->m_Map.m_Size,
aSha256,
pInternal->m_Map.m_Crc);
aSha256);
}
}

View file

@ -20,7 +20,7 @@ enum
class CMapBugs
{
friend CMapBugs GetMapBugs(const char *pName, int Size, SHA256_DIGEST Sha256, int Crc);
friend CMapBugs GetMapBugs(const char *pName, int Size, SHA256_DIGEST Sha256);
void *m_pData;
unsigned int m_Extra;
@ -30,5 +30,5 @@ public:
void Dump() const;
};
CMapBugs GetMapBugs(const char *pName, int Size, SHA256_DIGEST Sha256, int Crc);
CMapBugs GetMapBugs(const char *pName, int Size, SHA256_DIGEST Sha256);
#endif // GAME_MAPBUGS_H

View file

@ -3019,7 +3019,7 @@ void CGameContext::OnInit(/*class IKernel *pKernel*/)
SHA256_DIGEST MapSha256;
int MapCrc;
Server()->GetMapInfo(aMapName, sizeof(aMapName), &MapSize, &MapSha256, &MapCrc);
m_MapBugs = GetMapBugs(aMapName, MapSize, MapSha256, MapCrc);
m_MapBugs = GetMapBugs(aMapName, MapSize, MapSha256);
// reset everything here
//world = new GAMEWORLD;

View file

@ -6,32 +6,31 @@
static const char *BINARY_SHA256 = "65b410e197fd2298ec270e89a84b762f6739d1d18089529f8ef6cf2104d3d600";
static const char *DM1_SHA256 = "0b0c481d77519c32fbe85624ef16ec0fa9991aec7367ad538bd280f28d8c26cf";
static CMapBugs GetMapBugsImpl(const char *pName, int Size, const char *pSha256, int Crc)
static CMapBugs GetMapBugsImpl(const char *pName, int Size, const char *pSha256)
{
SHA256_DIGEST Sha256 = {{0}};
dbg_assert(sha256_from_str(&Sha256, pSha256) == 0, "invalid sha256 in tests");
return GetMapBugs(pName, Size, Sha256, Crc);
return GetMapBugs(pName, Size, Sha256);
}
TEST(MapBugs, Contains)
{
EXPECT_TRUE(GetMapBugsImpl("Binary", 2022597, BINARY_SHA256, 0x0ae3a3d5).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugsImpl("Binarx", 2022597, BINARY_SHA256, 0x0ae3a3d5).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugsImpl("Binary", 2022597, BINARY_SHA256, 0x0ae3a3d6).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugsImpl("Binary", 2022598, BINARY_SHA256, 0x0ae3a3d5).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugsImpl("dm1", 5805, DM1_SHA256, 0xf2159e6e).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_TRUE(GetMapBugsImpl("Binary", 2022597, BINARY_SHA256).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugsImpl("Binarx", 2022597, BINARY_SHA256).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugsImpl("Binary", 2022598, BINARY_SHA256).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugsImpl("dm1", 5805, DM1_SHA256).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
}
TEST(MapBugs, Update)
{
{
CMapBugs Binary = GetMapBugsImpl("Binary", 2022597, BINARY_SHA256, 0x0ae3a3d5);
CMapBugs Binary = GetMapBugsImpl("Binary", 2022597, BINARY_SHA256);
EXPECT_EQ(Binary.Update("grenade-doubleexplosion@ddnet.tw"), MAPBUGUPDATE_OVERRIDDEN);
EXPECT_EQ(Binary.Update("doesntexist@invalid"), MAPBUGUPDATE_NOTFOUND);
EXPECT_TRUE(Binary.Contains(BUG_GRENADE_DOUBLEEXPLOSION));
}
{
CMapBugs Dm1 = GetMapBugsImpl("dm1", 5805, DM1_SHA256, 0xf2159e6e);
CMapBugs Dm1 = GetMapBugsImpl("dm1", 5805, DM1_SHA256);
EXPECT_FALSE(Dm1.Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_EQ(Dm1.Update("doesntexist@invalid"), MAPBUGUPDATE_NOTFOUND);
EXPECT_FALSE(Dm1.Contains(BUG_GRENADE_DOUBLEEXPLOSION));
@ -42,6 +41,6 @@ TEST(MapBugs, Update)
TEST(MapBugs, Dump)
{
GetMapBugsImpl("Binary", 2022597, BINARY_SHA256, 0x0ae3a3d5).Dump();
GetMapBugsImpl("dm1", 5805, DM1_SHA256, 0xf2159e6e).Dump();
GetMapBugsImpl("Binary", 2022597, BINARY_SHA256).Dump();
GetMapBugsImpl("dm1", 5805, DM1_SHA256).Dump();
}