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