Skip to content

Commit

Permalink
UI: Use utf-8 string of file system paths due to Windows wchar use
Browse files Browse the repository at this point in the history
  • Loading branch information
PatTheMav committed Aug 2, 2024
1 parent 488105b commit c6353b8
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 54 deletions.
2 changes: 1 addition & 1 deletion UI/api-interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ struct OBSStudioAPI : obs_frontend_callbacks {
{
const OBSProfile &profile = main->GetCurrentProfile();

return bstrdup(profile.path.c_str());
return bstrdup(profile.path.u8string().c_str());
}

void obs_frontend_set_current_profile(const char *profile) override
Expand Down
4 changes: 2 additions & 2 deletions UI/obs-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ OBSApp::~OBSApp()
{
#ifdef _WIN32
bool disableAudioDucking =
config_get_bool(globalConfig, "Audio", "DisableAudioDucking");
config_get_bool(userConfig, "Audio", "DisableAudioDucking");
if (disableAudioDucking)
DisableAudioDucking(false);
#else
Expand Down Expand Up @@ -1428,7 +1428,7 @@ bool OBSApp::OBSInit()
browserHWAccel ? "true" : "false");
#endif
#ifdef _WIN32
bool hideFromCapture = config_get_bool(globalConfig, "BasicWindow",
bool hideFromCapture = config_get_bool(userConfig, "BasicWindow",
"HideOBSWindowsFromCapture");
blog(LOG_INFO, "Hide OBS windows from screen capture: %s",
hideFromCapture ? "true" : "false");
Expand Down
2 changes: 1 addition & 1 deletion UI/platform-windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ RunOnceMutex CheckIfAlreadyRunning(bool &already_running)
char absPath[512];
*path = 0;
*absPath = 0;
GetConfigPath(path, sizeof(path), "");
GetAppConfigPath(path, sizeof(path), "");
os_get_abs_path(path, absPath, sizeof(absPath));
name = "OBSStudioPortable";
name += absPath;
Expand Down
2 changes: 1 addition & 1 deletion UI/window-basic-auto-config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static OBSData OpenServiceSettings(std::string &type)
}

OBSDataAutoRelease data = obs_data_create_from_json_file_safe(
jsonFilePath.c_str(), "bak");
jsonFilePath.u8string().c_str(), "bak");

obs_data_set_default_string(data, "type", "rtmp_common");
type = obs_data_get_string(data, "type");
Expand Down
4 changes: 2 additions & 2 deletions UI/window-basic-main-outputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1621,8 +1621,8 @@ static OBSData GetDataFromJsonFile(const char *jsonFile)
OBSDataAutoRelease data = nullptr;

