Legends-of-Azeroth-Pandaria-5.4.8
340 строк · 9.9 Кб
1/*
2* This file is part of the Pandaria 5.4.8 Project. See THANKS file for Copyright information
3*
4* This program is free software; you can redistribute it and/or modify it
5* under the terms of the GNU General Public License as published by the
6* Free Software Foundation; either version 2 of the License, or (at your
7* option) any later version.
8*
9* This program is distributed in the hope that it will be useful, but WITHOUT
10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12* more details.
13*
14* You should have received a copy of the GNU General Public License along
15* with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "Config.h"
19#include "Log.h"
20#include "StringConvert.h"
21#include "Util.h"
22#include <boost/property_tree/ini_parser.hpp>
23#include <algorithm>
24#include <cstdlib>
25#include <memory>
26#include <mutex>
27
28namespace bpt = boost::property_tree;
29
30namespace
31{
32std::string _filename;
33std::vector<std::string> _additonalFiles;
34std::vector<std::string> _args;
35bpt::ptree _config;
36std::mutex _configLock;
37
38bool LoadFile(std::string const& file, bpt::ptree& fullTree, std::string& error)
39{
40try
41{
42bpt::ini_parser::read_ini(file, fullTree);
43
44if (fullTree.empty())
45{
46error = "empty file (" + file + ")";
47return false;
48}
49}
50catch (bpt::ini_parser::ini_parser_error const& e)
51{
52if (e.line() == 0)
53error = e.message() + " (" + e.filename() + ")";
54else
55error = e.message() + " (" + e.filename() + ":" + std::to_string(e.line()) + ")";
56return false;
57}
58
59return true;
60}
61
62// Converts ini keys to the environment variable key (upper snake case).
63// Example of conversions:
64// SomeConfig => SOME_CONFIG
65// myNestedConfig.opt1 => MY_NESTED_CONFIG_OPT_1
66// LogDB.Opt.ClearTime => LOG_DB_OPT_CLEAR_TIME
67std::string IniKeyToEnvVarKey(std::string const& key)
68{
69std::string result;
70
71const char *str = key.c_str();
72size_t n = key.length();
73
74char curr;
75bool isEnd;
76bool nextIsUpper;
77bool currIsNumeric;
78bool nextIsNumeric;
79
80for (size_t i = 0; i < n; ++i)
81{
82curr = str[i];
83if (curr == ' ' || curr == '.' || curr == '-')
84{
85result += '_';
86continue;
87}
88
89isEnd = i == n - 1;
90if (!isEnd)
91{
92nextIsUpper = isupper(str[i + 1]);
93
94// handle "aB" to "A_B"
95if (!isupper(curr) && nextIsUpper)
96{
97result += static_cast<char>(std::toupper(curr));
98result += '_';
99continue;
100}
101
102currIsNumeric = isNumeric(curr);
103nextIsNumeric = isNumeric(str[i + 1]);
104
105// handle "a1" to "a_1"
106if (!currIsNumeric && nextIsNumeric)
107{
108result += static_cast<char>(std::toupper(curr));
109result += '_';
110continue;
111}
112
113// handle "1a" to "1_a"
114if (currIsNumeric && !nextIsNumeric)
115{
116result += static_cast<char>(std::toupper(curr));
117result += '_';
118continue;
119}
120}
121
122result += static_cast<char>(std::toupper(curr));
123}
124return result;
125}
126
127Optional<std::string> EnvVarForIniKey(std::string const& key)
128{
129std::string envKey = "TC_" + IniKeyToEnvVarKey(key);
130char* val = std::getenv(envKey.c_str());
131if (!val)
132return std::nullopt;
133
134return std::string(val);
135}
136}
137
138bool ConfigMgr::LoadInitial(std::string file, std::vector<std::string> args,
139std::string& error)
140{
141std::lock_guard<std::mutex> lock(_configLock);
142
143_filename = std::move(file);
144_args = std::move(args);
145
146bpt::ptree fullTree;
147if (!LoadFile(_filename, fullTree, error))
148return false;
149
150// Since we're using only one section per config file, we skip the section and have direct property access
151_config = fullTree.begin()->second;
152
153return true;
154}
155
156bool ConfigMgr::LoadAdditionalFile(std::string file, bool keepOnReload, std::string& error)
157{
158bpt::ptree fullTree;
159if (!LoadFile(file, fullTree, error))
160return false;
161
162for (bpt::ptree::value_type const& child : fullTree.begin()->second)
163_config.put_child(bpt::ptree::path_type(child.first, '/'), child.second);
164
165if (keepOnReload)
166_additonalFiles.emplace_back(std::move(file));
167
168return true;
169}
170
171std::vector<std::string> ConfigMgr::OverrideWithEnvVariablesIfAny()
172{
173std::lock_guard<std::mutex> lock(_configLock);
174
175std::vector<std::string> overriddenKeys;
176
177for (bpt::ptree::value_type& itr: _config)
178{
179if (!itr.second.empty() || itr.first.empty())
180continue;
181
182Optional<std::string> envVar = EnvVarForIniKey(itr.first);
183if (!envVar)
184continue;
185
186itr.second = bpt::ptree(*envVar);
187
188overriddenKeys.push_back(itr.first);
189}
190
191return overriddenKeys;
192}
193
194ConfigMgr* ConfigMgr::instance()
195{
196static ConfigMgr instance;
197return &instance;
198}
199
200bool ConfigMgr::Reload(std::vector<std::string>& errors)
201{
202std::string error;
203if (!LoadInitial(_filename, std::move(_args), error))
204errors.push_back(std::move(error));
205
206for (std::string const& additionalFile : _additonalFiles)
207if (!LoadAdditionalFile(additionalFile, false, error))
208errors.push_back(std::move(error));
209
210OverrideWithEnvVariablesIfAny();
211
212return errors.empty();
213}
214
215template<class T>
216T ConfigMgr::GetValueDefault(std::string const& name, T def, bool quiet) const
217{
218try
219{
220return _config.get<T>(bpt::ptree::path_type(name, '/'));
221}
222catch (bpt::ptree_bad_path const&)
223{
224Optional<std::string> envVar = EnvVarForIniKey(name);
225if (envVar)
226{
227Optional<T> castedVar = Trinity::StringTo<T>(*envVar);
228if (!castedVar)
229{
230TC_LOG_ERROR("server.loading", "Bad value defined for name %s in environment variables, going to use default instead", name.c_str());
231return def;
232}
233
234if (!quiet)
235TC_LOG_WARN("server.loading", "Missing name %s in config file %s, recovered with environment '%s' value.", name.c_str(), _filename.c_str(), envVar->c_str());
236
237return *castedVar;
238}
239else if (!quiet)
240{
241TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file",
242name.c_str(), _filename.c_str(), name.c_str(), std::to_string(def).c_str());
243}
244}
245catch (bpt::ptree_bad_data const&)
246{
247TC_LOG_ERROR("server.loading", "Bad value defined for name %s in config file %s, going to use %s instead",
248name.c_str(), _filename.c_str(), std::to_string(def).c_str());
249}
250
251return def;
252}
253
254template<>
255std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std::string def, bool quiet) const
256{
257try
258{
259return _config.get<std::string>(bpt::ptree::path_type(name, '/'));
260}
261catch (bpt::ptree_bad_path const&)
262{
263Optional<std::string> envVar = EnvVarForIniKey(name);
264if (envVar)
265{
266if (!quiet)
267TC_LOG_WARN("server.loading", "Missing name %s in config file %s, recovered with environment '%s' value.", name.c_str(), _filename.c_str(), envVar->c_str());
268
269return *envVar;
270}
271else if (!quiet)
272{
273TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file",
274name.c_str(), _filename.c_str(), name.c_str(), def.c_str());
275}
276}
277catch (bpt::ptree_bad_data const&)
278{
279TC_LOG_ERROR("server.loading", "Bad value defined for name %s in config file %s, going to use %s instead",
280name.c_str(), _filename.c_str(), def.c_str());
281}
282
283return def;
284}
285
286std::string ConfigMgr::GetStringDefault(std::string const& name, const std::string& def, bool quiet) const
287{
288std::string val = GetValueDefault(name, def, quiet);
289val.erase(std::remove(val.begin(), val.end(), '"'), val.end());
290return val;
291}
292
293bool ConfigMgr::GetBoolDefault(std::string const& name, bool def, bool quiet) const
294{
295std::string val = GetValueDefault(name, std::string(def ? "1" : "0"), quiet);
296val.erase(std::remove(val.begin(), val.end(), '"'), val.end());
297Optional<bool> boolVal = Trinity::StringTo<bool>(val);
298if (boolVal)
299return *boolVal;
300else
301{
302TC_LOG_ERROR("server.loading", "Bad value defined for name %s in config file %s, going to use '%s' instead",
303name.c_str(), _filename.c_str(), def ? "true" : "false");
304return def;
305}
306}
307
308int ConfigMgr::GetIntDefault(std::string const& name, int def, bool quiet) const
309{
310return GetValueDefault(name, def, quiet);
311}
312
313float ConfigMgr::GetFloatDefault(std::string const& name, float def, bool quiet) const
314{
315return GetValueDefault(name, def, quiet);
316}
317
318std::string const& ConfigMgr::GetFilename()
319{
320std::lock_guard<std::mutex> lock(_configLock);
321return _filename;
322}
323
324std::vector<std::string> const& ConfigMgr::GetArguments() const
325{
326return _args;
327}
328
329std::vector<std::string> ConfigMgr::GetKeysByString(std::string const& name)
330{
331std::lock_guard<std::mutex> lock(_configLock);
332
333std::vector<std::string> keys;
334
335for (bpt::ptree::value_type const& child : _config)
336if (child.first.compare(0, name.length(), name) == 0)
337keys.push_back(child.first);
338
339return keys;
340}
341