Skip to content

Commit dafe63a

Browse files
committed
fix macOS bundle working directory
1 parent 8bd16d5 commit dafe63a

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

gframe/game.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#ifdef _WIN32
1616
#include <timeapi.h>
1717
#endif
18+
#ifdef __APPLE__
19+
#include <CoreFoundation/CoreFoundation.h>
20+
#endif
1821

1922
#if defined(__SSE2__) || (defined(_M_IX86_FP) && _M_IX86_FP >= 2) || \
2023
defined(__x86_64__) || defined(_M_X64) || defined(__x86_64) || defined(_M_AMD64)
@@ -78,6 +81,9 @@ bool Game::Initialize() {
7881
params.DriverType = irr::video::EDT_OPENGL;
7982
params.WindowSize = irr::core::dimension2d<irr::u32>(gameConf.window_width, gameConf.window_height);
8083
device = irr::createDeviceEx(params);
84+
#ifdef __APPLE__
85+
FixMacOSBundleWorkingDirectory();
86+
#endif
8187
if(!device) {
8288
ErrorLog("Failed to create Irrlicht Engine device!");
8389
return false;
@@ -2330,6 +2336,24 @@ irr::core::recti Game::ResizeFit(irr::s32 x, irr::s32 y, irr::s32 x2, irr::s32 y
23302336
y2 = y2 * mul;
23312337
return irr::core::recti(x, y, x2, y2);
23322338
}
2339+
2340+
void Game::FixMacOSBundleWorkingDirectory() {
2341+
#ifdef __APPLE__
2342+
CFURLRef bundle_url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
2343+
CFURLRef bundle_base_url = CFURLCreateCopyDeletingLastPathComponent(nullptr, bundle_url);
2344+
CFStringRef bundle_ext = CFURLCopyPathExtension(bundle_url);
2345+
if(bundle_ext) {
2346+
char path[PATH_MAX];
2347+
if(CFStringCompare(bundle_ext, CFSTR("app"), kCFCompareCaseInsensitive) == kCFCompareEqualTo
2348+
&& CFURLGetFileSystemRepresentation(bundle_base_url, true, (UInt8*)path, PATH_MAX)) {
2349+
chdir(path);
2350+
}
2351+
CFRelease(bundle_ext);
2352+
}
2353+
CFRelease(bundle_url);
2354+
CFRelease(bundle_base_url);
2355+
#endif //__APPLE__
2356+
}
23332357
void Game::SetWindowsIcon() {
23342358
#ifdef _WIN32
23352359
HINSTANCE hInstance = (HINSTANCE)GetModuleHandleW(nullptr);

gframe/game.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ class Game {
260260
irr::core::vector2di ResizeCardMid(irr::s32 x, irr::s32 y, irr::s32 midx, irr::s32 midy);
261261
irr::core::recti ResizeFit(irr::s32 x, irr::s32 y, irr::s32 x2, irr::s32 y2);
262262

263+
static void FixMacOSBundleWorkingDirectory();
263264
void SetWindowsIcon();
264265
void SetWindowsScale(float scale);
265266
void FlashWindow();

gframe/gframe.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
#include <event2/thread.h>
55
#include <clocale>
66
#include <memory>
7-
#ifdef __APPLE__
8-
#import <CoreFoundation/CoreFoundation.h>
9-
#endif
107

118
#if defined(_WIN32) && (!defined(WDK_NTDDI_VERSION) || (WDK_NTDDI_VERSION < 0x0A000005)) // Redstone 4, Version 1803, Build 17134.
129
#error "This program requires the Windows 10 SDK version 1803 or above to compile on Windows. Otherwise, non-ASCII characters will not be displayed or processed correctly."
@@ -30,19 +27,7 @@ int main(int argc, char* argv[]) {
3027
std::setlocale(LC_CTYPE, "");
3128
#endif
3229
#ifdef __APPLE__
33-
CFURLRef bundle_url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
34-
CFURLRef bundle_base_url = CFURLCreateCopyDeletingLastPathComponent(nullptr, bundle_url);
35-
CFStringRef bundle_ext = CFURLCopyPathExtension(bundle_url);
36-
if (bundle_ext) {
37-
char path[PATH_MAX];
38-
if (CFStringCompare(bundle_ext, CFSTR("app"), kCFCompareCaseInsensitive) == kCFCompareEqualTo
39-
&& CFURLGetFileSystemRepresentation(bundle_base_url, true, (UInt8*)path, PATH_MAX)) {
40-
chdir(path);
41-
}
42-
CFRelease(bundle_ext);
43-
}
44-
CFRelease(bundle_url);
45-
CFRelease(bundle_base_url);
30+
ygo::Game::FixMacOSBundleWorkingDirectory();
4631
#endif //__APPLE__
4732
#ifdef _WIN32
4833
if (argc == 2 && (ygo::IsExtension(argv[1], ".ydk") || ygo::IsExtension(argv[1], ".yrp"))) { // open file from explorer

0 commit comments

Comments
 (0)