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. More...
 
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) More...
 
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 = {} 
)
550{
551 _filename = initFileName;
552 _args = std::move(args);
553
554 // Add modules config if exist
555 if (!modulesConfigList.empty())
556 {
557 for (auto const& itr : Acore::Tokenize(modulesConfigList, ',', false))
558 {
559 _additonalFiles.emplace_back(itr);
560 }
561 }
562}
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
534{
535 return _args;
536}

◆ GetConfigPath()

std::string const ConfigMgr::GetConfigPath ( )
539{
540 std::lock_guard<std::mutex> lock(_configLock);
541
542#if AC_PLATFORM == AC_PLATFORM_WINDOWS
543 return "configs/";
544#else
545 return std::string(_CONF_DIR) + "/";
546#endif
547}

Referenced by LoadModulesConfigs().

◆ GetFilename()

std::string const ConfigMgr::GetFilename ( )
528{
529 std::lock_guard<std::mutex> lock(_configLock);
530 return _filename;
531}

◆ GetKeysByString()

std::vector< std::string > ConfigMgr::GetKeysByString ( std::string const &  name)
511{
512 std::lock_guard<std::mutex> lock(_configLock);
513
514 std::vector<std::string> keys;
515
516 for (auto const& [optionName, key] : _configOptions)
517 {
518 if (!optionName.compare(0, name.length(), name))
519 {
520 keys.emplace_back(optionName);
521 }
522 }
523
524 return keys;
525}

◆ GetOption() [1/2]

template<>
bool ConfigMgr::GetOption ( std::string const &  name,
bool const &  def,
bool  showLogs 
) const
492{
493 std::string val = GetValueDefault(name, std::string(def ? "1" : "0"), showLogs);
494
495 auto boolVal = Acore::StringTo<bool>(val);
496 if (!boolVal)
497 {
498 if (showLogs)
499 {
500 LOG_ERROR("server.loading", "> Config: Bad value defined for name '{}', going to use '{}' instead",
501 name, def ? "true" : "false");
502 }
503
504 return def;
505 }
506
507 return *boolVal;
508}
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
T GetValueDefault(std::string const &name, T const &def, bool showLogs=true) const
Definition: Config.cpp:380

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
486{
487 return GetValueDefault<T>(name, def, showLogs);
488}

◆ GetValueDefault()

template<class T >
T ConfigMgr::GetValueDefault ( std::string const &  name,
T const &  def,
bool  showLogs = true 
) const
private
381{
382 std::string strValue;
383
384 auto const& itr = _configOptions.find(name);
385 bool fatalConfig = false;
386 bool notFound = itr == _configOptions.end();
387 auto envVarName = GetEnvVarName(name);
388 Optional<std::string> envVar = GetEnvFromCache(name, envVarName);
389 if (envVar)
390 {
391 // If showLogs and this key/value pair wasn't found in the currently saved config
392 if (showLogs && (notFound || itr->second != envVar->c_str()))
393 {
394 LOG_INFO("server.loading", "> Config: Found config value '{}' from environment variable '{}'.", name, envVarName );
395 AddKey(name, envVar->c_str(), "ENVIRONMENT", false, false);
396 }
397
398 strValue = *envVar;
399 }
400 else if (notFound)
401 {
402 if (showLogs)
403 {
404 for (std::string s : _fatalConfigOptions)
405 if (s == name)
406 {
407 fatalConfig = true;
408 break;
409 }
410
411 if (fatalConfig)
412 LOG_FATAL("server.loading", "> 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!",
413 name, _filename, name, Acore::ToString(def), envVarName);
414 else
415 LOG_WARN("server.loading", "> Config: Missing property {} in config file {}, add \"{} = {}\" to this file or define '{}' as an environment variable.",
416 name, _filename, name, Acore::ToString(def), envVarName);
417 }
418 return def;
419 }
420 else
421 {
422 strValue = itr->second;
423 }
424
425 auto value = Acore::StringTo<T>(strValue);
426 if (!value)
427 {
428 if (showLogs)
429 {
430 LOG_ERROR("server.loading", "> Config: Bad value defined for name '{}', going to use '{}' instead",
431 name, Acore::ToString(def));
432 }
433
434 return def;
435 }
436
437 return *value;
438}
#define LOG_FATAL(filterType__,...)
Definition: Log.h:153
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
#define LOG_WARN(filterType__,...)
Definition: Log.h:161
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
std::string GetEnvVarName(std::string const &configName)
Definition: Config.cpp:281
std::unordered_map< std::string, std::string > _configOptions
Definition: Config.cpp:34
void AddKey(std::string const &optionName, std::string const &optionKey, std::string_view fileName, bool isOptional, bool isReload)
Definition: Config.cpp:80
Optional< std::string > GetEnvFromCache(std::string const &configName, std::string const &envVarName)
Definition: Config.cpp:335

