From 310ad1e4000b2699a269bebe57284589ff41c79a Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 10 Jan 2022 13:28:23 +0100 Subject: [PATCH] Upgrade platform_get_steam_dir() --- src/openrct2/config/Config.cpp | 8 ++-- src/openrct2/platform/Android.cpp | 5 --- src/openrct2/platform/Linux.cpp | 46 ---------------------- src/openrct2/platform/Platform.Android.cpp | 5 +++ src/openrct2/platform/Platform.Linux.cpp | 39 ++++++++++++++++++ src/openrct2/platform/Platform.Win32.cpp | 30 ++++++++++++++ src/openrct2/platform/Platform.macOS.mm | 19 +++++++++ src/openrct2/platform/Platform2.h | 1 + src/openrct2/platform/Windows.cpp | 28 ------------- src/openrct2/platform/macos.mm | 18 --------- src/openrct2/platform/platform.h | 1 - 11 files changed, 98 insertions(+), 102 deletions(-) diff --git a/src/openrct2/config/Config.cpp b/src/openrct2/config/Config.cpp index ee9e1a7071..3c79ab6eef 100644 --- a/src/openrct2/config/Config.cpp +++ b/src/openrct2/config/Config.cpp @@ -652,8 +652,8 @@ namespace Config } } - utf8 steamPath[2048] = { 0 }; - if (platform_get_steam_path(steamPath, sizeof(steamPath))) + auto steamPath = Platform::GetSteamPath(); + if (!steamPath.empty()) { std::string location = Path::Combine(steamPath, platform_get_rct1_steam_dir()); if (RCT1DataPresentAtLocation(location.c_str())) @@ -701,8 +701,8 @@ namespace Config } } - utf8 steamPath[2048] = { 0 }; - if (platform_get_steam_path(steamPath, sizeof(steamPath))) + auto steamPath = Platform::GetSteamPath(); + if (!steamPath.empty()) { std::string location = Path::Combine(steamPath, platform_get_rct2_steam_dir()); if (Platform::OriginalGameDataExists(location)) diff --git a/src/openrct2/platform/Android.cpp b/src/openrct2/platform/Android.cpp index 7c56f22f8e..9380edd844 100644 --- a/src/openrct2/platform/Android.cpp +++ b/src/openrct2/platform/Android.cpp @@ -42,11 +42,6 @@ float platform_get_default_scale() return displayScale; } -bool platform_get_steam_path(utf8* outPath, size_t outSize) -{ - return false; -} - AndroidClassLoader::AndroidClassLoader() { log_info("Obtaining JNI class loader"); diff --git a/src/openrct2/platform/Linux.cpp b/src/openrct2/platform/Linux.cpp index 4bd1f0edff..3720d779cb 100644 --- a/src/openrct2/platform/Linux.cpp +++ b/src/openrct2/platform/Linux.cpp @@ -34,52 +34,6 @@ # include -bool platform_get_steam_path(utf8* outPath, size_t outSize) -{ - const char* steamRoot = getenv("STEAMROOT"); - if (steamRoot != nullptr) - { - safe_strcpy(outPath, steamRoot, outSize); - safe_strcat_path(outPath, "ubuntu12_32/steamapps/content", outSize); - return true; - } - - char steamPath[1024] = { 0 }; - const char* localSharePath = getenv("XDG_DATA_HOME"); - if (localSharePath != nullptr) - { - safe_strcpy(steamPath, localSharePath, sizeof(steamPath)); - safe_strcat_path(steamPath, "Steam/ubuntu12_32/steamapps/content", sizeof(steamPath)); - if (Path::DirectoryExists(steamPath)) - { - safe_strcpy(outPath, steamPath, outSize); - return true; - } - } - - const char* homeDir = getpwuid(getuid())->pw_dir; - if (homeDir != nullptr) - { - safe_strcpy(steamPath, homeDir, sizeof(steamPath)); - safe_strcat_path(steamPath, ".local/share/Steam/ubuntu12_32/steamapps/content", sizeof(steamPath)); - if (Path::DirectoryExists(steamPath)) - { - safe_strcpy(outPath, steamPath, outSize); - return true; - } - - std::fill_n(steamPath, sizeof(steamPath), 0x00); - safe_strcpy(steamPath, homeDir, sizeof(steamPath)); - safe_strcat_path(steamPath, ".steam/steam/ubuntu12_32/steamapps/content", sizeof(steamPath)); - if (Path::DirectoryExists(steamPath)) - { - safe_strcpy(outPath, steamPath, outSize); - return true; - } - } - return false; -} - # ifndef NO_TTF bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size) { diff --git a/src/openrct2/platform/Platform.Android.cpp b/src/openrct2/platform/Platform.Android.cpp index 736c7bc2db..8fa0a80f26 100644 --- a/src/openrct2/platform/Platform.Android.cpp +++ b/src/openrct2/platform/Platform.Android.cpp @@ -70,6 +70,11 @@ namespace Platform { return MeasurementFormat::Metric; } + + std::string GetSteamPath() + { + return ""; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Linux.cpp b/src/openrct2/platform/Platform.Linux.cpp index e90b8c6e47..d4ae5b7daa 100644 --- a/src/openrct2/platform/Platform.Linux.cpp +++ b/src/openrct2/platform/Platform.Linux.cpp @@ -271,6 +271,45 @@ namespace Platform } return MeasurementFormat::Metric; } + + std::string GetSteamPath() + { + const char* steamRoot = getenv("STEAMROOT"); + if (steamRoot != nullptr) + { + return Path::Combine(steamRoot, "ubuntu12_32/steamapps/content"); + } + + const char* localSharePath = getenv("XDG_DATA_HOME"); + if (localSharePath != nullptr) + { + auto steamPath = Path::Combine(localSharePath, "Steam/ubuntu12_32/steamapps/content"); + if (Path::DirectoryExists(steamPath)) + { + return steamPath; + } + } + + const char* homeDir = getpwuid(getuid())->pw_dir; + if (homeDir == nullptr) + { + return ""; + } + + auto steamPath = Path::Combine(homeDir, ".local/share/Steam/ubuntu12_32/steamapps/content"); + if (Path::DirectoryExists(steamPath)) + { + return steamPath; + } + + steamPath = Path::Combine(homeDir, ".steam/steam/ubuntu12_32/steamapps/content"); + if (Path::DirectoryExists(steamPath)) + { + return steamPath; + } + + return ""; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index d7c04f04b8..5959c71afd 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -822,6 +822,36 @@ namespace Platform } return isElevated; } + + std::string GetSteamPath() + { + wchar_t* wSteamPath; + HKEY hKey; + DWORD type, size; + LRESULT result; + + if (RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\Valve\\Steam", &hKey) != ERROR_SUCCESS) + return ""; + + // Get the size of the path first + if (RegQueryValueExW(hKey, L"SteamPath", nullptr, &type, nullptr, &size) != ERROR_SUCCESS) + { + RegCloseKey(hKey); + return ""; + } + + std::string outPath = ""; + wSteamPath = reinterpret_cast(malloc(size)); + result = RegQueryValueExW(hKey, L"SteamPath", nullptr, &type, reinterpret_cast(wSteamPath), &size); + if (result == ERROR_SUCCESS) + { + auto utf8SteamPath = String::ToUtf8(wSteamPath); + outPath = Path::Combine(utf8SteamPath, "steamapps", "common"); + } + free(wSteamPath); + RegCloseKey(hKey); + return outPath; + } } // namespace Platform #endif diff --git a/src/openrct2/platform/Platform.macOS.mm b/src/openrct2/platform/Platform.macOS.mm index 5ff2962b33..2b23da6135 100644 --- a/src/openrct2/platform/Platform.macOS.mm +++ b/src/openrct2/platform/Platform.macOS.mm @@ -21,6 +21,7 @@ # include # include +# include namespace Platform { @@ -231,6 +232,24 @@ namespace Platform return MeasurementFormat::Imperial; } } + + std::string GetSteamPath() + { + const char* homeDir = getpwuid(getuid())->pw_dir; + if (homeDir == nullptr) + { + return ""; + } + + auto steamPath = Path::Combine( + homeDir, "Library/Application Support/Steam/Steam.AppBundle/Steam/Contents/MacOS/steamapps"); + if (Path::DirectoryExists(steamPath)) + { + return steamPath; + } + + return ""; + } } #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index a78fba368d..b7d2c6e2e5 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -59,6 +59,7 @@ namespace Platform std::string GetUsername(); + std::string GetSteamPath(); #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) std::string GetEnvironmentPath(const char* name); std::string GetHomePath(); diff --git a/src/openrct2/platform/Windows.cpp b/src/openrct2/platform/Windows.cpp index 7fdbf8a754..779952c5bf 100644 --- a/src/openrct2/platform/Windows.cpp +++ b/src/openrct2/platform/Windows.cpp @@ -96,35 +96,7 @@ int32_t platform_get_drives() return GetLogicalDrives(); } -bool platform_get_steam_path(utf8* outPath, size_t outSize) -{ - wchar_t* wSteamPath; - HKEY hKey; - DWORD type, size; - LRESULT result; - if (RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\Valve\\Steam", &hKey) != ERROR_SUCCESS) - return false; - - // Get the size of the path first - if (RegQueryValueExW(hKey, L"SteamPath", nullptr, &type, nullptr, &size) != ERROR_SUCCESS) - { - RegCloseKey(hKey); - return false; - } - - wSteamPath = reinterpret_cast(malloc(size)); - result = RegQueryValueExW(hKey, L"SteamPath", nullptr, &type, reinterpret_cast(wSteamPath), &size); - if (result == ERROR_SUCCESS) - { - auto utf8SteamPath = String::ToUtf8(wSteamPath); - safe_strcpy(outPath, utf8SteamPath.c_str(), outSize); - safe_strcat_path(outPath, "steamapps\\common", outSize); - } - free(wSteamPath); - RegCloseKey(hKey); - return result == ERROR_SUCCESS; -} std::string platform_get_rct1_steam_dir() { diff --git a/src/openrct2/platform/macos.mm b/src/openrct2/platform/macos.mm index 2ea7864562..7a3e49e995 100644 --- a/src/openrct2/platform/macos.mm +++ b/src/openrct2/platform/macos.mm @@ -46,22 +46,4 @@ bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size) } # endif // NO_TTF -bool platform_get_steam_path(utf8* outPath, size_t outSize) -{ - char steamPath[1024] = { 0 }; - const char* homeDir = getpwuid(getuid())->pw_dir; - if (homeDir != NULL) - { - safe_strcpy(steamPath, homeDir, sizeof(steamPath)); - safe_strcat_path( - steamPath, "Library/Application Support/Steam/Steam.AppBundle/Steam/Contents/MacOS/steamapps", sizeof(steamPath)); - if (Path::DirectoryExists(steamPath)) - { - safe_strcpy(outPath, steamPath, outSize); - return true; - } - } - return false; -} - #endif diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index d8692737a6..1b0358e1c7 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -98,7 +98,6 @@ void platform_sleep(uint32_t ms); void platform_get_user_directory(utf8* outPath, const utf8* subDirectory, size_t outSize); bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc, size_t outSize); utf8* platform_open_directory_browser(const utf8* title); -bool platform_get_steam_path(utf8* outPath, size_t outSize); std::string platform_get_rct1_steam_dir(); std::string platform_get_rct2_steam_dir();