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={}, ConfigPolicy policy={})
 
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
 
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 = {},
ConfigPolicy  policy = {} 
)
716{
717 _filename = initFileName;
718 _args = std::move(args);
719 _policy = policy;
720
721 if (char const* env = std::getenv("AC_CONFIG_POLICY"))
722 _policy = ApplyPolicyString(_policy, env);
723
724 _policy = ApplyPolicyFromArgs(_policy, _args);
725
726 _additonalFiles.clear();
727 _moduleConfigFiles.clear();
728
729 // Add modules config if exist
730 if (!modulesConfigList.empty())
731 {
732 for (auto const& itr : Acore::Tokenize(modulesConfigList, ',', false))
733 {
734 if (!itr.empty())
735 _additonalFiles.emplace_back(itr);
736 }
737 }
738}
std::vector< std::string > _moduleConfigFiles
Definition Config.h:84
Definition AsioHacksFwd.h:47
std::vector< std::string_view > Tokenize(std::string_view str, char sep, bool keepEmpty)
Definition Tokenize.cpp:20

References _moduleConfigFiles, and Acore::Tokenize().

◆ GetArguments()

std::vector< std::string > const & ConfigMgr::GetArguments ( ) const
700{
701 return _args;
702}

◆ GetConfigPath()

std::string const ConfigMgr::GetConfigPath ( )
705{
706 std::lock_guard<std::mutex> lock(_configLock);
707
708#if AC_PLATFORM == AC_PLATFORM_WINDOWS
709 return "configs/";
710#else
711 return std::string(_CONF_DIR) + "/";
712#endif
713}

Referenced by LoadModulesConfigs().

◆ GetFilename()

std::string const ConfigMgr::GetFilename ( )
694{
695 std::lock_guard<std::mutex> lock(_configLock);
696 return _filename;
697}

◆ GetKeysByString()

std::vector< std::string > ConfigMgr::GetKeysByString ( std::string const &  name)
677{
678 std::lock_guard<std::mutex> lock(_configLock);
679
680 std::vector<std::string> keys;
681
682 for (auto const& [optionName, key] : _configOptions)
683 {
684 if (!optionName.compare(0, name.length(), name))
685 {
686 keys.emplace_back(optionName);
687 }
688 }
689
690 return keys;
691}

◆ GetOption() [1/2]

template<>
bool ConfigMgr::GetOption ( std::string const &  name,
bool const &  def,
bool  showLogs 
) const
657{
658 std::string val = GetValueDefault(name, std::string(def ? "1" : "0"), showLogs);
659
660 auto boolVal = Acore::StringTo<bool>(val);
661 if (!boolVal)
662 {
663 if (showLogs)
664 {
665 LogWithSeverity(_policy.valueErrorSeverity, _filename,
666 "> Config: Bad value defined for name '{}', going to use '{}' instead",
667 name, def ? "true" : "false");
668 }
669
670 return def;
671 }
672
673 return *boolVal;
674}
T GetValueDefault(std::string const &name, T const &def, bool showLogs=true) const
Definition Config.cpp:534

References GetValueDefault(), and ConfigPolicy::valueErrorSeverity.

◆ GetOption() [2/2]

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

◆ GetValueDefault()

template<class T >
T ConfigMgr::GetValueDefault ( std::string const &  name,
T const &  def,
bool  showLogs = true 
) const
private
535{
536 std::string strValue;
537
538 auto const& itr = _configOptions.find(name);
539 bool notFound = itr == _configOptions.end();
540 auto envVarName = GetEnvVarName(name);
541 Optional<std::string> envVar = GetEnvFromCache(name, envVarName);
542 if (envVar)
543 {
544 // If showLogs and this key/value pair wasn't found in the currently saved config
545 if (showLogs && (notFound || itr->second != envVar->c_str()))
546 {
547 LOG_INFO("server.loading", "> Config: Found config value '{}' from environment variable '{}'.", name, envVarName );
548 AddKey(name, envVar->c_str(), "ENVIRONMENT", false, false);
549 }
550
551 strValue = *envVar;
552 }
553 else if (notFound)
554 {
555 if (showLogs)
556 {
557 bool isCritical = _criticalConfigOptions.find(name) != _criticalConfigOptions.end();
558 ConfigSeverity severity = isCritical ? _policy.criticalOptionSeverity : _policy.missingOptionSeverity;
559
560 if (isCritical)
561 {
562 LogWithSeverity(severity, _filename,
563 "> Config:\n\nFATAL ERROR: Missing property {} in config file {}, add \"{} = {}\" to this file or define '{}' as an environment variable\n\nYour server cannot start without this option!",
564 name, _filename, name, Acore::ToString(def), envVarName);
565 }
566 else
567 {
568 std::string configs = _filename;
569 if (!_moduleConfigFiles.empty())
570 configs += " or module config";
571
572 LogWithSeverity(severity, _filename,
573 "> Config: Missing property {} in config file {}, add \"{} = {}\" to this file or define '{}' as an environment variable.",
574 name, configs, name, def, envVarName);
575 }
576 }
577 return def;
578 }
579 else
580 {
581 strValue = itr->second;
582 }
583
584 auto value = Acore::StringTo<T>(strValue);
585 if (!value)
586 {
587 if (showLogs)
588 {
589 LogWithSeverity(_policy.valueErrorSeverity, _filename,
590 "> Config: Bad value defined for name '{}', going to use '{}' instead",
591 name, Acore::ToString(def));
592 }
593
594 return def;
595 }
596
597 return *value;
598}
#define LOG_INFO(filterType__,...)
Definition Log.h:166
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:24
Optional< std::string > GetEnvFromCache(std::string const &configName, std::string const &envVarName)
Definition Config.cpp:489
ConfigSeverity
Definition Config.h:27
std::string ToString(Type &&val, Params &&... params)
Definition StringConvert.h:250

