From 642d0dfca7e9efd32fe2312a23894da1cab5192b Mon Sep 17 00:00:00 2001 From: Palana Date: Thu, 9 Jan 2014 01:17:30 +0100 Subject: [PATCH] fix osx bundle loading of required resources --- obs/CMakeLists.txt | 4 ++++ obs/obs-app.cpp | 3 +++ obs/platform-osx.mm | 38 +++++++++++++++++++++++++++++++++++++- obs/platform-windows.cpp | 6 ++++++ obs/platform-x11.cpp | 6 ++++++ obs/platform.hpp | 4 ++++ 6 files changed, 60 insertions(+), 1 deletion(-) diff --git a/obs/CMakeLists.txt b/obs/CMakeLists.txt index f76c67c6c..b83d7454b 100644 --- a/obs/CMakeLists.txt +++ b/obs/CMakeLists.txt @@ -75,6 +75,10 @@ if(WIN32) elseif(APPLE) set(obs_platform_src platform-osx.mm) + add_definitions(-fobjc-arc) + if(BUILD_APP_BUNDLE) + add_definitions(-DOBS_OSX_BUNDLE=1) + endif() elseif(UNIX) set(obs_platform_src platform-x11.cpp) diff --git a/obs/obs-app.cpp b/obs/obs-app.cpp index b0a828585..2abbbdc20 100644 --- a/obs/obs-app.cpp +++ b/obs/obs-app.cpp @@ -183,6 +183,9 @@ bool OBSApp::OnInit() { base_set_log_handler(do_log); + if (!InitApplicationBundle()) + return false; + if (!wxApp::OnInit()) return false; wxInitAllImageHandlers(); diff --git a/obs/platform-osx.mm b/obs/platform-osx.mm index f634b49fe..f531b85b6 100644 --- a/obs/platform-osx.mm +++ b/obs/platform-osx.mm @@ -16,13 +16,15 @@ ******************************************************************************/ #include +#include #include "platform.hpp" -using namespace std; #include #import +using namespace std; + bool GetDataFilePath(const char *data, string &output) { stringstream str; @@ -41,3 +43,37 @@ void GetMonitors(vector &monitors) frame.size.width, frame.size.height); } } + +bool InitApplicationBundle() +{ +#ifdef OBS_OSX_BUNDLE + static bool initialized = false; + if (initialized) + return true; + + try { + NSBundle *bundle = [NSBundle mainBundle]; + if (!bundle) + throw "Could not find main bundle"; + + NSString *exe_path = [bundle executablePath]; + if (!exe_path) + throw "Could not find executable path"; + + NSString *path = [exe_path stringByDeletingLastPathComponent]; + + if (chdir([path fileSystemRepresentation])) + throw "Could not change working directory to " + "bundle path"; + + } catch (const char* error) { + blog(LOG_ERROR, "InitBundle: %s", error); + return false; + } + + return initialized = true; +#else + return true; +#endif +} + diff --git a/obs/platform-windows.cpp b/obs/platform-windows.cpp index d4d17bd88..7621d07bc 100644 --- a/obs/platform-windows.cpp +++ b/obs/platform-windows.cpp @@ -50,3 +50,9 @@ void GetMonitors(vector &monitors) monitors.clear(); EnumDisplayMonitors(NULL, NULL, OBSMonitorEnumProc, (LPARAM)&monitors); } + +bool InitApplicationBundle() +{ + return true; +} + diff --git a/obs/platform-x11.cpp b/obs/platform-x11.cpp index f17dbce4c..a01198e22 100644 --- a/obs/platform-x11.cpp +++ b/obs/platform-x11.cpp @@ -89,3 +89,9 @@ void GetMonitors(vector &monitors) XCloseDisplay(display); } + +bool InitApplicationBundle() +{ + return true; +} + diff --git a/obs/platform.hpp b/obs/platform.hpp index f479b5272..d05264afd 100644 --- a/obs/platform.hpp +++ b/obs/platform.hpp @@ -33,3 +33,7 @@ struct MonitorInfo { /* Gets the path of obs-studio specific data files (such as locale) */ bool GetDataFilePath(const char *data, std::string &path); void GetMonitors(std::vector &monitors); + +/* Updates the working directory for OSX application bundles */ +bool InitApplicationBundle(); +