From 65d28a2da3d3702c410fa15cd388cfc5157b11cd Mon Sep 17 00:00:00 2001 From: Yimura Date: Thu, 20 May 2021 18:00:03 +0200 Subject: [PATCH] feat(Globals): Added auto saving on change --- BigBaseV2/src/backend/backend.cpp | 3 +++ BigBaseV2/src/core/globals.hpp | 34 ++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/BigBaseV2/src/backend/backend.cpp b/BigBaseV2/src/backend/backend.cpp index efea1c35..59f30243 100644 --- a/BigBaseV2/src/backend/backend.cpp +++ b/BigBaseV2/src/backend/backend.cpp @@ -7,10 +7,13 @@ namespace big { void backend::loop() { + g.attempt_save(); + QUEUE_JOB_BEGIN_CLAUSE() { looped::self_godmode(); }QUEUE_JOB_END_CLAUSE + QUEUE_JOB_BEGIN_CLAUSE() { looped::self_noclip(); diff --git a/BigBaseV2/src/core/globals.hpp b/BigBaseV2/src/core/globals.hpp index 53fa1968..8842e793 100644 --- a/BigBaseV2/src/core/globals.hpp +++ b/BigBaseV2/src/core/globals.hpp @@ -6,6 +6,9 @@ using namespace big; struct globals { + nlohmann::json default_options; + nlohmann::json options; + struct self { bool godmode = false; bool noclip = false; @@ -59,8 +62,21 @@ struct globals { }; } + void attempt_save() + { + nlohmann::json& j = this->to_json(); + if (j != this->options) + { + this->save(); + + this->options = j; + } + } + bool load() { + this->default_options = this->to_json(); + std::string settings_file = std::getenv("appdata"); settings_file += "\\BigBaseV2\\settings.json"; @@ -73,31 +89,25 @@ struct globals { file.open(settings_file); } - nlohmann::json j; - file >> j; - - nlohmann::json default_j = this->to_json(); + file >> this->options; bool should_save = false; - for (auto& e : default_j.items()) + for (auto& e : this->default_options.items()) { - if (j.count(e.key()) == 0) + if (this->options.count(e.key()) == 0) { should_save = true; - j[e.key()] = e.value(); + this->options[e.key()] = e.value(); } } + this->from_json(this->options); + if (should_save) { LOG(INFO) << "Updating settings."; - - default_j.merge_patch(j); - j = default_j; - save(); } - this->from_json(j); return true; }