diff --git a/src/common/Configuration/Config.cpp b/src/common/Configuration/Config.cpp index c86efe7788..1c73d0d50a 100644 --- a/src/common/Configuration/Config.cpp +++ b/src/common/Configuration/Config.cpp @@ -16,6 +16,7 @@ */ #include "Config.h" +#include "ConfigBaseFile.h" #include "Common.h" #include "Log.h" #include "StringConvert.h" @@ -35,6 +36,7 @@ namespace { std::string _filename; std::vector _additonalFiles; + std::vector _baseFiles; // LoadBaseFile: applied after the main file, before _additonalFiles std::vector _args; bpt::ptree _config; std::mutex _configLock; @@ -172,6 +174,33 @@ bool ConfigMgr::LoadAdditionalFile(std::string file, bool keepOnReload, std::str return true; } +bool Trinity::Config::LoadBaseFile(std::string file, std::string& error) +{ + // check the file first, so a broken one is not kept for every later reload + bpt::ptree fullTree; + if (!LoadFile(file, fullTree, error, true)) + return false; + + { + std::scoped_lock lock(_configLock); + if (std::find(_baseFiles.begin(), _baseFiles.end(), file) == _baseFiles.end()) + _baseFiles.push_back(std::move(file)); + } + + // build the config again in the right order: main file, base files, additional files, environment + std::vector errors; + if (sConfigMgr->Reload(errors)) + return true; + + for (std::string& e : errors) + { + if (!error.empty()) + error += "; "; + error += e; + } + return false; +} + bool ConfigMgr::LoadAdditionalDir(std::string const& dir, bool keepOnReload, std::vector& loadedFiles, std::vector& errors) { fs::path dirPath = dir; @@ -242,6 +271,10 @@ bool ConfigMgr::Reload(std::vector& errors) if (!LoadInitial(_filename, std::move(_args), error)) errors.push_back(std::move(error)); + for (std::string const& baseFile : _baseFiles) + if (!LoadAdditionalFile(baseFile, false, error)) + errors.push_back(std::move(error)); + for (std::string const& additionalFile : _additonalFiles) if (!LoadAdditionalFile(additionalFile, false, error)) errors.push_back(std::move(error)); diff --git a/src/common/Configuration/ConfigBaseFile.h b/src/common/Configuration/ConfigBaseFile.h new file mode 100644 index 0000000000..aea29fb4fb --- /dev/null +++ b/src/common/Configuration/ConfigBaseFile.h @@ -0,0 +1,33 @@ +/* + * 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 . + */ + +#ifndef TRINITYCORE_CONFIG_BASE_FILE_H +#define TRINITYCORE_CONFIG_BASE_FILE_H + +#include "Define.h" +#include + +namespace Trinity::Config +{ + // Loads a config file that sits between the main file and the additional files (worldserver.conf.d): its keys beat the main + // file, and every additional file beats it, even one loaded before it. For a module's own config file (playerbot.conf), so + // the override directory stays the place that wins. sConfigMgr->Reload keeps it in the same place. Loading the same file + // again only reads it again. (Kept out of Config.h, which nearly every file includes.) + TC_COMMON_API bool LoadBaseFile(std::string file, std::string& error); +} + +#endif // TRINITYCORE_CONFIG_BASE_FILE_H