DDraceNetwork Docs
console.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_CONSOLE_H
4#define ENGINE_SHARED_CONSOLE_H
5
6#include "memheap.h"
7#include <base/math.h>
8#include <base/system.h>
9#include <engine/console.h>
10#include <engine/storage.h>
11
12class CConsole : public IConsole
13{
14 class CCommand : public CCommandInfo
15 {
16 public:
19 bool m_Temp;
22
23 const CCommandInfo *NextCommandInfo(int AccessLevel, int FlagMask) const override;
24
25 void SetAccessLevel(int AccessLevel) { m_AccessLevel = clamp(AccessLevel, (int)(ACCESS_LEVEL_ADMIN), (int)(ACCESS_LEVEL_USER)); }
26 };
27
28 class CChain
29 {
30 public:
35 };
36
39 const char *m_apStrokeStr[2];
41
43 {
44 public:
45 const char *m_pFilename;
47 };
48
52
55
56 static void TraverseChain(FCommandCallback *ppfnCallback, void **ppUserData);
57
58 static void Con_Chain(IResult *pResult, void *pUserData);
59 static void Con_Echo(IResult *pResult, void *pUserData);
60 static void Con_Exec(IResult *pResult, void *pUserData);
61 static void ConCommandAccess(IResult *pResult, void *pUser);
62 static void ConCommandStatus(IConsole::IResult *pResult, void *pUser);
63
64 void ExecuteLineStroked(int Stroke, const char *pStr, int ClientId = -1, bool InterpretSemicolons = true) override;
65
68
71
72 enum
73 {
76 };
77
78 class CResult : public IResult
79 {
80 public:
83
84 const char *m_pCommand;
85 const char *m_apArgs[MAX_PARTS];
86
87 CResult(int ClientId) :
88 IResult(ClientId)
89 {
91 m_pArgsStart = 0;
92 m_pCommand = 0;
93 mem_zero(m_apArgs, sizeof(m_apArgs));
94 }
95
96 CResult(const CResult &Other) :
97 IResult(Other)
98 {
102 for(unsigned i = 0; i < Other.m_NumArgs; ++i)
103 m_apArgs[i] = m_aStringStorage + (Other.m_apArgs[i] - Other.m_aStringStorage);
104 }
105
106 void AddArgument(const char *pArg)
107 {
108 m_apArgs[m_NumArgs++] = pArg;
109 }
110
111 const char *GetString(unsigned Index) const override;
112 int GetInteger(unsigned Index) const override;
113 float GetFloat(unsigned Index) const override;
114 std::optional<ColorHSLA> GetColor(unsigned Index, float DarkestLighting) const override;
115
116 void RemoveArgument(unsigned Index) override
117 {
118 dbg_assert(Index < m_NumArgs, "invalid argument index");
119 for(unsigned i = Index; i < m_NumArgs - 1; i++)
120 m_apArgs[i] = m_apArgs[i + 1];
121
122 m_apArgs[m_NumArgs--] = 0;
123 }
124
125 // DDRace
126
127 enum
128 {
132 };
133
135 void ResetVictim();
136 bool HasVictim() const;
137 void SetVictim(int Victim);
138 void SetVictim(const char *pVictim);
139 int GetVictim() const override;
140 };
141
142 int ParseStart(CResult *pResult, const char *pString, int Length);
143
144 enum
145 {
150 };
151
152 int ParseArgs(CResult *pResult, const char *pFormat, bool IsColor = false);
153
154 /*
155 this function will set pFormat to the next parameter (i,s,r,v,?) it contains and
156 return the parameter; descriptions in brackets like [file] will be skipped;
157 returns '\0' if there is no next parameter; expects pFormat to point at a
158 parameter
159 */
160 char NextParam(const char *&pFormat);
161
163 {
164 public:
168 m_pCommand(pCommand),
169 m_Result(Result) {}
170 };
171 std::vector<CExecutionQueueEntry> m_vExecutionQueue;
172
173 void AddCommandSorted(CCommand *pCommand);
174 CCommand *FindCommand(const char *pName, int FlagMask);
175
177
178public:
179 CConsole(int FlagMask);
180 ~CConsole();
181
182 void Init() override;
183 const CCommandInfo *FirstCommandInfo(int AccessLevel, int FlagMask) const override;
184 const CCommandInfo *GetCommandInfo(const char *pName, int FlagMask, bool Temp) override;
185 int PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser) override;
186
187 void ParseArguments(int NumArgs, const char **ppArguments) override;
188 void Register(const char *pName, const char *pParams, int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp) override;
189 void RegisterTemp(const char *pName, const char *pParams, int Flags, const char *pHelp) override;
190 void DeregisterTemp(const char *pName) override;
191 void DeregisterTempAll() override;
192 void Chain(const char *pName, FChainCommandCallback pfnChainFunc, void *pUser) override;
193 void StoreCommands(bool Store) override;
194
195 bool LineIsValid(const char *pStr) override;
196 void ExecuteLine(const char *pStr, int ClientId = -1, bool InterpretSemicolons = true) override;
197 void ExecuteLineFlag(const char *pStr, int FlagMask, int ClientId = -1, bool InterpretSemicolons = true) override;
198 bool ExecuteFile(const char *pFilename, int ClientId = -1, bool LogFailure = false, int StorageType = IStorage::TYPE_ALL) override;
199
200 void Print(int Level, const char *pFrom, const char *pStr, ColorRGBA PrintColor = gs_ConsoleDefaultColor) const override;
201 void SetTeeHistorianCommandCallback(FTeeHistorianCommandCallback pfnCallback, void *pUser) override;
202 void SetUnknownCommandCallback(FUnknownCommandCallback pfnCallback, void *pUser) override;
203 void InitChecksum(CChecksumData *pData) const override;
204
205 void SetAccessLevel(int AccessLevel) override { m_AccessLevel = clamp(AccessLevel, (int)(ACCESS_LEVEL_ADMIN), (int)(ACCESS_LEVEL_USER)); }
206
207 // DDRace
208
209 static void ConUserCommandStatus(IConsole::IResult *pResult, void *pUser);
210
211 bool Cheated() const override { return m_Cheated; }
212
213 int FlagMask() const override { return m_FlagMask; }
214 void SetFlagMask(int FlagMask) override { m_FlagMask = FlagMask; }
215};
216
217#endif
Definition: console.h:29
FCommandCallback m_pfnCallback
Definition: console.h:32
void * m_pUserData
Definition: console.h:34
void * m_pCallbackUserData
Definition: console.h:33
FChainCommandCallback m_pfnChainCallback
Definition: console.h:31
Definition: console.h:15
const CCommandInfo * NextCommandInfo(int AccessLevel, int FlagMask) const override
Definition: console.cpp:86
void SetAccessLevel(int AccessLevel)
Definition: console.h:25
CCommand * m_pNext
Definition: console.h:17
void * m_pUserData
Definition: console.h:21
bool m_Temp
Definition: console.h:19
FCommandCallback m_pfnCallback
Definition: console.h:20
int m_Flags
Definition: console.h:18
Definition: console.h:43
CExecFile * m_pPrev
Definition: console.h:46
const char * m_pFilename
Definition: console.h:45
Definition: console.h:163
CCommand * m_pCommand
Definition: console.h:165
CExecutionQueueEntry(CCommand *pCommand, CResult Result)
Definition: console.h:167
CResult m_Result
Definition: console.h:166
Definition: console.h:79
float GetFloat(unsigned Index) const override
Definition: console.cpp:36
const char * m_apArgs[MAX_PARTS]
Definition: console.h:85
void AddArgument(const char *pArg)
Definition: console.h:106
void RemoveArgument(unsigned Index) override
Definition: console.h:116
CResult(const CResult &Other)
Definition: console.h:96
CResult(int ClientId)
Definition: console.h:87
void SetVictim(int Victim)
Definition: console.cpp:1070
char m_aStringStorage[CONSOLE_MAX_STR_LENGTH+1]
Definition: console.h:81
int m_Victim
Definition: console.h:134
char * m_pArgsStart
Definition: console.h:82
bool HasVictim() const
Definition: console.cpp:1065
void ResetVictim()
Definition: console.cpp:1060
int GetVictim() const override
Definition: console.cpp:1055
@ VICTIM_ALL
Definition: console.h:131
@ VICTIM_ME
Definition: console.h:130
@ VICTIM_NONE
Definition: console.h:129
std::optional< ColorHSLA > GetColor(unsigned Index, float DarkestLighting) const override
Definition: console.cpp:43
const char * m_pCommand
Definition: console.h:84
int GetInteger(unsigned Index) const override
Definition: console.cpp:29
const char * GetString(unsigned Index) const override
Definition: console.cpp:22
Definition: console.h:13
static void ConUserCommandStatus(IConsole::IResult *pResult, void *pUser)
Definition: console.cpp:751
void ParseArguments(int NumArgs, const char **ppArguments) override
Definition: console.cpp:832
const CCommandInfo * FirstCommandInfo(int AccessLevel, int FlagMask) const override
Definition: console.cpp:98
~CConsole()
Definition: console.cpp:802
void SetFlagMask(int FlagMask) override
Definition: console.h:214
void DeregisterTempAll() override
Definition: console.cpp:973
bool m_Cheated
Definition: console.h:176
static void Con_Echo(IResult *pResult, void *pUserData)
Definition: console.cpp:675
FTeeHistorianCommandCallback m_pfnTeeHistorianCommandCallback
Definition: console.h:66
void StoreCommands(bool Store) override
Definition: console.cpp:1026
bool Cheated() const override
Definition: console.h:211
bool m_StoreCommands
Definition: console.h:38
void SetAccessLevel(int AccessLevel) override
Definition: console.h:205
void Init() override
Definition: console.cpp:827
void SetUnknownCommandCallback(FUnknownCommandCallback pfnCallback, void *pUser) override
Definition: console.cpp:348
void Register(const char *pName, const char *pParams, int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp) override
Definition: console.cpp:880
void AddCommandSorted(CCommand *pCommand)
Definition: console.cpp:856
CCommand * m_pFirstCommand
Definition: console.h:40
void ExecuteLineStroked(int Stroke, const char *pStr, int ClientId=-1, bool InterpretSemicolons=true) override
Definition: console.cpp:420
void * m_pUnknownCommandUserdata
Definition: console.h:70
CConsole(int FlagMask)
Definition: console.cpp:773
static void TraverseChain(FCommandCallback *ppfnCallback, void **ppUserData)
Definition: console.cpp:763
int m_FlagMask
Definition: console.h:37
int ParseStart(CResult *pResult, const char *pString, int Length)
Definition: console.cpp:111
const char * m_apStrokeStr[2]
Definition: console.h:39
FUnknownCommandCallback m_pfnUnknownCommandCallback
Definition: console.h:69
void DeregisterTemp(const char *pName) override
Definition: console.cpp:941
void * m_pTeeHistorianCommandUserdata
Definition: console.h:67
@ PARSEARGS_INVALID_INTEGER
Definition: console.h:148
@ PARSEARGS_OK
Definition: console.h:146
@ PARSEARGS_INVALID_FLOAT
Definition: console.h:149
@ PARSEARGS_MISSING_VALUE
Definition: console.h:147
int FlagMask() const override
Definition: console.h:213
void RegisterTemp(const char *pName, const char *pParams, int Flags, const char *pHelp) override
Definition: console.cpp:907
void ExecuteLineFlag(const char *pStr, int FlagMask, int ClientId=-1, bool InterpretSemicolons=true) override
Definition: console.cpp:624
CExecFile * m_pFirstExec
Definition: console.h:49
int ParseArgs(CResult *pResult, const char *pFormat, bool IsColor=false)
Definition: console.cpp:136
static void ConCommandAccess(IResult *pResult, void *pUser)
Definition: console.cpp:685
void Chain(const char *pName, FChainCommandCallback pfnChainFunc, void *pUser) override
Definition: console.cpp:1001
void ExecuteLine(const char *pStr, int ClientId=-1, bool InterpretSemicolons=true) override
Definition: console.cpp:618
CCommand * FindCommand(const char *pName, int FlagMask)
Definition: console.cpp:604
@ MAX_PARTS
Definition: console.h:75
@ CONSOLE_MAX_STR_LENGTH
Definition: console.h:74
bool ExecuteFile(const char *pFilename, int ClientId=-1, bool LogFailure=false, int StorageType=IStorage::TYPE_ALL) override
Definition: console.cpp:632
bool LineIsValid(const char *pStr) override
Definition: console.cpp:372
void InitChecksum(CChecksumData *pData) const override
Definition: console.cpp:354
char NextParam(const char *&pFormat)
Definition: console.cpp:270
static void Con_Exec(IResult *pResult, void *pUserData)
Definition: console.cpp:680
CCommand * m_pRecycleList
Definition: console.h:53
int m_AccessLevel
Definition: console.h:51
int PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser) override
Definition: console.cpp:587
IStorage * m_pStorage
Definition: console.h:50
const CCommandInfo * GetCommandInfo(const char *pName, int FlagMask, bool Temp) override
Definition: console.cpp:1039
void SetTeeHistorianCommandCallback(FTeeHistorianCommandCallback pfnCallback, void *pUser) override
Definition: console.cpp:342
static void ConCommandStatus(IConsole::IResult *pResult, void *pUser)
Definition: console.cpp:716
void Print(int Level, const char *pFrom, const char *pStr, ColorRGBA PrintColor=gs_ConsoleDefaultColor) const override
Definition: console.cpp:328
static void Con_Chain(IResult *pResult, void *pUserData)
Definition: console.cpp:995
CHeap m_TempCommands
Definition: console.h:54
std::vector< CExecutionQueueEntry > m_vExecutionQueue
Definition: console.h:171
Definition: memheap.h:11
Definition: color.h:210
Definition: console.h:74
int m_AccessLevel
Definition: console.h:76
Definition: console.h:45
unsigned m_NumArgs
Definition: console.h:47
Definition: console.h:18
static bool EmptyUnknownCommandCallback(const char *pCommand, void *pUser)
Definition: console.h:97
bool(* FUnknownCommandCallback)(const char *pCommand, void *pUser)
Definition: console.h:94
void(* FChainCommandCallback)(IResult *pResult, void *pUserData, FCommandCallback pfnCallback, void *pCallbackUserData)
Definition: console.h:93
void(* FPossibleCallback)(int Index, const char *pCmd, void *pUser)
Definition: console.h:91
void(* FCommandCallback)(IResult *pResult, void *pUserData)
Definition: console.h:92
@ ACCESS_LEVEL_ADMIN
Definition: console.h:28
@ ACCESS_LEVEL_USER
Definition: console.h:31
void(* FTeeHistorianCommandCallback)(int ClientId, int FlagMask, const char *pCmd, IResult *pResult, void *pUser)
Definition: console.h:90
Definition: storage.h:20
@ TYPE_ALL
Definition: storage.h:26
static const ColorRGBA gs_ConsoleDefaultColor(1, 1, 1, 1)
#define dbg_assert(test, msg)
Definition: system.h:76
void mem_zero(T *block, size_t size)
Definition: system.h:178
void mem_copy(void *dest, const void *source, size_t size)
Definition: system.cpp:174
Definition: checksum.h:7