From 87cd9f88b37b9b35b015d9881dfac2aea2bce2f9 Mon Sep 17 00:00:00 2001 From: Ted John Date: Fri, 1 Dec 2017 23:48:46 +0000 Subject: [PATCH 1/3] Rewrite Platform::GetInstallPath --- src/openrct2/Context.cpp | 11 +++ src/openrct2/core/Path.cpp | 11 +++ src/openrct2/core/Path.hpp | 2 + src/openrct2/platform/Android.cpp | 10 --- src/openrct2/platform/Linux.cpp | 66 --------------- src/openrct2/platform/Platform.Android.cpp | 12 +++ src/openrct2/platform/Platform.Linux.cpp | 99 ++++++++++++++++++++++ src/openrct2/platform/Platform.Posix.cpp | 8 -- src/openrct2/platform/Platform.Win32.cpp | 94 ++++++++++++++------ src/openrct2/platform/Platform.macOS.cpp | 56 ++++++++++++ src/openrct2/platform/Platform2.h | 1 + src/openrct2/platform/Posix.cpp | 48 ----------- src/openrct2/platform/Windows.cpp | 55 ------------ src/openrct2/platform/macos.m | 45 ---------- src/openrct2/platform/platform.h | 6 -- 15 files changed, 258 insertions(+), 266 deletions(-) diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index b9a15e6f4b..2700e359a1 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -1169,4 +1169,15 @@ extern "C" } String::Set(outPath, outSize, path.c_str()); } + + /** + * This function is deprecated. + * Use IPlatformEnvironment instad. + */ + void platform_get_openrct_data_path(utf8 * outPath, size_t outSize) + { + auto env = GetContext()->GetPlatformEnvironment(); + auto path = env->GetDirectoryPath(DIRBASE::OPENRCT2); + String::Set(outPath, outSize, path.c_str()); + } } diff --git a/src/openrct2/core/Path.cpp b/src/openrct2/core/Path.cpp index c58c77e6ac..0ca6056834 100644 --- a/src/openrct2/core/Path.cpp +++ b/src/openrct2/core/Path.cpp @@ -84,6 +84,11 @@ namespace Path platform_ensure_directory_exists(path.c_str()); } + bool DirectoryExists(const std::string &path) + { + return platform_directory_exists(path.c_str()); + } + std::string GetFileName(const std::string &path) { return GetFileName(path.c_str()); @@ -208,6 +213,12 @@ namespace Path #endif } + std::string GetAbsolute(const std::string &relative) + { + utf8 absolute[MAX_PATH]; + return GetAbsolute(absolute, sizeof(absolute), relative.c_str()); + } + bool Equals(const std::string &a, const std::string &b) { return String::Equals(a.c_str(), b.c_str()); diff --git a/src/openrct2/core/Path.hpp b/src/openrct2/core/Path.hpp index e5c4292263..c5986ba2a1 100644 --- a/src/openrct2/core/Path.hpp +++ b/src/openrct2/core/Path.hpp @@ -34,6 +34,7 @@ namespace Path utf8 * GetDirectory(const utf8 * path); utf8 * GetDirectory(utf8 * buffer, size_t bufferSize, const utf8 * path); void CreateDirectory(const std::string &path); + bool DirectoryExists(const std::string &path); std::string GetFileName(const std::string &path); const utf8 * GetFileName(const utf8 * path); std::string GetFileNameWithoutExtension(const std::string &path); @@ -42,6 +43,7 @@ namespace Path const std::string GetExtension(const std::string &path); const utf8 * GetExtension(const utf8 * path); utf8 * GetAbsolute(utf8 * buffer, size_t bufferSize, const utf8 * relativePath); + std::string GetAbsolute(const std::string &relative); bool Equals(const std::string &a, const std::string &b); bool Equals(const utf8 * a, const utf8 * b); diff --git a/src/openrct2/platform/Android.cpp b/src/openrct2/platform/Android.cpp index a80b562a08..9a25862d07 100644 --- a/src/openrct2/platform/Android.cpp +++ b/src/openrct2/platform/Android.cpp @@ -24,11 +24,6 @@ #include #include -void platform_get_exe_path(utf8 *outPath, size_t outSize) -{ - safe_strcpy(outPath, "/sdcard/openrct2", outSize); -} - #ifndef NO_TTF bool platform_get_font_path(TTFFontDescriptor *font, utf8 *buffer, size_t size) { @@ -37,11 +32,6 @@ bool platform_get_font_path(TTFFontDescriptor *font, utf8 *buffer, size_t size) } #endif -void platform_posix_sub_resolve_openrct_data_path(utf8 *out, size_t size) { - safe_strcpy(out, "/sdcard/openrct2", size); -} - - uint16 platform_get_locale_language() { return LANGUAGE_ENGLISH_UK; } diff --git a/src/openrct2/platform/Linux.cpp b/src/openrct2/platform/Linux.cpp index 00e3024860..4d751858c4 100644 --- a/src/openrct2/platform/Linux.cpp +++ b/src/openrct2/platform/Linux.cpp @@ -42,72 +42,6 @@ #include "../util/Util.h" #include "platform.h" -void platform_get_exe_path(utf8 *outPath, size_t outSize) -{ - char exePath[MAX_PATH]; -#ifdef __linux__ - ssize_t bytesRead; - bytesRead = readlink("/proc/self/exe", exePath, MAX_PATH); - if (bytesRead == -1) { - log_fatal("failed to read /proc/self/exe"); - } - exePath[bytesRead - 1] = '\0'; -#elif defined(__FreeBSD__) - size_t exeLen = sizeof(exePath); - const sint32 mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; - if (sysctl(mib, 4, exePath, &exeLen, NULL, 0) == -1) { - log_fatal("failed to get process path"); - - } -#elif defined(__OpenBSD__) - // There is no way to get the path name of a running executable. - // If you are not using the port or package, you may have to change this line! - strlcpy(exePath, "/usr/local/bin/", sizeof(exePath)); -#else -#error "Platform does not support full path exe retrieval" -#endif - char *exeDelimiter = strrchr(exePath, *PATH_SEPARATOR); - if (exeDelimiter == nullptr) - { - log_error("should never happen here"); - outPath[0] = '\0'; - return; - } - *exeDelimiter = '\0'; - - safe_strcpy(outPath, exePath, outSize); -} - -/** - * Default directory fallback is: - * - (command line argument) - * - /data - * - /usr/local/share/openrct2 - * - /var/lib/openrct2 - * - /usr/share/openrct2 - */ -void platform_posix_sub_resolve_openrct_data_path(utf8 *out, size_t size) { - static const utf8 *searchLocations[] = { - "../share/openrct2", -#ifdef ORCT2_RESOURCE_DIR - // defined in CMakeLists.txt - ORCT2_RESOURCE_DIR, -#endif // ORCT2_RESOURCE_DIR - "/usr/local/share/openrct2", - "/var/lib/openrct2", - "/usr/share/openrct2", - }; - for (auto searchLocation : searchLocations) - { - log_verbose("Looking for OpenRCT2 data in %s", searchLocation); - if (platform_directory_exists(searchLocation)) - { - safe_strcpy(out, searchLocation, size); - return; - } - } -} - uint16 platform_get_locale_language(){ const char *langString = setlocale(LC_MESSAGES, ""); if(langString != nullptr){ diff --git a/src/openrct2/platform/Platform.Android.cpp b/src/openrct2/platform/Platform.Android.cpp index bdabd83035..8262a55007 100644 --- a/src/openrct2/platform/Platform.Android.cpp +++ b/src/openrct2/platform/Platform.Android.cpp @@ -16,6 +16,7 @@ #ifdef __ANDROID__ +#include #include "Platform2.h" namespace Platform @@ -39,6 +40,17 @@ namespace Platform { return std::string(); } + + std::string GetInstallPath() + { + return "/sdcard/openrct2"; + } + + std::string GetCurrentExecutablePath() + { + assert(false, "GetCurrentExecutablePath() not implemented for Android."); + return std::string(); + } } #endif diff --git a/src/openrct2/platform/Platform.Linux.cpp b/src/openrct2/platform/Platform.Linux.cpp index 9db7b69b65..0f98e1b2e5 100644 --- a/src/openrct2/platform/Platform.Linux.cpp +++ b/src/openrct2/platform/Platform.Linux.cpp @@ -16,8 +16,11 @@ #if defined(__linux__) && !defined(__ANDROID__) +#include #include #include "../core/Path.hpp" +#include "../core/Util.hpp" +#include "../OpenRCT2.h" #include "platform.h" #include "Platform2.h" @@ -63,6 +66,102 @@ namespace Platform } return std::string(); } + + static std::string FindInstallPath() + { + static const char * SearchLocations[] = + { + "../share/openrct2", +#ifdef ORCT2_RESOURCE_DIR + // defined in CMakeLists.txt + ORCT2_RESOURCE_DIR, +#endif // ORCT2_RESOURCE_DIR + "/usr/local/share/openrct2", + "/var/lib/openrct2", + "/usr/share/openrct2", + }; + for (auto searchLocation : SearchLocations) + { + log_verbose("Looking for OpenRCT2 data in %s", searchLocation); + if (Path::DirectoryExists(searchLocation)) + { + return searchLocation; + } + } + return std::string(); + } + + static std::string GetCurrentWorkingDirectory() + { + char cwdPath[PATH_MAX]; + if (getcwd(cwdPath, sizeof(cwdPath)) != nullptr) + { + return cwdPath; + } + return std::string(); + } + + std::string GetInstallPath() + { + // 1. Try command line argument + auto path = std::string(gCustomOpenrctDataPath); + if (!path.empty()) + { + path = Path::GetAbsolute(path); + } + else + { + // 2. Try ${exeDir}/data + auto exePath = Platform::GetCurrentExecutablePath(); + auto exeDirectory = Path::GetDirectory(exePath); + path = Path::Combine(exeDirectory, "data"); + if (!Path::DirectoryExists(path)) + { + // 3. Try standard system app directories + path = FindInstallPath(); + if (path.empty()) + { + // 4. Fallback to ${cwd}/data + path = GetCurrentWorkingDirectory(); + if (!path.empty()) + { + path = Path::Combine(path, "data"); + } + else + { + return "/"; + } + } + } + } + return path; + } + + std::string GetCurrentExecutablePath() + { + char exePath[PATH_MAX] = { 0 }; +#ifdef __linux__ + auto bytesRead = readlink("/proc/self/exe", exePath, sizeof(exePath)); + if (bytesRead == -1) + { + log_fatal("failed to read /proc/self/exe"); + } +#elif defined(__FreeBSD__) + const sint32 mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; + auto exeLen = sizeof(exePath); + if (sysctl(mib, 4, exePath, &exeLen, nullptr, 0) == -1) + { + log_fatal("failed to get process path"); + } +#elif defined(__OpenBSD__) + // There is no way to get the path name of a running executable. + // If you are not using the port or package, you may have to change this line! + strlcpy(exePath, "/usr/local/bin/", sizeof(exePath)); +#else + #error "Platform does not support full path exe retrieval" +#endif + return exePath; + } } #endif diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index caa4a65839..038406cdb4 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -76,14 +76,6 @@ namespace Platform return path; } - std::string GetInstallPath() - { - utf8 path[MAX_PATH]; - platform_resolve_openrct_data_path(); - platform_get_openrct_data_path(path, sizeof(path)); - return path; - } - std::string FormatShortDate(std::time_t timestamp) { char date[20]; diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 048f8170bb..4dd9559b1a 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -16,6 +16,8 @@ #ifdef _WIN32 +#include + #define WIN32_LEAN_AND_MEAN #include #include @@ -30,11 +32,19 @@ #include "../core/Path.hpp" #include "../core/String.hpp" #include "../core/Util.hpp" +#include "../OpenRCT2.h" #include "Platform2.h" #include "platform.h" namespace Platform { +#ifdef __USE_SHGETKNOWNFOLDERPATH__ + static std::string WIN32_GetKnownFolderPath(REFKNOWNFOLDERID rfid); +#else + static std::string WIN32_GetFolderPath(int nFolder); +#endif + static std::string WIN32_GetModuleFileNameW(HMODULE hModule); + uint32 GetTicks() { return platform_get_ticks(); @@ -72,31 +82,6 @@ namespace Platform return result; } -#ifdef __USE_SHGETKNOWNFOLDERPATH__ - static std::string WIN32_GetKnownFolderPath(REFKNOWNFOLDERID rfid) - { - std::string path; - wchar_t * wpath = nullptr; - if (SUCCEEDED(SHGetKnownFolderPath(rfid, KF_FLAG_CREATE, nullptr, &wpath))) - { - path = String::ToUtf8(std::wstring(wpath)); - } - CoTaskMemFree(wpath); - return path; - } -#else - static std::string WIN32_GetFolderPath(int nFolder) - { - std::string path; - wchar_t wpath[MAX_PATH]; - if (SUCCEEDED(SHGetFolderPathW(nullptr, nFolder | CSIDL_FLAG_CREATE, nullptr, 0, wpath))) - { - path = String::ToUtf8(std::wstring(wpath)); - } - return path; - } -#endif - std::string GetFolderPath(SPECIAL_FOLDER folder) { switch (folder) @@ -141,12 +126,25 @@ namespace Platform std::string GetInstallPath() { - utf8 path[MAX_PATH]; - platform_resolve_openrct_data_path(); - platform_get_openrct_data_path(path, sizeof(path)); + auto path = std::string(gCustomOpenrctDataPath); + if (!path.empty()) + { + path = Path::GetAbsolute(path); + } + else + { + auto exePath = GetCurrentExecutablePath(); + auto exeDirectory = Path::GetDirectory(exePath); + path = Path::Combine(exeDirectory, "data"); + } return path; } + std::string GetCurrentExecutablePath() + { + return WIN32_GetModuleFileNameW(nullptr); + } + std::string GetDocsPath() { return std::string(); @@ -198,6 +196,46 @@ namespace Platform return result; } + +#ifdef __USE_SHGETKNOWNFOLDERPATH__ + static std::string WIN32_GetKnownFolderPath(REFKNOWNFOLDERID rfid) + { + std::string path; + wchar_t * wpath = nullptr; + if (SUCCEEDED(SHGetKnownFolderPath(rfid, KF_FLAG_CREATE, nullptr, &wpath))) + { + path = String::ToUtf8(std::wstring(wpath)); + } + CoTaskMemFree(wpath); + return path; + } +#else + static std::string WIN32_GetFolderPath(int nFolder) + { + std::string path; + wchar_t wpath[MAX_PATH]; + if (SUCCEEDED(SHGetFolderPathW(nullptr, nFolder | CSIDL_FLAG_CREATE, nullptr, 0, wpath))) + { + path = String::ToUtf8(std::wstring(wpath)); + } + return path; + } +#endif + + static std::string WIN32_GetModuleFileNameW(HMODULE hModule) + { + uint32 wExePathCapacity = MAX_PATH; + std::unique_ptr wExePath; + uint32 size; + do + { + wExePathCapacity *= 2; + wExePath = std::make_unique(wExePathCapacity); + size = GetModuleFileNameW(hModule, wExePath.get(), wExePathCapacity); + } + while (size >= wExePathCapacity); + return String::ToUtf8(wExePath.get()); + } } #endif diff --git a/src/openrct2/platform/Platform.macOS.cpp b/src/openrct2/platform/Platform.macOS.cpp index d82b28dc2f..35aaaebf1a 100644 --- a/src/openrct2/platform/Platform.macOS.cpp +++ b/src/openrct2/platform/Platform.macOS.cpp @@ -44,6 +44,62 @@ namespace Platform { return std::string(); } + + std::string GetInstallPath() + { + auto path = std::string(gCustomOpenrctDataPath); + if (!path.empty()) + { + path = Path::GetAbsolute(customPath); + } + else + { + auto exePath = GetCurrentExecutablePath(); + auto exeDirectory = Path::GetDirectory(exePath); + path = Path::Combine(exeDirectory, "data"); + if (!Directory::Exists(path)) + { + path = GetBundlePath(); + if (path.empty()) + { + path = "/"; + } + } + } + return path; + } + + std::string GetCurrentExecutablePath() + { + char exePath[MAX_PATH]; + uint32 size = MAX_PATH; + int result = _NSGetExecutablePath(exePath, &size); + if (result == 0) + { + return exePath; + } + else + { + return std::string(); + } + } + + static std::string GetBundlePath() + { + @autoreleasepool + { + NSBundle * bundle = [NSBundle mainBundle]; + if (bundle) + { + auto resources = bundle.resourcePath.UTF8String; + if (Path::DirectoryExists(resources)) + { + return resources; + } + } + return std::string(); + } + } } #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 38cdcefa77..45d71b158b 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -37,6 +37,7 @@ namespace Platform std::string GetFolderPath(SPECIAL_FOLDER folder); std::string GetInstallPath(); std::string GetDocsPath(); + std::string GetCurrentExecutablePath(); #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) std::string GetEnvironmentPath(const char * name); diff --git a/src/openrct2/platform/Posix.cpp b/src/openrct2/platform/Posix.cpp index 0b80c82ecc..ace051e46b 100644 --- a/src/openrct2/platform/Posix.cpp +++ b/src/openrct2/platform/Posix.cpp @@ -48,7 +48,6 @@ extern "C" { static utf8 _userDataDirectoryPath[MAX_PATH] = { 0 }; -static utf8 _openrctDataDirectoryPath[MAX_PATH] = { 0 }; void platform_get_date_utc(rct2_date *out_date) { @@ -400,53 +399,6 @@ static wchar_t *regular_to_wchar(const char* src) return w_buffer; } -void platform_get_openrct_data_path(utf8 *outPath, size_t outSize) -{ - safe_strcpy(outPath, _openrctDataDirectoryPath, outSize); -} - -/** - * Default directory fallback is: - * - (command line argument) - * - /data - * - - */ -void platform_resolve_openrct_data_path() -{ - if (gCustomOpenrctDataPath[0] != 0) { - // NOTE: second argument to `realpath` is meant to either be NULL or `PATH_MAX`-sized buffer, - // since our `MAX_PATH` macro is set to some other value, pass NULL to have `realpath` return - // a `malloc`ed buffer. - char *resolved_path = realpath(gCustomOpenrctDataPath, NULL); - if (resolved_path == nullptr) { - log_error("Could not resolve path \"%s\", errno = %d", gCustomOpenrctDataPath, errno); - return; - } else { - safe_strcpy(_openrctDataDirectoryPath, resolved_path, MAX_PATH); - free(resolved_path); - } - - path_end_with_separator(_openrctDataDirectoryPath, MAX_PATH); - return; - } - - char buffer[MAX_PATH]; - platform_get_exe_path(buffer, sizeof(buffer)); - - safe_strcat_path(buffer, "data", MAX_PATH); - log_verbose("Looking for OpenRCT2 data in %s", buffer); - if (platform_directory_exists(buffer)) - { - _openrctDataDirectoryPath[0] = '\0'; - safe_strcpy(_openrctDataDirectoryPath, buffer, MAX_PATH); - log_verbose("Found OpenRCT2 data in %s", _openrctDataDirectoryPath); - return; - } - - platform_posix_sub_resolve_openrct_data_path(_openrctDataDirectoryPath, sizeof(_openrctDataDirectoryPath)); - log_verbose("Trying to use OpenRCT2 data in %s", _openrctDataDirectoryPath); -} - time_t platform_file_get_modified_time(const utf8* path){ struct stat buf; if (stat(path, &buf) == 0) { diff --git a/src/openrct2/platform/Windows.cpp b/src/openrct2/platform/Windows.cpp index 1a20891c94..68f790f798 100644 --- a/src/openrct2/platform/Windows.cpp +++ b/src/openrct2/platform/Windows.cpp @@ -53,8 +53,6 @@ extern "C" { -static utf8 _openrctDataDirectoryPath[MAX_PATH] = { 0 }; - #define OPENRCT2_DLL_MODULE_NAME "openrct2.dll" static HMODULE _dllModule = nullptr; @@ -205,45 +203,6 @@ bool platform_file_delete(const utf8 *path) return success == TRUE; } -void platform_resolve_openrct_data_path() -{ - wchar_t wOutPath[MAX_PATH]; - - if (gCustomOpenrctDataPath[0] != 0) { - wchar_t *customOpenrctDataPathW = utf8_to_widechar(gCustomOpenrctDataPath); - if (GetFullPathNameW(customOpenrctDataPathW, (DWORD)Util::CountOf(wOutPath), wOutPath, NULL) == 0) { - log_fatal("Unable to resolve path '%s'.", gCustomOpenrctDataPath); - exit(-1); - } - utf8 *outPathTemp = widechar_to_utf8(wOutPath); - safe_strcpy(_openrctDataDirectoryPath, outPathTemp, sizeof(_openrctDataDirectoryPath)); - free(outPathTemp); - free(customOpenrctDataPathW); - - path_end_with_separator(_openrctDataDirectoryPath, sizeof(_openrctDataDirectoryPath)); - return; - } - char buffer[MAX_PATH]; - platform_get_exe_path(buffer, sizeof(buffer)); - - safe_strcat_path(buffer, "data", MAX_PATH); - - if (platform_directory_exists(buffer)) - { - _openrctDataDirectoryPath[0] = '\0'; - safe_strcpy(_openrctDataDirectoryPath, buffer, sizeof(_openrctDataDirectoryPath)); - return; - } else { - log_fatal("Unable to resolve openrct data path."); - exit(-1); - } -} - -void platform_get_openrct_data_path(utf8 *outPath, size_t outSize) -{ - safe_strcpy(outPath, _openrctDataDirectoryPath, outSize); -} - bool platform_get_steam_path(utf8 * outPath, size_t outSize) { wchar_t * wSteamPath; @@ -488,20 +447,6 @@ uint8 platform_get_locale_date_format() return DATE_FORMAT_DAY_MONTH_YEAR; } -void platform_get_exe_path(utf8 *outPath, size_t outSize) -{ - wchar_t exePath[MAX_PATH]; - wchar_t tempPath[MAX_PATH]; - wchar_t *exeDelimiter; - - GetModuleFileNameW(NULL, exePath, MAX_PATH); - exeDelimiter = wcsrchr(exePath, *PATH_SEPARATOR); - *exeDelimiter = L'\0'; - wcscpy_s(tempPath, MAX_PATH, exePath); - _wfullpath(exePath, tempPath, MAX_PATH); - WideCharToMultiByte(CP_UTF8, 0, exePath, MAX_PATH, outPath, (sint32) outSize, NULL, NULL); -} - bool platform_get_font_path(TTFFontDescriptor *font, utf8 *buffer, size_t size) { #if !defined(__MINGW32__) && ((NTDDI_VERSION >= NTDDI_VISTA) && !defined(_USING_V110_SDK71_) && !defined(_ATL_XP_TARGETING)) diff --git a/src/openrct2/platform/macos.m b/src/openrct2/platform/macos.m index cb29c7ca9f..ddf58ec298 100644 --- a/src/openrct2/platform/macos.m +++ b/src/openrct2/platform/macos.m @@ -34,51 +34,6 @@ void macos_disallow_automatic_window_tabbing() } } -void platform_get_exe_path(utf8 *outPath, size_t outSize) -{ - if (outSize == 0) return; - char exePath[MAX_PATH]; - uint32_t size = MAX_PATH; - int result = _NSGetExecutablePath(exePath, &size); - if (result != 0) { - log_fatal("failed to get path"); - } - exePath[MAX_PATH - 1] = '\0'; - char *exeDelimiter = strrchr(exePath, *PATH_SEPARATOR); - if (exeDelimiter == NULL) - { - log_error("should never happen here"); - outPath[0] = '\0'; - return; - } - *exeDelimiter = '\0'; - - safe_strcpy(outPath, exePath, outSize); -} - -/** - * Default directory fallback is: - * - (command line argument) - * - /data - * - - */ -void platform_posix_sub_resolve_openrct_data_path(utf8 *out, size_t size) { - @autoreleasepool - { - NSBundle *bundle = [NSBundle mainBundle]; - if (bundle) - { - const utf8 *resources = bundle.resourcePath.UTF8String; - if (platform_directory_exists(resources)) - { - out[0] = '\0'; - safe_strcpy(out, resources, size); - return; - } - } - } -} - utf8* macos_str_decomp_to_precomp(utf8 *input) { @autoreleasepool diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index a761d9454a..53e7ebf163 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -95,7 +95,6 @@ void platform_get_date_local(rct2_date *out_date); void platform_get_time_local(rct2_time *out_time); // Platform specific definitions -void platform_get_exe_path(utf8 *outPath, size_t outSize); bool platform_file_exists(const utf8 *path); bool platform_directory_exists(const utf8 *path); bool platform_original_game_data_exists(const utf8 *path); @@ -114,7 +113,6 @@ bool platform_file_move(const utf8 *srcPath, const utf8 *dstPath); bool platform_file_delete(const utf8 *path); uint32 platform_get_ticks(); void platform_sleep(uint32 ms); -void platform_resolve_openrct_data_path(); void platform_get_openrct_data_path(utf8 *outPath, size_t outSize); void platform_get_user_directory(utf8 *outPath, const utf8 *subDirectory, size_t outSize); utf8* platform_get_username(); @@ -162,10 +160,6 @@ void core_init(); __declspec(dllexport) sint32 StartOpenRCT(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, sint32 nCmdShow); #endif // _WIN32 -#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) || defined(__ANDROID__) - void platform_posix_sub_resolve_openrct_data_path(utf8 *out, size_t size); -#endif - #if defined(__APPLE__) && defined(__MACH__) void macos_disallow_automatic_window_tabbing(); utf8* macos_str_decomp_to_precomp(utf8 *input); From 172b5907fa7f798f20ca7150293b681e908f91f3 Mon Sep 17 00:00:00 2001 From: Richard Jenkins Date: Mon, 29 Jan 2018 00:03:00 +0000 Subject: [PATCH 2/3] Fix platform functions for macOS --- OpenRCT2.xcodeproj/project.pbxproj | 8 ++-- .../{Platform.macOS.cpp => Platform.macOS.mm} | 43 +++++++++++-------- 2 files changed, 28 insertions(+), 23 deletions(-) rename src/openrct2/platform/{Platform.macOS.cpp => Platform.macOS.mm} (91%) diff --git a/OpenRCT2.xcodeproj/project.pbxproj b/OpenRCT2.xcodeproj/project.pbxproj index 72c30a861a..9da1d15ae4 100644 --- a/OpenRCT2.xcodeproj/project.pbxproj +++ b/OpenRCT2.xcodeproj/project.pbxproj @@ -200,7 +200,6 @@ 4CE462411FD0710E0001CD98 /* Game.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4CE4623F1FD0710E0001CD98 /* Game.cpp */; }; 4CE462451FD161360001CD98 /* Platform.Android.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4CE462441FD161360001CD98 /* Platform.Android.cpp */; }; 4CE4624A1FD1613D0001CD98 /* Platform.Linux.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4CE462461FD1613D0001CD98 /* Platform.Linux.cpp */; }; - 4CE4624B1FD1613D0001CD98 /* Platform.macOS.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4CE462471FD1613D0001CD98 /* Platform.macOS.cpp */; }; 4CE4624C1FD1613D0001CD98 /* Platform.Posix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4CE462481FD1613D0001CD98 /* Platform.Posix.cpp */; }; 4CE4624D1FD1613D0001CD98 /* Platform.Win32.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4CE462491FD1613D0001CD98 /* Platform.Win32.cpp */; }; 4CFE4E801F90A3F1005243C2 /* Peep.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4CFE4E7B1F90A3F1005243C2 /* Peep.cpp */; }; @@ -485,6 +484,7 @@ F775F5371EE3724F001F00E7 /* DummyAudioContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F775F5361EE3724F001F00E7 /* DummyAudioContext.cpp */; }; F775F5381EE3725C001F00E7 /* DummyAudioContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F775F5361EE3724F001F00E7 /* DummyAudioContext.cpp */; }; F79F428F1F3260F1009E42F8 /* changelog.txt in Resources */ = {isa = PBXBuildFile; fileRef = F79F428E1F3260F1009E42F8 /* changelog.txt */; }; + F7B2048A201E91BF0000AD7E /* Platform.macOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = F7B20489201E91BF0000AD7E /* Platform.macOS.mm */; }; F7CB863F1EEDA0B50030C877 /* WindowManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F7CB863D1EEDA0B50030C877 /* WindowManager.cpp */; }; F7CB864A1EEDA1330030C877 /* KeyboardShortcuts.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F7CB86471EEDA1330030C877 /* KeyboardShortcuts.cpp */; }; F7CB864D1EEDA1A80030C877 /* DummyWindowManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F7CB864B1EEDA1A80030C877 /* DummyWindowManager.cpp */; }; @@ -867,7 +867,6 @@ 4CE462401FD0710E0001CD98 /* Game.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Game.h; sourceTree = ""; }; 4CE462441FD161360001CD98 /* Platform.Android.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Platform.Android.cpp; sourceTree = ""; }; 4CE462461FD1613D0001CD98 /* Platform.Linux.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Platform.Linux.cpp; sourceTree = ""; }; - 4CE462471FD1613D0001CD98 /* Platform.macOS.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Platform.macOS.cpp; sourceTree = ""; }; 4CE462481FD1613D0001CD98 /* Platform.Posix.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Platform.Posix.cpp; sourceTree = ""; }; 4CE462491FD1613D0001CD98 /* Platform.Win32.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Platform.Win32.cpp; sourceTree = ""; }; 4CFE4E7B1F90A3F1005243C2 /* Peep.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Peep.cpp; sourceTree = ""; }; @@ -1414,6 +1413,7 @@ F775F5331EE35A6B001F00E7 /* DummyUiContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DummyUiContext.cpp; sourceTree = ""; }; F775F5361EE3724F001F00E7 /* DummyAudioContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DummyAudioContext.cpp; sourceTree = ""; }; F79F428E1F3260F1009E42F8 /* changelog.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = changelog.txt; path = distribution/changelog.txt; sourceTree = SOURCE_ROOT; }; + F7B20489201E91BF0000AD7E /* Platform.macOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = Platform.macOS.mm; sourceTree = ""; }; F7CB863D1EEDA0B50030C877 /* WindowManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WindowManager.cpp; sourceTree = ""; }; F7CB863E1EEDA0B50030C877 /* WindowManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WindowManager.h; sourceTree = ""; }; F7CB86471EEDA1330030C877 /* KeyboardShortcuts.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = KeyboardShortcuts.cpp; sourceTree = ""; }; @@ -2243,7 +2243,7 @@ 4CE462441FD161360001CD98 /* Platform.Android.cpp */, F76C845E1EC4E7CC00FA49E2 /* platform.h */, 4CE462461FD1613D0001CD98 /* Platform.Linux.cpp */, - 4CE462471FD1613D0001CD98 /* Platform.macOS.cpp */, + F7B20489201E91BF0000AD7E /* Platform.macOS.mm */, 4CE462481FD1613D0001CD98 /* Platform.Posix.cpp */, 4CE462491FD1613D0001CD98 /* Platform.Win32.cpp */, F76C84601EC4E7CC00FA49E2 /* Platform2.h */, @@ -3201,7 +3201,6 @@ 4C7B53F3200143C200A52E21 /* Chat.cpp in Sources */, 4C7B53F4200143C200A52E21 /* Colour.cpp in Sources */, 4C7B547B2010DF4C00A52E21 /* Posix.cpp in Sources */, - 4CE4624B1FD1613D0001CD98 /* Platform.macOS.cpp in Sources */, 4C7B53A51FFC180400A52E21 /* ObjectList.cpp in Sources */, 4C93F1501F8B744400A9330D /* SideFrictionRollerCoaster.cpp in Sources */, 4C93F13D1F8B744400A9330D /* CompactInvertedCoaster.cpp in Sources */, @@ -3238,6 +3237,7 @@ 4C93F1541F8B744400A9330D /* TwisterRollerCoaster.cpp in Sources */, 4CC4B8E71FE00C4E00660D62 /* Diagnostic.cpp in Sources */, C666EE7B1F37ACB10061AA04 /* TitleExit.cpp in Sources */, + F7B2048A201E91BF0000AD7E /* Platform.macOS.mm in Sources */, F7D7747F1EC61E5100BE6EBC /* UiContext.macOS.mm in Sources */, F76C887E1EC5324E00FA49E2 /* CursorRepository.cpp in Sources */, 4C7B53FD200143C200A52E21 /* Window.cpp in Sources */, diff --git a/src/openrct2/platform/Platform.macOS.cpp b/src/openrct2/platform/Platform.macOS.mm similarity index 91% rename from src/openrct2/platform/Platform.macOS.cpp rename to src/openrct2/platform/Platform.macOS.mm index 35aaaebf1a..a212e377be 100644 --- a/src/openrct2/platform/Platform.macOS.cpp +++ b/src/openrct2/platform/Platform.macOS.mm @@ -16,7 +16,11 @@ #if defined(__APPLE__) && defined(__MACH__) +#include +#include + #include "../core/Path.hpp" +#include "../OpenRCT2.h" #include "Platform2.h" namespace Platform @@ -45,19 +49,37 @@ namespace Platform return std::string(); } + static std::string GetBundlePath() + { + @autoreleasepool + { + NSBundle * bundle = [NSBundle mainBundle]; + if (bundle) + { + auto resources = bundle.resourcePath.UTF8String; + if (Path::DirectoryExists(resources)) + { + return resources; + } + } + return std::string(); + } + } + std::string GetInstallPath() { auto path = std::string(gCustomOpenrctDataPath); if (!path.empty()) { - path = Path::GetAbsolute(customPath); + path = Path::GetAbsolute(path); } else { auto exePath = GetCurrentExecutablePath(); auto exeDirectory = Path::GetDirectory(exePath); path = Path::Combine(exeDirectory, "data"); - if (!Directory::Exists(path)) + NSString * nsPath = [NSString stringWithUTF8String:path.c_str()]; + if (![[NSFileManager defaultManager] fileExistsAtPath:nsPath]) { path = GetBundlePath(); if (path.empty()) @@ -83,23 +105,6 @@ namespace Platform return std::string(); } } - - static std::string GetBundlePath() - { - @autoreleasepool - { - NSBundle * bundle = [NSBundle mainBundle]; - if (bundle) - { - auto resources = bundle.resourcePath.UTF8String; - if (Path::DirectoryExists(resources)) - { - return resources; - } - } - return std::string(); - } - } } #endif From e8484d181647ad1c24fba5071cd8a0c555fe103f Mon Sep 17 00:00:00 2001 From: Brian Callahan Date: Mon, 29 Jan 2018 15:56:34 +0100 Subject: [PATCH 3/3] Fix build on OpenBSD and FreeBSD --- src/openrct2/platform/Platform.Linux.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openrct2/platform/Platform.Linux.cpp b/src/openrct2/platform/Platform.Linux.cpp index 0f98e1b2e5..31f26ec6b3 100644 --- a/src/openrct2/platform/Platform.Linux.cpp +++ b/src/openrct2/platform/Platform.Linux.cpp @@ -14,9 +14,9 @@ *****************************************************************************/ #pragma endregion -#if defined(__linux__) && !defined(__ANDROID__) +#if defined(__FreeBSD__) || defined(__OpenBSD__) || (defined(__linux__) && !defined(__ANDROID__)) -#include +#include #include #include "../core/Path.hpp" #include "../core/Util.hpp"