AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
anonymous_namespace{Config.cpp} Namespace Reference

Functions

bool IsAppConfig (std::string_view fileName)
 
bool IsLoggingSystemOptions (std::string_view optionName)
 
template<typename Format , typename... Args>
void PrintError (std::string_view filename, Format &&fmt, Args &&... args)
 
void AddKey (std::string const &optionName, std::string const &optionKey, std::string_view fileName, bool isOptional, bool isReload)
 
bool ParseFile (std::string const &file, bool isOptional, bool isReload)
 
bool LoadFile (std::string const &file, bool isOptional, bool isReload)
 
std::string IniKeyToEnvVarKey (std::string const &key)
 
std::string GetEnvVarName (std::string const &configName)
 
Optional< std::string > EnvVarForIniKey (std::string const &key)
 

Variables

std::string _filename
 
std::vector< std::string > _additonalFiles
 
std::vector< std::string > _args
 
std::unordered_map< std::string, std::string > _configOptions
 
std::unordered_map< std::string, std::string > _envVarCache
 
std::mutex _configLock
 
std::vector< std::string > _fatalConfigOptions
 

Function Documentation

◆ AddKey()

void anonymous_namespace{Config.cpp}::AddKey ( std::string const &  optionName,
std::string const &  optionKey,
std::string_view  fileName,
bool  isOptional,
bool  isReload 
)
81 {
82 auto const& itr = _configOptions.find(optionName);
83
84 // Check old option
85 if (isOptional && itr == _configOptions.end())
86 {
87 if (!IsLoggingSystemOptions(optionName) && !isReload)
88 {
89 PrintError(fileName, "> Config::LoadFile: Found incorrect option '{}' in config file '{}'. Skip", optionName, fileName);
90
91#ifdef CONFIG_ABORT_INCORRECT_OPTIONS
92 ABORT("> Core can't start if found incorrect options");
93#endif
94
95 return;
96 }
97 }
98
99 // Check exit option
100 if (itr != _configOptions.end())
101 {
102 _configOptions.erase(optionName);
103 }
104
105 _configOptions.emplace(optionName, optionKey);
106 }
#define ABORT
Definition: Errors.h:76
void PrintError(std::string_view filename, Format &&fmt, Args &&... args)
Definition: Config.cpp:66
std::unordered_map< std::string, std::string > _configOptions
Definition: Config.cpp:34
bool IsLoggingSystemOptions(std::string_view optionName)
Definition: Config.cpp:57

References _configOptions, ABORT, IsLoggingSystemOptions(), and PrintError().

Referenced by ConfigMgr::GetValueDefault< std::string >(), and ParseFile().

◆ EnvVarForIniKey()

Optional< std::string > anonymous_namespace{Config.cpp}::EnvVarForIniKey ( std::string const &  key)
287 {
288 std::string envKey = GetEnvVarName(key);
289 char* val = std::getenv(envKey.c_str());
290 if (!val)
291 return std::nullopt;
292
293 return std::string(val);
294 }
std::string GetEnvVarName(std::string const &configName)
Definition: Config.cpp:281

References GetEnvVarName().

Referenced by GetEnvFromCache().

◆ GetEnvVarName()

std::string anonymous_namespace{Config.cpp}::GetEnvVarName ( std::string const &  configName)
282 {
283 return "AC_" + IniKeyToEnvVarKey(configName);
284 }
std::string IniKeyToEnvVarKey(std::string const &key)
Definition: Config.cpp:221

References IniKeyToEnvVarKey().

Referenced by ConfigMgr::GetValueDefault< std::string >(), and EnvVarForIniKey().

◆ IniKeyToEnvVarKey()

