aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2020-08-29 22:01:21 +0200
committerShauren <shauren.trinity@gmail.com>2022-02-04 13:05:06 +0100
commit7cd98cd7d2e5723abdad12aed19213cb5b619189 (patch)
treee76978b0253d864aa3217384bf410a1530dc14cd
parent2aaebc193c231878133b70e466bd564645db5e4e (diff)
Core/Config: Restore ability to load additional config files for custom scripts
(cherry picked from commit c1e4cfd07eee19069a15e9b82a308d8a914d5637)
-rw-r--r--src/common/Configuration/Config.cpp80
-rw-r--r--src/common/Configuration/Config.h5
-rw-r--r--src/server/game/World/World.cpp8
3 files changed, 64 insertions, 29 deletions
diff --git a/src/common/Configuration/Config.cpp b/src/common/Configuration/Config.cpp
index 408e64ffdd7..12d5e6a1118 100644
--- a/src/common/Configuration/Config.cpp
+++ b/src/common/Configuration/Config.cpp
@@ -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);
+ bpt::ptree fullTree;
+ if (!LoadFile(_filename, fullTree, error))
+ return false;
- 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;
- // 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()) + ")";
+ 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>
diff --git a/src/common/Configuration/Config.h b/src/common/Configuration/Config.h
index 4c42b2e7b34..6a37be56bd6 100644
--- a/src/common/Configuration/Config.h
+++ b/src/common/Configuration/Config.h
@@ -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;
diff --git a/src/server/game/World/World.cpp b/src/server/game/World/World.cpp
index 9e03fa5ad10..2383642b917 100644
--- a/src/server/game/World/World.cpp
+++ b/src/server/game/World/World.cpp
@@ -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();