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