bool TryRecordHighscore(const utf8 * scenarioFileName, money32 companyValue, const utf8 * name) override { scenario_index_entry * scenario = GetByFilename(scenarioFileName); if (scenario != nullptr) { // Check if record company value has been broken or the highscore is the same but no name is registered scenario_highscore_entry * highscore = scenario->highscore; if (highscore == nullptr || companyValue > highscore->company_value || (highscore->name == nullptr && companyValue == highscore->company_value)) { if (highscore == nullptr) { highscore = InsertHighscore(); highscore->timestamp = platform_get_datetime_now_utc(); scenario->highscore = highscore; } else { if (highscore->name != nullptr) { highscore->timestamp = platform_get_datetime_now_utc(); } SafeFree(highscore->fileName); SafeFree(highscore->name); } highscore->fileName = String::Duplicate(Path::GetFileName(scenario->path)); highscore->name = String::Duplicate(name); highscore->company_value = companyValue; SaveHighscores(); return true; } } return false; }
PlayingState::~PlayingState(){ delete map_loader; for(list<GameObject*>::iterator iter = gobjects.begin(); iter != gobjects.end(); iter++) (*iter)->Destroy(); delete gui; SaveHighscores(); // we save list of highscores to file }
void LoadLegacyScores(const std::string &path) { if (!platform_file_exists(path.c_str())) { return; } bool highscoresDirty = false; try { auto fs = FileStream(path, FILE_MODE_OPEN); if (fs.GetLength() <= 4) { // Initial value of scores for RCT2, just ignore return; } // Load header auto header = fs.ReadValue<rct_scenario_scores_header>(); for (uint32 i = 0; i < header.scenario_count; i++) { // Read legacy entry auto scBasic = fs.ReadValue<rct_scenario_basic>(); // Ignore non-completed scenarios if (scBasic.flags & SCENARIO_FLAGS_COMPLETED) { bool notFound = true; for (size_t i = 0; i < _highscores.size(); i++) { scenario_highscore_entry * highscore = _highscores[i]; if (String::Equals(scBasic.path, highscore->fileName, true)) { notFound = false; // Check if legacy highscore is better if (scBasic.company_value > highscore->company_value) { SafeFree(highscore->name); highscore->name = win1252_to_utf8_alloc(scBasic.completed_by, Util::CountOf(scBasic.completed_by)); highscore->company_value = scBasic.company_value; highscore->timestamp = DATETIME64_MIN; break; } } } if (notFound) { scenario_highscore_entry * highscore = InsertHighscore(); highscore->fileName = String::Duplicate(scBasic.path); highscore->name = win1252_to_utf8_alloc(scBasic.completed_by, Util::CountOf(scBasic.completed_by)); highscore->company_value = scBasic.company_value; highscore->timestamp = DATETIME64_MIN; } } } } catch (Exception ex) { Console::Error::WriteLine("Error reading legacy scenario scores file: '%s'", path.c_str()); } if (highscoresDirty) { SaveHighscores(); } }