|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | #include "../ComponentManager.hpp" |
10 | | - |
11 | | -int getConfigOptionAsInt(const Impl::String& cvar) |
12 | | -{ |
13 | | - IConfig* config = &ComponentManager::Get()->core->getConfig(); |
14 | | - auto res = config->getNameFromAlias(cvar); |
15 | | - bool* v0 = nullptr; |
16 | | - int* v1 = nullptr; |
17 | | - if (!res.second.empty()) |
18 | | - { |
19 | | - if (res.first) |
20 | | - { |
21 | | - ComponentManager::Get()->core->logLn(LogLevel::Warning, "Deprecated console variable \"%s\", use \"%.*s\" instead.", cvar.c_str(), PRINT_VIEW(res.second)); |
22 | | - } |
23 | | - if (!(v1 = config->getInt(res.second))) |
24 | | - { |
25 | | - v0 = config->getBool(res.second); |
26 | | - } |
27 | | - } |
28 | | - else |
29 | | - { |
30 | | - if (!(v1 = config->getInt(cvar))) |
31 | | - { |
32 | | - v0 = config->getBool(cvar); |
33 | | - } |
34 | | - } |
35 | | - if (v1) |
36 | | - { |
37 | | - return *v1; |
38 | | - } |
39 | | - else if (v0) |
40 | | - { |
41 | | - ComponentManager::Get()->core->logLn(LogLevel::Warning, "Boolean console variable \"%s\" retreived as integer.", cvar.c_str()); |
42 | | - return *v0; |
43 | | - } |
44 | | - else |
45 | | - { |
46 | | - return 0; |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -bool getConfigOptionAsBool(const Impl::String& cvar) |
51 | | -{ |
52 | | - IConfig* config = &ComponentManager::Get()->core->getConfig(); |
53 | | - auto res = config->getNameFromAlias(cvar); |
54 | | - bool* v0 = nullptr; |
55 | | - int* v1 = nullptr; |
56 | | - if (!res.second.empty()) |
57 | | - { |
58 | | - if (res.first) |
59 | | - { |
60 | | - ComponentManager::Get()->core->logLn(LogLevel::Warning, "Deprecated console variable \"%s\", use \"%.*s\" instead.", cvar.c_str(), PRINT_VIEW(res.second)); |
61 | | - } |
62 | | - if (!(v0 = config->getBool(res.second))) |
63 | | - { |
64 | | - v1 = config->getInt(res.second); |
65 | | - } |
66 | | - } |
67 | | - else |
68 | | - { |
69 | | - if (!(v0 = config->getBool(cvar))) |
70 | | - { |
71 | | - v1 = config->getInt(cvar); |
72 | | - } |
73 | | - } |
74 | | - if (v0) |
75 | | - { |
76 | | - return *v0; |
77 | | - } |
78 | | - else if (v1) |
79 | | - { |
80 | | - ComponentManager::Get()->core->logLn(LogLevel::Warning, "Integer console variable \"%s\" retreived as boolean.", cvar.c_str()); |
81 | | - return *v1 != 0; |
82 | | - } |
83 | | - else |
84 | | - { |
85 | | - return false; |
86 | | - } |
87 | | -} |
88 | | - |
89 | | -float getConfigOptionAsFloat(const Impl::String& cvar) |
90 | | -{ |
91 | | - IConfig* config = &ComponentManager::Get()->core->getConfig(); |
92 | | - auto res = config->getNameFromAlias(cvar); |
93 | | - float* var = nullptr; |
94 | | - if (!res.second.empty()) |
95 | | - { |
96 | | - if (res.first) |
97 | | - { |
98 | | - ComponentManager::Get()->core->logLn(LogLevel::Warning, "Deprecated console variable \"%s\", use \"%.*s\" instead.", cvar.c_str(), PRINT_VIEW(res.second)); |
99 | | - } |
100 | | - var = config->getFloat(res.second); |
101 | | - } |
102 | | - else |
103 | | - { |
104 | | - var = config->getFloat(cvar); |
105 | | - } |
106 | | - if (var) |
107 | | - { |
108 | | - return *var; |
109 | | - } |
110 | | - else |
111 | | - { |
112 | | - return 0.0f; |
113 | | - } |
114 | | -} |
115 | | - |
116 | | -int getConfigOptionAsString(const Impl::String& cvar, Impl::String& buffer) |
117 | | -{ |
118 | | - // Special case, converting `gamemode0` to `pawn.main_scripts[0]`. It is the only string to |
119 | | - // array change. |
120 | | - IConfig* config = &ComponentManager::Get()->core->getConfig(); |
121 | | - bool gm = cvar.substr(0, 8) == "gamemode"; |
122 | | - auto res = config->getNameFromAlias(gm ? "gamemode" : cvar); |
123 | | - if (!res.second.empty()) |
124 | | - { |
125 | | - if (res.first) |
126 | | - { |
127 | | - ComponentManager::Get()->core->logLn(LogLevel::Warning, "Deprecated console variable \"%s\", use \"%.*s\" instead.", cvar.c_str(), PRINT_VIEW(res.second)); |
128 | | - } |
129 | | - if (gm) |
130 | | - { |
131 | | - size_t i = std::stoi("0" + cvar.substr(8)); |
132 | | - Impl::DynamicArray<StringView> mainScripts(i + 1); |
133 | | - size_t n = config->getStrings(res.second, Span<StringView>(mainScripts.data(), mainScripts.size())); |
134 | | - if (i < n) |
135 | | - { |
136 | | - buffer = mainScripts[i].data(); |
137 | | - } |
138 | | - } |
139 | | - else |
140 | | - { |
141 | | - buffer = config->getString(res.second).data(); |
142 | | - } |
143 | | - } |
144 | | - else |
145 | | - { |
146 | | - buffer = config->getString(cvar).data(); |
147 | | - } |
148 | | - return buffer.length(); |
149 | | -} |
| 10 | +#include <Impl/Utils/helpers.hpp> |
150 | 11 |
|
151 | 12 | OMP_CAPI(Config_GetAsBool, bool(StringCharPtr cvar)) |
152 | 13 | { |
153 | | - bool value = getConfigOptionAsBool(cvar); |
| 14 | + bool value = getConfigOptionAsBool(ComponentManager::Get()->core, cvar); |
154 | 15 | return value; |
155 | 16 | } |
156 | 17 |
|
157 | 18 | OMP_CAPI(Config_GetAsInt, int(StringCharPtr cvar)) |
158 | 19 | { |
159 | | - int value = getConfigOptionAsInt(cvar); |
| 20 | + int value = getConfigOptionAsInt(ComponentManager::Get()->core, cvar); |
160 | 21 | return value; |
161 | 22 | } |
162 | 23 |
|
163 | 24 | OMP_CAPI(Config_GetAsFloat, float(StringCharPtr cvar)) |
164 | 25 | { |
165 | | - float value = getConfigOptionAsFloat(cvar); |
| 26 | + float value = getConfigOptionAsFloat(ComponentManager::Get()->core, cvar); |
166 | 27 | return value; |
167 | 28 | } |
168 | 29 |
|
169 | 30 | OMP_CAPI(Config_GetAsString, int(StringCharPtr cvar, OutputStringViewPtr output)) |
170 | 31 | { |
171 | 32 | Impl::String value = Impl::String(); |
172 | | - int len = getConfigOptionAsString(cvar, value); |
| 33 | + int len = getConfigOptionAsString(ComponentManager::Get()->core, cvar, value); |
173 | 34 | COPY_STRING_TO_CAPI_STRING_VIEW(output, value.data(), len); |
174 | 35 | return len; |
175 | 36 | } |
0 commit comments