Przeglądaj źródła

libobs: Use proper resource paths when running from an OSX bundle

Colin Edwards 6 lat temu
rodzic
commit
747ce9b77c
3 zmienionych plików z 62 dodań i 5 usunięć
  1. 19 3
      UI/platform-osx.mm
  2. 1 1
      libobs/CMakeLists.txt
  3. 42 1
      libobs/obs-cocoa.m

+ 19 - 3
UI/platform-osx.mm

@@ -28,11 +28,27 @@
 
 using namespace std;
 
+bool isInBundle()
+{
+	NSRunningApplication *app = [NSRunningApplication currentApplication];
+	return [app bundleIdentifier] != nil;
+}
+
 bool GetDataFilePath(const char *data, string &output)
 {
-	stringstream str;
-	str << OBS_DATA_PATH "/obs-studio/" << data;
-	output = str.str();
+	if (isInBundle()) {
+		NSBundle *myBundle = [NSBundle mainBundle];
+		NSString *path = [NSString
+			stringWithFormat:@"data/obs-studio/%@",
+					 [NSString stringWithUTF8String:data]];
+		NSString *absPath = [myBundle pathForResource:path ofType:nil];
+		output = [absPath UTF8String];
+	} else {
+		stringstream str;
+		str << OBS_DATA_PATH "/obs-studio/" << data;
+		output = str.str();
+	}
+
 	return !access(output.c_str(), R_OK);
 }
 

+ 1 - 1
libobs/CMakeLists.txt

@@ -110,7 +110,7 @@ if(WIN32)
 	endif()
 elseif(APPLE)
 	set(libobs_PLATFORM_SOURCES
-		obs-cocoa.c
+		obs-cocoa.m
 		util/threading-posix.c
 		util/pipe-posix.c
 		util/platform-nix.c

+ 42 - 1
libobs/obs-cocoa.c → libobs/obs-cocoa.m

@@ -29,6 +29,14 @@
 #include <IOKit/hid/IOHIDDevice.h>
 #include <IOKit/hid/IOHIDManager.h>
 
+#import <AppKit/AppKit.h>
+
+bool is_in_bundle()
+{
+	NSRunningApplication *app = [NSRunningApplication currentApplication];
+	return [app bundleIdentifier] != nil;
+}
+
 const char *get_module_extension(void)
 {
 	return ".so";
@@ -51,12 +59,45 @@ void add_default_module_paths(void)
 {
 	for (int i = 0; i < module_patterns_size; i++)
 		obs_add_module_path(module_bin[i], module_data[i]);
+
+	if (is_in_bundle()) {
+		NSRunningApplication *app =
+			[NSRunningApplication currentApplication];
+		NSURL *bundleURL = [app bundleURL];
+		NSURL *pluginsURL = [bundleURL
+			URLByAppendingPathComponent:@"Contents/Plugins"];
+		NSURL *dataURL = [bundleURL
+			URLByAppendingPathComponent:
+				@"Contents/Resources/data/obs-plugins/%module%"];
+
+		const char *binPath = [[pluginsURL path]
+			cStringUsingEncoding:NSUTF8StringEncoding];
+		const char *dataPath = [[dataURL path]
+			cStringUsingEncoding:NSUTF8StringEncoding];
+
+		obs_add_module_path(binPath, dataPath);
+	}
 }
 
 char *find_libobs_data_file(const char *file)
 {
 	struct dstr path;
-	dstr_init_copy(&path, OBS_INSTALL_DATA_PATH "/libobs/");
+
+	if (is_in_bundle()) {
+		NSRunningApplication *app =
+			[NSRunningApplication currentApplication];
+		NSURL *bundleURL = [app bundleURL];
+		NSURL *libobsDataURL =
+			[bundleURL URLByAppendingPathComponent:
+					   @"Contents/Resources/data/libobs/"];
+		const char *libobsDataPath = [[libobsDataURL path]
+			cStringUsingEncoding:NSUTF8StringEncoding];
+		dstr_init_copy(&path, libobsDataPath);
+		dstr_cat(&path, "/");
+	} else {
+		dstr_init_copy(&path, OBS_INSTALL_DATA_PATH "/libobs/");
+	}
+
 	dstr_cat(&path, file);
 	return path.array;
 }