engine: reject mbf21 and shit24 wads. there is no way to know if it is safe to ignore...
[k8vavoom.git] / source / cmd.h
blob76236707939e8cbc7a141fd5fb306d4855152bc9
1 //**************************************************************************
2 //**
3 //** ## ## ## ## ## #### #### ### ###
4 //** ## ## ## ## ## ## ## ## ## ## #### ####
5 //** ## ## ## ## ## ## ## ## ## ## ## ## ## ##
6 //** ## ## ######## ## ## ## ## ## ## ## ### ##
7 //** ### ## ## ### ## ## ## ## ## ##
8 //** # ## ## # #### #### ## ##
9 //**
10 //** Copyright (C) 1999-2006 Jānis Legzdiņš
11 //** Copyright (C) 2018-2023 Ketmar Dark
12 //**
13 //** This program is free software: you can redistribute it and/or modify
14 //** it under the terms of the GNU General Public License as published by
15 //** the Free Software Foundation, version 3 of the License ONLY.
16 //**
17 //** This program is distributed in the hope that it will be useful,
18 //** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 //** GNU General Public License for more details.
21 //**
22 //** You should have received a copy of the GNU General Public License
23 //** along with this program. If not, see <http://www.gnu.org/licenses/>.
24 //**
25 //**************************************************************************
26 #ifndef VAVOOM_CMD_HEADER
27 #define VAVOOM_CMD_HEADER
30 #define CMD_FORWARD_TO_SERVER() do { \
31 if (Source == SRC_Command) { ForwardToServer(); return; } \
32 } while (0)
35 // a console command
36 class VCommand {
37 public:
38 enum ECmdSource {
39 SRC_Command,
40 SRC_Client,
43 public: // fuck you, shitplusplus
44 // console command alias
45 struct VAlias {
46 VStr Name;
47 VStr CmdLine;
48 bool Save;
51 private:
52 const char *Name;
53 VCommand *Next;
55 static bool Initialised;
56 static VStr Original;
58 static TArray<VStr> AutoCompleteTable;
60 static VCommand *Cmds;
61 static TArray<VAlias> AliasList;
62 static TMap<VStrCI, int> AliasMap;
64 static bool cliInserted;
66 protected:
67 static bool execLogInit;
69 private:
70 // substiture "${1}" and such args; used for aliases
71 static VStr SubstituteArgs (VStr str);
73 static VStr ExpandSigil (VStr str);
75 // this also expands sigils
76 static void TokeniseString (VStr str);
78 static void rebuildCommandCache ();
80 static void rebuildAliasMap ();
82 static void ProcessSetCommand ();
84 protected:
85 static TArray<VStr> Args;
86 static ECmdSource Source;
87 static VBasePlayer *Player; // for SRC_Client
89 static bool rebuildCache;
90 static TMap<VStrCI, VCommand *> locaseCache;
92 public:
93 static bool ParsingKeyConf;
94 static VStr CurrKeyConfKeySection;
95 static VStr CurrKeyConfWeaponSection;
96 static void (*onShowCompletionMatch) (bool isheader, VStr s);
98 // will be added before real CLI console commands
99 static VStr cliPreCmds;
101 public:
102 VCommand (const char *name);
103 virtual ~VCommand ();
105 virtual void Run () = 0;
107 // return non-empty string to replace arg
108 // note that aidx can be equal to `args.length()`, and will never be 0!
109 // args[0] is a command itself
110 // return string ends with space to move to next argument
111 virtual VStr AutoCompleteArg (const TArray<VStr> &args, int aidx);
113 static void Init ();
114 static void InsertCLICommands (); // should be called after loading startup scripts
115 static void WriteAlias (VStream *st);
116 static void Shutdown ();
117 //static void ProcessKeyConf ();
118 static void LoadKeyconfLump (int lump);
120 static void AddToAutoComplete (const char *string);
121 static void RemoveFromAutoComplete (const char *string);
122 static VStr GetAutoComplete (VStr prefix);
124 // returns empty string if no matches found, or list is empty
125 // if there is one exact match, return it with trailing space
126 // otherwise, return longest prefix
127 // if longest prefix is the same as input prefix, show all matches
128 // case-insensitive
129 // if `unchangedAsEmpty` is `true`, return empty string if result is equal to input prefix
130 static VStr AutoCompleteFromList (VStr prefix, const TArray <VStr> &list, bool unchangedAsEmpty=false, bool doSortHint=true, bool doQuoting=false);
132 // this provides correct arguments for in-command autocompletion
133 static inline VStr AutoCompleteFromListCmd (VStr prefix, const TArray <VStr> &list) { return AutoCompleteFromList(prefix, list, true, true, true); }
135 enum {
136 CT_UNKNOWN = 0,
137 CT_COMMAND,
138 CT_CVAR,
139 CT_ALIAS,
141 static int GetCommandType (VStr cmd);
143 static void ExecuteString (VStr, ECmdSource, VBasePlayer *);
144 static void ForwardToServer ();
145 static int CheckParm (const char *);
147 static int GetArgC ();
148 static VStr GetArgV (int idx);
150 friend class VCmdBuf;
151 friend class TCmdCmdList;
152 friend class TCmdAlias;
156 // ////////////////////////////////////////////////////////////////////////// //
157 // macro for declaring a console command
158 #define COMMAND(name) \
159 static class TCmd ## name : public VCommand { \
160 public: \
161 TCmd ## name() : VCommand(#name) { rebuildCache = true; } \
162 virtual void Run () override; \
163 } name ## _f; \
165 void TCmd ## name::Run ()
168 #define COMMAND_WITH_AC(name) \
169 static class TCmd ## name : public VCommand { \
170 public: \
171 TCmd ## name() : VCommand(#name) { rebuildCache = true; } \
172 virtual void Run () override; \
173 virtual VStr AutoCompleteArg (const TArray<VStr> &args, int aidx) override; \
174 } name ## _f; \
176 void TCmd ## name::Run()
178 #define COMMAND_AC(name) \
179 VStr TCmd ## name::AutoCompleteArg (const TArray<VStr> &args, int aidx)
181 // usage:
182 // COMMAND_AC_SIMPLE_LIST(TeleportNewMap, "forced")
183 #define COMMAND_AC_SIMPLE_LIST(name,...) \
184 VStr TCmd ## name::AutoCompleteArg (const TArray<VStr> &args, int aidx) { \
185 VStr prefix = (aidx < args.length() ? args[aidx] : VStr()); \
186 if (aidx == 1) { \
187 /*static*/ const char *acstrings[] = { __VA_ARGS__ , nullptr }; \
188 TArray<VStr> list; \
189 for (const char **ssp = acstrings; *ssp; ++ssp) list.append(VStr(*ssp)); \
190 return AutoCompleteFromListCmd(prefix, list); \
191 } else { \
192 return VStr::EmptyString; \
197 // ////////////////////////////////////////////////////////////////////////// //
198 // a command buffer
199 class VCmdBuf {
200 private:
201 VStr Buffer;
202 double WaitEndTime;
204 private:
205 // returns `false` if wait expired
206 bool checkWait ();
208 public:
209 VCmdBuf () : WaitEndTime(0) {}
210 void Insert (const char *);
211 void Insert (VStr);
212 void Print (const char *);
213 void Print (VStr);
214 void Exec ();
216 inline VCmdBuf &operator << (const char *data) {
217 Print(data);
218 return *this;
221 inline VCmdBuf &operator << (VStr data) {
222 Print(data);
223 return *this;
226 friend class TCmdwait;
229 // main command buffer
230 extern VCmdBuf GCmdBuf;
233 #endif