Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions docs/sphinx/reference-settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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*.
Expand Down
9 changes: 5 additions & 4 deletions frontend/widgets/OBSBasic_SceneCollections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions libobs/obs-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions libobs/obs-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading