feat(Globals): Added value checking to deep_compare method

This commit is contained in:
Yimura 2021-07-24 17:15:31 +02:00
parent 0ae142bd40
commit fcc796e8cb

View File

@ -120,8 +120,11 @@ struct globals {
{ {
nlohmann::json& j = this->to_json(); 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(); this->save();
}
} }
bool load() bool load()
@ -140,17 +143,17 @@ struct globals {
file.open(settings_file); file.open(settings_file);
} }
try try
{ {
file >> this->options; file >> this->options;
} }
catch (const std::exception&) catch (const std::exception&)
{ {
LOG(WARNING) << "Detected corrupt settings, writing default config..."; LOG(WARNING) << "Detected corrupt settings, writing default config...";
this->write_default_config(); this->write_default_config();
return this->load(); return this->load();
} }
bool should_save = this->deep_compare(this->options, this->default_options); bool should_save = this->deep_compare(this->options, this->default_options);
@ -169,7 +172,7 @@ struct globals {
private: private:
const char* settings_location = "\\BigBaseV2\\settings.json"; 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; bool should_save = false;
@ -177,7 +180,7 @@ private:
{ {
const std::string &key = e.key(); 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(); current_settings[key] = e.value();
@ -185,7 +188,7 @@ private:
} }
else if (current_settings[key].is_structured() && e.value().is_structured()) 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; should_save = true;
} }
else if (!current_settings[key].is_structured() && e.value().is_structured()) { else if (!current_settings[key].is_structured() && e.value().is_structured()) {