Skip to content

Commit

Permalink
Save and load map hashes in config file.
Browse files Browse the repository at this point in the history
This prevents a zero hash from being sent by the host when the user has not selected the map between starting the
program and hosting. This also means remembering exactly which map was last used, even if multiple maps have the
same name.
  • Loading branch information
Cyp committed Jun 20, 2012
1 parent 0c55d94 commit 9784c65
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
28 changes: 28 additions & 0 deletions lib/framework/crc.cpp
Expand Up @@ -190,3 +190,31 @@ std::string Sha256::toString() const
}
return str;
}

void Sha256::fromString(std::string const &s)
{
setZero();
unsigned nChars = std::min<unsigned>(Bytes*2, s.size());
for (unsigned n = 0; n < nChars; ++n)
{
unsigned h;
unsigned c = s[n];
if (c >= '0' && c <= '9')
{
h = c - '0';
}
else if (c >= 'a' && c <= 'f')
{
h = c - 'a' + 10;
}
else if (c >= 'A' && c <= 'F')
{
h = c - 'A' + 10;
}
else
{
break;
}
bytes[n/2] |= h << (n%2? 0 : 4);
}
}
1 change: 1 addition & 0 deletions lib/framework/crc.h
Expand Up @@ -38,6 +38,7 @@ struct Sha256
bool isZero() const;
void setZero();
std::string toString() const;
void fromString(std::string const &s);

uint8_t bytes[Bytes];
};
Expand Down
6 changes: 4 additions & 2 deletions src/configuration.cpp
Expand Up @@ -98,7 +98,7 @@ bool loadConfig()
if (ini.contains("mapName") && ini.contains("maxPlayers"))
{
sstrcpy(game.map, ini.value("mapName").toString().toUtf8().constData());
game.hash.setZero();
game.hash.fromString(ini.value("mapHash").toString().toUtf8().constData());
game.maxPlayers = ini.value("maxPlayers").toInt(); // FIXME: horrible kluge, MUST match map above
}
else
Expand Down Expand Up @@ -212,6 +212,7 @@ bool saveConfig()
ini.setValue("gameName", game.name); // last hosted game
}
ini.setValue("mapName", game.map); // map name
ini.setValue("mapHash", game.hash.toString().c_str()); // map hash
ini.setValue("maxPlayers", game.maxPlayers); // maxPlayers
ini.setValue("power", game.power); // power
ini.setValue("base", game.base); // size of base
Expand Down Expand Up @@ -268,6 +269,7 @@ bool reloadMPConfig(void)
}
}
ini.setValue("mapName", game.map); // map name
ini.setValue("mapHash", game.hash.toString().c_str()); // map hash
ini.setValue("maxPlayers", game.maxPlayers); // maxPlayers
ini.setValue("power", game.power); // power
ini.setValue("base", game.base); // size of base
Expand All @@ -285,7 +287,7 @@ bool reloadMPConfig(void)
if (ini.contains("mapName") && ini.contains("maxPlayers"))
{
sstrcpy(game.map, ini.value("mapName").toString().toUtf8().constData());
game.hash.setZero();
game.hash.fromString(ini.value("mapHash").toString().toUtf8().constData());
game.maxPlayers = ini.value("maxPlayers").toInt(); // FIXME: horrible kluge, MUST match map above
}
game.power = ini.value("power", LEV_MED).toInt();
Expand Down
13 changes: 13 additions & 0 deletions src/levels.cpp
Expand Up @@ -176,6 +176,19 @@ Sha256 levGetFileHash(LEVEL_DATASET *level)
return level->realFileHash;
}

Sha256 levGetMapNameHash(char const *mapName)
{
LEVEL_DATASET *level = levFindDataSet(mapName, NULL);
if (level == NULL)
{
debug(LOG_WARNING, "Couldn't find map \"%s\" to hash.", mapName);
Sha256 zero;
zero.setZero();
return zero;
}
return levGetFileHash(level);
}

// parse a level description data file
// the ignoreWrf hack is for compatibility with old maps that try to link in various
// data files that we have removed
Expand Down
1 change: 1 addition & 0 deletions src/levels.h
Expand Up @@ -93,6 +93,7 @@ bool levLoadData(char const *name, Sha256 const *hash, char *pSaveName, GAME_TYP
LEVEL_DATASET *levFindDataSet(char const *name, Sha256 const *hash = NULL);

Sha256 levGetFileHash(LEVEL_DATASET *level);
Sha256 levGetMapNameHash(char const *name);

// free the currently loaded dataset
extern bool levReleaseAll(void);
Expand Down
6 changes: 3 additions & 3 deletions src/multiint.cpp
Expand Up @@ -446,7 +446,7 @@ void loadMapPreview(bool hideInterface)
psLevel = levFindDataSet(game.map, &game.hash);
if (psLevel == NULL)
{
debug(LOG_INFO, "Could not find level dataset. Probably waiting for download.");
debug(LOG_INFO, "Could not find level dataset \"%s\" %s. Probably waiting for download.", game.map, game.hash.toString().c_str());
loadEmptyMapPreview();
return;
}
Expand Down Expand Up @@ -3758,7 +3758,7 @@ bool startMultiOptions(bool bReenter)
game.type = SKIRMISH;
game.scavengers = false;
sstrcpy(game.map, DEFAULTSKIRMISHMAP);
game.hash.setZero();
game.hash = levGetMapNameHash(game.map);
game.maxPlayers = 4;
}

Expand All @@ -3783,7 +3783,7 @@ bool startMultiOptions(bool bReenter)

challenge.beginGroup("challenge");
sstrcpy(game.map, challenge.value("Map", game.map).toString().toAscii().constData());
game.hash.setZero();
game.hash = levGetMapNameHash(game.map);
game.maxPlayers = challenge.value("MaxPlayers", game.maxPlayers).toInt(); // TODO, read from map itself, not here!!
game.scavengers = challenge.value("Scavengers", game.scavengers).toInt();
game.alliance = ALLIANCES_TEAMS;
Expand Down

0 comments on commit 9784c65

Please sign in to comment.