if (!jsonFilePath.empty()) {
BPtr<char> jsonData =
os_quick_read_utf8_file(jsonFilePath.c_str());
BPtr<char> jsonData = os_quick_read_utf8_file(
jsonFilePath.u8string().c_str());

if (!!jsonData) {
data = obs_data_create_from_json(jsonData);
Expand Down
12 changes: 8 additions & 4 deletions UI/window-basic-main-profiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void OBSBasic::SetupDuplicateProfile(const std::string &profileName)
std::filesystem::copy(currentProfile.path, newProfile.path,
copyOptions);
} catch (const std::filesystem::filesystem_error &error) {
blog(LOG_DEBUG, "%s", error.what());
throw std::logic_error(
"Failed to copy files for cloned profile: " +
newProfile.name);
Expand All @@ -104,6 +105,7 @@ void OBSBasic::SetupRenameProfile(const std::string &profileName)
std::filesystem::copy(currentProfile.path, newProfile.path,
copyOptions);
} catch (const std::filesystem::filesystem_error &error) {
blog(LOG_DEBUG, "%s", error.what());
throw std::logic_error("Failed to copy files for profile: " +
currentProfile.name);
}
Expand Down Expand Up @@ -167,7 +169,8 @@ const OBSProfile &OBSBasic::CreateProfile(const std::string &profileName)

auto [iterator, success] = profiles.try_emplace(
profileName,
OBSProfile{profileName, profileDirectoryPath.filename(),
OBSProfile{profileName,
profileDirectoryPath.filename().string(),
profileDirectoryPath, profileFile});

if (api) {
Expand All @@ -182,6 +185,7 @@ void OBSBasic::RemoveProfile(OBSProfile profile)
try {
std::filesystem::remove_all(profile.path);
} catch (const std::filesystem::filesystem_error &error) {
blog(LOG_DEBUG, "%s", error.what());
throw std::logic_error("Failed to remove profile directory: " +
profile.directoryName);
}
Expand Down Expand Up @@ -357,7 +361,7 @@ void OBSBasic::RefreshProfileCache()

ConfigFile config;

if (config.Open(profileCandidate.c_str(),
if (config.Open(profileCandidate.u8string().c_str(),
CONFIG_OPEN_EXISTING) != CONFIG_SUCCESS) {
continue;
}
Expand Down Expand Up @@ -715,8 +719,8 @@ void OBSBasic::on_actionExportProfile_triggered()
void OBSBasic::ActivateProfile(const OBSProfile &profile, bool reset)
{
ConfigFile config;
if (config.Open(profile.profileFile.c_str(), CONFIG_OPEN_ALWAYS) !=
CONFIG_SUCCESS) {
if (config.Open(profile.profileFile.u8string().c_str(),
CONFIG_OPEN_ALWAYS) != CONFIG_SUCCESS) {
throw std::logic_error(
"failed to open configuration file of new profile: " +
profile.profileFile.string());
Expand Down
39 changes: 21 additions & 18 deletions UI/window-basic-main-scene-collections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ void OBSBasic::SetupDuplicateSceneCollection(const std::string &collectionName)
newCollection.collectionFile,
copyOptions);
} catch (const std::filesystem::filesystem_error &error) {
blog(LOG_DEBUG, "%s", error.what());
throw std::logic_error(
"Failed to copy file for cloned scene collection: " +
newCollection.name);
}

OBSDataAutoRelease collection = obs_data_create_from_json_file(
newCollection.collectionFile.c_str());
newCollection.collectionFile.u8string().c_str());

obs_data_set_string(collection, "name", newCollection.name.c_str());

Expand All @@ -100,8 +101,8 @@ void OBSBasic::SetupDuplicateSceneCollection(const std::string &collectionName)
}

obs_data_save_json_safe(collection,
newCollection.collectionFile.c_str(), "tmp",
nullptr);
newCollection.collectionFile.u8string().c_str(),
"tmp", nullptr);

ActivateSceneCollection(newCollection);

Expand All @@ -127,6 +128,7 @@ void OBSBasic::SetupRenameSceneCollection(const std::string &collectionName)
newCollection.collectionFile,
copyOptions);
} catch (const std::filesystem::filesystem_error &error) {
blog(LOG_DEBUG, "%s", error.what());
throw std::logic_error(
"Failed to copy file for scene collection: " +
currentCollection.name);
Expand All @@ -135,13 +137,13 @@ void OBSBasic::SetupRenameSceneCollection(const std::string &collectionName)
collections.erase(currentCollection.name);

OBSDataAutoRelease collection = obs_data_create_from_json_file(
newCollection.collectionFile.c_str());
newCollection.collectionFile.u8string().c_str());

obs_data_set_string(collection, "name", newCollection.name.c_str());

obs_data_save_json_safe(collection,
newCollection.collectionFile.c_str(), "tmp",
nullptr);
newCollection.collectionFile.u8string().c_str(),
"tmp", nullptr);

ActivateSceneCollection(newCollection);
RemoveSceneCollection(currentCollection);
Expand Down Expand Up @@ -192,7 +194,7 @@ OBSBasic::CreateSceneCollection(const std::string &collectionName)
auto [iterator, success] = collections.try_emplace(
collectionName,
OBSSceneCollection{collectionName,
collectionFilePath.filename(),
collectionFilePath.filename().string(),
collectionFilePath});

