diff --git a/libobs/util/platform-cocoa.m b/libobs/util/platform-cocoa.m index 8c11d8a9b507a9a2fe646a3dd5a49e8c19f88bdf..2c51936fdd536c62fea16569f722ee4bb3f7938a 100644 --- a/libobs/util/platform-cocoa.m +++ b/libobs/util/platform-cocoa.m @@ -68,7 +68,20 @@ uint64_t os_gettime_ns(void) } /* gets the location ~/Library/Application Support/[name] */ -char *os_get_config_path(const char *name) +int os_get_config_path(char *dst, size_t size, const char *name) +{ + NSArray *paths = NSSearchPathForDirectoriesInDomains( + NSApplicationSupportDirectory, NSUserDomainMask, YES); + + if([paths count] == 0) + bcrash("Could not get home directory (platform-cocoa)"); + + NSString *application_support = paths[0]; + const char *base_path = [application_support UTF8String]; + return snprintf(dst, size, "%s/%s", base_path, name); +} + +char *os_get_config_path_ptr(const char *name) { NSArray *paths = NSSearchPathForDirectoriesInDomains( NSApplicationSupportDirectory, NSUserDomainMask, YES); diff --git a/libobs/util/platform-nix.c b/libobs/util/platform-nix.c index 5dda75b47af0d77d98444b73e7c21bada1e171c6..efdc615f2ed8454ec0a9f7e555544d649956f163 100644 --- a/libobs/util/platform-nix.c +++ b/libobs/util/platform-nix.c @@ -154,7 +154,16 @@ uint64_t os_gettime_ns(void) } /* should return $HOME/.[name] */ -char *os_get_config_path(const char *name) +int os_get_config_path(char *dst, size_t size, const char *name) +{ + char *path_ptr = getenv("HOME"); + if (path_ptr == NULL) + bcrash("Could not get $HOME\n"); + + return snprintf(dst, size, "%s/.%s", path_ptr, name); +} + +char *os_get_config_path_ptr(const char *name) { char *path_ptr = getenv("HOME"); if (path_ptr == NULL) diff --git a/libobs/util/platform-windows.c b/libobs/util/platform-windows.c index 1b4cd5a4fe34bf3d1235d5d06990a7e35eef365a..0db7acd1bd338c4dea1099a354e0ebf4fd605010 100644 --- a/libobs/util/platform-windows.c +++ b/libobs/util/platform-windows.c @@ -191,7 +191,25 @@ uint64_t os_gettime_ns(void) } /* returns %appdata%\[name] on windows */ -char *os_get_config_path(const char *name) +int os_get_config_path(char *dst, size_t size, const char *name) +{ + wchar_t path_utf16[MAX_PATH]; + + SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, + path_utf16); + + if (os_wcs_to_utf8(path_utf16, 0, dst, size) != 0) { + if (strcat_s(dst, size, "\\") == 0) { + if (strcat_s(dst, size, name) == 0) { + return (int)strlen(dst); + } + } + } + + return -1; +} + +char *os_get_config_path_ptr(const char *name) { char *ptr; wchar_t path_utf16[MAX_PATH]; diff --git a/libobs/util/platform.h b/libobs/util/platform.h index a5298c5dac7522544cf6be9ff2d3e109c80ab888..f0786e013f5a35c5d701812225869b0819c70c09 100644 --- a/libobs/util/platform.h +++ b/libobs/util/platform.h @@ -90,7 +90,8 @@ EXPORT void os_sleep_ms(uint32_t duration); EXPORT uint64_t os_gettime_ns(void); -EXPORT char *os_get_config_path(const char *name); +EXPORT int os_get_config_path(char *dst, size_t size, const char *name); +EXPORT char *os_get_config_path_ptr(const char *name); EXPORT bool os_file_exists(const char *path); diff --git a/obs/obs-app.cpp b/obs/obs-app.cpp index ec4b48a5b4994d1386901f037e321ac33f454e50..a13eb0f5fa5e0da15fde071d2d1f21465fb90b85 100644 --- a/obs/obs-app.cpp +++ b/obs/obs-app.cpp @@ -127,17 +127,20 @@ static bool do_mkdir(const char *path) static bool MakeUserDirs() { - BPtr path; + char path[512]; - path = os_get_config_path("obs-studio"); + if (os_get_config_path(path, sizeof(path), "obs-studio") <= 0) + return false; if (!do_mkdir(path)) return false; - path = os_get_config_path("obs-studio/basic"); + if (os_get_config_path(path, sizeof(path), "obs-studio/basic") <= 0) + return false; if (!do_mkdir(path)) return false; - path = os_get_config_path("obs-studio/logs"); + if (os_get_config_path(path, sizeof(path), "obs-studio/logs") <= 0) + return false; if (!do_mkdir(path)) return false; @@ -146,7 +149,13 @@ static bool MakeUserDirs() bool OBSApp::InitGlobalConfig() { - BPtr path(os_get_config_path("obs-studio/global.ini")); + char path[512]; + + int len = os_get_config_path(path, sizeof(path), + "obs-studio/global.ini"); + if (len <= 0) { + return false; + } int errorcode = globalConfig.Open(path, CONFIG_OPEN_ALWAYS); if (errorcode != CONFIG_SUCCESS) { @@ -408,7 +417,7 @@ static uint64_t convert_log_name(const char *name) static void delete_oldest_log(void) { - BPtr logDir(os_get_config_path("obs-studio/logs")); + BPtr logDir(os_get_config_path_ptr("obs-studio/logs")); string oldestLog; uint64_t oldest_ts = (uint64_t)-1; struct os_dirent *entry; @@ -449,7 +458,7 @@ static void delete_oldest_log(void) static void get_last_log(void) { - BPtr logDir(os_get_config_path("obs-studio/logs")); + BPtr logDir(os_get_config_path_ptr("obs-studio/logs")); struct os_dirent *entry; os_dir_t *dir = os_opendir(logDir); uint64_t highest_ts = 0; @@ -522,7 +531,7 @@ static void create_log_file(fstream &logFile) currentLogFile = GenerateTimeDateFilename("txt"); dst << "obs-studio/logs/" << currentLogFile.c_str(); - BPtr path(os_get_config_path(dst.str().c_str())); + BPtr path(os_get_config_path_ptr(dst.str().c_str())); logFile.open(path, ios_base::in | ios_base::out | ios_base::trunc); diff --git a/obs/window-basic-main.cpp b/obs/window-basic-main.cpp index 6cd6c3804776d1b723fa4930342b02e83b0078c7..450576ee994174c235f7bdad988fbfa63df968e0 100644 --- a/obs/window-basic-main.cpp +++ b/obs/window-basic-main.cpp @@ -62,10 +62,11 @@ Q_DECLARE_METATYPE(obs_order_movement); static void AddExtraModulePaths() { - BPtr base_module_dir = - os_get_config_path("obs-studio/plugins/%module%"); + char base_module_dir[512]; + int ret = os_get_config_path(base_module_dir, sizeof(base_module_dir), + "obs-studio/plugins/%module%"); - if (!base_module_dir) + if (ret <= 0) return; string path = (char*)base_module_dir; @@ -319,8 +320,10 @@ void OBSBasic::SaveService() if (!service) return; - BPtr serviceJsonPath(os_get_config_path(SERVICE_PATH)); - if (!serviceJsonPath) + char serviceJsonPath[512]; + int ret = os_get_config_path(serviceJsonPath, sizeof(serviceJsonPath), + SERVICE_PATH); + if (ret <= 0) return; obs_data_t *data = obs_data_create(); @@ -341,8 +344,10 @@ bool OBSBasic::LoadService() { const char *type; - BPtr serviceJsonPath(os_get_config_path(SERVICE_PATH)); - if (!serviceJsonPath) + char serviceJsonPath[512]; + int ret = os_get_config_path(serviceJsonPath, sizeof(serviceJsonPath), + SERVICE_PATH); + if (ret <= 0) return false; BPtr jsonText = os_quick_read_utf8_file(serviceJsonPath); @@ -498,7 +503,13 @@ bool OBSBasic::InitBasicConfigDefaults() bool OBSBasic::InitBasicConfig() { - BPtr configPath(os_get_config_path("obs-studio/basic/basic.ini")); + char configPath[512]; + int ret = os_get_config_path(configPath, sizeof(configPath), + "obs-studio/basic/basic.ini"); + if (ret <= 0) { + OBSErrorBox(nullptr, "Failed to get base.ini path"); + return false; + } int code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS); if (code != CONFIG_SUCCESS) { @@ -549,7 +560,11 @@ void OBSBasic::InitPrimitives() void OBSBasic::OBSInit() { - BPtr savePath(os_get_config_path("obs-studio/basic/scenes.json")); + char savePath[512]; + int ret = os_get_config_path(savePath, sizeof(savePath), + "obs-studio/basic/scenes.json"); + if (ret <= 0) + throw "Failed to get scenes.json file path"; /* make sure it's fully displayed before doing any initialization */ show(); @@ -562,7 +577,7 @@ void OBSBasic::OBSInit() if (!ResetAudio()) throw "Failed to initialize audio"; - int ret = ResetVideo(); + ret = ResetVideo(); switch (ret) { case OBS_VIDEO_MODULE_NOT_FOUND: @@ -646,7 +661,12 @@ OBSBasic::~OBSBasic() void OBSBasic::SaveProject() { - BPtr savePath(os_get_config_path("obs-studio/basic/scenes.json")); + char savePath[512]; + int ret = os_get_config_path(savePath, sizeof(savePath), + "obs-studio/basic/scenes.json"); + if (ret <= 0) + return; + SaveService(); Save(savePath); } @@ -1966,7 +1986,9 @@ void OBSBasic::on_actionMoveToBottom_triggered() static BPtr ReadLogFile(const char *log) { - BPtr logDir(os_get_config_path("obs-studio/logs")); + char logDir[512]; + if (os_get_config_path(logDir, sizeof(logDir), "obs-studio/logs") <= 0) + return nullptr; string path = (char*)logDir; path += "/"; @@ -2033,7 +2055,10 @@ void OBSBasic::UploadLog(const char *file) void OBSBasic::on_actionShowLogs_triggered() { - BPtr logDir(os_get_config_path("obs-studio/logs")); + char logDir[512]; + if (os_get_config_path(logDir, sizeof(logDir), "obs-studio/logs") <= 0) + return; + QUrl url = QUrl::fromLocalFile(QT_UTF8(logDir)); QDesktopServices::openUrl(url); }