Core: a module config file sits below worldserver.conf.d
A module that wants its own config file (playerbot.conf) could only add it with LoadAdditionalFile, after worldserver.conf.d was loaded. Its keys then beat every conf.d override, at startup and after every reload, and the module had to add the file again to re-read it, which put it on top once more and listed it again for every later reload. Trinity::Config::LoadBaseFile (ConfigBaseFile.h, kept out of Config.h, which nearly every file includes) adds a file between the main file and the additional files: worldserver.conf < base files < worldserver.conf.d < environment. It rebuilds the config in that order at once, and ConfigMgr::Reload keeps the order. Loading the same base file again only reads it again. A file that cannot be read is not kept.
This commit is contained in:
@@ -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<std::string> _additonalFiles;
|
||||
std::vector<std::string> _baseFiles; // LoadBaseFile: applied after the main file, before _additonalFiles
|
||||
std::vector<std::string> _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<std::string> 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<std::string>& loadedFiles, std::vector<std::string>& errors)
|
||||
{
|
||||
fs::path dirPath = dir;
|
||||
@@ -242,6 +271,10 @@ bool ConfigMgr::Reload(std::vector<std::string>& 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));
|
||||
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef TRINITYCORE_CONFIG_BASE_FILE_H
|
||||
#define TRINITYCORE_CONFIG_BASE_FILE_H
|
||||
|
||||
#include "Define.h"
|
||||
#include <string>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user