return iterator->second;
Expand All @@ -207,6 +209,7 @@ void OBSBasic::RemoveSceneCollection(OBSSceneCollection collection)
std::filesystem::remove(collection.collectionFile);
std::filesystem::remove(collectionBackupFile);
} catch (const std::filesystem::filesystem_error &error) {
blog(LOG_DEBUG, "%s", error.what());
throw std::logic_error(
"Failed to remove scene collection file: " +
collection.fileName);
Expand Down Expand Up @@ -382,7 +385,7 @@ void OBSBasic::RefreshSceneCollectionCache()

OBSDataAutoRelease collectionData =
obs_data_create_from_json_file_safe(
entry.path().c_str(), "bak");
entry.path().u8string().c_str(), "bak");

std::string candidateName;
const char *collectionName =
Expand Down Expand Up @@ -624,8 +627,8 @@ void OBSBasic::on_actionExportSceneCollection_triggered()
const std::filesystem::path destinationFile{
destinationFileName.toStdString()};

OBSDataAutoRelease collection =
obs_data_create_from_json_file(sourceFile.c_str());
OBSDataAutoRelease collection = obs_data_create_from_json_file(
sourceFile.u8string().c_str());

OBSDataArrayAutoRelease sources =
obs_data_get_array(collection, "sources");
Expand Down Expand Up @@ -664,31 +667,31 @@ void OBSBasic::on_actionExportSceneCollection_triggered()

obs_data_set_array(collection, "sources", newSources);
obs_data_save_json_pretty_safe(
collection, destinationFile.c_str(), "tmp", "bak");
collection, destinationFile.u8string().c_str(), "tmp",
"bak");
}
}

// MARK: - Scene Collection Management Helper Functions

