From b4b1624e085ed1e6961ad288fcf40960d5a51db5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Fri, 23 Oct 2015 16:06:26 +0200 Subject: [PATCH 1/2] Set path properly for OS X --- src/openrct2.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/openrct2.c b/src/openrct2.c index 6a6692d8d9..dc68917c8d 100644 --- a/src/openrct2.c +++ b/src/openrct2.c @@ -47,6 +47,9 @@ #include #include #include +#if defined(__APPLE__) && defined(__MACH__) +#include +#endif // defined(__APPLE__) && defined(__MACH__) #endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) int gOpenRCT2StartupAction = STARTUP_ACTION_TITLE; @@ -171,14 +174,23 @@ static void openrct2_set_exe_path() tempPath[exeDelimiterIndex] = L'\0'; _wfullpath(exePath, tempPath, MAX_PATH); WideCharToMultiByte(CP_UTF8, 0, exePath, countof(exePath), gExePath, countof(gExePath), NULL, NULL); -#else +#else // _WIN32 char exePath[MAX_PATH]; +#if defined(__APPLE__) && defined(__MACH__) + int size = MAX_PATH; + int result = _NSGetExecutablePath(exePath, &size); + if (result != 0) { + log_fatal("failed to get path"); + } + exePath[MAX_PATH - 1] = '\0'; +#else // defined(__APPLE__) && defined(__MACH__) 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'; +#endif // defined(__APPLE__) && defined(__MACH__) log_verbose("######################################## Setting exe path to %s", exePath); char *exeDelimiter = strrchr(exePath, platform_get_path_separator()); if (exeDelimiter == NULL) From d1b6d175c7b4e1a4ff0a2f8452711150f46abe42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Tue, 1 Dec 2015 21:38:35 +0100 Subject: [PATCH 2/2] Make each platform set its own exe path --- src/openrct2.c | 48 ++--------------------------------------- src/platform/linux.c | 22 +++++++++++++++++++ src/platform/osx.c | 24 +++++++++++++++++++++ src/platform/platform.h | 1 + src/platform/windows.c | 18 +++++++++++++++- 5 files changed, 66 insertions(+), 47 deletions(-) diff --git a/src/openrct2.c b/src/openrct2.c index dc68917c8d..6445a55572 100644 --- a/src/openrct2.c +++ b/src/openrct2.c @@ -47,9 +47,6 @@ #include #include #include -#if defined(__APPLE__) && defined(__MACH__) -#include -#endif // defined(__APPLE__) && defined(__MACH__) #endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) int gOpenRCT2StartupAction = STARTUP_ACTION_TITLE; @@ -161,49 +158,8 @@ static void openrct2_copy_files_over(const utf8 *originalDirectory, const utf8 * // TODO move to platform static void openrct2_set_exe_path() { -#ifdef _WIN32 - wchar_t exePath[MAX_PATH]; - wchar_t tempPath[MAX_PATH]; - wchar_t *exeDelimiter; - int exeDelimiterIndex; - - GetModuleFileNameW(NULL, exePath, MAX_PATH); - exeDelimiter = wcsrchr(exePath, platform_get_path_separator()); - exeDelimiterIndex = (int)(exeDelimiter - exePath); - lstrcpynW(tempPath, exePath, exeDelimiterIndex + 1); - tempPath[exeDelimiterIndex] = L'\0'; - _wfullpath(exePath, tempPath, MAX_PATH); - WideCharToMultiByte(CP_UTF8, 0, exePath, countof(exePath), gExePath, countof(gExePath), NULL, NULL); -#else // _WIN32 - char exePath[MAX_PATH]; -#if defined(__APPLE__) && defined(__MACH__) - int size = MAX_PATH; - int result = _NSGetExecutablePath(exePath, &size); - if (result != 0) { - log_fatal("failed to get path"); - } - exePath[MAX_PATH - 1] = '\0'; -#else // defined(__APPLE__) && defined(__MACH__) - 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'; -#endif // defined(__APPLE__) && defined(__MACH__) - log_verbose("######################################## Setting exe path to %s", exePath); - char *exeDelimiter = strrchr(exePath, platform_get_path_separator()); - if (exeDelimiter == NULL) - { - log_error("should never happen here"); - gExePath[0] = '\0'; - return; - } - int exeDelimiterIndex = (int)(exeDelimiter - exePath); - - safe_strncpy(gExePath, exePath, exeDelimiterIndex + 1); - gExePath[exeDelimiterIndex] = '\0'; -#endif // _WIN32 + platform_get_exe_path(gExePath); + log_verbose("Setting exe path to %s", gExePath); } /** diff --git a/src/platform/linux.c b/src/platform/linux.c index 7f9dcca4d3..f1f2a911fd 100644 --- a/src/platform/linux.c +++ b/src/platform/linux.c @@ -36,6 +36,28 @@ struct dummy { struct dummy* ptr; }; +void platform_get_exe_path(utf8 *outPath) +{ + char exePath[MAX_PATH]; + 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'; + char *exeDelimiter = strrchr(exePath, platform_get_path_separator()); + if (exeDelimiter == NULL) + { + log_error("should never happen here"); + outPath[0] = '\0'; + return; + } + int exeDelimiterIndex = (int)(exeDelimiter - exePath); + + safe_strncpy(outPath, exePath, exeDelimiterIndex + 1); + outPath[exeDelimiterIndex] = '\0'; +} + bool platform_check_steam_overlay_attached() { void* processHandle = dlopen(NULL, RTLD_NOW); diff --git a/src/platform/osx.c b/src/platform/osx.c index c1928d0f02..3a2c8a3cef 100644 --- a/src/platform/osx.c +++ b/src/platform/osx.c @@ -22,9 +22,33 @@ #include "platform.h" +#include + bool platform_check_steam_overlay_attached() { STUB(); return false; } +void platform_get_exe_path(utf8 *outPath) +{ + char exePath[MAX_PATH]; + int 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, platform_get_path_separator()); + if (exeDelimiter == NULL) + { + log_error("should never happen here"); + outPath[0] = '\0'; + return; + } + int exeDelimiterIndex = (int)(exeDelimiter - exePath); + + safe_strncpy(outPath, exePath, exeDelimiterIndex + 1); + outPath[exeDelimiterIndex] = '\0'; +} + #endif diff --git a/src/platform/platform.h b/src/platform/platform.h index 085fe0c645..b4b6ff7bc1 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -113,6 +113,7 @@ void platform_get_date(rct2_date *out_date); void platform_get_time(rct2_time *out_time); // Platform specific definitions +void platform_get_exe_path(utf8 *outPath); char platform_get_path_separator(); bool platform_file_exists(const utf8 *path); bool platform_directory_exists(const utf8 *path); diff --git a/src/platform/windows.c b/src/platform/windows.c index 17f96bb738..efe5cd6fda 100644 --- a/src/platform/windows.c +++ b/src/platform/windows.c @@ -427,7 +427,7 @@ void platform_resolve_user_data_path() safe_strncpy(_userDataDirectoryPath, outPathTemp, sizeof(_userDataDirectoryPath)); free(outPathTemp); free(customUserDataPathW); - + // Ensure path ends with separator int len = strlen(_userDataDirectoryPath); if (_userDataDirectoryPath[len - 1] != separator[0]) { @@ -835,4 +835,20 @@ char *strndup(const char *src, size_t size) dst[len] = '\0'; return (char *)dst; } + +void platform_get_exe_path(utf8 *outPath) +{ + wchar_t exePath[MAX_PATH]; + wchar_t tempPath[MAX_PATH]; + wchar_t *exeDelimiter; + int exeDelimiterIndex; + + GetModuleFileNameW(NULL, exePath, MAX_PATH); + exeDelimiter = wcsrchr(exePath, platform_get_path_separator()); + exeDelimiterIndex = (int)(exeDelimiter - exePath); + lstrcpynW(tempPath, exePath, exeDelimiterIndex + 1); + tempPath[exeDelimiterIndex] = L'\0'; + _wfullpath(exePath, tempPath, MAX_PATH); + WideCharToMultiByte(CP_UTF8, 0, exePath, countof(exePath), outPath, MAX_PATH, NULL, NULL); +} #endif