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