void OBSBasic::ActivateSceneCollection(const OBSSceneCollection &collection)
{
try {
const OBSSceneCollection &currentCollection =
GetCurrentSceneCollection();
const std::string currentCollectionName{config_get_string(
App()->GetUserConfig(), "Basic", "SceneCollection")};

if (collection.name != currentCollection.name) {
if (auto foundCollection =
GetSceneCollectionByName(currentCollectionName)) {
if (collection.name != foundCollection.value().name) {
SaveProjectNow();
}
} catch (const std::invalid_argument &error) {
;
}

config_set_string(App()->GetUserConfig(), "Basic", "SceneCollection",
collection.name.c_str());
config_set_string(App()->GetUserConfig(), "Basic",
"SceneCollectionFile", collection.fileName.c_str());

Load(collection.collectionFile.c_str());
Load(collection.collectionFile.u8string().c_str());

RefreshSceneCollections();

Expand Down
32 changes: 17 additions & 15 deletions UI/window-basic-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1469,8 +1469,8 @@ void OBSBasic::SaveService()
obs_data_set_string(data, "type", obs_service_get_type(service));
obs_data_set_obj(data, "settings", settings);

if (!obs_data_save_json_safe(data, jsonFilePath.c_str(), "tmp",
"bak")) {
if (!obs_data_save_json_safe(data, jsonFilePath.u8string().c_str(),
"tmp", "bak")) {
blog(LOG_WARNING, "Failed to save service");
}
}
Expand All @@ -1485,8 +1485,8 @@ bool OBSBasic::LoadService()
const std::filesystem::path jsonFilePath{
currentProfile.path / OBSServiceFileName.data()};

data = obs_data_create_from_json_file_safe(jsonFilePath.c_str(),
"bak");
data = obs_data_create_from_json_file_safe(
jsonFilePath.u8string().c_str(), "bak");

if (!data) {
return false;
Expand Down Expand Up @@ -1978,12 +1978,13 @@ bool OBSBasic::InitBasicConfig()
const OBSProfile &currentProfile = GetCurrentProfile();

int code = activeConfiguration.Open(
currentProfile.profileFile.c_str(), CONFIG_OPEN_ALWAYS);
currentProfile.profileFile.u8string().c_str(),
CONFIG_OPEN_ALWAYS);
if (code != CONFIG_SUCCESS) {
OBSErrorBox(NULL, "Failed to open basic.ini: %d", code);
return false;
}
} catch (const std::invalid_argument &error) {
} catch (const std::invalid_argument &) {
const std::string_view profileDirectory{config_get_string(
App()->GetUserConfig(), "Basic", "ProfileDir")};

Expand All @@ -2002,6 +2003,7 @@ bool OBSBasic::InitBasicConfig()
profilePath);
} catch (
const std::filesystem::filesystem_error &error) {
blog(LOG_DEBUG, "%s", error.what());
OBSErrorBox(nullptr,
"Failed to create profile path");
return false;
Expand All @@ -2016,8 +2018,8 @@ bool OBSBasic::InitBasicConfig()
return false;
}

int code = activeConfiguration.Open(userProfileFile.c_str(),
CONFIG_OPEN_ALWAYS);
int code = activeConfiguration.Open(
userProfileFile.u8string().c_str(), CONFIG_OPEN_ALWAYS);
if (code != CONFIG_SUCCESS) {
OBSErrorBox(NULL, "Failed to open basic.ini: %d", code);
return false;
Expand Down Expand Up @@ -2345,7 +2347,7 @@ void OBSBasic::OBSInit()
const OBSSceneCollection &currentCollection =
GetCurrentSceneCollection();
ActivateSceneCollection(currentCollection);
} catch (const std::invalid_argument &error) {
} catch (const std::invalid_argument &) {
const std::string collectionName =
config_get_string(App()->GetUserConfig(),
"Basic", "SceneCollection");
Expand Down Expand Up @@ -3285,7 +3287,7 @@ void OBSBasic::SaveProjectDeferred()
const OBSSceneCollection &currentCollection =
GetCurrentSceneCollection();

Save(currentCollection.collectionFile.c_str());
Save(currentCollection.collectionFile.u8string().c_str());
} catch (const std::invalid_argument &error) {
blog(LOG_ERROR, "%s", error.what());
}
Expand Down Expand Up @@ -4230,14 +4232,14 @@ void OBSBasic::TimedCheckForUpdates()
#if defined(ENABLE_SPARKLE_UPDATER)
CheckForUpdates(false);
#elif _WIN32
long long lastUpdate = config_get_int(App()->GlobalConfig(), "General",
long long lastUpdate = config_get_int(App()->GetAppConfig(), "General",
"LastUpdateCheck");
uint32_t lastVersion =
config_get_int(App()->GlobalConfig(), "General", "LastVersion");
config_get_int(App()->GetAppConfig(), "General", "LastVersion");

if (lastVersion < LIBOBS_API_VER) {
lastUpdate = 0;
config_set_int(App()->GlobalConfig(), "General",
config_set_int(App()->GetAppConfig(), "General",
"LastUpdateCheck", 0);
}

Expand Down Expand Up @@ -7352,15 +7354,15 @@ void OBSBasic::SetupBroadcast()
#ifdef _WIN32
static inline void UpdateProcessPriority()
{
const char *priority = config_get_string(App()->GlobalConfig(),
const char *priority = config_get_string(App()->GetAppConfig(),
"General", "ProcessPriority");
if (priority && strcmp(priority, "Normal") != 0)
SetProcessPriority(priority);
}

static inline void ClearProcessPriority()
{
const char *priority = config_get_string(App()->GlobalConfig(),
const char *priority = config_get_string(App()->GetAppConfig(),
"General", "ProcessPriority");
if (priority && strcmp(priority, "Normal") != 0)
SetProcessPriority("Normal");
Expand Down
Loading

0 comments on commit c6353b8

Please sign in to comment.