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
81enum
82{
83 CFGFLAG_SAVE = 1 << 0,
84 CFGFLAG_CLIENT = 1 << 1,
85 CFGFLAG_SERVER = 1 << 2,
86 CFGFLAG_STORE = 1 << 3,
87 CFGFLAG_MASTER = 1 << 4,
88 CFGFLAG_ECON = 1 << 5,
89 // DDRace
90
91 CMDFLAG_TEST = 1 << 6,
92 CFGFLAG_CHAT = 1 << 7,
93 CFGFLAG_GAME = 1 << 8,
94 CFGFLAG_NONTEEHISTORIC = 1 << 9,
95 CFGFLAG_COLLIGHT = 1 << 10,
96 CFGFLAG_COLLIGHT7 = 1 << 11,
97 CFGFLAG_COLALPHA = 1 << 12,
98 CFGFLAG_INSENSITIVE = 1 << 13,
99 CMDFLAG_PRACTICE = 1 << 14,
100};
101
102#ifdef CONF_DEBUG
103#define CFGFLAG_DEBUG_SERVER CFGFLAG_SERVER
104#define CFGFLAG_DEBUG_CLIENT CFGFLAG_CLIENT
105#else
106#define CFGFLAG_DEBUG_SERVER 0
107#define CFGFLAG_DEBUG_CLIENT 0
108#endif
109
110struct SConfigVariable
111{
112 enum EVariableType
113 {
114 VAR_INT,
115 VAR_COLOR,
116 VAR_STRING,
117 };
118 IConsole *m_pConsole;
119 const char *m_pScriptName;
120 EVariableType m_Type;
121 int m_Flags;
122 const char *m_pHelp;
123 // Note that this only applies to the console command and the SetValue function,
124 // but the underlying config variable can still be modified programmatically.
125 bool m_ReadOnly = false;
126
127 SConfigVariable(IConsole *pConsole, const char *pScriptName, EVariableType Type, int Flags, const char *pHelp) :
128 m_pConsole(pConsole),
129 m_pScriptName(pScriptName),
130 m_Type(Type),
131 m_Flags(Flags),
132 m_pHelp(pHelp)
133 {
134 }
135
136 virtual ~SConfigVariable() = default;
137
138 virtual void Register() = 0;
139 virtual bool IsDefault() const = 0;
140 virtual void Serialize(char *pOut, size_t Size) const = 0;
141 virtual void ResetToDefault() = 0;
142 virtual void ResetToOld() = 0;
143
144protected:
145 void ExecuteLine(const char *pLine) const;
146 bool CheckReadOnly() const;
147};
148
149struct SIntConfigVariable : public SConfigVariable
150{
151 int *m_pVariable;
152 int m_Default;
153 int m_Min;
154 int m_Max;
155 int m_OldValue;
156
157 SIntConfigVariable(IConsole *pConsole, const char *pScriptName, EVariableType Type, int Flags, const char *pHelp, int *pVariable, int Default, int Min, int Max) :
158 SConfigVariable(pConsole, pScriptName, Type, Flags, pHelp),
159 m_pVariable(pVariable),
160 m_Default(Default),
161 m_Min(Min),
162 m_Max(Max),
163 m_OldValue(Default)
164 {
165 *m_pVariable = m_Default;
166 }
167
168 ~SIntConfigVariable() override = default;
169
170 static void CommandCallback(IConsole::IResult *pResult, void *pUserData);
171 void Register() override;
172 bool IsDefault() const override;
173 void Serialize(char *pOut, size_t Size, int Value) const;
174 void Serialize(char *pOut, size_t Size) const override;
175 void SetValue(int Value);
176 void ResetToDefault() override;
177 void ResetToOld() override;
178};
179
180struct SColorConfigVariable : public SConfigVariable
181{
182 unsigned *m_pVariable;
183 unsigned m_Default;
184 float m_DarkestLighting;
185 bool m_Alpha;
186 unsigned m_OldValue;
187
188 SColorConfigVariable(IConsole *pConsole, const char *pScriptName, EVariableType Type, int Flags, const char *pHelp, unsigned *pVariable, unsigned Default) :
189 SConfigVariable(pConsole, pScriptName, Type, Flags, pHelp),
190 m_pVariable(pVariable),
191 m_Default(Default),
192 m_Alpha(Flags & CFGFLAG_COLALPHA),
193 m_OldValue(Default)
194 {
195 *m_pVariable = m_Default;
196 if(Flags & CFGFLAG_COLLIGHT)
197 {
198 m_DarkestLighting = ColorHSLA::DARKEST_LGT;
199 }
200 else if(Flags & CFGFLAG_COLLIGHT7)
201 {
202 m_DarkestLighting = ColorHSLA::DARKEST_LGT7;
203 }
204 else
205 {
206 m_DarkestLighting = 0.0f;
207 }
208 }
209
210 ~SColorConfigVariable() override = default;
211
212 static void CommandCallback(IConsole::IResult *pResult, void *pUserData);
213 void Register() override;
214 bool IsDefault() const override;
215 void Serialize(char *pOut, size_t Size, unsigned Value) const;
216 void Serialize(char *pOut, size_t Size) const override;
217 void SetValue(unsigned Value);
218 void ResetToDefault() override;
219 void ResetToOld() override;
220};
221
222struct SStringConfigVariable : public SConfigVariable
223{
224 char *m_pStr;
225 const char *m_pDefault;
226 size_t m_MaxSize;
227 char *m_pOldValue;
228
229 SStringConfigVariable(IConsole *pConsole, const char *pScriptName, EVariableType Type, int Flags, const char *pHelp, char *pStr, const char *pDefault, size_t MaxSize, char *pOldValue);
230 ~SStringConfigVariable() override = default;
231
232 static void CommandCallback(IConsole::IResult *pResult, void *pUserData);
233 void Register() override;
234 bool IsDefault() const override;
235 void Serialize(char *pOut, size_t Size, const char *pValue) const;
236 void Serialize(char *pOut, size_t Size) const override;
237 void SetValue(const char *pValue);
238 void ResetToDefault() override;
239 void ResetToOld() override;
240};
241
242class CConfigManager : public IConfigManager
243{
244 IConsole *m_pConsole;
245 class IStorage *m_pStorage;
246
247 IOHANDLE m_ConfigFile;
248 bool m_Failed;
249
250 struct SCallback
251 {
252 SAVECALLBACKFUNC m_pfnFunc;
253 void *m_pUserData;
254
255 SCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData) :
256 m_pfnFunc(pfnFunc),
257 m_pUserData(pUserData)
258 {
259 }
260 };
261 std::vector<SCallback> m_vCallbacks;
262
263 std::vector<SConfigVariable *> m_vpAllVariables;
264 std::vector<SConfigVariable *> m_vpGameVariables;
265 std::vector<const char *> m_vpUnknownCommands;
266 CHeap m_ConfigHeap;
267
268 static void Con_Reset(IConsole::IResult *pResult, void *pUserData);
269 static void Con_Toggle(IConsole::IResult *pResult, void *pUserData);
270 static void Con_ToggleStroke(IConsole::IResult *pResult, void *pUserData);
271
272public:
273 CConfigManager();
274
275 void Init() override;
276 void Reset(const char *pScriptName) override;
277 void ResetGameSettings() override;
278 void SetReadOnly(const char *pScriptName, bool ReadOnly) override;
279 void SetGameSettingsReadOnly(bool ReadOnly) override;
280 bool Save() override;
281 CConfig *Values() override { return &g_Config; }
282
283 void RegisterCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData) override;
284
285 void WriteLine(const char *pLine) override;
286
287 void StoreUnknownCommand(const char *pCommand) override;
288
289 void PossibleConfigVariables(const char *pStr, int FlagMask, POSSIBLECFGFUNC pfnCallback, void *pUserData) override;
290};
291
292#endif
293