added high score clearing for invalid data

fixed high score error messages
This commit is contained in:
thrust26 2021-01-03 23:41:08 +01:00
parent 68d54e3cf5
commit e90cb925b0
2 changed files with 24 additions and 15 deletions

View File

@ -573,7 +573,7 @@ void HighScoresManager::saveHighScores(ScoresData& data) const
json hsData = json::object();
// Add header so that if the high score format changes in the future,
// we'll know right away, without having to parse the rest of the file
// we'll know right away, without having to parse the rest of the data
hsData[VERSION] = HIGHSCORE_HEADER;
hsData[MD5] = data.md5;
hsData[VARIATION] = data.variation;
@ -608,16 +608,10 @@ void HighScoresManager::saveHighScores(ScoresData& data) const
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void HighScoresManager::loadHighScores(ScoresData& data)
{
for(uInt32 r = 0; r < NUM_RANKS; ++r)
{
data.scores[r].score = 0;
data.scores[r].special = 0;
data.scores[r].name = "";
data.scores[r].date = "";
}
ostringstream buf;
clearHighScores(data);
bool invalid = false;
try {
Variant serializedHighscore;
@ -631,10 +625,9 @@ void HighScoresManager::loadHighScores(ScoresData& data)
const json hsData = hsObject.at(DATA);
// First test if we have a valid header
// If so, do a complete high data load
// If so, do a complete high score data load
if(!hsData.contains(VERSION) || hsData.at(VERSION) != HIGHSCORE_HEADER)
buf << "Error: Incompatible high scores file for variation "
<< data.variation << ".";
buf << "Error: Incompatible high scores data for variation " << data.variation << ".";
else
{
if(!load(hsData, data)
@ -651,9 +644,11 @@ void HighScoresManager::loadHighScores(ScoresData& data)
}
catch(...) { invalid = true; }
if (invalid)
buf << "Error: Invalid data in high scores file for variation " << data.variation << ".";
if(invalid)
{
clearHighScores(data);
buf << "Error: Invalid high scores data for variation " << data.variation << ".";
}
myOSystem.frameBuffer().showTextMessage(buf.str());
}
@ -688,6 +683,18 @@ bool HighScoresManager::load(const json& hsData, ScoresData& data)
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void HighScoresManager::clearHighScores(ScoresData& data)
{
for(uInt32 r = 0; r < NUM_RANKS; ++r)
{
data.scores[r].score = 0;
data.scores[r].special = 0;
data.scores[r].name = "";
data.scores[r].date = "";
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const string HighScoresManager::VARIATIONS_COUNT = "variations_count";
const string HighScoresManager::VARIATIONS_ADDRESS = "variations_address";

View File

@ -243,6 +243,8 @@ class HighScoresManager
*/
bool load(const json& hsData, HSM::ScoresData& scores);
void clearHighScores(HSM::ScoresData& data);
private:
// Reference to the osystem object
OSystem& myOSystem;