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_CONSOLE_H
4#define ENGINE_CONSOLE_H
5
6#include "kernel.h"
7#include <base/color.h>
8#include <engine/storage.h>
9
10#include <memory>
11
12static const ColorRGBA gs_ConsoleDefaultColor(1, 1, 1, 1);
13
14enum LEVEL : char;
15struct CChecksumData;
16
17class IConsole : public IInterface
18{
19 MACRO_INTERFACE("console")
20public:
21 // TODO: rework/cleanup
22 enum
23 {
24 OUTPUT_LEVEL_STANDARD = 0,
25 OUTPUT_LEVEL_ADDINFO,
26 OUTPUT_LEVEL_DEBUG,
27
28 ACCESS_LEVEL_ADMIN = 0,
29 ACCESS_LEVEL_MOD,
30 ACCESS_LEVEL_HELPER,
31 ACCESS_LEVEL_USER,
32
33 TEMPCMD_NAME_LENGTH = 32,
34 TEMPCMD_HELP_LENGTH = 192,
35 TEMPCMD_PARAMS_LENGTH = 96,
36
37 CMDLINE_LENGTH = 512,
38
39 CLIENT_ID_GAME = -2,
40 CLIENT_ID_NO_GAME = -3,
41 };
42
43 // TODO: rework this interface to reduce the amount of virtual calls
44 class IResult
45 {
46 protected:
47 unsigned m_NumArgs;
48
49 public:
50 IResult() { m_NumArgs = 0; }
51 virtual ~IResult() {}
52
53 virtual int GetInteger(unsigned Index) const = 0;
54 virtual float GetFloat(unsigned Index) const = 0;
55 virtual const char *GetString(unsigned Index) const = 0;
56 virtual ColorHSLA GetColor(unsigned Index, bool Light) const = 0;
57
58 virtual void RemoveArgument(unsigned Index) = 0;
59
60 int NumArguments() const { return m_NumArgs; }
61 int m_ClientId;
62
63 // DDRace
64
65 virtual int GetVictim() const = 0;
66 };
67
68 class CCommandInfo
69 {
70 protected:
71 int m_AccessLevel;
72
73 public:
74 CCommandInfo() { m_AccessLevel = ACCESS_LEVEL_ADMIN; }
75 virtual ~CCommandInfo() {}
76 const char *m_pName;
77 const char *m_pHelp;
78 const char *m_pParams;
79
80 virtual const CCommandInfo *NextCommandInfo(int AccessLevel, int FlagMask) const = 0;
81
82 int GetAccessLevel() const { return m_AccessLevel; }
83 };
84
85 typedef void (*FTeeHistorianCommandCallback)(int ClientId, int FlagMask, const char *pCmd, IResult *pResult, void *pUser);
86 typedef void (*FPrintCallback)(const char *pStr, void *pUser, ColorRGBA PrintColor);
87 typedef void (*FPossibleCallback)(int Index, const char *pCmd, void *pUser);
88 typedef void (*FCommandCallback)(IResult *pResult, void *pUserData);
89 typedef void (*FChainCommandCallback)(IResult *pResult, void *pUserData, FCommandCallback pfnCallback, void *pCallbackUserData);
90 typedef bool (*FUnknownCommandCallback)(const char *pCommand, void *pUser); // returns true if the callback has handled the argument
91
92 static void EmptyPossibleCommandCallback(int Index, const char *pCmd, void *pUser) {}
93 static bool EmptyUnknownCommandCallback(const char *pCommand, void *pUser) { return false; }
94
95 virtual void Init() = 0;
96 virtual const CCommandInfo *FirstCommandInfo(int AccessLevel, int Flagmask) const = 0;
97 virtual const CCommandInfo *GetCommandInfo(const char *pName, int FlagMask, bool Temp) = 0;
98 virtual int PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback = EmptyPossibleCommandCallback, void *pUser = nullptr) = 0;
99 virtual void ParseArguments(int NumArgs, const char **ppArguments) = 0;
100
101 virtual void Register(const char *pName, const char *pParams, int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp) = 0;
102 virtual void RegisterTemp(const char *pName, const char *pParams, int Flags, const char *pHelp) = 0;
103 virtual void DeregisterTemp(const char *pName) = 0;
104 virtual void DeregisterTempAll() = 0;
105 virtual void Chain(const char *pName, FChainCommandCallback pfnChainFunc, void *pUser) = 0;
106 virtual void StoreCommands(bool Store) = 0;
107
108 virtual bool LineIsValid(const char *pStr) = 0;
109 virtual void ExecuteLine(const char *pStr, int ClientId = -1, bool InterpretSemicolons = true) = 0;
110 virtual void ExecuteLineFlag(const char *pStr, int FlasgMask, int ClientId = -1, bool InterpretSemicolons = true) = 0;
111 virtual void ExecuteLineStroked(int Stroke, const char *pStr, int ClientId = -1, bool InterpretSemicolons = true) = 0;
112 virtual bool ExecuteFile(const char *pFilename, int ClientId = -1, bool LogFailure = false, int StorageType = IStorage::TYPE_ALL) = 0;
113
114 virtual char *Format(char *pBuf, int Size, const char *pFrom, const char *pStr) = 0;
115 virtual void Print(int Level, const char *pFrom, const char *pStr, ColorRGBA PrintColor = gs_ConsoleDefaultColor) const = 0;
116 virtual void SetTeeHistorianCommandCallback(FTeeHistorianCommandCallback pfnCallback, void *pUser) = 0;
117 virtual void SetUnknownCommandCallback(FUnknownCommandCallback pfnCallback, void *pUser) = 0;
118 virtual void InitChecksum(CChecksumData *pData) const = 0;
119
120 virtual void SetAccessLevel(int AccessLevel) = 0;
121
122 static LEVEL ToLogLevel(int ConsoleLevel);
123 static int ToLogLevelFilter(int ConsoleLevel);
124
125 // DDRace
126
127 virtual bool Cheated() const = 0;
128
129 virtual int FlagMask() const = 0;
130 virtual void SetFlagMask(int FlagMask) = 0;
131};
132
133std::unique_ptr<IConsole> CreateConsole(int FlagMask);
134
135#endif // FILE_ENGINE_CONSOLE_H
136