References _moduleConfigFiles, ConfigPolicy::criticalOptionSeverity, GetEnvFromCache(), LOG_INFO, ConfigPolicy::missingOptionSeverity, Acore::ToString(), and ConfigPolicy::valueErrorSeverity.

Referenced by GetOption().

◆ instance()

ConfigMgr * ConfigMgr::instance ( )
static
465{
466 static ConfigMgr instance;
467 return &instance;
468}
Definition Config.h:45
static ConfigMgr * instance()
Definition Config.cpp:464

References instance().

Referenced by instance().

◆ isDryRun()

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

References dryRun.

◆ LoadAdditionalFile()

bool ConfigMgr::LoadAdditionalFile ( std::string  file,
bool  isOptional = false,
bool  isReload = false 
)
private
459{
460 std::lock_guard<std::mutex> lock(_configLock);
461 return LoadFile(file, isOptional, isReload);
462}

Referenced by LoadModulesConfigs().

◆ LoadAppConfigs()

bool ConfigMgr::LoadAppConfigs ( bool  isReload = false)
741{
742 // #1 - Load init config file .conf
743 if (!LoadInitial(_filename, isReload))
744 {
745 return false;
746 }
747
748 return true;
749}
bool LoadInitial(std::string const &file, bool isReload=false)
Method used only for loading main configuration files (authserver.conf and worldserver....
Definition Config.cpp:451

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)

452{
453 std::lock_guard<std::mutex> lock(_configLock);
454 _configOptions.clear();
455 return LoadFile(file, false, isReload);
456}

Referenced by LoadAppConfigs().

◆ LoadModulesConfigs()

bool ConfigMgr::LoadModulesConfigs ( bool  isReload = false,
bool  isNeedPrintInfo = true 
)
752{
753 if (_additonalFiles.empty())
754 {
755 // Send successful load if no found files
756 return true;
757 }
758
759 if (isNeedPrintInfo)
760 {
761 LOG_INFO("server.loading", " ");
762 LOG_INFO("server.loading", "Loading Modules Configuration...");
763 }
764
765 // Start loading module configs
766 std::string const& moduleConfigPath = GetConfigPath() + "modules/";
767
768 for (auto const& fileName : _additonalFiles)
769 {
770 bool isExistConfig = LoadAdditionalFile(moduleConfigPath + fileName, false, isReload);
771
772 if (isExistConfig)
773 _moduleConfigFiles.emplace_back(fileName);
774 }
775
776 if (isNeedPrintInfo)
777 {
778 if (!_moduleConfigFiles.empty())
779 {
780 // Print modules configurations
781 LOG_INFO("server.loading", " ");
782 LOG_INFO("server.loading", "Using modules configuration:");
783
784 for (auto const& itr : _moduleConfigFiles)
785 {
786 LOG_INFO("server.loading", "> {}", itr);
787 }
788 }
789 else
790 {
791 LOG_INFO("server.loading", "> Not found modules config files");
792 }
793 }
794
795 if (isNeedPrintInfo)
796 {
797 LOG_INFO("server.loading", " ");
798 }
799
800 return true;
801}
std::string const GetConfigPath()
Definition Config.cpp:704
bool LoadAdditionalFile(std::string file, bool isOptional=false, bool isReload=false)
Definition Config.cpp:458

References _moduleConfigFiles, GetConfigPath(), LoadAdditionalFile(), 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.

511{
512 std::lock_guard<std::mutex> lock(_configLock);
513
514 std::vector<std::string> overriddenKeys;
515
516 for (auto& itr : _configOptions)
517 {
518 if (itr.first.empty())
519 continue;
520
521 Optional<std::string> envVar = EnvVarForIniKey(itr.first);
522 if (!envVar)
523 continue;
524
525 itr.second = *envVar;
526
527 overriddenKeys.push_back(itr.first);
528 }
529
530 return overriddenKeys;
531}

Referenced by Reload().

◆ Reload()

bool ConfigMgr::Reload ( )
471{
472 if (!LoadAppConfigs(true))
473 {
474 return false;
475 }
476
477 if (!LoadModulesConfigs(true, false))
478 {
479 return false;
480 }
481
483
484 return true;
485}
std::vector< std::string > OverrideWithEnvVariablesIfAny()
Overrides configuration with environment variables and returns overridden keys.
Definition Config.cpp:510
bool LoadModulesConfigs(bool isReload=false, bool isNeedPrintInfo=true)
Definition Config.cpp:751
bool LoadAppConfigs(bool isReload=false)
Definition Config.cpp:740

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

◆ setDryRun()

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

References dryRun.

Member Data Documentation

◆ _moduleConfigFiles

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

◆ dryRun

bool ConfigMgr::dryRun = false
private

Referenced by isDryRun(), and setDryRun().


The documentation for this class was generated from the following files: