diff --git a/libobs/obs-internal.h b/libobs/obs-internal.h index e1ab509dd..0b36d469d 100644 --- a/libobs/obs-internal.h +++ b/libobs/obs-internal.h @@ -52,6 +52,7 @@ struct draw_callback { /* modules */ struct obs_module { + char *mod_name; const char *file; char *bin_path; char *data_path; diff --git a/libobs/obs-module.c b/libobs/obs-module.c index b2853d5c4..6c22e45c0 100644 --- a/libobs/obs-module.c +++ b/libobs/obs-module.c @@ -56,6 +56,21 @@ static int load_module_exports(struct obs_module *mod, const char *path) return MODULE_SUCCESS; } +static inline char *get_module_name(const char *file) +{ + static int ext_len = 0; + struct dstr name = {0}; + + if (ext_len == 0) { + const char *ext = get_module_extension(); + ext_len = strlen(ext); + } + + dstr_copy(&name, file); + dstr_resize(&name, name.len - ext_len); + return name.array; +} + int obs_open_module(obs_module_t **module, const char *path, const char *data_path) { @@ -78,6 +93,7 @@ int obs_open_module(obs_module_t **module, const char *path, mod.bin_path = bstrdup(path); mod.file = strrchr(mod.bin_path, '/'); mod.file = (!mod.file) ? mod.bin_path : (mod.file + 1); + mod.mod_name = get_module_name(mod.file); mod.data_path = bstrdup(data_path); mod.next = obs->first_module; @@ -167,6 +183,20 @@ char *obs_find_module_file(obs_module_t *module, const char *file) return output.array; } +char *obs_module_get_config_path(obs_module_t *module, const char *file) +{ + struct dstr output = {0}; + + dstr_copy(&output, obs->module_config_path); + if (!dstr_is_empty(&output) && dstr_end(&output) != '/') + dstr_cat_ch(&output, '/'); + dstr_cat(&output, module->mod_name); + dstr_cat_ch(&output, '/'); + dstr_cat(&output, file); + + return output.array; +} + void obs_add_module_path(const char *bin, const char *data) { struct obs_module_path omp; @@ -380,6 +410,7 @@ void free_module(struct obs_module *mod) /* os_dlclose(mod->module); */ } + bfree(mod->mod_name); bfree(mod->bin_path); bfree(mod->data_path); bfree(mod); diff --git a/libobs/obs-module.h b/libobs/obs-module.h index ef7a0875e..7b9890428 100644 --- a/libobs/obs-module.h +++ b/libobs/obs-module.h @@ -137,6 +137,15 @@ MODULE_EXTERN obs_module_t *obs_current_module(void); */ #define obs_module_file(file) obs_find_module_file(obs_current_module(), file) +/** + * Returns the location to a module config file associated with the current + * module. Free with bfree when complete. Will return NULL if configuration + * directory is not set. Equivalent to: + * obs_module_get_config_path(obs_current_modile(), file); + */ +#define obs_module_config_path(file) \ + obs_module_get_config_path(obs_current_module(), file) + /** * Optional: Declares the author(s) of the module * diff --git a/libobs/obs.h b/libobs/obs.h index 09615660b..a06a9e591 100644 --- a/libobs/obs.h +++ b/libobs/obs.h @@ -402,6 +402,19 @@ EXPORT lookup_t *obs_module_load_locale(obs_module_t *module, */ EXPORT char *obs_find_module_file(obs_module_t *module, const char *file); +/** + * Returns the path of a plugin module config file (whether it exists or not) + * + * @note Modules should use obs_module_config_path function defined in + * obs-module.h as a more elegant means of getting their files without + * having to specify the module parameter. + * + * @param module The module associated with the path + * @param file The file to get a path to + * @return Path string, or NULL if not found. Use bfree to free string. + */ +EXPORT char *obs_module_get_config_path(obs_module_t *module, const char *file); + /** * Enumerates all available inputs source types. *