std::string anonymous_namespace{Config.cpp}::IniKeyToEnvVarKey ( std::string const &  key)
222 {
223 std::string result;
224
225 const char* str = key.c_str();
226 std::size_t n = key.length();
227
228 char curr;
229 bool isEnd;
230 bool nextIsUpper;
231 bool currIsNumeric;
232 bool nextIsNumeric;
233
234 for (std::size_t i = 0; i < n; ++i)
235 {
236 curr = str[i];
237 if (curr == ' ' || curr == '.' || curr == '-')
238 {
239 result += '_';
240 continue;
241 }
242
243 isEnd = i == n - 1;
244 if (!isEnd)
245 {
246 nextIsUpper = isupper(str[i + 1]);
247
248 // handle "aB" to "A_B"
249 if (!isupper(curr) && nextIsUpper)
250 {
251 result += static_cast<char>(std::toupper(curr));
252 result += '_';
253 continue;
254 }
255
256 currIsNumeric = isNumeric(curr);
257 nextIsNumeric = isNumeric(str[i + 1]);
258
259 // handle "a1" to "a_1"
260 if (!currIsNumeric && nextIsNumeric)
261 {
262 result += static_cast<char>(std::toupper(curr));
263 result += '_';
264 continue;
265 }
266
267 // handle "1a" to "1_a"
268 if (currIsNumeric && !nextIsNumeric)
269 {
270 result += static_cast<char>(std::toupper(curr));
271 result += '_';
272 continue;
273 }
274 }
275
276 result += static_cast<char>(std::toupper(curr));
277 }
278 return result;
279 }
bool isNumeric(wchar_t wchar)
Definition: Util.h:204

References isNumeric().

Referenced by GetEnvVarName().

◆ IsAppConfig()

bool anonymous_namespace{Config.cpp}::IsAppConfig ( std::string_view  fileName)
48 {
49 std::size_t foundAuth = fileName.find("authserver.conf");
50 std::size_t foundWorld = fileName.find("worldserver.conf");
51 std::size_t foundImport = fileName.find("dbimport.conf");
52
53 return foundAuth != std::string_view::npos || foundWorld != std::string_view::npos || foundImport != std::string_view::npos;
54 }

Referenced by PrintError().

◆ IsLoggingSystemOptions()

bool anonymous_namespace{Config.cpp}::IsLoggingSystemOptions ( std::string_view  optionName)
58 {
59 std::size_t foundAppender = optionName.find("Appender.");
60 std::size_t foundLogger = optionName.find("Logger.");
61
62 return foundAppender != std::string_view::npos || foundLogger != std::string_view::npos;
63 }

Referenced by AddKey().

◆ LoadFile()

bool anonymous_namespace{Config.cpp}::LoadFile ( std::string const &  file,
bool  isOptional,
bool  isReload 
)
203 {
204 try
205 {
206 return ParseFile(file, isOptional, isReload);
207 }
208 catch (const std::exception& e)
209 {
210 PrintError(file, "> {}", e.what());
211 }
212
213 return false;
214 }
bool ParseFile(std::string const &file, bool isOptional, bool isReload)
Definition: Config.cpp:108

References ParseFile(), and PrintError().

◆ ParseFile()