References GetEnvFromCache(), LOG_ERROR, LOG_FATAL, LOG_INFO, LOG_WARN, and Acore::ToString().

Referenced by GetOption().

◆ instance()

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

References instance().

Referenced by instance().

◆ isDryRun()

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

References dryRun.

◆ LoadAdditionalFile()

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

Referenced by LoadModulesConfigs().

◆ LoadAppConfigs()

bool ConfigMgr::LoadAppConfigs ( bool  isReload = false)
565{
566 // #1 - Load init config file .conf
567 if (!LoadInitial(_filename, isReload))
568 {
569 return false;
570 }
571
572 return true;
573}
bool LoadInitial(std::string const &file, bool isReload=false)
Method used only for loading main configuration files (authserver.conf and worldserver....
Definition: Config.cpp:297

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)

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

Referenced by LoadAppConfigs().

◆ LoadModulesConfigs()

bool ConfigMgr::LoadModulesConfigs ( bool  isReload = false,
bool  isNeedPrintInfo = true 
)
576{
577 if (_additonalFiles.empty())
578 {
579 // Send successful load if no found files
580 return true;
581 }
582
583 if (isNeedPrintInfo)
584 {
585 LOG_INFO("server.loading", " ");
586 LOG_INFO("server.loading", "Loading Modules Configuration...");
587 }
588
589 // Start loading module configs
590 std::string const& moduleConfigPath = GetConfigPath() + "modules/";
591 bool isExistDefaultConfig = true;
592 bool isExistDistConfig = true;
593
594 for (auto const& distFileName : _additonalFiles)
595 {
596 std::string defaultFileName = distFileName;
597
598 if (!defaultFileName.empty())
599 {
600 defaultFileName.erase(defaultFileName.end() - 5, defaultFileName.end());
601 }
602
603 // Load .conf.dist config
604 isExistDistConfig = LoadAdditionalFile(moduleConfigPath + distFileName, false, isReload);
605
606 if (!isReload && !isExistDistConfig)
607 {
608 LOG_FATAL("server.loading", "> ConfigMgr::LoadModulesConfigs: Not found original config '{}'. Stop loading", distFileName);
609 ABORT();
610 }
611
612 // Load .conf config
613 isExistDefaultConfig = LoadAdditionalFile(moduleConfigPath + defaultFileName, true, isReload);
614
615 if (isExistDefaultConfig && isExistDistConfig)
616 {
617 _moduleConfigFiles.emplace_back(defaultFileName);
618 }
619 else if (!isExistDefaultConfig && isExistDistConfig)
620 {
621 _moduleConfigFiles.emplace_back(distFileName);
622 }
623 }
624
625 if (isNeedPrintInfo)
626 {
627 if (!_moduleConfigFiles.empty())
628 {
629 // Print modules configurations
630 LOG_INFO("server.loading", " ");
631 LOG_INFO("server.loading", "Using modules configuration:");
632
633 for (auto const& itr : _moduleConfigFiles)
634 {
635 LOG_INFO("server.loading", "> {}", itr);
636 }
637 }
638 else
639 {
640 LOG_INFO("server.loading", "> Not found modules config files");
641 }
642 }
643
644 if (isNeedPrintInfo)
645 {
646 LOG_INFO("server.loading", " ");
647 }
648
649 return true;
650}
#define ABORT
Definition: Errors.h:76
std::string const GetConfigPath()
Definition: Config.cpp:538
bool LoadAdditionalFile(std::string file, bool isOptional=false, bool isReload=false)
Definition: Config.cpp:304
std::vector< std::string > _moduleConfigFiles
Definition: Config.h:65

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.

357{
358 std::lock_guard<std::mutex> lock(_configLock);
359
360 std::vector<std::string> overriddenKeys;
361
362 for (auto& itr : _configOptions)
363 {
364 if (itr.first.empty())
365 continue;
366
367 Optional<std::string> envVar = EnvVarForIniKey(itr.first);
368 if (!envVar)
369 continue;
370
371 itr.second = *envVar;
372
373 overriddenKeys.push_back(itr.first);
374 }
375
376 return overriddenKeys;
377}
Optional< std::string > EnvVarForIniKey(std::string const &key)
Definition: Config.cpp:286

Referenced by Reload().

◆ Reload()

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

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

◆ setDryRun()

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