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::vector< std::string > OverrideWithEnvVariablesIfAny ()
 Overrides configuration with environment variables and returns overridden keys.
 
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 = {} 
)
498{
499 _filename = initFileName;
500 _args = std::move(args);
501
502 // Add modules config if exist
503 if (!modulesConfigList.empty())
504 {
505 for (auto const& itr : Acore::Tokenize(modulesConfigList, ',', false))
506 {
507 _additonalFiles.emplace_back(itr);
508 }
509 }
510}
std::vector< std::string_view > Tokenize(std::string_view str, char sep, bool keepEmpty)
Definition: Tokenize.cpp:20
std::string _filename
Definition: Config.cpp:31
std::vector< std::string > _args
Definition: Config.cpp:33
std::vector< std::string > _additonalFiles
Definition: Config.cpp:32

References Acore::Tokenize().

◆ GetArguments()

std::vector< std::string > const & ConfigMgr::GetArguments ( ) const
482{
483 return _args;
484}

◆ GetBoolDefault()

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

◆ GetConfigPath()

std::string const ConfigMgr::GetConfigPath ( )
487{
488 std::lock_guard<std::mutex> lock(_configLock);
489
490#if AC_PLATFORM == AC_PLATFORM_WINDOWS
491 return "configs/";
492#else
493 return std::string(_CONF_DIR) + "/";
494#endif
495}

Referenced by LoadModulesConfigs().

◆ GetFilename()

std::string const ConfigMgr::GetFilename ( )
476{
477 std::lock_guard<std::mutex> lock(_configLock);
478 return _filename;
479}

◆ GetFloatDefault()

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

◆ GetIntDefault()

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

◆ GetKeysByString()

std::vector< std::string > ConfigMgr::GetKeysByString ( std::string const &  name)
459{
460 std::lock_guard<std::mutex> lock(_configLock);
461
462 std::vector<std::string> keys;
463
464 for (auto const& [optionName, key] : _configOptions)
465 {
466 if (!optionName.compare(0, name.length(), name))
467 {
468 keys.emplace_back(optionName);
469 }
470 }
471
472 return keys;
473}

◆ GetOption() [1/2]

template<>
bool ConfigMgr::GetOption ( std::string const &  name,
bool const &  def,
bool  showLogs 
) const
440{
441 std::string val = GetValueDefault(name, std::string(def ? "1" : "0"), showLogs);
442
443 auto boolVal = Acore::StringTo<bool>(val);
444 if (!boolVal)
445 {
446 if (showLogs)
447 {
448 LOG_ERROR("server.loading", "> Config: Bad value defined for name '{}', going to use '{}' instead",
449 name, def ? "true" : "false");
450 }
451
452 return def;
453 }
454
455 return *boolVal;
456}
#define LOG_ERROR(filterType__,...)
Definition: Log.h:159
T GetValueDefault(std::string const &name, T const &def, bool showLogs=true) const
Definition: Config.cpp:356

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
434{
435 return GetValueDefault<T>(name, def, showLogs);
436}

◆ 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.
602{
603 return GetOption<std::string>(name, def, showLogs);
604}

◆ GetValueDefault()

template<class T >
T ConfigMgr::GetValueDefault ( std::string const &  name,
T const &  def,
bool  showLogs = true 
) const
private
357{
358 std::string strValue;
359 auto const& itr = _configOptions.find(name);
360 if (itr == _configOptions.end())
361 {
363 if (!envVar)
364 {
365 if (showLogs)
366 {
367 LOG_ERROR("server.loading", "> Config: Missing property {} in config file {}, add \"{} = {}\" to this file.",
368 name, _filename, name, Acore::ToString(def));
369 }
370
371 return def;
372 }
373
374 if (showLogs)
375 {
376 LOG_WARN("server.loading", "Missing property {} in config file {}, recovered with environment '{}' value.",
377 name.c_str(), _filename.c_str(), envVar->c_str());
378 }
379
380 strValue = *envVar;
381 }
382 else
383 {
384 strValue = itr->second;
385 }
386
387 auto value = Acore::StringTo<T>(strValue);
388 if (!value)
389 {
390 if (showLogs)
391 {
392 LOG_ERROR("server.loading", "> Config: Bad value defined for name '{}', going to use '{}' instead",
393 name, Acore::ToString(def));
394 }
395
396 return def;
397 }
398
399 return *value;
400}
#define LOG_WARN(filterType__,...)
Definition: Log.h:163
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:24
std::string ToString(Type &&val, Params &&... params)
Definition: StringConvert.h:250
Optional< std::string > EnvVarForIniKey(std::string const &key)
Definition: Config.cpp:285
std::unordered_map< std::string, std::string > _configOptions
Definition: Config.cpp:34

References LOG_ERROR, LOG_WARN, and Acore::ToString().

Referenced by GetOption().

◆ instance()

ConfigMgr * ConfigMgr::instance ( )
static
310{
311 static ConfigMgr instance;
312 return &instance;
313}
Definition: Config.h:27
static ConfigMgr * instance()
Definition: Config.cpp:309

References instance().

Referenced by instance().

◆ isDryRun()

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

References dryRun.

◆ LoadAdditionalFile()

