AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
ConfigMgr Class Reference

#include "Config.h"

Public Member Functions

bool LoadAppConfigs (bool isReload=false)
 
bool LoadModulesConfigs (bool isReload=false, bool isNeedPrintInfo=true)
 
void Configure (std::string const &initFileName, std::vector< std::string > args, std::string_view modulesConfigList={})
 
bool Reload ()
 
std::string const GetFilename ()
 
std::string const GetConfigPath ()
 
std::vector< std::string > const & GetArguments () const
 
std::vector< std::string > GetKeysByString (std::string const &name)
 
template<class T >
GetOption (std::string const &name, T const &def, bool showLogs=true) const
 
std::string GetStringDefault (std::string const &name, const std::string &def, bool showLogs=true)
 
bool GetBoolDefault (std::string const &name, bool def, bool showLogs=true)
 
int GetIntDefault (std::string const &name, int def, bool showLogs=true)
 
float GetFloatDefault (std::string const &name, float def, bool showLogs=true)
 
bool isDryRun ()
 
void setDryRun (bool mode)
 
template<>
bool GetOption (std::string const &name, bool const &def, bool showLogs) const
 

Static Public Member Functions

static ConfigMgrinstance ()
 

Private Member Functions

 ConfigMgr ()=default
 
 ConfigMgr (ConfigMgr const &)=delete
 
ConfigMgroperator= (ConfigMgr const &)=delete
 
 ~ConfigMgr ()=default
 
bool LoadInitial (std::string const &file, bool isReload=false)
 Method used only for loading main configuration files (authserver.conf and worldserver.conf)
 
bool LoadAdditionalFile (std::string file, bool isOptional=false, bool isReload=false)
 
template<class T >
GetValueDefault (std::string const &name, T const &def, bool showLogs=true) const
 

Private Attributes

bool dryRun = false
 
std::vector< std::string > _moduleConfigFiles
 

Detailed Description

Constructor & Destructor Documentation

◆ ConfigMgr() [1/2]

ConfigMgr::ConfigMgr ( )
privatedefault

◆ ConfigMgr() [2/2]

ConfigMgr::ConfigMgr ( ConfigMgr const &  )
privatedelete

◆ ~ConfigMgr()

ConfigMgr::~ConfigMgr ( )
privatedefault

Member Function Documentation

◆ Configure()

void ConfigMgr::Configure ( std::string const &  initFileName,
std::vector< std::string >  args,
std::string_view  modulesConfigList = {} 
)
363{
364 _filename = initFileName;
365 _args = std::move(args);
366
367 // Add modules config if exist
368 if (!modulesConfigList.empty())
369 {
370 for (auto const& itr : Acore::Tokenize(modulesConfigList, ',', false))
371 {
372 _additonalFiles.emplace_back(itr);
373 }
374 }
375}
std::vector< std::string_view > Tokenize(std::string_view str, char sep, bool keepEmpty)
Definition: Tokenize.cpp:20
std::string _filename
Definition: Config.cpp:30
std::vector< std::string > _args
Definition: Config.cpp:32
std::vector< std::string > _additonalFiles
Definition: Config.cpp:31

References Acore::Tokenize().

◆ GetArguments()

std::vector< std::string > const & ConfigMgr::GetArguments ( ) const
347{
348 return _args;
349}

◆ GetBoolDefault()

bool ConfigMgr::GetBoolDefault ( std::string const &  name,
bool  def,
bool  showLogs = true 
)
Deprecated:
DO NOT USE - use GetOption<bool> instead.
473{
474 return GetOption<bool>(name, def, showLogs);
475}

◆ GetConfigPath()

std::string const ConfigMgr::GetConfigPath ( )
352{
353 std::lock_guard<std::mutex> lock(_configLock);
354
355#if AC_PLATFORM == AC_PLATFORM_WINDOWS
356 return "configs/";
357#else
358 return std::string(_CONF_DIR) + "/";
359#endif
360}

Referenced by LoadModulesConfigs().

◆ GetFilename()

std::string const ConfigMgr::GetFilename ( )
341{
342 std::lock_guard<std::mutex> lock(_configLock);
343 return _filename;
344}

◆ GetFloatDefault()

float ConfigMgr::GetFloatDefault ( std::string const &  name,
float  def,
bool  showLogs = true 
)
Deprecated:
DO NOT USE - use GetOption<float> instead.
485{
486 return GetOption<float>(name, def, showLogs);
487}

◆ GetIntDefault()

