mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-16 07:30:42 +01:00
Core/Config: Restore ability to load additional config files for custom scripts
(cherry picked from commit c1e4cfd07e)
This commit is contained in:
@@ -29,41 +29,65 @@ namespace bpt = boost::property_tree;
|
||||
namespace
|
||||
{
|
||||
std::string _filename;
|
||||
std::vector<std::string> _additonalFiles;
|
||||
std::vector<std::string> _args;
|
||||
bpt::ptree _config;
|
||||
std::mutex _configLock;
|
||||
|
||||
bool LoadFile(std::string const& file, bpt::ptree& fullTree, std::string& error)
|
||||
{
|
||||
try
|
||||
{
|
||||
bpt::ini_parser::read_ini(file, fullTree);
|
||||
|
||||
if (fullTree.empty())
|
||||
{
|
||||
error = "empty file (" + file + ")";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool ConfigMgr::LoadInitial(std::string const& file, std::vector<std::string> args,
|
||||
bool ConfigMgr::LoadInitial(std::string file, std::vector<std::string> args,
|
||||
std::string& error)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_configLock);
|
||||
|
||||
_filename = file;
|
||||
_args = args;
|
||||
_filename = std::move(file);
|
||||
_args = std::move(args);
|
||||
|
||||
try
|
||||
{
|
||||
bpt::ptree fullTree;
|
||||
bpt::ini_parser::read_ini(file, fullTree);
|
||||
|
||||
if (fullTree.empty())
|
||||
{
|
||||
error = "empty file (" + file + ")";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Since we're using only one section per config file, we skip the section and have direct property access
|
||||
_config = fullTree.begin()->second;
|
||||
}
|
||||
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()) + ")";
|
||||
bpt::ptree fullTree;
|
||||
if (!LoadFile(_filename, fullTree, error))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Since we're using only one section per config file, we skip the section and have direct property access
|
||||
_config = fullTree.begin()->second;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ConfigMgr::LoadAdditionalFile(std::string file, bool keepOnReload, std::string& error)
|
||||
{
|
||||
bpt::ptree fullTree;
|
||||
if (!LoadFile(file, fullTree, error))
|
||||
return false;
|
||||
|
||||
for (bpt::ptree::value_type const& child : fullTree.begin()->second)
|
||||
_config.put_child(bpt::ptree::path_type(child.first, '/'), child.second);
|
||||
|
||||
if (keepOnReload)
|
||||
_additonalFiles.emplace_back(std::move(file));
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -74,9 +98,17 @@ ConfigMgr* ConfigMgr::instance()
|
||||
return &instance;
|
||||
}
|
||||
|
||||
bool ConfigMgr::Reload(std::string& error)
|
||||
bool ConfigMgr::Reload(std::vector<std::string>& errors)
|
||||
{
|
||||
return LoadInitial(_filename, std::move(_args), error);
|
||||
std::string error;
|
||||
if (!LoadInitial(_filename, std::move(_args), error))
|
||||
errors.push_back(std::move(error));
|
||||
|
||||
for (std::string const& additionalFile : _additonalFiles)
|
||||
if (!LoadAdditionalFile(additionalFile, false, error))
|
||||
errors.push_back(std::move(error));
|
||||
|
||||
return errors.empty();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
||||
@@ -31,11 +31,12 @@ class TC_COMMON_API ConfigMgr
|
||||
|
||||
public:
|
||||
/// Method used only for loading main configuration files (bnetserver.conf and worldserver.conf)
|
||||
bool LoadInitial(std::string const& file, std::vector<std::string> args, std::string& error);
|
||||
bool LoadInitial(std::string file, std::vector<std::string> args, std::string& error);
|
||||
bool LoadAdditionalFile(std::string file, bool keepOnReload, std::string& error);
|
||||
|
||||
static ConfigMgr* instance();
|
||||
|
||||
bool Reload(std::string& error);
|
||||
bool Reload(std::vector<std::string>& errors);
|
||||
|
||||
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;
|
||||
|
||||
@@ -534,10 +534,12 @@ void World::LoadConfigSettings(bool reload)
|
||||
{
|
||||
if (reload)
|
||||
{
|
||||
std::string configError;
|
||||
if (!sConfigMgr->Reload(configError))
|
||||
std::vector<std::string> configErrors;
|
||||
if (!sConfigMgr->Reload(configErrors))
|
||||
{
|
||||
TC_LOG_ERROR("misc", "World settings reload fail: %s.", configError.c_str());
|
||||
for (std::string const& configError : configErrors)
|
||||
TC_LOG_ERROR("misc", "World settings reload fail: %s.", configError.c_str());
|
||||
|
||||
return;
|
||||
}
|
||||
sLog->LoadFromConfig();
|
||||
|
||||
Reference in New Issue
Block a user