Merge pull request #2396 from janisozaur/osx-path

Set path properly for OS X
This commit is contained in:
Ted John 2015-12-05 14:21:41 +00:00
commit e77537b27f
5 changed files with 66 additions and 35 deletions

View file

@ -158,40 +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
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';
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);
}
/**

View file

@ -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);

View file

@ -22,9 +22,33 @@
#include "platform.h"
#include <mach-o/dyld.h>
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

View file

@ -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);

View file

@ -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