]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/framework/CmdSystem.h
Various Mac OS X tweaks to get this to build. Probably breaking things.
[icculus/iodoom3.git] / neo / framework / CmdSystem.h
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 /*
33 ===============================================================================
34
35         Console command execution and command text buffering.
36
37         Any number of commands can be added in a frame from several different
38         sources. Most commands come from either key bindings or console line input,
39         but entire text files can be execed.
40
41         Command execution takes a null terminated string, breaks it into tokens,
42         then searches for a command or variable that matches the first token.
43
44 ===============================================================================
45 */
46
47 // command flags
48 typedef enum {
49         CMD_FL_ALL                              = -1,
50         CMD_FL_CHEAT                    = BIT(0),       // command is considered a cheat
51         CMD_FL_SYSTEM                   = BIT(1),       // system command
52         CMD_FL_RENDERER                 = BIT(2),       // renderer command
53         CMD_FL_SOUND                    = BIT(3),       // sound command
54         CMD_FL_GAME                             = BIT(4),       // game command
55         CMD_FL_TOOL                             = BIT(5)        // tool command
56 } cmdFlags_t;
57
58 // parameters for command buffer stuffing
59 typedef enum {
60         CMD_EXEC_NOW,                                           // don't return until completed
61         CMD_EXEC_INSERT,                                        // insert at current position, but don't run yet
62         CMD_EXEC_APPEND                                         // add to end of the command buffer (normal case)
63 } cmdExecution_t;
64
65 // command function
66 typedef void (*cmdFunction_t)( const idCmdArgs &args );
67
68 // argument completion function
69 typedef void (*argCompletion_t)( const idCmdArgs &args, void(*callback)( const char *s ) );
70
71
72 class idCmdSystem {
73 public:
74         virtual                         ~idCmdSystem( void ) {}
75
76         virtual void            Init( void ) = 0;
77         virtual void            Shutdown( void ) = 0;
78
79                                                 // Registers a command and the function to call for it.
80         virtual void            AddCommand( const char *cmdName, cmdFunction_t function, int flags, const char *description, argCompletion_t argCompletion = NULL ) = 0;
81                                                 // Removes a command.
82         virtual void            RemoveCommand( const char *cmdName ) = 0;
83                                                 // Remove all commands with one of the flags set.
84         virtual void            RemoveFlaggedCommands( int flags ) = 0;
85
86                                                 // Command and argument completion using callback for each valid string.
87         virtual void            CommandCompletion( void(*callback)( const char *s ) ) = 0;
88         virtual void            ArgCompletion( const char *cmdString, void(*callback)( const char *s ) ) = 0;
89
90                                                 // Adds command text to the command buffer, does not add a final \n
91         virtual void            BufferCommandText( cmdExecution_t exec, const char *text ) = 0;
92                                                 // Pulls off \n \r or ; terminated lines of text from the command buffer and
93                                                 // executes the commands. Stops when the buffer is empty.
94                                                 // Normally called once per frame, but may be explicitly invoked.
95         virtual void            ExecuteCommandBuffer( void ) = 0;
96
97                                                 // Base for path/file auto-completion.
98         virtual void            ArgCompletion_FolderExtension( const idCmdArgs &args, void(*callback)( const char *s ), const char *folder, bool stripFolder, ... ) = 0;
99                                                 // Base for decl name auto-completion.
100         virtual void            ArgCompletion_DeclName( const idCmdArgs &args, void(*callback)( const char *s ), int type ) = 0;
101
102                                                 // Adds to the command buffer in tokenized form ( CMD_EXEC_NOW or CMD_EXEC_APPEND only )
103         virtual void            BufferCommandArgs( cmdExecution_t exec, const idCmdArgs &args ) = 0;
104
105                                                 // Setup a reloadEngine to happen on next command run, and give a command to execute after reload
106         virtual void            SetupReloadEngine( const idCmdArgs &args ) = 0;
107         virtual bool            PostReloadEngine( void ) = 0;
108
109                                                 // Default argument completion functions.
110         static void                     ArgCompletion_Boolean( const idCmdArgs &args, void(*callback)( const char *s ) );
111         template<int min,int max>
112         static void                     ArgCompletion_Integer( const idCmdArgs &args, void(*callback)( const char *s ) );
113         template<const char **strings>
114         static void                     ArgCompletion_String( const idCmdArgs &args, void(*callback)( const char *s ) );
115         template<int type>
116         static void                     ArgCompletion_Decl( const idCmdArgs &args, void(*callback)( const char *s ) );
117         static void                     ArgCompletion_FileName( const idCmdArgs &args, void(*callback)( const char *s ) );
118         static void                     ArgCompletion_MapName( const idCmdArgs &args, void(*callback)( const char *s ) );
119         static void                     ArgCompletion_ModelName( const idCmdArgs &args, void(*callback)( const char *s ) );
120         static void                     ArgCompletion_SoundName( const idCmdArgs &args, void(*callback)( const char *s ) );
121         static void                     ArgCompletion_ImageName( const idCmdArgs &args, void(*callback)( const char *s ) );
122         static void                     ArgCompletion_VideoName( const idCmdArgs &args, void(*callback)( const char *s ) );
123         static void                     ArgCompletion_ConfigName( const idCmdArgs &args, void(*callback)( const char *s ) );
124         static void                     ArgCompletion_SaveGame( const idCmdArgs &args, void(*callback)( const char *s ) );
125         static void                     ArgCompletion_DemoName( const idCmdArgs &args, void(*callback)( const char *s ) );
126 };
127
128 extern idCmdSystem *    cmdSystem;
129
130
131 ID_INLINE void idCmdSystem::ArgCompletion_Boolean( const idCmdArgs &args, void(*callback)( const char *s ) ) {
132         callback( va( "%s 0", args.Argv( 0 ) ) );
133         callback( va( "%s 1", args.Argv( 0 ) ) );
134 }
135
136 template<int min,int max> ID_STATIC_TEMPLATE ID_INLINE void idCmdSystem::ArgCompletion_Integer( const idCmdArgs &args, void(*callback)( const char *s ) ) {
137         for ( int i = min; i <= max; i++ ) {
138                 callback( va( "%s %d", args.Argv( 0 ), i ) );
139         }
140 }
141
142 template<const char **strings> ID_STATIC_TEMPLATE ID_INLINE void idCmdSystem::ArgCompletion_String( const idCmdArgs &args, void(*callback)( const char *s ) ) {
143         for ( int i = 0; strings[i]; i++ ) {
144                 callback( va( "%s %s", args.Argv( 0 ), strings[i] ) );
145         }
146 }
147
148 template<int type> ID_STATIC_TEMPLATE ID_INLINE void idCmdSystem::ArgCompletion_Decl( const idCmdArgs &args, void(*callback)( const char *s ) ) {
149         cmdSystem->ArgCompletion_DeclName( args, callback, type );
150 }
151
152 ID_INLINE void idCmdSystem::ArgCompletion_FileName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
153         cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", true, "", NULL );
154 }
155
156 ID_INLINE void idCmdSystem::ArgCompletion_MapName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
157         cmdSystem->ArgCompletion_FolderExtension( args, callback, "maps/", true, ".map", NULL );
158 }
159
160 ID_INLINE void idCmdSystem::ArgCompletion_ModelName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
161         cmdSystem->ArgCompletion_FolderExtension( args, callback, "models/", false, ".lwo", ".ase", ".md5mesh", ".ma", NULL );
162 }
163
164 ID_INLINE void idCmdSystem::ArgCompletion_SoundName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
165         cmdSystem->ArgCompletion_FolderExtension( args, callback, "sound/", false, ".wav", ".ogg", NULL );
166 }
167
168 ID_INLINE void idCmdSystem::ArgCompletion_ImageName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
169         cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", false, ".tga", ".dds", ".jpg", ".pcx", NULL );
170 }
171
172 ID_INLINE void idCmdSystem::ArgCompletion_VideoName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
173         cmdSystem->ArgCompletion_FolderExtension( args, callback, "video/", false, ".roq", NULL );
174 }
175
176 ID_INLINE void idCmdSystem::ArgCompletion_ConfigName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
177         cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", true, ".cfg", NULL );
178 }
179
180 ID_INLINE void idCmdSystem::ArgCompletion_SaveGame( const idCmdArgs &args, void(*callback)( const char *s ) ) {
181         cmdSystem->ArgCompletion_FolderExtension( args, callback, "SaveGames/", true, ".save", NULL );
182 }
183
184 ID_INLINE void idCmdSystem::ArgCompletion_DemoName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
185         cmdSystem->ArgCompletion_FolderExtension( args, callback, "demos/", true, ".demo", NULL );
186 }
187
188 #endif /* !__CMDSYSTEM_H__ */