int ConfigMgr::GetIntDefault ( std::string const &  name,
int  def,
bool  showLogs = true 
)
Deprecated:
DO NOT USE - use GetOption<int32> instead.
479{
480 return GetOption<int32>(name, def, showLogs);
481}

◆ GetKeysByString()

std::vector< std::string > ConfigMgr::GetKeysByString ( std::string const &  name)
324{
325 std::lock_guard<std::mutex> lock(_configLock);
326
327 std::vector<std::string> keys;
328
329 for (auto const& [optionName, key] : _configOptions)
330 {
331 if (!optionName.compare(0, name.length(), name))
332 {
333 keys.emplace_back(optionName);
334 }
335 }
336
337 return keys;
338}

◆ GetOption() [1/2]

template<>
bool ConfigMgr::GetOption ( std::string const &  name,
bool const &  def,
bool  showLogs 
) const
305{
306 std::string val = GetValueDefault(name, std::string(def ? "1" : "0"), showLogs);
307
308 auto boolVal = Acore::StringTo<bool>(val);
309 if (!boolVal)
310 {
311 if (showLogs)
312 {
313 LOG_ERROR("server.loading", "> Config: Bad value defined for name '{}', going to use '{}' instead",
314 name, def ? "true" : "false");
315 }
316
317 return def;
318 }
319
320 return *boolVal;
321}
#define LOG_ERROR(filterType__,...)
Definition: Log.h:159
T GetValueDefault(std::string const &name, T const &def, bool showLogs=true) const
Definition: Config.cpp:250

References GetValueDefault(), and LOG_ERROR.

◆ GetOption() [2/2]

template<class T >
T ConfigMgr::GetOption ( std::string const &  name,
T const &  def,
bool  showLogs = true 
) const
299{
300 return GetValueDefault<T>(name, def, showLogs);
301}

◆ GetStringDefault()

std::string ConfigMgr::GetStringDefault ( std::string const &  name,
const std::string &  def,
bool  showLogs = true 
)
Deprecated:
DO NOT USE - use GetOption<std::string> instead.
467{
468 return GetOption<std::string>(name, def, showLogs);
469}

◆ GetValueDefault()

template<class T >
T ConfigMgr::GetValueDefault ( std::string const &  name,
T const &  def,
bool  showLogs = true 
) const
private
251{
252 auto const& itr = _configOptions.find(name);
253 if (itr == _configOptions.end())
254 {
255 if (showLogs)
256 {
257 LOG_ERROR("server.loading", "> Config: Missing property {} in config file {}, add \"{} = {}\" to this file.",
258 name, _filename, name, Acore::ToString(def));
259 }
260
261 return def;
262 }
263
264 auto value = Acore::StringTo<T>(itr->second);
265 if (!value)
266 {
267 if (showLogs)
268 {
269 LOG_ERROR("server.loading", "> Config: Bad value defined for name '{}', going to use '{}' instead",
270 name, Acore::ToString(def));
271 }
272
273 return def;
274 }
275
276 return *value;
277}
std::string ToString(Type &&val, Params &&... params)
Definition: StringConvert.h:250
std::unordered_map< std::string, std::string > _configOptions
Definition: Config.cpp:33

References LOG_ERROR, and Acore::ToString().

Referenced by GetOption().

◆ instance()

ConfigMgr * ConfigMgr::instance ( )
static
234{
235 static ConfigMgr instance;
236 return &instance;
237}
Definition: Config.h:27
static ConfigMgr * instance()
Definition: Config.cpp:233

References instance().

Referenced by instance().

◆ isDryRun()

bool ConfigMgr::isDryRun ( )
inline
70{ return dryRun; }
bool dryRun
Definition: Config.h:81

References dryRun.

◆ LoadAdditionalFile()

bool ConfigMgr::LoadAdditionalFile ( std::string  file,
bool  isOptional = false,
bool  isReload = false 
)
private
228{
229 std::lock_guard<std::mutex> lock(_configLock);
230 return LoadFile(file, isOptional, isReload);
231}
bool LoadFile(std::string const &file, bool isOptional, bool isReload)
Definition: Config.cpp:205

Referenced by LoadModulesConfigs().

◆ LoadAppConfigs()