bool ConfigMgr::LoadAdditionalFile ( std::string  file,
bool  isOptional = false,
bool  isReload = false 
)
private
304{
305 std::lock_guard<std::mutex> lock(_configLock);
306 return LoadFile(file, isOptional, isReload);
307}
bool LoadFile(std::string const &file, bool isOptional, bool isReload)
Definition: Config.cpp:206

Referenced by LoadModulesConfigs().

◆ LoadAppConfigs()

bool ConfigMgr::LoadAppConfigs ( bool  isReload = false)
513{
514 // #1 - Load init config file .conf
515 if (!LoadInitial(_filename, isReload))
516 {
517 return false;
518 }
519
520 return true;
521}
bool LoadInitial(std::string const &file, bool isReload=false)
Method used only for loading main configuration files (authserver.conf and worldserver....
Definition: Config.cpp:296

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)

297{
298 std::lock_guard<std::mutex> lock(_configLock);
299 _configOptions.clear();
300 return LoadFile(file, false, isReload);
301}

Referenced by LoadAppConfigs().

◆ LoadModulesConfigs()

bool ConfigMgr::LoadModulesConfigs ( bool  isReload = false,
bool  isNeedPrintInfo = true 
)
524{
525 if (_additonalFiles.empty())
526 {
527 // Send successful load if no found files
528 return true;
529 }
530
531 if (isNeedPrintInfo)
532 {
533 LOG_INFO("server.loading", " ");
534 LOG_INFO("server.loading", "Loading Modules Configuration...");
535 }
536
537 // Start loading module configs
538 std::string const& moduleConfigPath = GetConfigPath() + "modules/";
539 bool isExistDefaultConfig = true;
540 bool isExistDistConfig = true;
541
542 for (auto const& distFileName : _additonalFiles)
543 {
544 std::string defaultFileName = distFileName;
545
546 if (!defaultFileName.empty())
547 {
548 defaultFileName.erase(defaultFileName.end() - 5, defaultFileName.end());
549 }
550
551 // Load .conf.dist config
552 isExistDistConfig = LoadAdditionalFile(moduleConfigPath + distFileName, false, isReload);
553
554 if (!isReload && !isExistDistConfig)
555 {
556 LOG_FATAL("server.loading", "> ConfigMgr::LoadModulesConfigs: Not found original config '{}'. Stop loading", distFileName);
557 ABORT();
558 }
559
560 // Load .conf config
561 isExistDefaultConfig = LoadAdditionalFile(moduleConfigPath + defaultFileName, true, isReload);
562
563 if (isExistDefaultConfig && isExistDistConfig)
564 {
565 _moduleConfigFiles.emplace_back(defaultFileName);
566 }
567 else if (!isExistDefaultConfig && isExistDistConfig)
568 {
569 _moduleConfigFiles.emplace_back(distFileName);
570 }
571 }
572
573 if (isNeedPrintInfo)
574 {
575 if (!_moduleConfigFiles.empty())
576 {
577 // Print modules configurations
578 LOG_INFO("server.loading", " ");
579 LOG_INFO("server.loading", "Using modules configuration:");
580
581 for (auto const& itr : _moduleConfigFiles)
582 {
583 LOG_INFO("server.loading", "> {}", itr);
584 }
585 }
586 else
587 {
588 LOG_INFO("server.loading", "> Not found modules config files");
589 }
590 }
591
592 if (isNeedPrintInfo)
593 {
594 LOG_INFO("server.loading", " ");
595 }
596
597 return true;
598}
#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:486
bool LoadAdditionalFile(std::string file, bool isOptional=false, bool isReload=false)
Definition: Config.cpp:303
std::vector< std::string > _moduleConfigFiles
Definition: Config.h:86

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

Referenced by Reload().

◆ operator=()

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

◆ OverrideWithEnvVariablesIfAny()

std::vector< std::string > ConfigMgr::OverrideWithEnvVariablesIfAny ( )

Overrides configuration with environment variables and returns overridden keys.

333{
334 std::lock_guard<std::mutex> lock(_configLock);
335
336 std::vector<std::string> overriddenKeys;
337
338 for (auto& itr : _configOptions)
339 {
340 if (itr.first.empty())
341 continue;
342
343 Optional<std::string> envVar = EnvVarForIniKey(itr.first);
344 if (!envVar)
345 continue;
346
347 itr.second = *envVar;
348
349 overriddenKeys.push_back(itr.first);
350 }
351
352 return overriddenKeys;
353}

Referenced by Reload().

◆ Reload()

bool ConfigMgr::Reload ( )
316{
317 if (!LoadAppConfigs(true))
318 {
319 return false;
320 }
321
322 if (!LoadModulesConfigs(true, false))
323 {
324 return false;
325 }
326
328
329 return true;
330}
std::vector< std::string > OverrideWithEnvVariablesIfAny()
Overrides configuration with environment variables and returns overridden keys.
Definition: Config.cpp:332
bool LoadModulesConfigs(bool isReload=false, bool isNeedPrintInfo=true)
Definition: Config.cpp:523
bool LoadAppConfigs(bool isReload=false)
Definition: Config.cpp:512

References LoadAppConfigs(), LoadModulesConfigs(), and OverrideWithEnvVariablesIfAny().

◆ setDryRun()

void ConfigMgr::setDryRun ( bool  mode)
inline
74{ 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().