diff options
author | Treeston <treeston.mmoc@gmail.com> | 2020-07-31 17:12:09 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2022-01-24 15:08:46 +0100 |
commit | 23ad14526cfbb61aa686f3d7966e74c6dfc0cf95 (patch) | |
tree | 943dfd3687e644c2baeefa11614dc91ceadc6d09 /src/common | |
parent | ab7680157b78da6ca527fcb1222a07da4c9eb39e (diff) |
Core/Common: Allow config settings to be loaded as optional (PR #25137)
If the config setting is not present, returns the default without issuing a warning.
(cherry picked from commit 5ad064976edfc931cdda6faffe723d7c3dfb0ae4)
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/Configuration/Config.cpp | 38 | ||||
-rw-r--r-- | src/common/Configuration/Config.h | 12 |
2 files changed, 28 insertions, 22 deletions
diff --git a/src/common/Configuration/Config.cpp b/src/common/Configuration/Config.cpp index 00869ee9400..4ac24715646 100644 --- a/src/common/Configuration/Config.cpp +++ b/src/common/Configuration/Config.cpp @@ -79,7 +79,7 @@ bool ConfigMgr::Reload(std::string& error) } template<class T> -T ConfigMgr::GetValueDefault(std::string const& name, T def) const +T ConfigMgr::GetValueDefault(std::string const& name, T def, bool quiet) const { try { @@ -87,8 +87,11 @@ T ConfigMgr::GetValueDefault(std::string const& name, T def) const } catch (bpt::ptree_bad_path const&) { - TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file", - name.c_str(), _filename.c_str(), name.c_str(), std::to_string(def).c_str()); + if (!quiet) + { + TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file", + name.c_str(), _filename.c_str(), name.c_str(), std::to_string(def).c_str()); + } } catch (bpt::ptree_bad_data const&) { @@ -100,7 +103,7 @@ T ConfigMgr::GetValueDefault(std::string const& name, T def) const } template<> -std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std::string def) const +std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std::string def, bool quiet) const { try { @@ -108,8 +111,11 @@ std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std } catch (bpt::ptree_bad_path const&) { - TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file", - name.c_str(), _filename.c_str(), name.c_str(), def.c_str()); + if (!quiet) + { + TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file", + name.c_str(), _filename.c_str(), name.c_str(), def.c_str()); + } } catch (bpt::ptree_bad_data const&) { @@ -120,33 +126,33 @@ std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std return def; } -std::string ConfigMgr::GetStringDefault(std::string const& name, const std::string& def) const +std::string ConfigMgr::GetStringDefault(std::string const& name, const std::string& def, bool quiet) const { - std::string val = GetValueDefault(name, def); + std::string val = GetValueDefault(name, def, quiet); val.erase(std::remove(val.begin(), val.end(), '"'), val.end()); return val; } -bool ConfigMgr::GetBoolDefault(std::string const& name, bool def) const +bool ConfigMgr::GetBoolDefault(std::string const& name, bool def, bool quiet) const { - std::string val = GetValueDefault(name, std::string(def ? "1" : "0")); + std::string val = GetValueDefault(name, std::string(def ? "1" : "0"), quiet); val.erase(std::remove(val.begin(), val.end(), '"'), val.end()); return StringToBool(val); } -int32 ConfigMgr::GetIntDefault(std::string const& name, int32 def) const +int32 ConfigMgr::GetIntDefault(std::string const& name, int32 def, bool quiet) const { - return GetValueDefault(name, def); + return GetValueDefault(name, def, quiet); } -int64 ConfigMgr::GetInt64Default(std::string const& name, int64 def) const +int64 ConfigMgr::GetInt64Default(std::string const& name, int64 def, bool quiet) const { - return GetValueDefault(name, def); + return GetValueDefault(name, def, quiet); } -float ConfigMgr::GetFloatDefault(std::string const& name, float def) const +float ConfigMgr::GetFloatDefault(std::string const& name, float def, bool quiet) const { - return GetValueDefault(name, def); + return GetValueDefault(name, def, quiet); } std::string const& ConfigMgr::GetFilename() diff --git a/src/common/Configuration/Config.h b/src/common/Configuration/Config.h index bbfef4af1fc..4c42b2e7b34 100644 --- a/src/common/Configuration/Config.h +++ b/src/common/Configuration/Config.h @@ -37,11 +37,11 @@ public: bool Reload(std::string& error); - std::string GetStringDefault(std::string const& name, const std::string& def) const; - bool GetBoolDefault(std::string const& name, bool def) const; - int32 GetIntDefault(std::string const& name, int32 def) const; - int64 GetInt64Default(std::string const& name, int64 def) const; - float GetFloatDefault(std::string const& name, float def) const; + std::string GetStringDefault(std::string const& name, const std::string& def, bool quiet = false) const; + bool GetBoolDefault(std::string const& name, bool def, bool quiet = false) const; + int32 GetIntDefault(std::string const& name, int32 def, bool quiet = false) const; + int64 GetInt64Default(std::string const& name, int64 def, bool quiet = false) const; + float GetFloatDefault(std::string const& name, float def, bool quiet = false) const; std::string const& GetFilename(); std::vector<std::string> const& GetArguments() const; @@ -49,7 +49,7 @@ public: private: template<class T> - T GetValueDefault(std::string const& name, T def) const; + T GetValueDefault(std::string const& name, T def, bool quiet) const; }; #define sConfigMgr ConfigMgr::instance() |