2020-08-16 03:39:05 +02:00
|
|
|
#include <config.h>
|
2020-10-07 22:44:54 +02:00
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
#include <fstream>
|
|
|
|
#include <filesystem>
|
2020-08-16 03:39:05 +02:00
|
|
|
|
2020-09-24 19:36:57 +02:00
|
|
|
ConfigManager::ConfigManager() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-08 04:36:37 +01:00
|
|
|
ConfigManager::~ConfigManager() {
|
|
|
|
disableAutoSave();
|
|
|
|
}
|
|
|
|
|
2020-09-24 19:36:57 +02:00
|
|
|
void ConfigManager::setPath(std::string file) {
|
|
|
|
path = file;
|
|
|
|
}
|
|
|
|
|
2020-09-24 19:50:22 +02:00
|
|
|
void ConfigManager::load(json def, bool lock) {
|
2020-09-24 19:36:57 +02:00
|
|
|
if (lock) { mtx.lock(); }
|
|
|
|
if (path == "") {
|
|
|
|
spdlog::error("Config manager tried to load file with no path specified");
|
|
|
|
return;
|
2020-08-16 03:39:05 +02:00
|
|
|
}
|
2020-09-24 19:36:57 +02:00
|
|
|
if (!std::filesystem::exists(path)) {
|
|
|
|
spdlog::warn("Config file '{0}' does not exist, creating it", path);
|
2020-09-24 19:50:22 +02:00
|
|
|
conf = def;
|
2020-09-24 19:36:57 +02:00
|
|
|
save(false);
|
2020-08-16 03:39:05 +02:00
|
|
|
}
|
2020-09-24 19:36:57 +02:00
|
|
|
if (!std::filesystem::is_regular_file(path)) {
|
|
|
|
spdlog::error("Config file '{0}' isn't a file", path);
|
|
|
|
return;
|
2020-08-16 03:39:05 +02:00
|
|
|
}
|
2020-09-24 19:36:57 +02:00
|
|
|
|
|
|
|
std::ifstream file(path.c_str());
|
|
|
|
file >> conf;
|
|
|
|
file.close();
|
|
|
|
if (lock) { mtx.unlock(); }
|
|
|
|
}
|
2020-08-16 03:39:05 +02:00
|
|
|
|
2020-09-24 19:36:57 +02:00
|
|
|
void ConfigManager::save(bool lock) {
|
|
|
|
if (lock) { mtx.lock(); }
|
|
|
|
std::ofstream file(path.c_str());
|
|
|
|
file << conf.dump(4);
|
|
|
|
file.close();
|
|
|
|
if (lock) { mtx.unlock(); }
|
|
|
|
}
|
2020-09-06 15:39:09 +02:00
|
|
|
|
2020-09-24 19:36:57 +02:00
|
|
|
void ConfigManager::enableAutoSave() {
|
2020-12-08 04:36:37 +01:00
|
|
|
if (!autoSaveEnabled) {
|
|
|
|
autoSaveEnabled = true;
|
2021-04-21 18:36:45 +02:00
|
|
|
termFlag = false;
|
2020-12-08 04:36:37 +01:00
|
|
|
autoSaveThread = std::thread(autoSaveWorker, this);
|
|
|
|
}
|
2020-09-24 19:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigManager::disableAutoSave() {
|
2020-12-08 04:36:37 +01:00
|
|
|
if (autoSaveEnabled) {
|
2021-04-21 19:24:58 +02:00
|
|
|
{
|
2021-04-21 20:06:30 +02:00
|
|
|
std::lock_guard<std::mutex> lock(termMtx);
|
2021-04-21 19:24:58 +02:00
|
|
|
autoSaveEnabled = false;
|
|
|
|
termFlag = true;
|
|
|
|
}
|
2021-04-22 02:00:33 +02:00
|
|
|
termCond.notify_one();
|
|
|
|
if (autoSaveThread.joinable()) { autoSaveThread.join(); }
|
2020-12-08 04:36:37 +01:00
|
|
|
}
|
2020-09-24 19:36:57 +02:00
|
|
|
}
|
2020-09-06 15:39:09 +02:00
|
|
|
|
2020-09-24 19:36:57 +02:00
|
|
|
void ConfigManager::aquire() {
|
|
|
|
mtx.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigManager::release(bool changed) {
|
|
|
|
this->changed |= changed;
|
|
|
|
mtx.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigManager::autoSaveWorker(ConfigManager* _this) {
|
|
|
|
while (_this->autoSaveEnabled) {
|
|
|
|
if (!_this->mtx.try_lock()) {
|
2020-10-20 00:32:17 +02:00
|
|
|
spdlog::warn("ConfigManager locked, waiting...");
|
2020-09-24 19:36:57 +02:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (_this->changed) {
|
|
|
|
_this->changed = false;
|
|
|
|
_this->save(false);
|
|
|
|
}
|
|
|
|
_this->mtx.unlock();
|
2021-04-21 18:36:45 +02:00
|
|
|
|
|
|
|
// Sleep but listen for wakeup call
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_this->termMtx);
|
|
|
|
_this->termCond.wait_for(lock, std::chrono::milliseconds(1000), [_this]() { return _this->termFlag; } );
|
|
|
|
}
|
2020-09-06 15:39:09 +02:00
|
|
|
}
|
2021-04-20 01:38:32 +02:00
|
|
|
}
|