diff --git a/src/openrct2-ui/windows/About.cpp b/src/openrct2-ui/windows/About.cpp index ef4ff8cbe6..e03aab27f8 100644 --- a/src/openrct2-ui/windows/About.cpp +++ b/src/openrct2-ui/windows/About.cpp @@ -199,12 +199,10 @@ namespace OpenRCT2::Ui::Windows ScreenCoordsXY logoCoords = windowPos + ScreenCoordsXY(widgets[WIDX_OPENRCT2_LOGO].left, widgets[WIDX_OPENRCT2_LOGO].top); GfxDrawSprite(dpi, ImageId(SPR_G2_LOGO), logoCoords); - // Version info - utf8 buffer[256]; - utf8* ch = buffer; - OpenRCT2WriteFullVersionInfo(ch, sizeof(buffer) - (ch - buffer)); + + u8string versionInfo = gVersionInfoFull; auto ft = Formatter(); - ft.Add(buffer); + ft.Add(versionInfo.c_str()); auto const& versionPlaceholder = widgets[WIDX_VERSION]; auto versionPlaceHolderWidth = versionPlaceholder.right - versionPlaceholder.left; diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index d19f79c02c..4f2684257b 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -1566,11 +1566,6 @@ bool ContextLoadParkFromStream(void* stream) return GetContext()->LoadParkFromStream(static_cast(stream), ""); } -void OpenRCT2WriteFullVersionInfo(utf8* buffer, size_t bufferSize) -{ - String::Set(buffer, bufferSize, gVersionInfoFull); -} - void OpenRCT2Finish() { GetContext()->Finish(); @@ -1738,22 +1733,6 @@ void ContextQuit() GetContext()->Quit(); } -bool ContextOpenCommonFileDialog(utf8* outFilename, OpenRCT2::Ui::FileDialogDesc& desc, size_t outSize) -{ - try - { - std::string result = GetContext()->GetUiContext()->ShowFileDialog(desc); - String::Set(outFilename, outSize, result.c_str()); - return !result.empty(); - } - catch (const std::exception& ex) - { - LOG_ERROR(ex.what()); - outFilename[0] = '\0'; - return false; - } -} - u8string ContextOpenCommonFileDialog(OpenRCT2::Ui::FileDialogDesc& desc) { try diff --git a/src/openrct2/Context.h b/src/openrct2/Context.h index 2087fee0f4..31e4110bd8 100644 --- a/src/openrct2/Context.h +++ b/src/openrct2/Context.h @@ -232,5 +232,4 @@ void ContextHandleInput(); void ContextInputHandleKeyboard(bool isTitle); void ContextQuit(); bool ContextLoadParkFromStream(void* stream); -bool ContextOpenCommonFileDialog(utf8* outFilename, OpenRCT2::Ui::FileDialogDesc& desc, size_t outSize); u8string ContextOpenCommonFileDialog(OpenRCT2::Ui::FileDialogDesc& desc); diff --git a/src/openrct2/OpenRCT2.h b/src/openrct2/OpenRCT2.h index 9d72ea7e78..1f8729f611 100644 --- a/src/openrct2/OpenRCT2.h +++ b/src/openrct2/OpenRCT2.h @@ -60,7 +60,6 @@ extern uint8_t gScreenFlags; extern uint32_t gScreenAge; extern PromptMode gSavePromptMode; -void OpenRCT2WriteFullVersionInfo(utf8* buffer, size_t bufferSize); void OpenRCT2Finish(); int32_t CommandLineRun(const char** argv, int32_t argc); diff --git a/src/openrct2/command_line/RootCommands.cpp b/src/openrct2/command_line/RootCommands.cpp index 0092f76002..2fe5c348e0 100644 --- a/src/openrct2/command_line/RootCommands.cpp +++ b/src/openrct2/command_line/RootCommands.cpp @@ -447,9 +447,8 @@ static void PrintAbout() static void PrintVersion() { - char buffer[256]; - OpenRCT2WriteFullVersionInfo(buffer, sizeof(buffer)); - Console::WriteLine(buffer); + u8string versionInfo = gVersionInfoFull; + Console::WriteLine(versionInfo.c_str()); Console::WriteFormat("%s (%s)", OPENRCT2_PLATFORM, OPENRCT2_ARCHITECTURE); Console::WriteLine(); Console::WriteFormat("Network version: %s", NetworkGetVersion().c_str()); @@ -481,9 +480,8 @@ static void PrintLaunchInformation() struct tm* tmInfo; // Print name and version information - OpenRCT2WriteFullVersionInfo(buffer, sizeof(buffer)); - Console::WriteFormat("%s", buffer); - Console::WriteLine(); + u8string versionInfo = gVersionInfoFull; + Console::WriteLine(versionInfo.c_str()); Console::WriteFormat("%s (%s)", OPENRCT2_PLATFORM, OPENRCT2_ARCHITECTURE); Console::WriteLine(); Console::WriteLine(); diff --git a/src/openrct2/config/Config.cpp b/src/openrct2/config/Config.cpp index c29f8a78be..bf35f11209 100644 --- a/src/openrct2/config/Config.cpp +++ b/src/openrct2/config/Config.cpp @@ -749,7 +749,7 @@ namespace OpenRCT2::Config return {}; } - static bool SelectGogInstaller(utf8* installerPath) + static u8string SelectGogInstaller() { FileDialogDesc desc{}; desc.Type = FileDialogType::Open; @@ -760,7 +760,7 @@ namespace OpenRCT2::Config const auto userHomePath = Platform::GetFolderPath(SPECIAL_FOLDER::USER_HOME); desc.InitialDirectory = userHomePath; - return ContextOpenCommonFileDialog(installerPath, desc, 4096); + return ContextOpenCommonFileDialog(desc); } static bool ExtractGogInstaller(const u8string& installerPath, const u8string& targetPath) @@ -888,8 +888,8 @@ namespace OpenRCT2::Config while (true) { uiContext->ShowMessageBox(LanguageGetString(STR_PLEASE_SELECT_GOG_INSTALLER)); - utf8 gogPath[4096]; - if (!SelectGogInstaller(gogPath)) + auto gogPath = SelectGogInstaller(); + if (gogPath.empty()) { // The user clicked "Cancel", so stop trying. return false; diff --git a/src/openrct2/core/String.cpp b/src/openrct2/core/String.cpp index ebc599e947..b0b09dca5b 100644 --- a/src/openrct2/core/String.cpp +++ b/src/openrct2/core/String.cpp @@ -525,11 +525,6 @@ namespace OpenRCT2::String return ch; } - utf8* TrimStart(utf8* buffer, size_t bufferSize, const utf8* src) - { - return String::Set(buffer, bufferSize, TrimStart(src)); - } - std::string TrimStart(const std::string& s) { const utf8* trimmed = TrimStart(s.c_str()); diff --git a/src/openrct2/core/String.hpp b/src/openrct2/core/String.hpp index 19ccf5d77b..bdcf2ce5aa 100644 --- a/src/openrct2/core/String.hpp +++ b/src/openrct2/core/String.hpp @@ -84,9 +84,8 @@ namespace OpenRCT2::String bool IsWhiteSpace(codepoint_t codepoint); utf8* Trim(utf8* str); const utf8* TrimStart(const utf8* str); - utf8* TrimStart(utf8* buffer, size_t bufferSize, const utf8* src); - std::string TrimStart(const std::string& s); - std::string Trim(const std::string& s); + [[nodiscard]] std::string TrimStart(const std::string& s); + [[nodiscard]] std::string Trim(const std::string& s); /** * Converts a multi-byte string from one code page to UTF-8. diff --git a/src/openrct2/localisation/Language.cpp b/src/openrct2/localisation/Language.cpp index 0d6100c524..c1dd601cc9 100644 --- a/src/openrct2/localisation/Language.cpp +++ b/src/openrct2/localisation/Language.cpp @@ -96,7 +96,7 @@ bool LanguageOpen(int32_t id) } } -bool LanguageGetLocalisedScenarioStrings(const utf8* scenarioFilename, StringId* outStringIds) +bool LanguageGetLocalisedScenarioStrings(u8string_view scenarioFilename, StringId* outStringIds) { const auto& localisationService = OpenRCT2::GetContext()->GetLocalisationService(); auto result = localisationService.GetLocalisedScenarioStrings(scenarioFilename); diff --git a/src/openrct2/localisation/Language.h b/src/openrct2/localisation/Language.h index c679b5f95b..70703be8d0 100644 --- a/src/openrct2/localisation/Language.h +++ b/src/openrct2/localisation/Language.h @@ -88,6 +88,6 @@ uint8_t LanguageGetIDFromLocale(const char* locale); const char* LanguageGetString(StringId id); bool LanguageOpen(int32_t id); -bool LanguageGetLocalisedScenarioStrings(const utf8* scenarioFilename, StringId* outStringIds); +bool LanguageGetLocalisedScenarioStrings(u8string_view scenarioFilename, StringId* outStringIds); void LanguageFreeObjectString(StringId stringId); StringId LanguageAllocateObjectString(const std::string& target); diff --git a/src/openrct2/localisation/LanguagePack.cpp b/src/openrct2/localisation/LanguagePack.cpp index 0165c03987..2599843860 100644 --- a/src/openrct2/localisation/LanguagePack.cpp +++ b/src/openrct2/localisation/LanguagePack.cpp @@ -155,9 +155,8 @@ public: return nullptr; } - StringId GetScenarioOverrideStringId(const utf8* scenarioFilename, uint8_t index) override + StringId GetScenarioOverrideStringId(u8string_view scenarioFilename, uint8_t index) override { - Guard::ArgumentNotNull(scenarioFilename); Guard::Assert(index < ScenarioOverrideMaxStringCount); int32_t ooIndex = 0; diff --git a/src/openrct2/localisation/LanguagePack.h b/src/openrct2/localisation/LanguagePack.h index c692320f9a..ae510f180b 100644 --- a/src/openrct2/localisation/LanguagePack.h +++ b/src/openrct2/localisation/LanguagePack.h @@ -24,7 +24,7 @@ struct ILanguagePack virtual void RemoveString(StringId stringId) = 0; virtual void SetString(StringId stringId, const std::string& str) = 0; virtual const utf8* GetString(StringId stringId) const = 0; - virtual StringId GetScenarioOverrideStringId(const utf8* scenarioFilename, uint8_t index) = 0; + virtual StringId GetScenarioOverrideStringId(u8string_view scenarioFilename, uint8_t index) = 0; }; namespace OpenRCT2::LanguagePackFactory diff --git a/src/openrct2/localisation/LocalisationService.cpp b/src/openrct2/localisation/LocalisationService.cpp index c1e7692447..daa2a79ce6 100644 --- a/src/openrct2/localisation/LocalisationService.cpp +++ b/src/openrct2/localisation/LocalisationService.cpp @@ -143,13 +143,12 @@ void LocalisationService::CloseLanguages() _currentLanguage = LANGUAGE_UNDEFINED; } -std::tuple LocalisationService::GetLocalisedScenarioStrings( - const std::string& scenarioFilename) const +std::tuple LocalisationService::GetLocalisedScenarioStrings(u8string_view scenarioFilename) const { Guard::Assert(!_loadedLanguages.empty()); - auto result0 = _loadedLanguages[0]->GetScenarioOverrideStringId(scenarioFilename.c_str(), 0); - auto result1 = _loadedLanguages[0]->GetScenarioOverrideStringId(scenarioFilename.c_str(), 1); - auto result2 = _loadedLanguages[0]->GetScenarioOverrideStringId(scenarioFilename.c_str(), 2); + auto result0 = _loadedLanguages[0]->GetScenarioOverrideStringId(scenarioFilename, 0); + auto result1 = _loadedLanguages[0]->GetScenarioOverrideStringId(scenarioFilename, 1); + auto result2 = _loadedLanguages[0]->GetScenarioOverrideStringId(scenarioFilename, 2); return std::make_tuple(result0, result1, result2); } diff --git a/src/openrct2/localisation/LocalisationService.h b/src/openrct2/localisation/LocalisationService.h index a2090e42d2..5f02d00140 100644 --- a/src/openrct2/localisation/LocalisationService.h +++ b/src/openrct2/localisation/LocalisationService.h @@ -9,6 +9,7 @@ #pragma once +#include "../core/StringTypes.h" #include "../localisation/StringIdType.h" #include @@ -58,7 +59,7 @@ namespace OpenRCT2::Localisation ~LocalisationService(); const char* GetString(StringId id) const; - std::tuple GetLocalisedScenarioStrings(const std::string& scenarioFilename) const; + std::tuple GetLocalisedScenarioStrings(u8string_view scenarioFilename) const; std::string GetLanguagePath(uint32_t languageId) const; void OpenLanguage(int32_t id); diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp index 3ba6acb201..d8050f5a84 100644 --- a/src/openrct2/rct2/S6Importer.cpp +++ b/src/openrct2/rct2/S6Importer.cpp @@ -258,9 +258,9 @@ namespace OpenRCT2::RCT2 } else { - String::Set(dst->Name, sizeof(dst->Name), _s6.Info.Name); // Normalise the name to make the scenario as recognisable as possible. - ScenarioSources::NormaliseName(dst->Name, sizeof(dst->Name), dst->Name); + auto normalisedName = ScenarioSources::NormaliseName(_s6.Info.Name); + String::Set(dst->Name, sizeof(dst->Name), normalisedName.c_str()); } // dst->name will be translated later so keep the untranslated name here diff --git a/src/openrct2/scenario/Scenario.cpp b/src/openrct2/scenario/Scenario.cpp index 6b6382c9bc..99e4a862c0 100644 --- a/src/openrct2/scenario/Scenario.cpp +++ b/src/openrct2/scenario/Scenario.cpp @@ -108,8 +108,7 @@ void ScenarioReset(GameState_t& gameState) gameState.Cash = gameState.InitialCash; { - utf8 normalisedName[64]; - ScenarioSources::NormaliseName(normalisedName, sizeof(normalisedName), gameState.ScenarioName.c_str()); + auto normalisedName = ScenarioSources::NormaliseName(gameState.ScenarioName); StringId localisedStringIds[3]; if (LanguageGetLocalisedScenarioStrings(normalisedName, localisedStringIds)) diff --git a/src/openrct2/scenario/ScenarioSources.cpp b/src/openrct2/scenario/ScenarioSources.cpp index 13e2d1b229..00004dde56 100644 --- a/src/openrct2/scenario/ScenarioSources.cpp +++ b/src/openrct2/scenario/ScenarioSources.cpp @@ -421,36 +421,39 @@ namespace OpenRCT2::ScenarioSources return false; } - void NormaliseName(utf8* buffer, size_t bufferSize, const utf8* name) + u8string NormaliseName(u8string_view input) { - size_t nameLength = String::LengthOf(name); - - // Strip "RCT(1|2)?" prefix off scenario names. - if (nameLength >= 3 && (name[0] == 'R' && name[1] == 'C' && name[2] == 'T')) - { - if (nameLength >= 4 && (name[3] == '1' || name[3] == '2')) - { - LOG_VERBOSE("Stripping RCT/1/2 from name: %s", name); - String::Set(buffer, bufferSize, name + 4); - } - else - { - String::Set(buffer, bufferSize, name + 3); - } - } - - // Trim (for the sake of the above and WW / TT scenarios - String::TrimStart(buffer, bufferSize, name); - // American scenario titles should be converted to British name // Don't worry, names will be translated using language packs later for (const ScenarioAlias& alias : ScenarioAliases) { - if (String::Equals(alias.Alternative, name)) + if (String::Equals(alias.Alternative, input)) { - LOG_VERBOSE("Found alias: %s; will treat as: %s", name, alias.Original); - String::Set(buffer, bufferSize, alias.Original); + LOG_VERBOSE("Found alias: %s; will treat as: %s", input, alias.Original); + return u8string(alias.Original); } } + + u8string normalisedName; + // Strip "RCT(1|2)?" prefix off scenario names. + if (input.starts_with("RCT")) + { + LOG_VERBOSE("Stripping RCT/1/2 from name: %s", u8string(input).c_str()); + if (input.length() >= 4 && (input[3] == '1' || input[3] == '2')) + { + normalisedName = input.substr(4); + } + else + { + normalisedName = input.substr(3); + } + } + else + { + normalisedName = input; + } + + // Trim (for the sake of the above and WW / TT scenarios) + return String::TrimStart(normalisedName); } } // namespace OpenRCT2::ScenarioSources diff --git a/src/openrct2/scenario/ScenarioSources.h b/src/openrct2/scenario/ScenarioSources.h index fe3196e96e..2bfddbd1e2 100644 --- a/src/openrct2/scenario/ScenarioSources.h +++ b/src/openrct2/scenario/ScenarioSources.h @@ -24,7 +24,7 @@ namespace OpenRCT2::ScenarioSources { bool TryGetByName(const utf8* name, SourceDescriptor* outDesc); bool TryGetById(uint8_t id, SourceDescriptor* outDesc); - void NormaliseName(utf8* buffer, size_t bufferSize, const utf8* name); + u8string NormaliseName(u8string_view input); } // namespace OpenRCT2::ScenarioSources // RCT1 scenario index map