1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 #ifndef __CMDSYSTEM_H__
30 #define __CMDSYSTEM_H__
31
32 #include "idlib/Str.h"
33
34 /*
35 ===============================================================================
36
37 Console command execution and command text buffering.
38
39 Any number of commands can be added in a frame from several different
40 sources. Most commands come from either key bindings or console line input,
41 but entire text files can be execed.
42
43 Command execution takes a null terminated string, breaks it into tokens,
44 then searches for a command or variable that matches the first token.
45
46 ===============================================================================
47 */
48
49 // command flags
50 typedef enum {
51 CMD_FL_ALL = -1,
52 CMD_FL_CHEAT = BIT(0), // command is considered a cheat
53 CMD_FL_SYSTEM = BIT(1), // system command
54 CMD_FL_RENDERER = BIT(2), // renderer command
55 CMD_FL_SOUND = BIT(3), // sound command
56 CMD_FL_GAME = BIT(4), // game command
57 CMD_FL_TOOL = BIT(5) // tool command
58 } cmdFlags_t;
59
60 // parameters for command buffer stuffing
61 typedef enum {
62 CMD_EXEC_NOW, // don't return until completed
63 CMD_EXEC_INSERT, // insert at current position, but don't run yet
64 CMD_EXEC_APPEND // add to end of the command buffer (normal case)
65 } cmdExecution_t;
66
67 // command function
68 typedef void (*cmdFunction_t)( const idCmdArgs &args );
69
70 // argument completion function
71 typedef void (*argCompletion_t)( const idCmdArgs &args, void(*callback)( const char *s ) );
72
73
74 class idCmdSystem {
75 public:
~idCmdSystem(void)76 virtual ~idCmdSystem( void ) {}
77
78 virtual void Init( void ) = 0;
79 virtual void Shutdown( void ) = 0;
80
81 // Registers a command and the function to call for it.
82 virtual void AddCommand( const char *cmdName, cmdFunction_t function, int flags, const char *description, argCompletion_t argCompletion = NULL ) = 0;
83 // Removes a command.
84 virtual void RemoveCommand( const char *cmdName ) = 0;
85 // Remove all commands with one of the flags set.
86 virtual void RemoveFlaggedCommands( int flags ) = 0;
87
88 // Command and argument completion using callback for each valid string.
89 virtual void CommandCompletion( void(*callback)( const char *s ) ) = 0;
90 virtual void ArgCompletion( const char *cmdString, void(*callback)( const char *s ) ) = 0;
91
92 // Adds command text to the command buffer, does not add a final \n
93 virtual void BufferCommandText( cmdExecution_t exec, const char *text ) = 0;
94 // Pulls off \n \r or ; terminated lines of text from the command buffer and
95 // executes the commands. Stops when the buffer is empty.
96 // Normally called once per frame, but may be explicitly invoked.
97 virtual void ExecuteCommandBuffer( void ) = 0;
98
99 // Base for path/file auto-completion.
100 virtual void ArgCompletion_FolderExtension( const idCmdArgs &args, void(*callback)( const char *s ), const char *folder, bool stripFolder, ... ) = 0;
101 // Base for decl name auto-completion.
102 virtual void ArgCompletion_DeclName( const idCmdArgs &args, void(*callback)( const char *s ), int type ) = 0;
103
104 // Adds to the command buffer in tokenized form ( CMD_EXEC_NOW or CMD_EXEC_APPEND only )
105 virtual void BufferCommandArgs( cmdExecution_t exec, const idCmdArgs &args ) = 0;
106
107 // Setup a reloadEngine to happen on next command run, and give a command to execute after reload
108 virtual void SetupReloadEngine( const idCmdArgs &args ) = 0;
109 virtual bool PostReloadEngine( void ) = 0;
110
111 // Default argument completion functions.
112 static void ArgCompletion_Boolean( const idCmdArgs &args, void(*callback)( const char *s ) );
113 template<int min,int max>
114 static void ArgCompletion_Integer( const idCmdArgs &args, void(*callback)( const char *s ) );
115 template<const char **strings>
116 static void ArgCompletion_String( const idCmdArgs &args, void(*callback)( const char *s ) );
117 template<int type>
118 static void ArgCompletion_Decl( const idCmdArgs &args, void(*callback)( const char *s ) );
119 static void ArgCompletion_FileName( const idCmdArgs &args, void(*callback)( const char *s ) );
120 static void ArgCompletion_MapName( const idCmdArgs &args, void(*callback)( const char *s ) );
121 static void ArgCompletion_ModelName( const idCmdArgs &args, void(*callback)( const char *s ) );
122 static void ArgCompletion_SoundName( const idCmdArgs &args, void(*callback)( const char *s ) );
123 static void ArgCompletion_ImageName( const idCmdArgs &args, void(*callback)( const char *s ) );
124 static void ArgCompletion_VideoName( const idCmdArgs &args, void(*callback)( const char *s ) );
125 static void ArgCompletion_ConfigName( const idCmdArgs &args, void(*callback)( const char *s ) );
126 static void ArgCompletion_SaveGame( const idCmdArgs &args, void(*callback)( const char *s ) );
127 static void ArgCompletion_DemoName( const idCmdArgs &args, void(*callback)( const char *s ) );
128 };
129
130 extern idCmdSystem * cmdSystem;
131
132
ArgCompletion_Boolean(const idCmdArgs & args,void (* callback)(const char * s))133 ID_INLINE void idCmdSystem::ArgCompletion_Boolean( const idCmdArgs &args, void(*callback)( const char *s ) ) {
134 callback( va( "%s 0", args.Argv( 0 ) ) );
135 callback( va( "%s 1", args.Argv( 0 ) ) );
136 }
137
ArgCompletion_Integer(const idCmdArgs & args,void (* callback)(const char * s))138 template<int min,int max> ID_STATIC_TEMPLATE ID_INLINE void idCmdSystem::ArgCompletion_Integer( const idCmdArgs &args, void(*callback)( const char *s ) ) {
139 for ( int i = min; i <= max; i++ ) {
140 callback( va( "%s %d", args.Argv( 0 ), i ) );
141 }
142 }
143
ArgCompletion_String(const idCmdArgs & args,void (* callback)(const char * s))144 template<const char **strings> ID_STATIC_TEMPLATE ID_INLINE void idCmdSystem::ArgCompletion_String( const idCmdArgs &args, void(*callback)( const char *s ) ) {
145 for ( int i = 0; strings[i]; i++ ) {
146 callback( va( "%s %s", args.Argv( 0 ), strings[i] ) );
147 }
148 }
149
ArgCompletion_Decl(const idCmdArgs & args,void (* callback)(const char * s))150 template<int type> ID_STATIC_TEMPLATE ID_INLINE void idCmdSystem::ArgCompletion_Decl( const idCmdArgs &args, void(*callback)( const char *s ) ) {
151 cmdSystem->ArgCompletion_DeclName( args, callback, type );
152 }
153
ArgCompletion_FileName(const idCmdArgs & args,void (* callback)(const char * s))154 ID_INLINE void idCmdSystem::ArgCompletion_FileName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
155 cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", true, "", NULL );
156 }
157
ArgCompletion_MapName(const idCmdArgs & args,void (* callback)(const char * s))158 ID_INLINE void idCmdSystem::ArgCompletion_MapName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
159 cmdSystem->ArgCompletion_FolderExtension( args, callback, "maps/", true, ".map", NULL );
160 }
161
ArgCompletion_ModelName(const idCmdArgs & args,void (* callback)(const char * s))162 ID_INLINE void idCmdSystem::ArgCompletion_ModelName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
163 cmdSystem->ArgCompletion_FolderExtension( args, callback, "models/", false, ".lwo", ".ase", ".md5mesh", ".ma", NULL );
164 }
165
ArgCompletion_SoundName(const idCmdArgs & args,void (* callback)(const char * s))166 ID_INLINE void idCmdSystem::ArgCompletion_SoundName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
167 cmdSystem->ArgCompletion_FolderExtension( args, callback, "sound/", false, ".wav", ".ogg", NULL );
168 }
169
ArgCompletion_ImageName(const idCmdArgs & args,void (* callback)(const char * s))170 ID_INLINE void idCmdSystem::ArgCompletion_ImageName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
171 cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", false, ".tga", ".dds", ".jpg", ".pcx", NULL );
172 }
173
ArgCompletion_VideoName(const idCmdArgs & args,void (* callback)(const char * s))174 ID_INLINE void idCmdSystem::ArgCompletion_VideoName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
175 cmdSystem->ArgCompletion_FolderExtension( args, callback, "video/", false, ".roq", NULL );
176 }
177
ArgCompletion_ConfigName(const idCmdArgs & args,void (* callback)(const char * s))178 ID_INLINE void idCmdSystem::ArgCompletion_ConfigName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
179 cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", true, ".cfg", NULL );
180 }
181
ArgCompletion_SaveGame(const idCmdArgs & args,void (* callback)(const char * s))182 ID_INLINE void idCmdSystem::ArgCompletion_SaveGame( const idCmdArgs &args, void(*callback)( const char *s ) ) {
183 cmdSystem->ArgCompletion_FolderExtension( args, callback, "SaveGames/", true, ".save", NULL );
184 }
185
ArgCompletion_DemoName(const idCmdArgs & args,void (* callback)(const char * s))186 ID_INLINE void idCmdSystem::ArgCompletion_DemoName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
187 cmdSystem->ArgCompletion_FolderExtension( args, callback, "demos/", true, ".demo", NULL );
188 }
189
190 #endif /* !__CMDSYSTEM_H__ */
191