Files
TCPlayerbotCore/src/common/Configuration/Config.cpp
T

190 lines
5.4 KiB
C++
Raw Normal View History

/*
2020-01-02 06:44:10 +01:00
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
#include "Config.h"
#include "Log.h"
#include "StringConvert.h"
2017-05-11 22:29:51 +02:00
#include "Util.h"
#include <boost/property_tree/ini_parser.hpp>
#include <algorithm>
#include <memory>
#include <mutex>
2009-10-17 15:51:44 -07:00
2017-05-11 22:29:51 +02:00
namespace bpt = boost::property_tree;
namespace
{
std::string _filename;
std::vector<std::string> _args;
bpt::ptree _config;
std::mutex _configLock;
}
2009-10-17 15:51:44 -07:00
bool ConfigMgr::LoadInitial(std::string const& file, std::vector<std::string> args,
std::string& error)
2008-10-02 16:23:55 -05:00
{
std::lock_guard<std::mutex> lock(_configLock);
2013-07-15 17:31:44 +02:00
_filename = file;
_args = args;
2013-07-15 17:31:44 +02:00
try
{
2017-05-11 22:29:51 +02:00
bpt::ptree fullTree;
bpt::ini_parser::read_ini(file, fullTree);
2009-10-17 15:51:44 -07:00
2014-07-03 15:08:10 -05:00
if (fullTree.empty())
{
error = "empty file (" + file + ")";
2014-07-03 15:08:10 -05:00
return false;
}
2014-07-03 15:08:10 -05:00
2014-06-22 16:29:49 +02:00
// Since we're using only one section per config file, we skip the section and have direct property access
2014-07-04 15:20:23 +02:00
_config = fullTree.begin()->second;
}
2017-05-11 22:29:51 +02:00
catch (bpt::ini_parser::ini_parser_error const& e)
{
if (e.line() == 0)
error = e.message() + " (" + e.filename() + ")";
else
error = e.message() + " (" + e.filename() + ":" + std::to_string(e.line()) + ")";
return false;
}
2013-07-15 17:31:44 +02:00
return true;
2013-07-15 17:31:44 +02:00
}
ConfigMgr* ConfigMgr::instance()
{
static ConfigMgr instance;
return &instance;
}
bool ConfigMgr::Reload(std::string& error)
2013-07-15 17:31:44 +02:00
{
return LoadInitial(_filename, std::move(_args), error);
2013-07-15 17:31:44 +02:00
}
template<class T>
T ConfigMgr::GetValueDefault(std::string const& name, T def, bool quiet) const
2013-07-15 17:31:44 +02:00
{
try
{
2017-05-11 22:29:51 +02:00
return _config.get<T>(bpt::ptree::path_type(name, '/'));
}
2020-05-21 22:15:58 +02:00
catch (bpt::ptree_bad_path const&)
{
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());
}
}
2020-05-21 22:15:58 +02:00
catch (bpt::ptree_bad_data const&)
{
TC_LOG_ERROR("server.loading", "Bad value defined for name %s in config file %s, going to use %s instead",
name.c_str(), _filename.c_str(), std::to_string(def).c_str());
}
return def;
}
2009-10-17 15:51:44 -07:00
template<>
std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std::string def, bool quiet) const
2008-10-02 16:23:55 -05:00
{
try
{
2017-05-11 22:29:51 +02:00
return _config.get<std::string>(bpt::ptree::path_type(name, '/'));
}
2020-05-21 22:15:58 +02:00
catch (bpt::ptree_bad_path const&)
{
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());
}
}
2020-05-21 22:15:58 +02:00
catch (bpt::ptree_bad_data const&)
{
TC_LOG_ERROR("server.loading", "Bad value defined for name %s in config file %s, going to use %s instead",
name.c_str(), _filename.c_str(), def.c_str());
}
return def;
}
std::string ConfigMgr::GetStringDefault(std::string const& name, const std::string& def, bool quiet) const
{
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, bool quiet) const
{
std::string val = GetValueDefault(name, std::string(def ? "1" : "0"), quiet);
val.erase(std::remove(val.begin(), val.end(), '"'), val.end());
Optional<bool> boolVal = Trinity::StringTo<bool>(val);
if (boolVal)
return *boolVal;
else
{
TC_LOG_ERROR("server.loading", "Bad value defined for name %s in config file %s, going to use '%s' instead",
name.c_str(), _filename.c_str(), def ? "true" : "false");
return def;
}
}
2009-10-17 15:51:44 -07:00
int32 ConfigMgr::GetIntDefault(std::string const& name, int32 def, bool quiet) const
{
return GetValueDefault(name, def, quiet);
}
int64 ConfigMgr::GetInt64Default(std::string const& name, int64 def, bool quiet) const
2008-10-02 16:23:55 -05:00
{
return GetValueDefault(name, def, quiet);
}
2009-10-17 15:51:44 -07:00
float ConfigMgr::GetFloatDefault(std::string const& name, float def, bool quiet) const
2008-10-02 16:23:55 -05:00
{
return GetValueDefault(name, def, quiet);
}
2013-07-15 17:31:44 +02:00
std::string const& ConfigMgr::GetFilename()
{
std::lock_guard<std::mutex> lock(_configLock);
return _filename;
}
2017-05-11 22:29:51 +02:00
std::vector<std::string> const& ConfigMgr::GetArguments() const
{
return _args;
}
std::vector<std::string> ConfigMgr::GetKeysByString(std::string const& name)
{
std::lock_guard<std::mutex> lock(_configLock);
2017-05-11 22:29:51 +02:00
std::vector<std::string> keys;
2017-05-11 22:29:51 +02:00
for (bpt::ptree::value_type const& child : _config)
if (child.first.compare(0, name.length(), name) == 0)
keys.push_back(child.first);
2014-07-21 18:13:48 +02:00
return keys;
}