Browse Source

obs-scripting: Refactor Lua C paths

tytan652 1 year ago
parent
commit
508f9c2e3c
1 changed files with 25 additions and 5 deletions
  1. 25 5
      deps/obs-scripting/obs-scripting-lua.c

+ 25 - 5
deps/obs-scripting/obs-scripting-lua.c

@@ -39,13 +39,11 @@
 #define SO_EXT "so"
 #endif
 
-static const char *startup_script_template =
-	"\
+static const char *startup_script_template = "\
 for val in pairs(package.preload) do\n\
 	package.preload[val] = nil\n\
 end\n\
-package.cpath = package.cpath .. \";\" .. \"%s/?." SO_EXT
-	"\" .. \";\" .. \"%s\" .. \"/?." SO_EXT "\"\n\
+%s\
 require \"obslua\"\n";
 
 static const char *get_script_path_func = "\
@@ -1345,6 +1343,11 @@ void obs_lua_script_save(obs_script_t *s)
 
 /* -------------------------------------------- */
 
+static inline void add_package_cpath(struct dstr *cpath, const char *path)
+{
+	dstr_catf(cpath, " .. \";\" .. \"%s\" .. \"/?." SO_EXT "\"", path);
+}
+
 void obs_lua_load(void)
 {
 	struct dstr tmp = {0};
@@ -1360,6 +1363,7 @@ void obs_lua_load(void)
 #define PATH_MAX MAX_PATH
 #endif
 
+	struct dstr package_cpath = {0};
 	char import_path[PATH_MAX];
 
 #ifdef __APPLE__
@@ -1377,7 +1381,23 @@ void obs_lua_load(void)
 #else
 	strcpy(import_path, "./");
 #endif
-	dstr_printf(&tmp, startup_script_template, import_path, SCRIPT_DIR);
+	dstr_cat(&package_cpath, "package.cpath = package.cpath");
+
+	add_package_cpath(&package_cpath, import_path);
+
+#if !defined(_WIN32) && !defined(__APPLE__)
+	char *relative_script_path =
+		os_get_executable_path_ptr("../" SCRIPT_DIR);
+	if (relative_script_path)
+		add_package_cpath(&package_cpath, relative_script_path);
+	bfree(relative_script_path);
+#endif
+
+	add_package_cpath(&package_cpath, SCRIPT_DIR);
+	dstr_cat(&package_cpath, "\n");
+
+	dstr_printf(&tmp, startup_script_template, package_cpath.array);
+	dstr_free(&package_cpath);
 	startup_script = tmp.array;
 
 	obs_add_tick_callback(lua_tick, NULL);