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
24class CConfig
25{
26public:
27#define MACRO_CONFIG_INT(Name, ScriptName, Def, Min, Max, Flags, Desc) \
28 static constexpr int ms_##Name = Def; \
29 int m_##Name;
30#define MACRO_CONFIG_COL(Name, ScriptName, Def, Flags, Desc) \
31 static constexpr unsigned ms_##Name = Def; \
32 unsigned m_##Name;
33#define MACRO_CONFIG_STR(Name, ScriptName, Len, Def, Flags, Desc) \
34 static constexpr const char *ms_p##Name = Def; \
35 char m_##Name[Len]; // Flawfinder: ignore
36#include "config_variables.h"
37#undef MACRO_CONFIG_INT
38#undef MACRO_CONFIG_COL
39#undef MACRO_CONFIG_STR
40};
41
42extern CConfig g_Config;
43
44enum
45{
46 CFGFLAG_SAVE = 1 << 0,
47 CFGFLAG_CLIENT = 1 << 1,
48 CFGFLAG_SERVER = 1 << 2,
49 CFGFLAG_STORE = 1 << 3,
50 CFGFLAG_MASTER = 1 << 4,
51 CFGFLAG_ECON = 1 << 5,
52 // DDRace
53
54 CMDFLAG_TEST = 1 << 6,
55 CFGFLAG_CHAT = 1 << 7,
56 CFGFLAG_GAME = 1 << 8,
57 CFGFLAG_NONTEEHISTORIC = 1 << 9,
58 CFGFLAG_COLLIGHT = 1 << 10,
59 CFGFLAG_COLLIGHT7 = 1 << 11,
60 CFGFLAG_COLALPHA = 1 << 12,
61 CFGFLAG_INSENSITIVE = 1 << 13,
62 CMDFLAG_PRACTICE = 1 << 14,
63};
64
65struct SConfigVariable
66{
67 enum EVariableType
68 {
69 VAR_INT,
70 VAR_COLOR,
71 VAR_STRING,
72 };
73 IConsole *m_pConsole;
74 const char *m_pScriptName;
75 EVariableType m_Type;
76 int m_Flags;
77 const char *m_pHelp;
78 // Note that this only applies to the console command and the SetValue function,
79 // but the underlying config variable can still be modified programmatically.
80 bool m_ReadOnly = false;
81
82 SConfigVariable(IConsole *pConsole, const char *pScriptName, EVariableType Type, int Flags, const char *pHelp) :
83 m_pConsole(pConsole),
84 m_pScriptName(pScriptName),
85 m_Type(Type),
86 m_Flags(Flags),
87 m_pHelp(pHelp)
88 {
89 }
90
91 virtual ~SConfigVariable() = default;
92
93 virtual void Register() = 0;
94 virtual bool IsDefault() const = 0;
95 virtual void Serialize(char *pOut, size_t Size) const = 0;
96 virtual void ResetToDefault() = 0;
97 virtual void ResetToOld() = 0;
98
99protected:
100 void ExecuteLine(const char *pLine) const;
101 bool CheckReadOnly() const;
102};
103
104struct SIntConfigVariable : public SConfigVariable
105{
106 int *m_pVariable;
107 int m_Default;
108 int m_Min;
109 int m_Max;
110 int m_OldValue;
111
112 SIntConfigVariable(IConsole *pConsole, const char *pScriptName, EVariableType Type, int Flags, const char *pHelp, int *pVariable, int Default, int Min, int Max) :
113 SConfigVariable(pConsole, pScriptName, Type, Flags, pHelp),
114 m_pVariable(pVariable),
115 m_Default(Default),
116 m_Min(Min),
117 m_Max(Max),
118 m_OldValue(Default)
119 {
120 *m_pVariable = m_Default;
121 }
122
123 ~SIntConfigVariable() override = default;
124
125 static void CommandCallback(IConsole::IResult *pResult, void *pUserData);
126 void Register() override;
127 bool IsDefault() const override;
128 void Serialize(char *pOut, size_t Size, int Value) const;
129 void Serialize(char *pOut, size_t Size) const override;
130 void SetValue(int Value);
131 void ResetToDefault() override;
132 void ResetToOld() override;
133};
134
135struct SColorConfigVariable : public SConfigVariable
136{
137 unsigned *m_pVariable;
138 unsigned m_Default;
139 float m_DarkestLighting;
140 bool m_Alpha;
141 unsigned m_OldValue;
142
143 SColorConfigVariable(IConsole *pConsole, const char *pScriptName, EVariableType Type, int Flags, const char *pHelp, unsigned *pVariable, unsigned Default) :
144 SConfigVariable(pConsole, pScriptName, Type, Flags, pHelp),
145 m_pVariable(pVariable),
146 m_Default(Default),
147 m_Alpha(Flags & CFGFLAG_COLALPHA),
148 m_OldValue(Default)
149 {
150 *m_pVariable = m_Default;
151 if(Flags & CFGFLAG_COLLIGHT)
152 {
153 m_DarkestLighting = ColorHSLA::DARKEST_LGT;
154 }
155 else if(Flags & CFGFLAG_COLLIGHT7)
156 {
157 m_DarkestLighting = ColorHSLA::DARKEST_LGT7;
158 }
159 else
160 {
161 m_DarkestLighting = 0.0f;
162 }
163 }
164
165 ~SColorConfigVariable() override = default;
166
167 static void CommandCallback(IConsole::IResult *pResult, void *pUserData);
168 void Register() override;
169 bool IsDefault() const override;
170 void Serialize(char *pOut, size_t Size, unsigned Value) const;
171 void Serialize(char *pOut, size_t Size) const override;
172 void SetValue(unsigned Value);
173 void ResetToDefault() override;
174 void ResetToOld() override;
175};
176
177struct SStringConfigVariable : public SConfigVariable
178{
179 char *m_pStr;
180 const char *m_pDefault;
181 size_t m_MaxSize;
182 char *m_pOldValue;
183
184 SStringConfigVariable(IConsole *pConsole, const char *pScriptName, EVariableType Type, int Flags, const char *pHelp, char *pStr, const char *pDefault, size_t MaxSize, char *pOldValue);
185 ~SStringConfigVariable() override = default;
186
187 static void CommandCallback(IConsole::IResult *pResult, void *pUserData);
188 void Register() override;
189 bool IsDefault() const override;
190 void Serialize(char *pOut, size_t Size, const char *pValue) const;
191 void Serialize(char *pOut, size_t Size) const override;
192 void SetValue(const char *pValue);
193 void ResetToDefault() override;
194 void ResetToOld() override;
195};
196
197class CConfigManager : public IConfigManager
198{
199 IConsole *m_pConsole;
200 class IStorage *m_pStorage;
201
202 IOHANDLE m_ConfigFile;
203 bool m_Failed;
204
205 struct SCallback
206 {
207 SAVECALLBACKFUNC m_pfnFunc;
208 void *m_pUserData;
209
210 SCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData) :
211 m_pfnFunc(pfnFunc),
212 m_pUserData(pUserData)
213 {
214 }
215 };
216 std::vector<SCallback> m_vCallbacks;
217
218 std::vector<SConfigVariable *> m_vpAllVariables;
219 std::vector<SConfigVariable *> m_vpGameVariables;
220 std::vector<const char *> m_vpUnknownCommands;
221 CHeap m_ConfigHeap;
222
223 static void Con_Reset(IConsole::IResult *pResult, void *pUserData);
224 static void Con_Toggle(IConsole::IResult *pResult, void *pUserData);
225 static void Con_ToggleStroke(IConsole::IResult *pResult, void *pUserData);
226
227public:
228 CConfigManager();
229
230 void Init() override;
231 void Reset(const char *pScriptName) override;
232 void ResetGameSettings() override;
233 void SetReadOnly(const char *pScriptName, bool ReadOnly) override;
234 void SetGameSettingsReadOnly(bool ReadOnly) override;
235 bool Save() override;
236 CConfig *Values() override { return &g_Config; }
237
238 void RegisterCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData) override;
239
240 void WriteLine(const char *pLine) override;
241
242 void StoreUnknownCommand(const char *pCommand) override;
243
244 void PossibleConfigVariables(const char *pStr, int FlagMask, POSSIBLECFGFUNC pfnCallback, void *pUserData) override;
245};
246
247#endif
248