1 //**************************************************************************
3 //** ## ## ## ## ## #### #### ### ###
4 //** ## ## ## ## ## ## ## ## ## ## #### ####
5 //** ## ## ## ## ## ## ## ## ## ## ## ## ## ##
6 //** ## ## ######## ## ## ## ## ## ## ## ### ##
7 //** ### ## ## ### ## ## ## ## ## ##
8 //** # ## ## # #### #### ## ##
10 //** Copyright (C) 1999-2006 Jānis Legzdiņš
11 //** Copyright (C) 2018-2023 Ketmar Dark
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.
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.
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/>.
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; } \
43 public: // fuck you, shitplusplus
44 // console command alias
55 static bool Initialised
;
58 static TArray
<VStr
> AutoCompleteTable
;
60 static VCommand
*Cmds
;
61 static TArray
<VAlias
> AliasList
;
62 static TMap
<VStrCI
, int> AliasMap
;
64 static bool cliInserted
;
67 static bool execLogInit
;
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 ();
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
;
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
;
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
);
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
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); }
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 { \
161 TCmd ## name() : VCommand(#name) { rebuildCache = true; } \
162 virtual void Run () override; \
165 void TCmd ## name::Run ()
168 #define COMMAND_WITH_AC(name) \
169 static class TCmd ## name : public VCommand { \
171 TCmd ## name() : VCommand(#name) { rebuildCache = true; } \
172 virtual void Run () override; \
173 virtual VStr AutoCompleteArg (const TArray<VStr> &args, int aidx) override; \
176 void TCmd ## name::Run()
178 #define COMMAND_AC(name) \
179 VStr TCmd ## name::AutoCompleteArg (const TArray<VStr> &args, int aidx)
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()); \
187 /*static*/ const char *acstrings[] = { __VA_ARGS__ , nullptr }; \
189 for (const char **ssp = acstrings; *ssp; ++ssp) list.append(VStr(*ssp)); \
190 return AutoCompleteFromListCmd(prefix, list); \
192 return VStr::EmptyString; \
197 // ////////////////////////////////////////////////////////////////////////// //
205 // returns `false` if wait expired
209 VCmdBuf () : WaitEndTime(0) {}
210 void Insert (const char *);
212 void Print (const char *);
216 inline VCmdBuf
&operator << (const char *data
) {
221 inline VCmdBuf
&operator << (VStr data
) {
226 friend class TCmdwait
;
229 // main command buffer
230 extern VCmdBuf GCmdBuf
;