1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SHARED_CONFIG_H
4#define ENGINE_SHARED_CONFIG_H
5
6#include <base/detect.h>
7
8#include <engine/config.h>
9#include <engine/console.h>
10#include <engine/shared/memheap.h>
11
12#include <vector>
13
14// include protocol for MAX_CLIENT used in config_variables
15#include <engine/shared/protocol.h>
16
17static constexpr const char *DEFAULT_SAVED_RCON_USER = "local-server";
18
19#define CONFIG_FILE "settings_ddnet.cfg"
20#define AUTOEXEC_FILE "autoexec.cfg"
21#define AUTOEXEC_CLIENT_FILE "autoexec_client.cfg"
22#define AUTOEXEC_SERVER_FILE "autoexec_server.cfg"
23
24/**
25 * Stores the current values of all client, server and game config variables.
26 *
27 * The config variables are declared in `src/engine/shared/config_variables.h`.
28 */
29class CConfig
30{
31public:
32#define MACRO_CONFIG_INT(Name, ScriptName, Def, Min, Max, Flags, Desc) \
33 /** Config variable: ScriptName\n \
34 Type: Integer\n \
35 Default: Def\n \
36 Minimum: Min\n \
37 Maximum: Max\n \
38 Description: Desc */ \
39 int m_##Name;
40#define MACRO_CONFIG_COL(Name, ScriptName, Def, Flags, Desc) \
41 /** Config variable: ScriptName\n \
42 Type: Color\n \
43 Default: Def\n \
44 Description: Desc */ \
45 unsigned m_##Name;
46#define MACRO_CONFIG_STR(Name, ScriptName, Len, Def, Flags, Desc) \
47 /** Config variable: ScriptName\n \
48 Type: String\n \
49 Length: Len\n \
50 Default: Def\n \
51 Description: Desc */ \
52 char m_##Name[Len]; // Flawfinder: ignore
53#include "config_variables.h"
54#undef MACRO_CONFIG_INT
55#undef MACRO_CONFIG_COL
56#undef MACRO_CONFIG_STR
57};
58
59extern CConfig g_Config;
60
61/**
62 * The default values of all config variables in @link CConfig @endlink.
63 */
64namespace DefaultConfig
65{
66#define MACRO_CONFIG_INT(Name, ScriptName, Def, Min, Max, Flags, Desc) \
67 /** Default value of the integer config variable 'ScriptName' (see CConfig::m_##Name). */ \
68 static constexpr int Name = Def;
69#define MACRO_CONFIG_COL(Name, ScriptName, Def, Flags, Desc) \
70 /** Default value of the color config variable 'ScriptName' (see CConfig::m_##Name). */ \
71 static constexpr unsigned Name = Def;
72#define MACRO_CONFIG_STR(Name, ScriptName, Len, Def, Flags, Desc) \
73 /** Default value of the string config variable 'ScriptName' (see CConfig::m_##Name). */ \
74 static constexpr const char *const Name = Def;
75#include "config_variables.h"
76#undef MACRO_CONFIG_INT
77#undef MACRO_CONFIG_COL
78#undef MACRO_CONFIG_STR
79}
80
81/**
82 * Declared limits of integer config variables. Equal limits disable clamping.
83 * Otherwise a maximum of zero leaves the value unbounded above.
84 */
85namespace ConfigLimits
86{
87 class CIntLimits
88 {
89 public:
90 int m_Min = 0;
91 int m_Max = 0;
92 };
93
94#define MACRO_CONFIG_INT(Name, ScriptName, Def, Min, Max, Flags, Desc) \
95 inline constexpr CIntLimits Name{.m_Min = Min, .m_Max = Max};
96#define MACRO_CONFIG_COL(Name, ScriptName, Def, Flags, Desc)
97#define MACRO_CONFIG_STR(Name, ScriptName, Len, Def, Flags, Desc)
98#include "config_variables.h"
99#undef MACRO_CONFIG_INT
100#undef MACRO_CONFIG_COL
101#undef MACRO_CONFIG_STR
102}
103
104// Note: The values of some of these flags cannot be changed because they are recorded to Teehistorian files.
105enum
106{
107 CFGFLAG_SAVE = 1 << 0,
108 CFGFLAG_CLIENT = 1 << 1,
109 CFGFLAG_SERVER = 1 << 2,
110 CFGFLAG_STORE = 1 << 3,
111 // 1 << 4 has been removed. Do not reuse this flag.
112 CFGFLAG_ECON = 1 << 5,
113 CMDFLAG_TEST = 1 << 6,
114 CFGFLAG_CHAT = 1 << 7,
115 CFGFLAG_GAME = 1 << 8,
116 CFGFLAG_NONTEEHISTORIC = 1 << 9,
117 CFGFLAG_COLLIGHT = 1 << 10,
118 CFGFLAG_COLLIGHT7 = 1 << 11,
119 CFGFLAG_COLALPHA = 1 << 12,
120 CFGFLAG_INSENSITIVE = 1 << 13,
121 CMDFLAG_PRACTICE = 1 << 14,
122};
123
124#ifdef CONF_DEBUG
125#define CFGFLAG_DEBUG_SERVER CFGFLAG_SERVER
126#define CFGFLAG_DEBUG_CLIENT CFGFLAG_CLIENT
127#else
128#define CFGFLAG_DEBUG_SERVER 0
129#define CFGFLAG_DEBUG_CLIENT 0
130#endif
131
132struct SConfigVariable
133{
134 enum EVariableType
135 {
136 VAR_INT,
137 VAR_COLOR,
138 VAR_STRING,
139 };
140 IConsole *m_pConsole;
141 const char *m_pScriptName;
142 EVariableType m_Type;
143 int m_Flags;
144 const char *m_pHelp;
145 // Note that this only applies to the console command and the SetValue function,
146 // but the underlying config variable can still be modified programmatically.
147 bool m_ReadOnly = false;
148
149 SConfigVariable(IConsole *pConsole, const char *pScriptName, EVariableType Type, int Flags, const char *pHelp) :
150 m_pConsole(pConsole),
151 m_pScriptName(pScriptName),
152 m_Type(Type),
153 m_Flags(Flags),
154 m_pHelp(pHelp)
155 {
156 }
157
158 virtual ~SConfigVariable() = default;
159
160 virtual void Register() = 0;
161 virtual bool IsDefault() const = 0;
162 virtual void Serialize(char *pOut, size_t Size) const = 0;
163 virtual void SerializeHumanReadable(char *pOut, size_t Size) const = 0;
164 virtual void ResetToDefault() = 0;
165 virtual void ResetToOld() = 0;
166
167protected:
168 void ExecuteLine(const char *pLine) const;
169 bool CheckReadOnly() const;
170};
171
172struct SIntConfigVariable : public SConfigVariable
173{
174 int *m_pVariable;
175 int m_Default;
176 int m_Min;
177 int m_Max;
178 int m_OldValue;
179
180 SIntConfigVariable(IConsole *pConsole, const char *pScriptName, EVariableType Type, int Flags, const char *pHelp, int *pVariable, int Default, int Min, int Max) :
181 SConfigVariable(pConsole, pScriptName, Type, Flags, pHelp),
182 m_pVariable(pVariable),
183 m_Default(Default),
184 m_Min(Min),
185 m_Max(Max),
186 m_OldValue(Default)
187 {
188 *m_pVariable = m_Default;
189 }
190
191 ~SIntConfigVariable() override = default;
192
193 static void CommandCallback(IConsole::IResult *pResult, void *pUserData);
194 void Register() override;
195 bool IsDefault() const override;
196 void Serialize(char *pOut, size_t Size, int Value) const;
197 void Serialize(char *pOut, size_t Size) const override;
198 void SerializeHumanReadable(char *pOut, size_t Size) const override;
199 void SetValue(int Value);
200 void ResetToDefault() override;
201 void ResetToOld() override;
202};
203
204struct SColorConfigVariable : public SConfigVariable
205{
206 unsigned *m_pVariable;
207 unsigned m_Default;
208 float m_DarkestLighting;
209 bool m_Alpha;
210 unsigned m_OldValue;
211
212 SColorConfigVariable(IConsole *pConsole, const char *pScriptName, EVariableType Type, int Flags, const char *pHelp, unsigned *pVariable, unsigned Default) :
213 SConfigVariable(pConsole, pScriptName, Type, Flags, pHelp),
214 m_pVariable(pVariable),
215 m_Default(Default),
216 m_Alpha(Flags & CFGFLAG_COLALPHA),
217 m_OldValue(Default)
218 {
219 *m_pVariable = m_Default;
220 if(Flags & CFGFLAG_COLLIGHT)
221 {
222 m_DarkestLighting = ColorHSLA::DARKEST_LGT;
223 }
224 else if(Flags & CFGFLAG_COLLIGHT7)
225 {
226 m_DarkestLighting = ColorHSLA::DARKEST_LGT7;
227 }
228 else
229 {
230 m_DarkestLighting = 0.0f;
231 }
232 }
233
234 ~SColorConfigVariable() override = default;
235
236 static void CommandCallback(IConsole::IResult *pResult, void *pUserData);
237 void Register() override;
238 bool IsDefault() const override;
239 void Serialize(char *pOut, size_t Size, unsigned Value) const;
240 void Serialize(char *pOut, size_t Size) const override;
241 void SerializeHumanReadable(char *pOut, size_t Size) const override;
242 void SetValue(unsigned Value);
243 void ResetToDefault() override;
244 void ResetToOld() override;
245};
246
247struct SStringConfigVariable : public SConfigVariable
248{
249 char *m_pStr;
250 const char *m_pDefault;
251 size_t m_MaxSize;
252 char *m_pOldValue;
253
254 SStringConfigVariable(IConsole *pConsole, const char *pScriptName, EVariableType Type, int Flags, const char *pHelp, char *pStr, const char *pDefault, size_t MaxSize, char *pOldValue);
255 ~SStringConfigVariable() override = default;
256
257 static void CommandCallback(IConsole::IResult *pResult, void *pUserData);
258 void Register() override;
259 bool IsDefault() const override;
260 void Serialize(char *pOut, size_t Size, const char *pValue) const;
261 void Serialize(char *pOut, size_t Size) const override;
262 void SerializeHumanReadable(char *pOut, size_t Size) const override;
263 void SetValue(const char *pValue);
264 void ResetToDefault() override;
265 void ResetToOld() override;
266};
267
268class CConfigManager : public IConfigManager
269{
270 IConsole *m_pConsole;
271 class IStorage *m_pStorage;
272
273 IOHANDLE m_ConfigFile;
274 bool m_Failed;
275
276 struct SCallback
277 {
278 SAVECALLBACKFUNC m_pfnFunc;
279 void *m_pUserData;
280
281 SCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData) :
282 m_pfnFunc(pfnFunc),
283 m_pUserData(pUserData)
284 {
285 }
286 };
287 std::vector<SCallback> m_vCallbacks;
288
289 std::vector<SConfigVariable *> m_vpAllVariables;
290 std::vector<SConfigVariable *> m_vpGameVariables;
291 std::vector<const char *> m_vpUnknownCommands;
292 CHeap m_ConfigHeap;
293
294 static void Con_Reset(IConsole::IResult *pResult, void *pUserData);
295 static void Con_Toggle(IConsole::IResult *pResult, void *pUserData);
296 static void Con_ToggleStroke(IConsole::IResult *pResult, void *pUserData);
297
298public:
299 CConfigManager();
300
301 void Init() override;
302 void Reset(const char *pScriptName) override;
303 void ResetGameSettings() override;
304 void SetReadOnly(const char *pScriptName, bool ReadOnly) override;
305 void SetGameSettingsReadOnly(bool ReadOnly) override;
306 bool Save() override;
307 CConfig *Values() override { return &g_Config; }
308
309 void RegisterCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData) override;
310
311 void WriteLine(const char *pLine) override;
312
313 void StoreUnknownCommand(const char *pCommand) override;
314
315 void PossibleConfigVariables(const char *pStr, int FlagMask, POSSIBLECFGFUNC pfnCallback, void *pUserData) override;
316
317 const SConfigVariable *FindVariable(const char *pScriptName) const override;
318};
319
320#endif
321