DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

CmdSystem.h (12275B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition 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 BFG Edition 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 ================================================
     73 idCommandLink is a convenient way to get a function registered as a 
     74 ConsoleCommand without having to add an explicit call to idCmdSystem->AddCommand() in a startup 
     75 function somewhere. Simply declare a static variable with the parameters and it will get 
     76 executed before main(). For example:
     77 
     78 static idCommandLink sys_dumpMemory( "sys_dumpMemory", Sys_DumpMemory_f, "Walks the heap and reports stats" );
     79 ================================================
     80 */
     81 
     82 class idCommandLink {
     83 public:
     84 	idCommandLink( const char *cmdName, cmdFunction_t function,
     85 		const char *description, argCompletion_t argCompletion = NULL );
     86 	idCommandLink *	next;
     87 	const char *	cmdName_;
     88 	cmdFunction_t	function_;
     89 	const char *	description_;
     90 	argCompletion_t argCompletion_;
     91 };
     92 
     93 // The command system will create commands for all the static definitions
     94 // when it initializes.
     95 idCommandLink *CommandLinks( idCommandLink *cl = NULL );
     96 
     97 /*
     98 ================================================
     99 The CONSOLE_COMMAND macro is an even easier way to create a console command by
    100 automatically generating the idCommandLink variable, and it also allows all the
    101 command code to be stripped from a build with a single define.  For example:
    102 
    103 CONSOLE_COMMAND( Sys_DumpMemory, "Walks the heap and reports stats" ) {
    104 	// do stuff
    105 }
    106 
    107 NOTE: All CONSOLE_COMMANDs will be stripped with the shipping build unless it's
    108 created using the CONSOLE_COMMAND_SHIP macro.
    109 ================================================
    110 */
    111 
    112 #if defined ( ID_RETAIL ) && !defined( ID_RETAIL_INTERNAL )
    113 #define CONSOLE_COMMAND_SHIP			CONSOLE_COMMAND_COMPILE
    114 #define CONSOLE_COMMAND					CONSOLE_COMMAND_NO_COMPILE
    115 // We need to disable this warning to get commands that were made friends
    116 // of classes to compile as inline.
    117 // warning C4211: nonstandard extension used : redefined extern to static
    118 #pragma warning( disable : 4211 )
    119 // warning C4505: 'xxx' : unreferenced local function has been removed
    120 #pragma warning( disable : 4505 )
    121 #else
    122 #define CONSOLE_COMMAND_SHIP			CONSOLE_COMMAND_COMPILE
    123 #define CONSOLE_COMMAND					CONSOLE_COMMAND_COMPILE
    124 #endif
    125 
    126 // Turn console commands into static inline code, which will cause them to be
    127 // removed from the build.
    128 #define CONSOLE_COMMAND_NO_COMPILE( name, comment, completion ) \
    129 	static inline void name ## _f( const idCmdArgs &args )
    130 
    131 // lint incorrectly gives this for all console commands: Issue 1568: (Warning -- Variable 'TestAtomicString_v' accesses variable 'atomicStringManager' before the latter is initialized through calls: 'TestAtomicString_f() => idAtomicString::FreeDynamic()')
    132 // I can't figure out how to disable this just around CONSOLE_COMMAND, so it must stay disabled everywhere,
    133 // which is a shame.
    134 //lint -e1568
    135 #define CONSOLE_COMMAND_COMPILE( name, comment, completion ) \
    136 	void name ## _f( const idCmdArgs &args ); \
    137 	idCommandLink name ## _v( #name, name ## _f, comment, completion  ); \
    138 	void name ## _f( const idCmdArgs &args )
    139 
    140 class idCmdSystem {
    141 public:
    142 	virtual				~idCmdSystem() {}
    143 
    144 	virtual void		Init() = 0;
    145 	virtual void		Shutdown() = 0;
    146 
    147 						// Registers a command and the function to call for it.
    148 	virtual void		AddCommand( const char *cmdName, cmdFunction_t function, int flags, const char *description, argCompletion_t argCompletion = NULL ) = 0;
    149 						// Removes a command.
    150 	virtual void		RemoveCommand( const char *cmdName ) = 0;
    151 						// Remove all commands with one of the flags set.
    152 	virtual void		RemoveFlaggedCommands( int flags ) = 0;
    153 
    154 						// Command and argument completion using callback for each valid string.
    155 	virtual void		CommandCompletion( void(*callback)( const char *s ) ) = 0;
    156 	virtual void		ArgCompletion( const char *cmdString, void(*callback)( const char *s ) ) = 0;
    157 
    158 	virtual void		ExecuteCommandText( const char * text ) = 0;
    159 	virtual void		AppendCommandText( const char * text ) = 0;
    160 
    161 						// Adds command text to the command buffer, does not add a final \n
    162 	virtual void		BufferCommandText( cmdExecution_t exec, const char *text ) = 0;
    163 						// Pulls off \n \r or ; terminated lines of text from the command buffer and
    164 						// executes the commands. Stops when the buffer is empty.
    165 						// Normally called once per frame, but may be explicitly invoked.
    166 	virtual void		ExecuteCommandBuffer() = 0;
    167 
    168 						// Base for path/file auto-completion.
    169 	virtual void		ArgCompletion_FolderExtension( const idCmdArgs &args, void(*callback)( const char *s ), const char *folder, bool stripFolder, ... ) = 0;
    170 						// Base for decl name auto-completion.
    171 	virtual void		ArgCompletion_DeclName( const idCmdArgs &args, void(*callback)( const char *s ), int type ) = 0;
    172 
    173 						// Adds to the command buffer in tokenized form ( CMD_EXEC_NOW or CMD_EXEC_APPEND only )
    174 	virtual void		BufferCommandArgs( cmdExecution_t exec, const idCmdArgs &args ) = 0;
    175 
    176 						// Setup a reloadEngine to happen on next command run, and give a command to execute after reload
    177 	virtual void		SetupReloadEngine( const idCmdArgs &args ) = 0;
    178 	virtual bool		PostReloadEngine() = 0;
    179 
    180 						// Default argument completion functions.
    181 	static void			ArgCompletion_Boolean( const idCmdArgs &args, void(*callback)( const char *s ) );
    182 	template<int min,int max>
    183 	static void			ArgCompletion_Integer( const idCmdArgs &args, void(*callback)( const char *s ) );
    184 	template<const char **strings>
    185 	static void			ArgCompletion_String( const idCmdArgs &args, void(*callback)( const char *s ) );
    186 	template<int type>
    187 	static void			ArgCompletion_Decl( const idCmdArgs &args, void(*callback)( const char *s ) );
    188 	static void			ArgCompletion_FileName( const idCmdArgs &args, void(*callback)( const char *s ) );
    189 	static void			ArgCompletion_MapName( const idCmdArgs &args, void(*callback)( const char *s ) );
    190 	static void			ArgCompletion_ModelName( const idCmdArgs &args, void(*callback)( const char *s ) );
    191 	static void			ArgCompletion_SoundName( const idCmdArgs &args, void(*callback)( const char *s ) );
    192 	static void			ArgCompletion_ImageName( const idCmdArgs &args, void(*callback)( const char *s ) );
    193 	static void			ArgCompletion_VideoName( const idCmdArgs &args, void(*callback)( const char *s ) );
    194 	static void			ArgCompletion_ConfigName( const idCmdArgs &args, void(*callback)( const char *s ) );
    195 	static void			ArgCompletion_SaveGame( const idCmdArgs &args, void(*callback)( const char *s ) );
    196 	static void			ArgCompletion_DemoName( const idCmdArgs &args, void(*callback)( const char *s ) );
    197 };
    198 
    199 extern idCmdSystem *	cmdSystem;
    200 
    201 
    202 ID_INLINE void idCmdSystem::ArgCompletion_Boolean( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    203 	callback( va( "%s 0", args.Argv( 0 ) ) );
    204 	callback( va( "%s 1", args.Argv( 0 ) ) );
    205 }
    206 
    207 template<int min,int max> ID_INLINE void idCmdSystem::ArgCompletion_Integer( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    208 	for ( int i = min; i <= max; i++ ) {
    209 		callback( va( "%s %d", args.Argv( 0 ), i ) );
    210 	}
    211 }
    212 
    213 template<const char **strings> ID_INLINE void idCmdSystem::ArgCompletion_String( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    214 	for ( int i = 0; strings[i]; i++ ) {
    215 		callback( va( "%s %s", args.Argv( 0 ), strings[i] ) );
    216 	}
    217 }
    218 
    219 template<int type> ID_INLINE void idCmdSystem::ArgCompletion_Decl( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    220 	cmdSystem->ArgCompletion_DeclName( args, callback, type );
    221 }
    222 
    223 ID_INLINE void idCmdSystem::ArgCompletion_FileName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    224 	cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", true, "", NULL );
    225 }
    226 
    227 ID_INLINE void idCmdSystem::ArgCompletion_MapName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    228 	cmdSystem->ArgCompletion_FolderExtension( args, callback, "maps/", true, ".map", NULL );
    229 }
    230 
    231 ID_INLINE void idCmdSystem::ArgCompletion_ModelName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    232 	cmdSystem->ArgCompletion_FolderExtension( args, callback, "models/", false, ".lwo", ".ase", ".md5mesh", ".ma", NULL );
    233 }
    234 
    235 ID_INLINE void idCmdSystem::ArgCompletion_SoundName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    236 	cmdSystem->ArgCompletion_FolderExtension( args, callback, "sound/", false, ".wav", NULL );
    237 }
    238 
    239 ID_INLINE void idCmdSystem::ArgCompletion_ImageName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    240 	cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", false, ".tga", ".dds", ".jpg", ".pcx", NULL );
    241 }
    242 
    243 ID_INLINE void idCmdSystem::ArgCompletion_VideoName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    244 	cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", false, ".bik", NULL );
    245 }
    246 
    247 ID_INLINE void idCmdSystem::ArgCompletion_ConfigName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    248 	cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", true, ".cfg", NULL );
    249 }
    250 
    251 ID_INLINE void idCmdSystem::ArgCompletion_SaveGame( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    252 	cmdSystem->ArgCompletion_FolderExtension( args, callback, "SaveGames/", true, ".save", NULL );
    253 }
    254 
    255 ID_INLINE void idCmdSystem::ArgCompletion_DemoName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
    256 	cmdSystem->ArgCompletion_FolderExtension( args, callback, "demos/", true, ".demo", NULL );
    257 }
    258 
    259 #endif /* !__CMDSYSTEM_H__ */