From b465b9d6e58e575e9473d500a8d5f8552422ea5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:52:13 +0200 Subject: [PATCH] Use string to store ScenarioIndexEntry name --- src/openrct2-ui/windows/ScenarioSelect.cpp | 13 ++++++------- src/openrct2/scenario/ScenarioRepository.cpp | 17 +++++++---------- src/openrct2/scenario/ScenarioRepository.h | 2 +- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/openrct2-ui/windows/ScenarioSelect.cpp b/src/openrct2-ui/windows/ScenarioSelect.cpp index 2f4108a09b..4766919cab 100644 --- a/src/openrct2-ui/windows/ScenarioSelect.cpp +++ b/src/openrct2-ui/windows/ScenarioSelect.cpp @@ -539,14 +539,14 @@ static void WindowScenarioselectPaint(WindowBase* w, DrawPixelInfo* dpi) if (scenario->Highscore != nullptr) { // TODO: Should probably be translatable - const utf8* completedByName = "???"; - if (!String::IsNullOrEmpty(scenario->Highscore->name)) + u8string completedByName = "???"; + if (!scenario->Highscore->name.empty()) { completedByName = scenario->Highscore->name; } ft = Formatter(); ft.Add(STR_STRING); - ft.Add(completedByName); + ft.Add(completedByName.c_str()); ft.Add(scenario->Highscore->company_value); screenPos.y += DrawTextWrapped(dpi, screenPos, 170, STR_COMPLETED_BY_WITH_COMPANY_VALUE, ft); } @@ -623,16 +623,15 @@ static void WindowScenarioselectScrollpaint(WindowBase* w, DrawPixelInfo* dpi, i { window_scenarioselect_widgets[WIDX_SCENARIOLIST].width() - 45, y + 1 }); // Draw completion score - const utf8* completedByName = "???"; - if (!String::IsNullOrEmpty(scenario->Highscore->name)) + u8string completedByName = "???"; + if (!scenario->Highscore->name.empty()) { completedByName = scenario->Highscore->name; } - SafeStrCpy(buffer, completedByName, 64); ft = Formatter(); ft.Add(STR_COMPLETED_BY); ft.Add(STR_STRING); - ft.Add(buffer); + ft.Add(completedByName.c_str()); DrawTextBasic( dpi, { scrollCentre, y + scenarioTitleHeight + 1 }, format, ft, { FontStyle::Small, TextAlignment::CENTRE }); diff --git a/src/openrct2/scenario/ScenarioRepository.cpp b/src/openrct2/scenario/ScenarioRepository.cpp index d403f9b97a..e7de5281ea 100644 --- a/src/openrct2/scenario/ScenarioRepository.cpp +++ b/src/openrct2/scenario/ScenarioRepository.cpp @@ -117,7 +117,6 @@ static int32_t ScenarioIndexEntryCompareByIndex(const ScenarioIndexEntry& entryA static void ScenarioHighscoreFree(ScenarioHighscoreEntry* highscore) { - SafeFree(highscore->name); SafeDelete(highscore); } @@ -464,7 +463,7 @@ public: // Check if record company value has been broken or the highscore is the same but no name is registered ScenarioHighscoreEntry* highscore = scenario->Highscore; if (highscore == nullptr || companyValue > highscore->company_value - || (String::IsNullOrEmpty(highscore->name) && companyValue == highscore->company_value)) + || (highscore->name.empty() && companyValue == highscore->company_value)) { if (highscore == nullptr) { @@ -474,14 +473,13 @@ public: } else { - if (!String::IsNullOrEmpty(highscore->name)) + if (!highscore->name.empty()) { highscore->timestamp = Platform::GetDatetimeNowUTC(); } - SafeFree(highscore->name); } highscore->fileName = Path::GetFileName(scenario->Path); - highscore->name = String::Duplicate(name); + highscore->name = name; highscore->company_value = companyValue; SaveHighscores(); return true; @@ -505,7 +503,7 @@ private: } return nullptr; } - + ScenarioIndexEntry* GetByPath(const utf8* path) { const ScenarioRepository* repo = this; @@ -631,7 +629,7 @@ private: { ScenarioHighscoreEntry* highscore = InsertHighscore(); highscore->fileName = fs.ReadStdString(); - highscore->name = fs.ReadString(); + highscore->name = fs.ReadStdString(); highscore->company_value = fileVersion == 1 ? fs.ReadValue() : fs.ReadValue(); highscore->timestamp = fs.ReadValue(); } @@ -691,9 +689,8 @@ private: // Check if legacy highscore is better if (scBasic.CompanyValue > highscore->company_value) { - SafeFree(highscore->name); std::string name = RCT2StringToUTF8(scBasic.CompletedBy, RCT2LanguageId::EnglishUK); - highscore->name = String::Duplicate(name.c_str()); + highscore->name = name; highscore->company_value = scBasic.CompanyValue; highscore->timestamp = DATETIME64_MIN; break; @@ -705,7 +702,7 @@ private: ScenarioHighscoreEntry* highscore = InsertHighscore(); highscore->fileName = scBasic.Path; std::string name = RCT2StringToUTF8(scBasic.CompletedBy, RCT2LanguageId::EnglishUK); - highscore->name = String::Duplicate(name.c_str()); + highscore->name = name; highscore->company_value = scBasic.CompanyValue; highscore->timestamp = DATETIME64_MIN; } diff --git a/src/openrct2/scenario/ScenarioRepository.h b/src/openrct2/scenario/ScenarioRepository.h index 6854556ede..f07b3499ea 100644 --- a/src/openrct2/scenario/ScenarioRepository.h +++ b/src/openrct2/scenario/ScenarioRepository.h @@ -21,7 +21,7 @@ struct RCTObjectEntry; struct ScenarioHighscoreEntry { u8string fileName; - utf8* name; + u8string name; money64 company_value; datetime64 timestamp; };