diff --git a/docs/sphinx/reference-settings.rst b/docs/sphinx/reference-settings.rst index dcb2ed63089f09..61e03054b82ff9 100644 --- a/docs/sphinx/reference-settings.rst +++ b/docs/sphinx/reference-settings.rst @@ -121,6 +121,15 @@ General Functions --------------------- +.. function:: bool obs_data_save_json_with_defaults(obs_data_t *data, const char *file) + + Saves the data to a file as Json text and includes default values. + + :param file: The file to save to + :return: *true* if successful, *false* otherwise + +--------------------- + .. function:: bool obs_data_save_json_safe(obs_data_t *data, const char *file, const char *temp_ext, const char *backup_ext) Saves the data to a file as Json text, and if overwriting an old @@ -134,6 +143,44 @@ General Functions --------------------- +.. function:: bool obs_data_save_json_safe_with_defaults(obs_data_t *data, const char *file, const char *temp_ext, const char *backup_ext) + + Saves the data, including defaults, to a file as Json text, and if + overwriting an old file, backs up that old file to help prevent potential + file corruption. + + :param file: The file to save to + :param backup_ext: The backup extension to use for the overwritten + file if it exists + :return: *true* if successful, *false* otherwise + +--------------------- + +.. function:: bool obs_data_save_json_pretty_safe(obs_data_t *data, const char *file, const char *temp_ext, const char *backup_ext) + + Saves the data to a file as formatted Json text, and if overwriting an old + file, backs up that old file to help prevent potential file corruption. + + :param file: The file to save to + :param backup_ext: The backup extension to use for the overwritten + file if it exists + :return: *true* if successful, *false* otherwise + +--------------------- + +.. function:: bool obs_data_save_json_pretty_safe_with_defaults(obs_data_t *data, const char *file, const char *temp_ext, const char *backup_ext) + + Saves the data, including defaults, to a file as formatted Json text, and if + overwriting an old file, backs up that old file to help prevent potential file + corruption. + + :param file: The file to save to + :param backup_ext: The backup extension to use for the overwritten + file if it exists + :return: *true* if successful, *false* otherwise + +--------------------- + .. function:: void obs_data_apply(obs_data_t *target, obs_data_t *apply_data) Merges the data of *apply_data* in to *target*. diff --git a/frontend/widgets/OBSBasic_SceneCollections.cpp b/frontend/widgets/OBSBasic_SceneCollections.cpp index 84ecbbb90134e6..d8f2c91c4a11db 100644 --- a/frontend/widgets/OBSBasic_SceneCollections.cpp +++ b/frontend/widgets/OBSBasic_SceneCollections.cpp @@ -688,7 +688,8 @@ void OBSBasic::on_actionExportSceneCollection_triggered() } obs_data_set_array(collection, "sources", newSources); - obs_data_save_json_pretty_safe(collection, destinationFile.u8string().c_str(), "tmp", "bak"); + obs_data_save_json_pretty_safe_with_defaults(collection, destinationFile.u8string().c_str(), "tmp", + "bak"); } } @@ -977,7 +978,7 @@ void OBSBasic::Save(SceneCollection &collection) obs_data_set_int(saveData, "version", sceneCollectionVersion); const std::string collectionFileName = collection.getFilePathString(); - bool success = obs_data_save_json_pretty_safe(saveData, collectionFileName.c_str(), "tmp", "bak"); + bool success = obs_data_save_json_pretty_safe_with_defaults(saveData, collectionFileName.c_str(), "tmp", "bak"); if (!success) { blog(LOG_ERROR, "Could not save scene data to %s", collectionFileName.c_str()); @@ -1284,8 +1285,8 @@ void OBSBasic::LoadData(obs_data_t *data, SceneCollection &collection) backupFilePath.replace_extension(".json.v1"); if (!std::filesystem::exists(backupFilePath)) { - bool success = obs_data_save_json_pretty_safe(data, backupFilePath.u8string().c_str(), - "tmp", nullptr); + bool success = obs_data_save_json_pretty_safe_with_defaults( + data, backupFilePath.u8string().c_str(), "tmp", nullptr); if (!success) { blog(LOG_WARNING, diff --git a/libobs/obs-data.c b/libobs/obs-data.c index b049b398669159..6a17666e06f3f6 100644 --- a/libobs/obs-data.c +++ b/libobs/obs-data.c @@ -755,6 +755,17 @@ bool obs_data_save_json(obs_data_t *data, const char *file) return false; } +bool obs_data_save_json_with_defaults(obs_data_t *data, const char *file) +{ + const char *json = obs_data_get_json_with_defaults(data); + + if (json && *json) { + return os_quick_write_utf8_file(file, json, strlen(json), false); + } + + return false; +} + bool obs_data_save_json_safe(obs_data_t *data, const char *file, const char *temp_ext, const char *backup_ext) { const char *json = obs_data_get_json(data); @@ -766,6 +777,18 @@ bool obs_data_save_json_safe(obs_data_t *data, const char *file, const char *tem return false; } +bool obs_data_save_json_safe_with_defaults(obs_data_t *data, const char *file, const char *temp_ext, + const char *backup_ext) +{ + const char *json = obs_data_get_json_with_defaults(data); + + if (json && *json) { + return os_quick_write_utf8_file_safe(file, json, strlen(json), false, temp_ext, backup_ext); + } + + return false; +} + bool obs_data_save_json_pretty_safe(obs_data_t *data, const char *file, const char *temp_ext, const char *backup_ext) { const char *json = obs_data_get_json_pretty(data); @@ -777,6 +800,18 @@ bool obs_data_save_json_pretty_safe(obs_data_t *data, const char *file, const ch return false; } +bool obs_data_save_json_pretty_safe_with_defaults(obs_data_t *data, const char *file, const char *temp_ext, + const char *backup_ext) +{ + const char *json = obs_data_get_json_pretty_with_defaults(data); + + if (json && *json) { + return os_quick_write_utf8_file_safe(file, json, strlen(json), false, temp_ext, backup_ext); + } + + return false; +} + static void get_defaults_array_cb(obs_data_t *data, void *vp) { obs_data_array_t *defs = (obs_data_array_t *)vp; diff --git a/libobs/obs-data.h b/libobs/obs-data.h index 9eb1a05cd10e90..ae6881cc521bfb 100644 --- a/libobs/obs-data.h +++ b/libobs/obs-data.h @@ -70,9 +70,14 @@ EXPORT const char *obs_data_get_json_pretty(obs_data_t *data); EXPORT const char *obs_data_get_json_pretty_with_defaults(obs_data_t *data); EXPORT const char *obs_data_get_last_json(obs_data_t *data); EXPORT bool obs_data_save_json(obs_data_t *data, const char *file); +EXPORT bool obs_data_save_json_with_defaults(obs_data_t *data, const char *file); EXPORT bool obs_data_save_json_safe(obs_data_t *data, const char *file, const char *temp_ext, const char *backup_ext); +EXPORT bool obs_data_save_json_safe_with_defaults(obs_data_t *data, const char *file, const char *temp_ext, + const char *backup_ext); EXPORT bool obs_data_save_json_pretty_safe(obs_data_t *data, const char *file, const char *temp_ext, const char *backup_ext); +EXPORT bool obs_data_save_json_pretty_safe_with_defaults(obs_data_t *data, const char *file, const char *temp_ext, + const char *backup_ext); EXPORT void obs_data_apply(obs_data_t *target, obs_data_t *apply_data);