bool ConfigMgr::LoadAppConfigs ( bool  isReload = false)
378{
379 // #1 - Load init config file .conf
380 if (!LoadInitial(_filename, isReload))
381 {
382 return false;
383 }
384
385 return true;
386}
bool LoadInitial(std::string const &file, bool isReload=false)
Method used only for loading main configuration files (authserver.conf and worldserver....
Definition: Config.cpp:220

References LoadInitial().

Referenced by Reload().

◆ LoadInitial()

bool ConfigMgr::LoadInitial ( std::string const &  file,
bool  isReload = false 
)
private

Method used only for loading main configuration files (authserver.conf and worldserver.conf)

221{
222 std::lock_guard<std::mutex> lock(_configLock);
223 _configOptions.clear();
224 return LoadFile(file, false, isReload);
225}

Referenced by LoadAppConfigs().

◆ LoadModulesConfigs()

bool ConfigMgr::LoadModulesConfigs ( bool  isReload = false,
bool  isNeedPrintInfo = true 
)
389{
390 if (_additonalFiles.empty())
391 {
392 // Send successful load if no found files
393 return true;
394 }
395
396 if (isNeedPrintInfo)
397 {
398 LOG_INFO("server.loading", " ");
399 LOG_INFO("server.loading", "Loading Modules Configuration...");
400 }
401
402 // Start loading module configs
403 std::string const& moduleConfigPath = GetConfigPath() + "modules/";
404 bool isExistDefaultConfig = true;
405 bool isExistDistConfig = true;
406
407 for (auto const& distFileName : _additonalFiles)
408 {
409 std::string defaultFileName = distFileName;
410
411 if (!defaultFileName.empty())
412 {
413 defaultFileName.erase(defaultFileName.end() - 5, defaultFileName.end());
414 }
415
416 // Load .conf.dist config
417 isExistDistConfig = LoadAdditionalFile(moduleConfigPath + distFileName, false, isReload);
418
419 if (!isReload && !isExistDistConfig)
420 {
421 LOG_FATAL("server.loading", "> ConfigMgr::LoadModulesConfigs: Not found original config '{}'. Stop loading", distFileName);
422 ABORT();
423 }
424
425 // Load .conf config
426 isExistDefaultConfig = LoadAdditionalFile(moduleConfigPath + defaultFileName, true, isReload);
427
428 if (isExistDefaultConfig && isExistDistConfig)
429 {
430 _moduleConfigFiles.emplace_back(defaultFileName);
431 }
432 else if (!isExistDefaultConfig && isExistDistConfig)
433 {
434 _moduleConfigFiles.emplace_back(distFileName);
435 }
436 }
437
438 if (isNeedPrintInfo)
439 {
440 if (!_moduleConfigFiles.empty())
441 {
442 // Print modules configurations
443 LOG_INFO("server.loading", " ");
444 LOG_INFO("server.loading", "Using modules configuration:");
445
446 for (auto const& itr : _moduleConfigFiles)
447 {
448 LOG_INFO("server.loading", "> {}", itr);
449 }
450 }
451 else
452 {
453 LOG_INFO("server.loading", "> Not found modules config files");
454 }
455 }
456
457 if (isNeedPrintInfo)
458 {
459 LOG_INFO("server.loading", " ");
460 }
461
462 return true;
463}
#define ABORT
Definition: Errors.h:76
#define LOG_FATAL(filterType__,...)
Definition: Log.h:155
#define LOG_INFO(filterType__,...)
Definition: Log.h:167
std::string const GetConfigPath()
Definition: Config.cpp:351
bool LoadAdditionalFile(std::string file, bool isOptional=false, bool isReload=false)
Definition: Config.cpp:227
std::vector< std::string > _moduleConfigFiles
Definition: Config.h:83

References _moduleConfigFiles, ABORT, GetConfigPath(), LoadAdditionalFile(), LOG_FATAL, and LOG_INFO.

Referenced by Reload().

◆ operator=()

ConfigMgr & ConfigMgr::operator= ( ConfigMgr const &  )
privatedelete

◆ Reload()

bool ConfigMgr::Reload ( )
240{
241 if (!LoadAppConfigs(true))
242 {
243 return false;
244 }
245
246 return LoadModulesConfigs(true, false);
247}
bool LoadModulesConfigs(bool isReload=false, bool isNeedPrintInfo=true)
Definition: Config.cpp:388
bool LoadAppConfigs(bool isReload=false)
Definition: Config.cpp:377

References LoadAppConfigs(), and LoadModulesConfigs().

◆ setDryRun()

void ConfigMgr::setDryRun ( bool  mode)
inline
71{ dryRun = mode; }

References dryRun.

Member Data Documentation

◆ _moduleConfigFiles

std::vector<std::string > ConfigMgr::_moduleConfigFiles
private

Referenced by LoadModulesConfigs().

◆ dryRun

bool ConfigMgr::dryRun = false
private

Referenced by isDryRun(), and setDryRun().