Use string to store ScenarioIndexEntry name

This commit is contained in:
ζeh Matt 2023-02-16 19:52:13 +02:00
parent 1b216c4d01
commit b465b9d6e5
No known key found for this signature in database
GPG Key ID: 18CE582C71A225B0
3 changed files with 14 additions and 18 deletions

View File

@ -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<StringId>(STR_STRING);
ft.Add<const char*>(completedByName);
ft.Add<const char*>(completedByName.c_str());
ft.Add<money64>(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<StringId>(STR_COMPLETED_BY);
ft.Add<StringId>(STR_STRING);
ft.Add<char*>(buffer);
ft.Add<const char*>(completedByName.c_str());
DrawTextBasic(
dpi, { scrollCentre, y + scenarioTitleHeight + 1 }, format, ft,
{ FontStyle::Small, TextAlignment::CENTRE });

View File

@ -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<money32>() : fs.ReadValue<money64>();
highscore->timestamp = fs.ReadValue<datetime64>();
}
@ -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;
}

View File

@ -21,7 +21,7 @@ struct RCTObjectEntry;
struct ScenarioHighscoreEntry
{
u8string fileName;
utf8* name;
u8string name;
money64 company_value;
datetime64 timestamp;
};