From b51c6023bab3fd3df63bcdda2c84c1d0ebdb7543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Tue, 14 Feb 2017 18:05:54 +0100 Subject: [PATCH] Config: Only save settings if they have been changed --- Source/Core/Common/Config.cpp | 42 +++++++++++++++++++++++++++++++---- Source/Core/Common/Config.h | 9 +++++++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/Source/Core/Common/Config.cpp b/Source/Core/Common/Config.cpp index e41f123243..5ed41ef589 100644 --- a/Source/Core/Common/Config.cpp +++ b/Source/Core/Common/Config.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include #include #include @@ -119,16 +120,23 @@ bool Section::Delete(const std::string& key) return false; m_values.erase(it); + m_dirty = true; return true; } void Section::Set(const std::string& key, const std::string& value) { auto it = m_values.find(key); - if (it != m_values.end()) + if (it != m_values.end() && it->second != value) + { it->second = value; - else + m_dirty = true; + } + else if (it == m_values.end()) + { m_values[key] = value; + m_dirty = true; + } } void Section::Set(const std::string& key, u32 newValue) @@ -165,6 +173,12 @@ void Section::Set(const std::string& key, const std::string& newValue, Delete(key); } +void Section::SetLines(const std::vector& lines) +{ + m_lines = lines; + m_dirty = true; +} + bool Section::Get(const std::string& key, std::string* value, const std::string& default_value) const { @@ -326,13 +340,33 @@ void Layer::Load() { if (m_loader) m_loader->Load(this); + ClearDirty(); CallbackSystems(); } void Layer::Save() { - if (m_loader) - m_loader->Save(this); + if (!m_loader || !IsDirty()) + return; + + m_loader->Save(this); + ClearDirty(); +} + +bool Layer::IsDirty() const +{ + return std::any_of(m_sections.begin(), m_sections.end(), [](const auto& system) { + return std::any_of(system.second.begin(), system.second.end(), + [](const auto& section) { return section->IsDirty(); }); + }); +} + +void Layer::ClearDirty() +{ + std::for_each(m_sections.begin(), m_sections.end(), [](const auto& system) { + std::for_each(system.second.begin(), system.second.end(), + [](const auto& section) { section->ClearDirty(); }); + }); } Section* GetOrCreateSection(System system, const std::string& section_name) diff --git a/Source/Core/Common/Config.h b/Source/Core/Common/Config.h index ac7fb5d6ad..3b8614f264 100644 --- a/Source/Core/Common/Config.h +++ b/Source/Core/Common/Config.h @@ -92,13 +92,17 @@ public: bool Get(const std::string& key, double* value, double defaultValue = 0.0) const; // Section chunk - void SetLines(const std::vector& lines) { m_lines = lines; } + void SetLines(const std::vector& lines); // XXX: Add to recursive layer virtual bool GetLines(std::vector* lines, const bool remove_comments = true) const; virtual bool HasLines() const { return m_lines.size() > 0; } const std::string& GetName() const { return m_name; } const SectionValueMap& GetValues() const { return m_values; } + bool IsDirty() const { return m_dirty; } + void ClearDirty() { m_dirty = false; } protected: + bool m_dirty = false; + LayerType m_layer; System m_system; const std::string m_name; @@ -154,6 +158,9 @@ public: // Stay away from this routine as much as possible ConfigLayerLoader* GetLoader() { return m_loader.get(); } protected: + bool IsDirty() const; + void ClearDirty(); + LayerMap m_sections; const LayerType m_layer; std::unique_ptr m_loader;