From 862396e0fa103c8b99eb129fda929f42c8618750 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 15 Oct 2015 17:55:56 +0200 Subject: [PATCH] Improved config loading to read default options from one file and overrides from another and merge differences properly --- assets/{Config.ini => DefaultConfig.ini} | 22 +++++++-------- include/Core/ConfigFile.h | 12 ++++++--- src/game/Core/ConfigFile.cpp | 34 +++++++++++++++++++----- 3 files changed, 47 insertions(+), 21 deletions(-) rename assets/{Config.ini => DefaultConfig.ini} (91%) diff --git a/assets/Config.ini b/assets/DefaultConfig.ini similarity index 91% rename from assets/Config.ini rename to assets/DefaultConfig.ini index efb3c49..071f3df 100755 --- a/assets/Config.ini +++ b/assets/DefaultConfig.ini @@ -1,12 +1,12 @@ -[Debug] -SkipStory=false -LogLevel=1 - -[Audio] -SFXVolume=0.5 -BGMVolume=0.5 - -[Video] -Fullscreen=false -Width=675 +[Debug] +SkipStory=false +LogLevel=1 + +[Audio] +SFXVolume=0.5 +BGMVolume=0.5 + +[Video] +Fullscreen=false +Width=675 Height=1080 \ No newline at end of file diff --git a/include/Core/ConfigFile.h b/include/Core/ConfigFile.h index 9089d0e..746bdff 100755 --- a/include/Core/ConfigFile.h +++ b/include/Core/ConfigFile.h @@ -2,6 +2,7 @@ #define ConfigFile_h__ #include +#include #include #include @@ -26,20 +27,23 @@ public: void SaveToDisk(); private: - std::string m_Path; - boost::property_tree::ptree m_Ptree; + boost::filesystem::path m_Path; + boost::property_tree::ptree m_PTreeDefaults; + boost::property_tree::ptree m_PTreeOverrides; + boost::property_tree::ptree m_PTreeMerged; }; template T ConfigFile::GetValue(std::string key, T defaultValue) { - return m_Ptree.get(key, defaultValue); + return m_PTreeMerged.get(key, defaultValue); } template void ConfigFile::SetValue(std::string key, T value) { - m_Ptree.put(key, value); + m_PTreeOverrides.put(key, value); + m_PTreeMerged.put(key, value); } }; diff --git a/src/game/Core/ConfigFile.cpp b/src/game/Core/ConfigFile.cpp index 18db4c4..5701393 100755 --- a/src/game/Core/ConfigFile.cpp +++ b/src/game/Core/ConfigFile.cpp @@ -4,14 +4,36 @@ dd::ConfigFile::ConfigFile(std::string path) { m_Path = path; - try { - boost::property_tree::ini_parser::read_ini(path, m_Ptree); - } catch (boost::property_tree::ptree_error& e) { - LOG_ERROR("Failed to load %s", path.c_str()); + + boost::filesystem::path defaultFile; + defaultFile = m_Path.parent_path() / ("Default" + m_Path.filename().string()); + + // Read defaults + if (boost::filesystem::exists(defaultFile)) { + try { + boost::property_tree::ini_parser::read_ini(defaultFile.string(), m_PTreeDefaults); + } catch (boost::property_tree::ptree_error& e) { + LOG_ERROR("Failed to parse \"%s\":\n%s", defaultFile.string().c_str(), e.what()); + } + } else { + LOG_ERROR("Failed to find \"%s\"! Relying on hardcoded default values!", defaultFile.string().c_str()); } -} + + m_PTreeMerged = m_PTreeDefaults; + + // Read overrides + if (boost::filesystem::exists(m_Path)) { + try { + boost::property_tree::ini_parser::read_ini(m_Path.string(), m_PTreeOverrides); + for (auto& node : m_PTreeOverrides) { + m_PTreeMerged.put_child(node.first, node.second); + } + } catch (boost::property_tree::ptree_error& e) { + LOG_ERROR("Failed to parse \"%s\":\n%s", m_Path.filename().string().c_str(), e.what()); + } + }} void dd::ConfigFile::SaveToDisk() { - boost::property_tree::ini_parser::write_ini(m_Path, m_Ptree); + boost::property_tree::ini_parser::write_ini(m_Path.string(), m_PTreeOverrides); }