mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-15 10:00:28 +00:00
feat(Core/Config): implement loading files optional (#8198)
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include "StringConvert.h"
|
||||
#include "StringFormat.h"
|
||||
#include "Util.h"
|
||||
#include "Tokenize.h"
|
||||
#include <fstream>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
@@ -31,64 +32,82 @@ namespace
|
||||
std::vector<std::string> _args;
|
||||
std::unordered_map<std::string /*name*/, std::string /*value*/> _configOptions;
|
||||
std::mutex _configLock;
|
||||
bool _usingDistConfig = false;
|
||||
|
||||
// Check system configs like *server.conf*
|
||||
bool IsAppConfig(std::string_view fileName)
|
||||
{
|
||||
size_t found = fileName.find_first_of("authserver.conf");
|
||||
if (found != std::string::npos)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
size_t foundAuth = fileName.find("authserver.conf");
|
||||
size_t foundWorld = fileName.find("worldserver.conf");
|
||||
|
||||
found = fileName.find_first_of("worldserver.conf");
|
||||
if (found != std::string::npos)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return foundAuth != std::string_view::npos || foundWorld != std::string_view::npos;
|
||||
}
|
||||
|
||||
return false;
|
||||
// Check logging system configs like Appender.* and Logger.*
|
||||
bool IsLoggingSystemOptions(std::string_view optionName)
|
||||
{
|
||||
size_t foundAppender = optionName.find("Appender.");
|
||||
size_t foundLogger = optionName.find("Logger.");
|
||||
|
||||
return foundAppender != std::string_view::npos || foundLogger != std::string_view::npos;
|
||||
}
|
||||
|
||||
template<typename Format, typename... Args>
|
||||
inline void PrintError(std::string_view filename, Format&& fmt, Args&& ... args)
|
||||
{
|
||||
std::string message = Acore::StringFormat(std::forward<Format>(fmt), std::forward<Args>(args)...);
|
||||
std::string message = Acore::StringFormatFmt(std::forward<Format>(fmt), std::forward<Args>(args)...);
|
||||
|
||||
if (IsAppConfig(filename))
|
||||
{
|
||||
printf("%s\n", message.c_str());
|
||||
fmt::print("{}\n", message);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("server.loading", "%s", message.c_str());
|
||||
FMT_LOG_ERROR("server.loading", message);
|
||||
}
|
||||
}
|
||||
|
||||
void AddKey(std::string const& optionName, std::string const& optionKey, bool replace = true)
|
||||
void AddKey(std::string const& optionName, std::string const& optionKey, std::string_view fileName, bool isOptional, [[maybe_unused]] bool isReload)
|
||||
{
|
||||
auto const& itr = _configOptions.find(optionName);
|
||||
if (itr != _configOptions.end())
|
||||
|
||||
// Check old option
|
||||
if (isOptional && itr == _configOptions.end())
|
||||
{
|
||||
if (!replace)
|
||||
if (!IsLoggingSystemOptions(optionName) && !isReload)
|
||||
{
|
||||
LOG_ERROR("server.loading", "> Config: Option '%s' is exist! Option key - '%s'", optionName.c_str(), itr->second.c_str());
|
||||
PrintError(fileName, "> Config::LoadFile: Found incorrect option '{}' in config file '{}'. Skip", optionName, fileName);
|
||||
|
||||
#ifdef CONFIG_ABORT_INCORRECT_OPTIONS
|
||||
ABORT_MSG("> Core can't start if found incorrect options");
|
||||
#endif
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Check exit option
|
||||
if (itr != _configOptions.end())
|
||||
{
|
||||
_configOptions.erase(optionName);
|
||||
}
|
||||
|
||||
_configOptions.emplace(optionName, optionKey);
|
||||
}
|
||||
|
||||
void ParseFile(std::string const& file)
|
||||
bool ParseFile(std::string const& file, bool isOptional, bool isReload)
|
||||
{
|
||||
std::ifstream in(file);
|
||||
|
||||
if (in.fail())
|
||||
{
|
||||
throw ConfigException(Acore::StringFormat("Config::LoadFile: Failed open file '%s'", file.c_str()));
|
||||
if (isOptional)
|
||||
{
|
||||
// No display erorr if file optional
|
||||
return false;
|
||||
}
|
||||
|
||||
throw ConfigException(Acore::StringFormatFmt("Config::LoadFile: Failed open {}file '{}'", isOptional ? "optional " : "", file));
|
||||
}
|
||||
|
||||
uint32 count = 0;
|
||||
@@ -100,7 +119,7 @@ namespace
|
||||
auto const& itr = fileConfigs.find(confOption);
|
||||
if (itr != fileConfigs.end())
|
||||
{
|
||||
PrintError(file, "> Config::LoadFile: Dublicate key name '%s' in config file '%s'", std::string(confOption).c_str(), file.c_str());
|
||||
PrintError(file, "> Config::LoadFile: Dublicate key name '{}' in config file '{}'", confOption, file);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -116,7 +135,7 @@ namespace
|
||||
// read line error
|
||||
if (!in.good() && !in.eof())
|
||||
{
|
||||
throw ConfigException(Acore::StringFormat("> Config::LoadFile: Failure to read line number %u in file '%s'", lineNumber, file.c_str()));
|
||||
throw ConfigException(Acore::StringFormatFmt("> Config::LoadFile: Failure to read line number {} in file '{}'", lineNumber, file));
|
||||
}
|
||||
|
||||
// remove whitespace in line
|
||||
@@ -143,7 +162,7 @@ namespace
|
||||
|
||||
if (equal_pos == std::string::npos || equal_pos == line.length())
|
||||
{
|
||||
PrintError(file, "> Config::LoadFile: Failure to read line number %u in file '%s'. Skip this line", lineNumber, file.c_str());
|
||||
PrintError(file, "> Config::LoadFile: Failure to read line number {} in file '{}'. Skip this line", lineNumber, file);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -166,43 +185,50 @@ namespace
|
||||
// No lines read
|
||||
if (!count)
|
||||
{
|
||||
throw ConfigException(Acore::StringFormat("Config::LoadFile: Empty file '%s'", file.c_str()));
|
||||
if (isOptional)
|
||||
{
|
||||
// No display erorr if file optional
|
||||
return false;
|
||||
}
|
||||
|
||||
throw ConfigException(Acore::StringFormatFmt("Config::LoadFile: Empty file '{}'", file));
|
||||
}
|
||||
|
||||
// Add correct keys if file load without errors
|
||||
for (auto const& [entry, key] : fileConfigs)
|
||||
{
|
||||
AddKey(entry, key);
|
||||
AddKey(entry, key, file, isOptional, isReload);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LoadFile(std::string const& file)
|
||||
bool LoadFile(std::string const& file, bool isOptional, bool isReload)
|
||||
{
|
||||
try
|
||||
{
|
||||
ParseFile(file);
|
||||
return true;
|
||||
return ParseFile(file, isOptional, isReload);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
PrintError(file, "> %s", e.what());
|
||||
PrintError(file, "> {}", e.what());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ConfigMgr::LoadInitial(std::string const& file)
|
||||
bool ConfigMgr::LoadInitial(std::string const& file, bool isReload /*= false*/)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_configLock);
|
||||
_configOptions.clear();
|
||||
return LoadFile(file);
|
||||
return LoadFile(file, false, isReload);
|
||||
}
|
||||
|
||||
bool ConfigMgr::LoadAdditionalFile(std::string file)
|
||||
bool ConfigMgr::LoadAdditionalFile(std::string file, bool isOptional /*= false*/, bool isReload /*= false*/)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_configLock);
|
||||
return LoadFile(file);
|
||||
return LoadFile(file, isOptional, isReload);
|
||||
}
|
||||
|
||||
ConfigMgr* ConfigMgr::instance()
|
||||
@@ -213,12 +239,12 @@ ConfigMgr* ConfigMgr::instance()
|
||||
|
||||
bool ConfigMgr::Reload()
|
||||
{
|
||||
if (!LoadAppConfigs())
|
||||
if (!LoadAppConfigs(true))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return LoadModulesConfigs();
|
||||
return LoadModulesConfigs(true, false);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
@@ -229,8 +255,8 @@ T ConfigMgr::GetValueDefault(std::string const& name, T const& def, bool showLog
|
||||
{
|
||||
if (showLogs)
|
||||
{
|
||||
LOG_ERROR("server.loading", "> Config: Missing name %s in config, add \"%s = %s\"",
|
||||
name.c_str(), name.c_str(), Acore::ToString(def).c_str());
|
||||
FMT_LOG_ERROR("server.loading", "> Config: Missing property {} in all config files, at least the .dist file must contain: \"{} = {}\"",
|
||||
name, name, Acore::ToString(def));
|
||||
}
|
||||
|
||||
return def;
|
||||
@@ -241,8 +267,8 @@ T ConfigMgr::GetValueDefault(std::string const& name, T const& def, bool showLog
|
||||
{
|
||||
if (showLogs)
|
||||
{
|
||||
LOG_ERROR("server.loading", "> Config: Bad value defined for name '%s', going to use '%s' instead",
|
||||
name.c_str(), Acore::ToString(def).c_str());
|
||||
FMT_LOG_ERROR("server.loading", "> Config: Bad value defined for name '{}', going to use '{}' instead",
|
||||
name, Acore::ToString(def));
|
||||
}
|
||||
|
||||
return def;
|
||||
@@ -259,8 +285,8 @@ std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std
|
||||
{
|
||||
if (showLogs)
|
||||
{
|
||||
LOG_ERROR("server.loading", "> Config: Missing name %s in config, add \"%s = %s\"",
|
||||
name.c_str(), name.c_str(), def.c_str());
|
||||
FMT_LOG_ERROR("server.loading", "> Config: Missing option {}, add \"{} = {}\"",
|
||||
name, name, def);
|
||||
}
|
||||
|
||||
return def;
|
||||
@@ -285,8 +311,8 @@ bool ConfigMgr::GetOption<bool>(std::string const& name, bool const& def, bool s
|
||||
{
|
||||
if (showLogs)
|
||||
{
|
||||
LOG_ERROR("server.loading", "> Config: Bad value defined for name '%s', going to use '%s' instead",
|
||||
name.c_str(), def ? "true" : "false");
|
||||
FMT_LOG_ERROR("server.loading", "> Config: Bad value defined for name '{}', going to use '{}' instead",
|
||||
name, def ? "true" : "false");
|
||||
}
|
||||
|
||||
return def;
|
||||
@@ -302,18 +328,20 @@ std::vector<std::string> ConfigMgr::GetKeysByString(std::string const& name)
|
||||
std::vector<std::string> keys;
|
||||
|
||||
for (auto const& [optionName, key] : _configOptions)
|
||||
{
|
||||
if (!optionName.compare(0, name.length(), name))
|
||||
{
|
||||
keys.emplace_back(optionName);
|
||||
}
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
std::string const& ConfigMgr::GetFilename()
|
||||
std::string const ConfigMgr::GetFilename()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_configLock);
|
||||
return _filename;
|
||||
return _usingDistConfig ? _filename + ".dist" : _filename;
|
||||
}
|
||||
|
||||
std::vector<std::string> const& ConfigMgr::GetArguments() const
|
||||
@@ -340,38 +368,44 @@ void ConfigMgr::Configure(std::string const& initFileName, std::vector<std::stri
|
||||
// Add modules config if exist
|
||||
if (!modulesConfigList.empty())
|
||||
{
|
||||
Tokenizer configFileList(modulesConfigList, ',');
|
||||
for (auto const& itr : configFileList)
|
||||
for (auto const& itr : Acore::Tokenize(modulesConfigList, ',', false))
|
||||
{
|
||||
_additonalFiles.emplace_back(std::string(itr));
|
||||
_additonalFiles.emplace_back(itr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ConfigMgr::LoadAppConfigs()
|
||||
bool ConfigMgr::LoadAppConfigs(bool isReload /*= false*/)
|
||||
{
|
||||
// #1 - Load init config file .conf.dist
|
||||
if (!LoadInitial(_filename + ".dist"))
|
||||
if (!LoadInitial(_filename + ".dist", isReload))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// #2 - Load .conf file
|
||||
if (!LoadAdditionalFile(_filename))
|
||||
if (!LoadAdditionalFile(_filename, true, isReload))
|
||||
{
|
||||
return false;
|
||||
_usingDistConfig = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ConfigMgr::LoadModulesConfigs()
|
||||
bool ConfigMgr::LoadModulesConfigs(bool isReload /*= false*/, bool isNeedPrintInfo /*= true*/)
|
||||
{
|
||||
if (_additonalFiles.empty())
|
||||
{
|
||||
// Send successful load if no found files
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isNeedPrintInfo)
|
||||
{
|
||||
FMT_LOG_INFO("server.loading", " ");
|
||||
FMT_LOG_INFO("server.loading", "Loading modules configuration...");
|
||||
}
|
||||
|
||||
// Start loading module configs
|
||||
std::string const& moduleConfigPath = GetConfigPath() + "modules/";
|
||||
bool isExistDefaultConfig = true;
|
||||
@@ -387,16 +421,16 @@ bool ConfigMgr::LoadModulesConfigs()
|
||||
}
|
||||
|
||||
// Load .conf.dist config
|
||||
if (!LoadAdditionalFile(moduleConfigPath + distFileName))
|
||||
isExistDistConfig = LoadAdditionalFile(moduleConfigPath + distFileName, false, isReload);
|
||||
|
||||
if (!isReload && !isExistDistConfig)
|
||||
{
|
||||
isExistDistConfig = false;
|
||||
FMT_LOG_FATAL("server.loading", "> ConfigMgr::LoadModulesConfigs: Not found original config '{}'. Stop loading", distFileName);
|
||||
ABORT();
|
||||
}
|
||||
|
||||
// Load .conf config
|
||||
if (!LoadAdditionalFile(moduleConfigPath + defaultFileName))
|
||||
{
|
||||
isExistDefaultConfig = false;
|
||||
}
|
||||
isExistDefaultConfig = LoadAdditionalFile(moduleConfigPath + defaultFileName, true, isReload);
|
||||
|
||||
if (isExistDefaultConfig && isExistDistConfig)
|
||||
{
|
||||
@@ -408,27 +442,32 @@ bool ConfigMgr::LoadModulesConfigs()
|
||||
}
|
||||
}
|
||||
|
||||
// If module configs not exist - no load
|
||||
return !_moduleConfigFiles.empty();
|
||||
}
|
||||
|
||||
void ConfigMgr::PrintLoadedModulesConfigs()
|
||||
{
|
||||
// Print modules configurations
|
||||
LOG_INFO("server.loading", " ");
|
||||
LOG_INFO("server.loading", "Using modules configuration:");
|
||||
|
||||
for (auto const& itr : _moduleConfigFiles)
|
||||
if (isNeedPrintInfo)
|
||||
{
|
||||
LOG_INFO("server.loading", "> %s", itr.c_str());
|
||||
if (!_moduleConfigFiles.empty())
|
||||
{
|
||||
// Print modules configurations
|
||||
FMT_LOG_INFO("server.loading", " ");
|
||||
FMT_LOG_INFO("server.loading", "Using modules configuration:");
|
||||
|
||||
for (auto const& itr : _moduleConfigFiles)
|
||||
{
|
||||
FMT_LOG_INFO("server.loading", "> {}", itr);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FMT_LOG_INFO("server.loading", "> Not found modules config files");
|
||||
}
|
||||
}
|
||||
|
||||
LOG_INFO("server.loading", " ");
|
||||
}
|
||||
if (isNeedPrintInfo)
|
||||
{
|
||||
FMT_LOG_INFO("server.loading", " ");
|
||||
}
|
||||
|
||||
/*
|
||||
* Deprecated geters. This geters will be deleted
|
||||
*/
|
||||
return true;
|
||||
}
|
||||
|
||||
// @deprecated DO NOT USE - use GetOption<std::string> instead.
|
||||
std::string ConfigMgr::GetStringDefault(std::string const& name, const std::string& def, bool showLogs /*= true*/)
|
||||
@@ -454,10 +493,6 @@ float ConfigMgr::GetFloatDefault(std::string const& name, float def, bool showLo
|
||||
return GetOption<float>(name, def, showLogs);
|
||||
}
|
||||
|
||||
/*
|
||||
* End deprecated geters
|
||||
*/
|
||||
|
||||
#define TEMPLATE_CONFIG_OPTION(__typename) \
|
||||
template __typename ConfigMgr::GetOption<__typename>(std::string const& name, __typename const& def, bool showLogs /*= true*/) const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user