From 149b164ee823a598922b3419e91dece8a5df9d3e Mon Sep 17 00:00:00 2001 From: Michael Steenbeek Date: Thu, 27 Jan 2022 14:21:46 +0100 Subject: [PATCH] Use u8string for custom paths --- src/openrct2/Context.cpp | 8 ++--- src/openrct2/Game.cpp | 2 +- src/openrct2/OpenRCT2.cpp | 12 ++++---- src/openrct2/OpenRCT2.h | 12 ++++---- src/openrct2/PlatformEnvironment.cpp | 12 ++++---- src/openrct2/cmdline/CommandLine.cpp | 2 +- src/openrct2/cmdline/RootCommands.cpp | 37 ++++++++++-------------- src/openrct2/network/NetworkBase.cpp | 7 ++--- src/openrct2/network/NetworkBase.h | 2 +- src/openrct2/platform/Platform.Linux.cpp | 5 ++-- 10 files changed, 45 insertions(+), 54 deletions(-) diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index 664ebd58dc..72d6795afd 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -770,7 +770,7 @@ namespace OpenRCT2 std::string GetOrPromptRCT2Path() { auto result = std::string(); - if (String::IsNullOrEmpty(gCustomRCT2DataPath)) + if (gCustomRCT2DataPath.empty()) { // Check install directory if (gConfigGeneral.rct2_path.empty() || !Platform::OriginalGameDataExists(gConfigGeneral.rct2_path)) @@ -786,11 +786,11 @@ namespace OpenRCT2 return std::string(); } } - result = std::string(gConfigGeneral.rct2_path); + result = gConfigGeneral.rct2_path; } else { - result = std::string(gCustomRCT2DataPath); + result = gCustomRCT2DataPath; } return result; } @@ -906,7 +906,7 @@ namespace OpenRCT2 gNetworkStartAddress = gConfigNetwork.listen_address; } - if (String::IsNullOrEmpty(gCustomPassword)) + if (gCustomPassword.empty()) { _network.SetPassword(gConfigNetwork.default_password.c_str()); } diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index c012028653..bf96e6ba79 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -775,7 +775,7 @@ void start_silent_record() { OpenRCT2::ReplayRecordInfo info; replayManager->GetCurrentReplayInfo(info); - safe_strcpy(gSilentRecordingName, info.FilePath.c_str(), MAX_PATH); + gSilentRecordingName = info.FilePath; const char* logFmt = "Silent replay recording started: (%s) %s\n"; Console::WriteLine(logFmt, info.Name.c_str(), info.FilePath.c_str()); diff --git a/src/openrct2/OpenRCT2.cpp b/src/openrct2/OpenRCT2.cpp index 48808f6529..05cabe9f06 100644 --- a/src/openrct2/OpenRCT2.cpp +++ b/src/openrct2/OpenRCT2.cpp @@ -11,12 +11,12 @@ StartupAction gOpenRCT2StartupAction = StartupAction::Title; utf8 gOpenRCT2StartupActionPath[512] = { 0 }; -utf8 gCustomUserDataPath[MAX_PATH] = { 0 }; -utf8 gCustomOpenRCT2DataPath[MAX_PATH] = { 0 }; -utf8 gCustomRCT1DataPath[MAX_PATH] = { 0 }; -utf8 gCustomRCT2DataPath[MAX_PATH] = { 0 }; -utf8 gCustomPassword[MAX_PATH] = { 0 }; -utf8 gSilentRecordingName[MAX_PATH] = { 0 }; +u8string gCustomUserDataPath = {}; +u8string gCustomOpenRCT2DataPath = {}; +u8string gCustomRCT1DataPath = {}; +u8string gCustomRCT2DataPath = {}; +u8string gCustomPassword = {}; +u8string gSilentRecordingName = {}; bool gOpenRCT2Headless = false; bool gOpenRCT2NoGraphics = false; diff --git a/src/openrct2/OpenRCT2.h b/src/openrct2/OpenRCT2.h index a31b428345..0c90f17ee5 100644 --- a/src/openrct2/OpenRCT2.h +++ b/src/openrct2/OpenRCT2.h @@ -39,16 +39,16 @@ enum extern StartupAction gOpenRCT2StartupAction; extern utf8 gOpenRCT2StartupActionPath[512]; -extern utf8 gCustomUserDataPath[MAX_PATH]; -extern utf8 gCustomOpenRCT2DataPath[MAX_PATH]; -extern utf8 gCustomRCT1DataPath[MAX_PATH]; -extern utf8 gCustomRCT2DataPath[MAX_PATH]; -extern utf8 gCustomPassword[MAX_PATH]; +extern u8string gCustomUserDataPath; +extern u8string gCustomOpenRCT2DataPath; +extern u8string gCustomRCT1DataPath; +extern u8string gCustomRCT2DataPath; +extern u8string gCustomPassword; extern bool gOpenRCT2Headless; extern bool gOpenRCT2NoGraphics; extern bool gOpenRCT2ShowChangelog; extern bool gOpenRCT2SilentBreakpad; -extern utf8 gSilentRecordingName[MAX_PATH]; +extern u8string gSilentRecordingName; #ifndef DISABLE_NETWORK extern int32_t gNetworkStart; diff --git a/src/openrct2/PlatformEnvironment.cpp b/src/openrct2/PlatformEnvironment.cpp index 62dc58a040..4a98e7747f 100644 --- a/src/openrct2/PlatformEnvironment.cpp +++ b/src/openrct2/PlatformEnvironment.cpp @@ -135,19 +135,19 @@ std::unique_ptr OpenRCT2::CreatePlatformEnvironment() basePaths[static_cast(DIRBASE::DOCUMENTATION)] = Platform::GetDocsPath(); // Override paths that have been specified via the command line - if (!String::IsNullOrEmpty(gCustomRCT1DataPath)) + if (!gCustomRCT1DataPath.empty()) { basePaths[static_cast(DIRBASE::RCT1)] = gCustomRCT1DataPath; } - if (!String::IsNullOrEmpty(gCustomRCT2DataPath)) + if (!gCustomRCT2DataPath.empty()) { basePaths[static_cast(DIRBASE::RCT2)] = gCustomRCT2DataPath; } - if (!String::IsNullOrEmpty(gCustomOpenRCT2DataPath)) + if (!gCustomOpenRCT2DataPath.empty()) { basePaths[static_cast(DIRBASE::OPENRCT2)] = gCustomOpenRCT2DataPath; } - if (!String::IsNullOrEmpty(gCustomUserDataPath)) + if (!gCustomUserDataPath.empty()) { basePaths[static_cast(DIRBASE::USER)] = gCustomUserDataPath; basePaths[static_cast(DIRBASE::CONFIG)] = gCustomUserDataPath; @@ -168,11 +168,11 @@ std::unique_ptr OpenRCT2::CreatePlatformEnvironment() { config_save(configPath.c_str()); } - if (String::IsNullOrEmpty(gCustomRCT1DataPath)) + if (gCustomRCT1DataPath.empty()) { env->SetBasePath(DIRBASE::RCT1, String::ToStd(gConfigGeneral.rct1_path)); } - if (String::IsNullOrEmpty(gCustomRCT2DataPath)) + if (gCustomRCT2DataPath.empty()) { env->SetBasePath(DIRBASE::RCT2, gConfigGeneral.rct2_path); } diff --git a/src/openrct2/cmdline/CommandLine.cpp b/src/openrct2/cmdline/CommandLine.cpp index 78ceadaac8..a5e41a01c5 100644 --- a/src/openrct2/cmdline/CommandLine.cpp +++ b/src/openrct2/cmdline/CommandLine.cpp @@ -486,7 +486,7 @@ namespace CommandLine *(static_cast(option->OutAddress)) = static_cast(atof(valueString)); return true; case CMDLINE_TYPE_STRING: - *(static_cast(option->OutAddress)) = String::Duplicate(valueString); + *(static_cast(option->OutAddress)) = u8string(valueString); return true; default: Console::Error::WriteLine("Unknown CMDLINE_TYPE for: %s", option->LongName); diff --git a/src/openrct2/cmdline/RootCommands.cpp b/src/openrct2/cmdline/RootCommands.cpp index 09d9a74df3..b0fc69ee6c 100644 --- a/src/openrct2/cmdline/RootCommands.cpp +++ b/src/openrct2/cmdline/RootCommands.cpp @@ -54,11 +54,11 @@ static bool _all = false; static bool _about = false; static bool _verbose = false; static bool _headless = false; -static utf8* _password = nullptr; -static utf8* _userDataPath = nullptr; -static utf8* _openrct2DataPath = nullptr; -static utf8* _rct1DataPath = nullptr; -static utf8* _rct2DataPath = nullptr; +static u8string _password = {}; +static u8string _userDataPath = {}; +static u8string _openrct2DataPath = {}; +static u8string _rct1DataPath = {}; +static u8string _rct2DataPath = {}; static bool _silentBreakpad = false; // clang-format off @@ -199,36 +199,29 @@ exitcode_t CommandLine::HandleCommandDefault() gOpenRCT2NoGraphics = _headless; gOpenRCT2SilentBreakpad = _silentBreakpad || _headless; - if (_userDataPath != nullptr) + if (!_userDataPath.empty()) { - const auto absolutePath = Path::GetAbsolute(_userDataPath); - String::Set(gCustomUserDataPath, std::size(gCustomUserDataPath), absolutePath.c_str()); - Memory::Free(_userDataPath); + gCustomUserDataPath = Path::GetAbsolute(_userDataPath); } - if (_openrct2DataPath != nullptr) + if (!_openrct2DataPath.empty()) { - const auto absolutePath = Path::GetAbsolute(_openrct2DataPath); - String::Set(gCustomOpenRCT2DataPath, std::size(gCustomOpenRCT2DataPath), absolutePath.c_str()); - Memory::Free(_openrct2DataPath); + gCustomOpenRCT2DataPath = Path::GetAbsolute(_openrct2DataPath); } - if (_rct1DataPath != nullptr) + if (!_rct1DataPath.empty()) { - String::Set(gCustomRCT1DataPath, std::size(gCustomRCT1DataPath), _rct1DataPath); - Memory::Free(_rct1DataPath); + gCustomRCT1DataPath = _rct1DataPath; } - if (_rct2DataPath != nullptr) + if (!_rct2DataPath.empty()) { - String::Set(gCustomRCT2DataPath, std::size(gCustomRCT2DataPath), _rct2DataPath); - Memory::Free(_rct2DataPath); + gCustomRCT2DataPath = _rct2DataPath; } - if (_password != nullptr) + if (!_password.empty()) { - String::Set(gCustomPassword, std::size(gCustomPassword), _password); - Memory::Free(_password); + gCustomPassword = _password; } return result; diff --git a/src/openrct2/network/NetworkBase.cpp b/src/openrct2/network/NetworkBase.cpp index f32e5c60da..3c906d6938 100644 --- a/src/openrct2/network/NetworkBase.cpp +++ b/src/openrct2/network/NetworkBase.cpp @@ -828,9 +828,9 @@ void NetworkBase::KickPlayer(int32_t playerId) } } -void NetworkBase::SetPassword(const char* password) +void NetworkBase::SetPassword(u8string_view password) { - _password = password == nullptr ? "" : password; + _password = password; } void NetworkBase::ServerClientDisconnected() @@ -2170,8 +2170,7 @@ void NetworkBase::Client_Handle_TOKEN(NetworkConnection& connection, NetworkPack // when process dump gets collected at some point in future. _key.Unload(); - const char* password = String::IsNullOrEmpty(gCustomPassword) ? "" : gCustomPassword; - Client_Send_AUTH(gConfigNetwork.player_name.c_str(), password, pubkey.c_str(), signature); + Client_Send_AUTH(gConfigNetwork.player_name.c_str(), gCustomPassword.c_str(), pubkey.c_str(), signature); } void NetworkBase::Server_Handle_REQUEST_GAMESTATE(NetworkConnection& connection, NetworkPacket& packet) diff --git a/src/openrct2/network/NetworkBase.h b/src/openrct2/network/NetworkBase.h index 79f49f8615..1d703b1c13 100644 --- a/src/openrct2/network/NetworkBase.h +++ b/src/openrct2/network/NetworkBase.h @@ -41,7 +41,7 @@ public: // Common std::vector>::iterator GetGroupIteratorByID(uint8_t id); NetworkPlayer* GetPlayerByID(uint8_t id); NetworkGroup* GetGroupByID(uint8_t id); - void SetPassword(const char* password); + void SetPassword(u8string_view password); uint8_t GetDefaultGroup(); std::string BeginLog(const std::string& directory, const std::string& midName, const std::string& filenameFormat); void AppendLog(std::ostream& fs, std::string_view s); diff --git a/src/openrct2/platform/Platform.Linux.cpp b/src/openrct2/platform/Platform.Linux.cpp index a2cf73b05b..3106d351a5 100644 --- a/src/openrct2/platform/Platform.Linux.cpp +++ b/src/openrct2/platform/Platform.Linux.cpp @@ -89,10 +89,9 @@ namespace Platform std::string GetInstallPath() { // 1. Try command line argument - auto path = std::string(gCustomOpenRCT2DataPath); - if (!path.empty()) + if (!gCustomOpenRCT2DataPath.empty()) { - return Path::GetAbsolute(path); + return Path::GetAbsolute(gCustomOpenRCT2DataPath); } // 2. Try {${exeDir},${cwd},/}/{data,standard system app directories} // exeDir should come first to allow installing into build dir