bool anonymous_namespace{Config.cpp}::ParseFile ( std::string const &  file,
bool  isOptional,
bool  isReload 
)
109 {
110 std::ifstream in(file);
111
112 if (in.fail())
113 {
114 if (isOptional)
115 {
116 // No display erorr if file optional
117 return false;
118 }
119
120 throw ConfigException(Acore::StringFormat("Config::LoadFile: Failed open {}file '{}'", isOptional ? "optional " : "", file));
121 }
122
123 uint32 count = 0;
124 uint32 lineNumber = 0;
125 std::unordered_map<std::string /*name*/, std::string /*value*/> fileConfigs;
126
127 auto IsDuplicateOption = [&](std::string const& confOption)
128 {
129 auto const& itr = fileConfigs.find(confOption);
130 if (itr != fileConfigs.end())
131 {
132 PrintError(file, "> Config::LoadFile: Duplicate key name '{}' in config file '{}'", confOption, file);
133 return true;
134 }
135
136 return false;
137 };
138
139 while (in.good())
140 {
141 lineNumber++;
142 std::string line;
143 std::getline(in, line);
144
145 // read line error
146 if (!in.good() && !in.eof())
147 throw ConfigException(Acore::StringFormat("> Config::LoadFile: Failure to read line number {} in file '{}'", lineNumber, file));
148
149 // remove whitespace in line
150 line = Acore::String::Trim(line, in.getloc());
151
152 if (line.empty())
153 continue;
154
155 // comments and headers
156 if (line[0] == '#' || line[0] == '[')
157 continue;
158
159 auto const equal_pos = line.find('=');
160
161 if (equal_pos == std::string::npos || equal_pos == line.length())
162 {
163 PrintError(file, "> Config::LoadFile: Failure to read line number {} in file '{}'. Skip this line", lineNumber, file);
164 continue;
165 }
166
167 auto entry = Acore::String::Trim(line.substr(0, equal_pos), in.getloc());
168 auto value = Acore::String::Trim(line.substr(equal_pos + 1, std::string::npos), in.getloc());
169
170 value.erase(std::remove(value.begin(), value.end(), '"'), value.end());
171
172 // Skip if 2+ same options in one config file
173 if (IsDuplicateOption(entry))
174 continue;
175
176 // Add to temp container
177 fileConfigs.emplace(entry, value);
178 count++;
179 }
180
181 // No lines read
182 if (!count)
183 {
184 if (isOptional)
185 {
186 // No display erorr if file optional
187 return false;
188 }
189
190 throw ConfigException(Acore::StringFormat("Config::LoadFile: Empty file '{}'", file));
191 }
192
193 // Add correct keys if file load without errors
194 for (auto const& [entry, key] : fileConfigs)
195 {
196 AddKey(entry, key, file, isOptional, isReload);
197 }
198
199 return true;
200 }
std::uint32_t uint32
Definition: Define.h:107
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default AC string format function.
Definition: StringFormat.h:34
void AddKey(std::string const &optionName, std::string const &optionKey, std::string_view fileName, bool isOptional, bool isReload)
Definition: Config.cpp:80
AC_COMMON_API Str Trim(const Str &s, const std::locale &loc=std::locale())
Definition: StringFormat.cpp:22
Definition: Config.h:69

References AddKey(), PrintError(), Acore::StringFormat(), and Acore::String::Trim().

Referenced by LoadFile().

◆ PrintError()

template<typename Format , typename... Args>
void anonymous_namespace{Config.cpp}::PrintError ( std::string_view  filename,
Format &&  fmt,
Args &&...  args 
)
inline
67 {
68 std::string message = Acore::StringFormat(std::forward<Format>(fmt), std::forward<Args>(args)...);
69
70 if (IsAppConfig(filename))
71 {
72 fmt::print("{}\n", message);
73 }
74 else
75 {
76 LOG_ERROR("server.loading", message);
77 }
78 }
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
bool IsAppConfig(std::string_view fileName)
Definition: Config.cpp:47

References IsAppConfig(), LOG_ERROR, and Acore::StringFormat().

Referenced by AddKey(), LoadFile(), and ParseFile().

Variable Documentation

◆ _additonalFiles

std::vector<std::string> anonymous_namespace{Config.cpp}::_additonalFiles

◆ _args

std::vector<std::string> anonymous_namespace{Config.cpp}::_args

◆ _configLock

std::mutex anonymous_namespace{Config.cpp}::_configLock

◆ _configOptions

std::unordered_map<std::string , std::string > anonymous_namespace{Config.cpp}::_configOptions

◆ _envVarCache

std::unordered_map<std::string , std::string > anonymous_namespace{Config.cpp}::_envVarCache

Referenced by GetEnvFromCache().

◆ _fatalConfigOptions

std::vector<std::string> anonymous_namespace{Config.cpp}::_fatalConfigOptions
Initial value:
=
{
{ "RealmID" },
{ "LoginDatabaseInfo" },
{ "WorldDatabaseInfo" },
{ "CharacterDatabaseInfo" },
}

Referenced by ConfigMgr::GetValueDefault< std::string >().

◆ _filename

std::string anonymous_namespace{Config.cpp}::_filename