1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CONFIGVALUECACHE_H
#define CONFIGVALUECACHE_H
#include "Common.h"
#include "Config.h"
#include "Errors.h"
#include "Log.h"
#include <variant>
template<typename ConfigEnum>
class ConfigValueCache
{
static_assert(std::is_enum_v<ConfigEnum>);
public:
enum class Reloadable : bool
{
No = false,
Yes = true
};
ConfigValueCache(ConfigEnum const configCount)
{
_configs.resize(static_cast<uint32>(configCount));
_reloading = false;
}
void Initialize(bool reload)
{
_reloading = reload;
BuildConfigCache();
_reloading = false;
VerifyAllConfigsLoaded();
}
template<class T>
void SetConfigValue(ConfigEnum const config, std::string const& configName, T const& defaultValue, Reloadable reloadable = Reloadable::Yes, std::function<bool(T const& value)>&& checker = {}, std::string const& validationErrorText = "")
{
uint32 const configIndex = static_cast<uint32>(config);
ASSERT(configIndex < _configs.size(), "Config index out of bounds");
T const& configValue = sConfigMgr->GetOption<T>(configName, defaultValue);
bool configValueChanged = false;
if (_reloading)
{
if (std::get<T>(_configs[configIndex]) != configValue)
configValueChanged = true;
if (reloadable == Reloadable::No)
{
if (configValueChanged)
LOG_ERROR("server.loading", "Server Config (Name: {}) cannot be changed by reload. A server restart is required to update this config value.", configName);
return;
}
}
else
ASSERT(_configs[configIndex].index() == 0, "Config overwriting an existing value");
if (checker && !checker(configValue))
{
LOG_ERROR("server.loading", "Server Config (Name: {}) failed validation check '{}'. Default value '{}' will be used instead.", configName, validationErrorText, defaultValue);
_configs[configIndex] = defaultValue;
}
else
_configs[configIndex] = configValue;
}
template<class T>
void OverwriteConfigValue(ConfigEnum const config, T const& value)
{
uint32 const configIndex = static_cast<uint32>(config);
ASSERT(configIndex < _configs.size(), "Config index out of bounds");
size_t const oldValueTypeIndex = _configs[configIndex].index();
ASSERT(oldValueTypeIndex != 0, "Config value must already be set");
_configs[configIndex] = value;
ASSERT(oldValueTypeIndex == _configs[configIndex].index(), "Config value type changed");
}
template<class T>
T GetConfigValue(ConfigEnum const config) const
{
uint32 const configIndex = static_cast<uint32>(config);
ASSERT(configIndex < _configs.size(), "Config index out of bounds");
ASSERT(_configs[configIndex].index() != 0, "Config value must already be set");
T const* value = std::get_if<T>(&_configs[configIndex]);
ASSERT(value, "Wrong config variant type");
return *value;
}
// Custom handling for string configs to convert from std::string to std::string_view
std::string_view GetConfigValue(ConfigEnum const config) const
{
uint32 const configIndex = static_cast<uint32>(config);
ASSERT(configIndex < _configs.size(), "Config index out of bounds");
ASSERT(_configs[configIndex].index() != 0, "Config value must already be set");
std::string const* stringValue = std::get_if<std::string>(&_configs[configIndex]);
ASSERT(stringValue, "Wrong config variant type");
return std::string_view(*stringValue);
}
protected:
virtual void BuildConfigCache() = 0;
private:
void VerifyAllConfigsLoaded()
{
uint32 configIndex = 0;
for (auto const& variant : _configs)
{
if (variant.index() == 0)
{
LOG_ERROR("server.loading", "Server Config (Index: {}) is defined but not loaded, unable to continue.", configIndex);
ASSERT(false);
}
++configIndex;
}
}
std::vector<std::variant<std::monostate, float, bool, uint32, std::string>> _configs;
bool _reloading;
};
#endif
|