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 = {} 
)
532{
533 _filename = initFileName;
534 _args = std::move(args);
535
536 // Add modules config if exist
537 if (!modulesConfigList.empty())
538 {
539 for (auto const& itr : Acore::Tokenize(modulesConfigList, ',', false))
540 {
541 _additonalFiles.emplace_back(itr);
542 }
543 }
544}
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
516{
517 return _args;
518}

◆ GetBoolDefault()

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

◆ GetConfigPath()

std::string const ConfigMgr::GetConfigPath ( )
521{
522 std::lock_guard<std::mutex> lock(_configLock);
523
524#if AC_PLATFORM == AC_PLATFORM_WINDOWS
525 return "configs/";
526#else
527 return std::string(_CONF_DIR) + "/";
528#endif
529}

Referenced by LoadModulesConfigs().

◆ GetFilename()

std::string const ConfigMgr::GetFilename ( )
510{
511 std::lock_guard<std::mutex> lock(_configLock);
512 return _filename;
513}

◆ GetFloatDefault()

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

◆ GetIntDefault()

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

◆ GetKeysByString()

std::vector< std::string > ConfigMgr::GetKeysByString ( std::string const &  name)
493{
494 std::lock_guard<std::mutex> lock(_configLock);
495
496 std::vector<std::string> keys;
497
498 for (auto const& [optionName, key] : _configOptions)
499 {
500 if (!optionName.compare(0, name.length(), name))
501 {
502 keys.emplace_back(optionName);
503 }
504 }
505
506 return keys;
507}

◆ GetOption() [1/2]

template<>
bool ConfigMgr::GetOption ( std::string const &  name,
bool const &  def,
bool  showLogs 
) const
474{
475 std::string val = GetValueDefault(name, std::string(def ? "1" : "0"), showLogs);
476
477 auto boolVal = Acore::StringTo<bool>(val);
478 if (!boolVal)
479 {
480 if (showLogs)
481 {
482 LOG_ERROR("server.loading", "> Config: Bad value defined for name '{}', going to use '{}' instead",
483 name, def ? "true" : "false");
484 }
485
486 return def;
487 }
488
489 return *boolVal;
490}
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
T GetValueDefault(std::string const &name, T const &def, bool showLogs=true) const
Definition: Config.cpp:386

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
468{
469 return GetValueDefault<T>(name, def, showLogs);
470}

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

◆ GetValueDefault()

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

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

Referenced by GetOption().

◆ instance()

ConfigMgr * ConfigMgr::instance ( )
static
317{
318 static ConfigMgr instance;
319 return &instance;
320}
Definition: Config.h:27
static ConfigMgr * instance()
Definition: Config.cpp:316

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
311{
312 std::lock_guard<std::mutex> lock(_configLock);
313 return LoadFile(file, isOptional, isReload);
314}
bool LoadFile(std::string const &file, bool isOptional, bool isReload)
Definition: Config.cpp:208

Referenced by LoadModulesConfigs().

◆ LoadAppConfigs()

bool ConfigMgr::LoadAppConfigs ( bool  isReload = false)
547{
548 // #1 - Load init config file .conf
549 if (!LoadInitial(_filename, isReload))
550 {
551 return false;
552 }
553
554 return true;
555}
bool LoadInitial(std::string const &file, bool isReload=false)
Method used only for loading main configuration files (authserver.conf and worldserver....
Definition: Config.cpp:303

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)

304{
305 std::lock_guard<std::mutex> lock(_configLock);
306 _configOptions.clear();
307 return LoadFile(file, false, isReload);
308}

Referenced by LoadAppConfigs().

◆ LoadModulesConfigs()

bool ConfigMgr::LoadModulesConfigs ( bool  isReload = false,
bool  isNeedPrintInfo = true 
)
558{
559 if (_additonalFiles.empty())
560 {
561 // Send successful load if no found files
562 return true;
563 }
564
565 if (isNeedPrintInfo)
566 {
567 LOG_INFO("server.loading", " ");
568 LOG_INFO("server.loading", "Loading Modules Configuration...");
569 }
570
571 // Start loading module configs
572 std::string const& moduleConfigPath = GetConfigPath() + "modules/";
573 bool isExistDefaultConfig = true;
574 bool isExistDistConfig = true;
575
576 for (auto const& distFileName : _additonalFiles)
577 {
578 std::string defaultFileName = distFileName;
579
580 if (!defaultFileName.empty())
581 {
582 defaultFileName.erase(defaultFileName.end() - 5, defaultFileName.end());
583 }
584
585 // Load .conf.dist config
586 isExistDistConfig = LoadAdditionalFile(moduleConfigPath + distFileName, false, isReload);
587
588 if (!isReload && !isExistDistConfig)
589 {
590 LOG_FATAL("server.loading", "> ConfigMgr::LoadModulesConfigs: Not found original config '{}'. Stop loading", distFileName);
591 ABORT();
592 }
593
594 // Load .conf config
595 isExistDefaultConfig = LoadAdditionalFile(moduleConfigPath + defaultFileName, true, isReload);
596
597 if (isExistDefaultConfig && isExistDistConfig)
598 {
599 _moduleConfigFiles.emplace_back(defaultFileName);
600 }
601 else if (!isExistDefaultConfig && isExistDistConfig)
602 {
603 _moduleConfigFiles.emplace_back(distFileName);
604 }
605 }
606
607 if (isNeedPrintInfo)
608 {
609 if (!_moduleConfigFiles.empty())
610 {
611 // Print modules configurations
612 LOG_INFO("server.loading", " ");
613 LOG_INFO("server.loading", "Using modules configuration:");
614
615 for (auto const& itr : _moduleConfigFiles)
616 {
617 LOG_INFO("server.loading", "> {}", itr);
618 }
619 }
620 else
621 {
622 LOG_INFO("server.loading", "> Not found modules config files");
623 }
624 }
625
626 if (isNeedPrintInfo)
627 {
628 LOG_INFO("server.loading", " ");
629 }
630
631 return true;
632}
#define ABORT
Definition: Errors.h:76
#define LOG_FATAL(filterType__,...)
Definition: Log.h:153
std::string const GetConfigPath()
Definition: Config.cpp:520
bool LoadAdditionalFile(std::string file, bool isOptional=false, bool isReload=false)
Definition: Config.cpp:310
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.

363{
364 std::lock_guard<std::mutex> lock(_configLock);
365
366 std::vector<std::string> overriddenKeys;
367
368 for (auto& itr : _configOptions)
369 {
370 if (itr.first.empty())
371 continue;
372
373 Optional<std::string> envVar = EnvVarForIniKey(itr.first);
374 if (!envVar)
375 continue;
376
377 itr.second = *envVar;
378
379 overriddenKeys.push_back(itr.first);
380 }
381
382 return overriddenKeys;
383}
Optional< std::string > EnvVarForIniKey(std::string const &key)
Definition: Config.cpp:292

Referenced by Reload().

◆ Reload()

bool ConfigMgr::Reload ( )
323{
324 if (!LoadAppConfigs(true))
325 {
326 return false;
327 }
328
329 if (!LoadModulesConfigs(true, false))
330 {
331 return false;
332 }
333
335
336 return true;
337}
std::vector< std::string > OverrideWithEnvVariablesIfAny()
Overrides configuration with environment variables and returns overridden keys.
Definition: Config.cpp:362
bool LoadModulesConfigs(bool isReload=false, bool isNeedPrintInfo=true)
Definition: Config.cpp:557
bool LoadAppConfigs(bool isReload=false)
Definition: Config.cpp:546

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().