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
 

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 
)
73 {
74 auto const& itr = _configOptions.find(optionName);
75
76 // Check old option
77 if (isOptional && itr == _configOptions.end())
78 {
79 if (!IsLoggingSystemOptions(optionName) && !isReload)
80 {
81 PrintError(fileName, "> Config::LoadFile: Found incorrect option '{}' in config file '{}'. Skip", optionName, fileName);
82
83#ifdef CONFIG_ABORT_INCORRECT_OPTIONS
84 ABORT("> Core can't start if found incorrect options");
85#endif
86
87 return;
88 }
89 }
90
91 // Check exit option
92 if (itr != _configOptions.end())
93 {
94 _configOptions.erase(optionName);
95 }
96
97 _configOptions.emplace(optionName, optionKey);
98 }
#define ABORT
Definition: Errors.h:76
void PrintError(std::string_view filename, Format &&fmt, Args &&... args)
Definition: Config.cpp:58
std::unordered_map< std::string, std::string > _configOptions
Definition: Config.cpp:34
bool IsLoggingSystemOptions(std::string_view optionName)
Definition: Config.cpp:49

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)
293 {
294 std::string envKey = GetEnvVarName(key);
295 char* val = std::getenv(envKey.c_str());
296 if (!val)
297 return std::nullopt;
298
299 return std::string(val);
300 }
std::string GetEnvVarName(std::string const &configName)
Definition: Config.cpp:287

References GetEnvVarName().

Referenced by GetEnvFromCache().

◆ GetEnvVarName()

std::string anonymous_namespace{Config.cpp}::GetEnvVarName ( std::string const &  configName)
288 {
289 return "AC_" + IniKeyToEnvVarKey(configName);
290 }
std::string IniKeyToEnvVarKey(std::string const &key)
Definition: Config.cpp:227

References IniKeyToEnvVarKey().

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

◆ IniKeyToEnvVarKey()

std::string anonymous_namespace{Config.cpp}::IniKeyToEnvVarKey ( std::string const &  key)
228 {
229 std::string result;
230
231 const char* str = key.c_str();
232 size_t n = key.length();
233
234 char curr;
235 bool isEnd;
236 bool nextIsUpper;
237 bool currIsNumeric;
238 bool nextIsNumeric;
239
240 for (size_t i = 0; i < n; ++i)
241 {
242 curr = str[i];
243 if (curr == ' ' || curr == '.' || curr == '-')
244 {
245 result += '_';
246 continue;
247 }
248
249 isEnd = i == n - 1;
250 if (!isEnd)
251 {
252 nextIsUpper = isupper(str[i + 1]);
253
254 // handle "aB" to "A_B"
255 if (!isupper(curr) && nextIsUpper)
256 {
257 result += static_cast<char>(std::toupper(curr));
258 result += '_';
259 continue;
260 }
261
262 currIsNumeric = isNumeric(curr);
263 nextIsNumeric = isNumeric(str[i + 1]);
264
265 // handle "a1" to "a_1"
266 if (!currIsNumeric && nextIsNumeric)
267 {
268 result += static_cast<char>(std::toupper(curr));
269 result += '_';
270 continue;
271 }
272
273 // handle "1a" to "1_a"
274 if (currIsNumeric && !nextIsNumeric)
275 {
276 result += static_cast<char>(std::toupper(curr));
277 result += '_';
278 continue;
279 }
280 }
281
282 result += static_cast<char>(std::toupper(curr));
283 }
284 return result;
285 }
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)
40 {
41 size_t foundAuth = fileName.find("authserver.conf");
42 size_t foundWorld = fileName.find("worldserver.conf");
43 size_t foundImport = fileName.find("dbimport.conf");
44
45 return foundAuth != std::string_view::npos || foundWorld != std::string_view::npos || foundImport != std::string_view::npos;
46 }

Referenced by PrintError().

◆ IsLoggingSystemOptions()

bool anonymous_namespace{Config.cpp}::IsLoggingSystemOptions ( std::string_view  optionName)
50 {
51 size_t foundAppender = optionName.find("Appender.");
52 size_t foundLogger = optionName.find("Logger.");
53
54 return foundAppender != std::string_view::npos || foundLogger != std::string_view::npos;
55 }

Referenced by AddKey().

◆ LoadFile()

bool anonymous_namespace{Config.cpp}::LoadFile ( std::string const &  file,
bool  isOptional,
bool  isReload 
)
209 {
210 try
211 {
212 return ParseFile(file, isOptional, isReload);
213 }
214 catch (const std::exception& e)
215 {
216 PrintError(file, "> {}", e.what());
217 }
218
219 return false;
220 }
bool ParseFile(std::string const &file, bool isOptional, bool isReload)
Definition: Config.cpp:100

References ParseFile(), and PrintError().

◆ ParseFile()

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

References AddKey(), PrintError(), Acore::StringFormatFmt(), 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
59 {
60 std::string message = Acore::StringFormatFmt(std::forward<Format>(fmt), std::forward<Args>(args)...);
61
62 if (IsAppConfig(filename))
63 {
64 fmt::print("{}\n", message);
65 }
66 else
67 {
68 LOG_ERROR("server.loading", message);
69 }
70 }
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
bool IsAppConfig(std::string_view fileName)
Definition: Config.cpp:39

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

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

◆ _filename

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