configuration: Save after some time has passed

Prevents data loss after 100ms have passed, which should improve the saving situation.

Fixes #805
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2022-09-18 19:53:11 +02:00
parent 589f6c4018
commit 39dc1fa5e1
4 changed files with 85 additions and 47 deletions

View File

@ -1,47 +1,52 @@
/* // Copyright (C) 2020-2022 Michael Fabian Dirks
* Modern effects for a modern Streamer //
* Copyright (C) 2020 Michael Fabian Dirks // This program is free software; you can redistribute it and/or modify
* // it under the terms of the GNU General Public License as published by
* This program is free software; you can redistribute it and/or modify // the Free Software Foundation; either version 2 of the License, or
* it under the terms of the GNU General Public License as published by // (at your option) any later version.
* the Free Software Foundation; either version 2 of the License, or //
* (at your option) any later version. // This program is distributed in the hope that it will be useful,
* // but WITHOUT ANY WARRANTY; without even the implied warranty of
* This program is distributed in the hope that it will be useful, // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* but WITHOUT ANY WARRANTY; without even the implied warranty of // GNU General Public License for more details.
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
* GNU General Public License for more details. // You should have received a copy of the GNU General Public License
* // along with this program; if not, write to the Free Software
* You should have received a copy of the GNU General Public License // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "configuration.hpp" #include "configuration.hpp"
#include "obs/obs-tools.hpp" #include "obs/obs-tools.hpp"
#include "plugin.hpp" #include "plugin.hpp"
#include "util/util-logging.hpp"
#ifdef _DEBUG
#define ST_PREFIX "<%s> "
#define D_LOG_ERROR(x, ...) P_LOG_ERROR(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
#define D_LOG_WARNING(x, ...) P_LOG_WARN(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
#define D_LOG_INFO(x, ...) P_LOG_INFO(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
#define D_LOG_DEBUG(x, ...) P_LOG_DEBUG(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
#else
#define ST_PREFIX "<configuration> "
#define D_LOG_ERROR(...) P_LOG_ERROR(ST_PREFIX __VA_ARGS__)
#define D_LOG_WARNING(...) P_LOG_WARN(ST_PREFIX __VA_ARGS__)
#define D_LOG_INFO(...) P_LOG_INFO(ST_PREFIX __VA_ARGS__)
#define D_LOG_DEBUG(...) P_LOG_DEBUG(ST_PREFIX __VA_ARGS__)
#endif
constexpr std::string_view version_tag_name = "Version"; constexpr std::string_view version_tag_name = "Version";
constexpr std::string_view path_backup_ext = ".bk"; constexpr std::string_view path_backup_ext = ".bk";
streamfx::configuration::~configuration() streamfx::configuration::~configuration()
{ {
// Update version tag.
obs_data_set_int(_data.get(), version_tag_name.data(), STREAMFX_VERSION);
try { try {
if (_config_path.has_parent_path()) { save();
std::filesystem::create_directories(_config_path.parent_path()); _save_task->wait();
}
if (!obs_data_save_json_safe(_data.get(), _config_path.u8string().c_str(), ".tmp", path_backup_ext.data())) {
throw std::exception();
}
} catch (std::exception const& ex) { } catch (std::exception const& ex) {
DLOG_ERROR("Failed to save configuration: %s", ex.what()); DLOG_ERROR("Failed to save configuration: %s", ex.what());
} }
} }
streamfx::configuration::configuration() : _data(), _config_path() streamfx::configuration::configuration() : _config_path(), _data(), _task_lock(), _save_task()
{ {
// Retrieve global configuration path. // Retrieve global configuration path.
_config_path = streamfx::config_file_path("config.json"); _config_path = streamfx::config_file_path("config.json");
@ -63,6 +68,28 @@ streamfx::configuration::configuration() : _data(), _config_path()
} }
} }
void streamfx::configuration::save()
{
std::lock_guard<std::mutex> lg(_task_lock);
if (!_save_task || _save_task->is_completed()) {
_save_task = streamfx::threadpool()->push([this](streamfx::util::threadpool::task_data_t) {
// ToDo: Implement delayed tasks in ::threadpool.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Update version tag.
obs_data_set_int(_data.get(), version_tag_name.data(), STREAMFX_VERSION);
if (_config_path.has_parent_path()) {
std::filesystem::create_directories(_config_path.parent_path());
}
if (!obs_data_save_json_safe(_data.get(), _config_path.u8string().c_str(), ".tmp",
path_backup_ext.data())) {
D_LOG_ERROR("Failed to save configuration file.", nullptr);
}
});
}
}
std::shared_ptr<obs_data_t> streamfx::configuration::get() std::shared_ptr<obs_data_t> streamfx::configuration::get()
{ {
obs_data_addref(_data.get()); obs_data_addref(_data.get());

View File

@ -1,21 +1,18 @@
/* // Copyright (C) 2020-2022 Michael Fabian Dirks
* Modern effects for a modern Streamer //
* Copyright (C) 2020 Michael Fabian Dirks // This program is free software; you can redistribute it and/or modify
* // it under the terms of the GNU General Public License as published by
* This program is free software; you can redistribute it and/or modify // the Free Software Foundation; either version 2 of the License, or
* it under the terms of the GNU General Public License as published by // (at your option) any later version.
* the Free Software Foundation; either version 2 of the License, or //
* (at your option) any later version. // This program is distributed in the hope that it will be useful,
* // but WITHOUT ANY WARRANTY; without even the implied warranty of
* This program is distributed in the hope that it will be useful, // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* but WITHOUT ANY WARRANTY; without even the implied warranty of // GNU General Public License for more details.
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
* GNU General Public License for more details. // You should have received a copy of the GNU General Public License
* // along with this program; if not, write to the Free Software
* You should have received a copy of the GNU General Public License // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#pragma once #pragma once
#include "common.hpp" #include "common.hpp"
@ -23,18 +20,29 @@
#include "warning-disable.hpp" #include "warning-disable.hpp"
#include <filesystem> #include <filesystem>
#include <memory> #include <memory>
#include <new>
#include "warning-enable.hpp" #include "warning-enable.hpp"
namespace streamfx { namespace streamfx {
class configuration { class configuration {
std::filesystem::path _config_path;
std::shared_ptr<obs_data_t> _data; std::shared_ptr<obs_data_t> _data;
std::filesystem::path _config_path; #if __cpp_lib_hardware_interference_size >= 201603
alignas(std::hardware_destructive_interference_size)
#endif
std::mutex _task_lock;
std::shared_ptr<streamfx::util::threadpool::task> _save_task;
public: public:
~configuration(); ~configuration();
configuration(); configuration();
public:
void save();
public: public:
std::shared_ptr<obs_data_t> get(); std::shared_ptr<obs_data_t> get();

View File

@ -67,6 +67,7 @@ bool streamfx::ui::handler::have_shown_about_streamfx(bool shown)
auto data = config->get(); auto data = config->get();
if (shown) { if (shown) {
obs_data_set_bool(data.get(), _cfg_have_shown_about.data(), true); obs_data_set_bool(data.get(), _cfg_have_shown_about.data(), true);
config->save();
} }
if (config->is_different_version()) { if (config->is_different_version()) {
return false; return false;

View File

@ -402,6 +402,8 @@ void streamfx::updater::save()
obs_data_set_bool(dataptr.get(), ST_CFG_AUTOMATION, _automation); obs_data_set_bool(dataptr.get(), ST_CFG_AUTOMATION, _automation);
obs_data_set_int(dataptr.get(), ST_CFG_CHANNEL, static_cast<long long>(_channel)); obs_data_set_int(dataptr.get(), ST_CFG_CHANNEL, static_cast<long long>(_channel));
obs_data_set_int(dataptr.get(), ST_CFG_LASTCHECKEDAT, static_cast<long long>(_lastcheckedat.count())); obs_data_set_int(dataptr.get(), ST_CFG_LASTCHECKEDAT, static_cast<long long>(_lastcheckedat.count()));
config->save();
} }
} }