From fcc796e8cb8ab5f8346e5b38e58883938d04fbda Mon Sep 17 00:00:00 2001 From: Yimura Date: Sat, 24 Jul 2021 17:15:31 +0200 Subject: [PATCH] feat(Globals): Added value checking to deep_compare method --- BigBaseV2/src/core/globals.hpp | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/BigBaseV2/src/core/globals.hpp b/BigBaseV2/src/core/globals.hpp index 7dbdd728..60a8faf0 100644 --- a/BigBaseV2/src/core/globals.hpp +++ b/BigBaseV2/src/core/globals.hpp @@ -120,8 +120,11 @@ struct globals { { nlohmann::json& j = this->to_json(); - if (deep_compare(this->options, j)) + if (deep_compare(this->options, j, true)) + { + LOG(INFO) << "Settings changed, saving..."; this->save(); + } } bool load() @@ -140,17 +143,17 @@ struct globals { file.open(settings_file); } - try + try { - file >> this->options; - } - catch (const std::exception&) - { - LOG(WARNING) << "Detected corrupt settings, writing default config..."; - - this->write_default_config(); - - return this->load(); + file >> this->options; + } + catch (const std::exception&) + { + LOG(WARNING) << "Detected corrupt settings, writing default config..."; + + this->write_default_config(); + + return this->load(); } bool should_save = this->deep_compare(this->options, this->default_options); @@ -169,7 +172,7 @@ struct globals { private: const char* settings_location = "\\BigBaseV2\\settings.json"; - bool deep_compare(nlohmann::json& current_settings, const nlohmann::json& default_settings) + bool deep_compare(nlohmann::json& current_settings, const nlohmann::json& default_settings, bool compare_value = false) { bool should_save = false; @@ -177,7 +180,7 @@ private: { const std::string &key = e.key(); - if (current_settings.count(key) == 0) + if (current_settings.count(key) == 0 || (compare_value && current_settings[key] != e.value())) { current_settings[key] = e.value(); @@ -185,7 +188,7 @@ private: } else if (current_settings[key].is_structured() && e.value().is_structured()) { - if (deep_compare(current_settings[key], e.value())) + if (deep_compare(current_settings[key], e.value(), compare_value)) should_save = true; } else if (!current_settings[key].is_structured() && e.value().is_structured()) {