CmdSystem.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  17. 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.
  18. ===========================================================================
  19. */
  20. #ifndef __CMDSYSTEM_H__
  21. #define __CMDSYSTEM_H__
  22. /*
  23. ===============================================================================
  24. Console command execution and command text buffering.
  25. Any number of commands can be added in a frame from several different
  26. sources. Most commands come from either key bindings or console line input,
  27. but entire text files can be execed.
  28. Command execution takes a null terminated string, breaks it into tokens,
  29. then searches for a command or variable that matches the first token.
  30. ===============================================================================
  31. */
  32. // command flags
  33. typedef enum {
  34. CMD_FL_ALL = -1,
  35. CMD_FL_CHEAT = BIT(0), // command is considered a cheat
  36. CMD_FL_SYSTEM = BIT(1), // system command
  37. CMD_FL_RENDERER = BIT(2), // renderer command
  38. CMD_FL_SOUND = BIT(3), // sound command
  39. CMD_FL_GAME = BIT(4), // game command
  40. CMD_FL_TOOL = BIT(5) // tool command
  41. } cmdFlags_t;
  42. // parameters for command buffer stuffing
  43. typedef enum {
  44. CMD_EXEC_NOW, // don't return until completed
  45. CMD_EXEC_INSERT, // insert at current position, but don't run yet
  46. CMD_EXEC_APPEND // add to end of the command buffer (normal case)
  47. } cmdExecution_t;
  48. // command function
  49. typedef void (*cmdFunction_t)( const idCmdArgs &args );
  50. // argument completion function
  51. typedef void (*argCompletion_t)( const idCmdArgs &args, void(*callback)( const char *s ) );
  52. /*
  53. ================================================
  54. idCommandLink is a convenient way to get a function registered as a
  55. ConsoleCommand without having to add an explicit call to idCmdSystem->AddCommand() in a startup
  56. function somewhere. Simply declare a static variable with the parameters and it will get
  57. executed before main(). For example:
  58. static idCommandLink sys_dumpMemory( "sys_dumpMemory", Sys_DumpMemory_f, "Walks the heap and reports stats" );
  59. ================================================
  60. */
  61. class idCommandLink {
  62. public:
  63. idCommandLink( const char *cmdName, cmdFunction_t function,
  64. const char *description, argCompletion_t argCompletion = NULL );
  65. idCommandLink * next;
  66. const char * cmdName_;
  67. cmdFunction_t function_;
  68. const char * description_;
  69. argCompletion_t argCompletion_;
  70. };
  71. // The command system will create commands for all the static definitions
  72. // when it initializes.
  73. idCommandLink *CommandLinks( idCommandLink *cl = NULL );
  74. /*
  75. ================================================
  76. The CONSOLE_COMMAND macro is an even easier way to create a console command by
  77. automatically generating the idCommandLink variable, and it also allows all the
  78. command code to be stripped from a build with a single define. For example:
  79. CONSOLE_COMMAND( Sys_DumpMemory, "Walks the heap and reports stats" ) {
  80. // do stuff
  81. }
  82. NOTE: All CONSOLE_COMMANDs will be stripped with the shipping build unless it's
  83. created using the CONSOLE_COMMAND_SHIP macro.
  84. ================================================
  85. */
  86. #if defined ( ID_RETAIL ) && !defined( ID_RETAIL_INTERNAL )
  87. #define CONSOLE_COMMAND_SHIP CONSOLE_COMMAND_COMPILE
  88. #define CONSOLE_COMMAND CONSOLE_COMMAND_NO_COMPILE
  89. // We need to disable this warning to get commands that were made friends
  90. // of classes to compile as inline.
  91. // warning C4211: nonstandard extension used : redefined extern to static
  92. #pragma warning( disable : 4211 )
  93. // warning C4505: 'xxx' : unreferenced local function has been removed
  94. #pragma warning( disable : 4505 )
  95. #else
  96. #define CONSOLE_COMMAND_SHIP CONSOLE_COMMAND_COMPILE
  97. #define CONSOLE_COMMAND CONSOLE_COMMAND_COMPILE
  98. #endif
  99. // Turn console commands into static inline code, which will cause them to be
  100. // removed from the build.
  101. #define CONSOLE_COMMAND_NO_COMPILE( name, comment, completion ) \
  102. static inline void name ## _f( const idCmdArgs &args )
  103. // 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()')
  104. // I can't figure out how to disable this just around CONSOLE_COMMAND, so it must stay disabled everywhere,
  105. // which is a shame.
  106. //lint -e1568
  107. #define CONSOLE_COMMAND_COMPILE( name, comment, completion ) \
  108. void name ## _f( const idCmdArgs &args ); \
  109. idCommandLink name ## _v( #name, name ## _f, comment, completion ); \
  110. void name ## _f( const idCmdArgs &args )
  111. class idCmdSystem {
  112. public:
  113. virtual ~idCmdSystem() {}
  114. virtual void Init() = 0;
  115. virtual void Shutdown() = 0;
  116. // Registers a command and the function to call for it.
  117. virtual void AddCommand( const char *cmdName, cmdFunction_t function, int flags, const char *description, argCompletion_t argCompletion = NULL ) = 0;
  118. // Removes a command.
  119. virtual void RemoveCommand( const char *cmdName ) = 0;
  120. // Remove all commands with one of the flags set.
  121. virtual void RemoveFlaggedCommands( int flags ) = 0;
  122. // Command and argument completion using callback for each valid string.
  123. virtual void CommandCompletion( void(*callback)( const char *s ) ) = 0;
  124. virtual void ArgCompletion( const char *cmdString, void(*callback)( const char *s ) ) = 0;
  125. virtual void ExecuteCommandText( const char * text ) = 0;
  126. virtual void AppendCommandText( const char * text ) = 0;
  127. // Adds command text to the command buffer, does not add a final \n
  128. virtual void BufferCommandText( cmdExecution_t exec, const char *text ) = 0;
  129. // Pulls off \n \r or ; terminated lines of text from the command buffer and
  130. // executes the commands. Stops when the buffer is empty.
  131. // Normally called once per frame, but may be explicitly invoked.
  132. virtual void ExecuteCommandBuffer() = 0;
  133. // Base for path/file auto-completion.
  134. virtual void ArgCompletion_FolderExtension( const idCmdArgs &args, void(*callback)( const char *s ), const char *folder, bool stripFolder, ... ) = 0;
  135. // Base for decl name auto-completion.
  136. virtual void ArgCompletion_DeclName( const idCmdArgs &args, void(*callback)( const char *s ), int type ) = 0;
  137. // Adds to the command buffer in tokenized form ( CMD_EXEC_NOW or CMD_EXEC_APPEND only )
  138. virtual void BufferCommandArgs( cmdExecution_t exec, const idCmdArgs &args ) = 0;
  139. // Setup a reloadEngine to happen on next command run, and give a command to execute after reload
  140. virtual void SetupReloadEngine( const idCmdArgs &args ) = 0;
  141. virtual bool PostReloadEngine() = 0;
  142. // Default argument completion functions.
  143. static void ArgCompletion_Boolean( const idCmdArgs &args, void(*callback)( const char *s ) );
  144. template<int min,int max>
  145. static void ArgCompletion_Integer( const idCmdArgs &args, void(*callback)( const char *s ) );
  146. template<const char **strings>
  147. static void ArgCompletion_String( const idCmdArgs &args, void(*callback)( const char *s ) );
  148. template<int type>
  149. static void ArgCompletion_Decl( const idCmdArgs &args, void(*callback)( const char *s ) );
  150. static void ArgCompletion_FileName( const idCmdArgs &args, void(*callback)( const char *s ) );
  151. static void ArgCompletion_MapName( const idCmdArgs &args, void(*callback)( const char *s ) );
  152. static void ArgCompletion_ModelName( const idCmdArgs &args, void(*callback)( const char *s ) );
  153. static void ArgCompletion_SoundName( const idCmdArgs &args, void(*callback)( const char *s ) );
  154. static void ArgCompletion_ImageName( const idCmdArgs &args, void(*callback)( const char *s ) );
  155. static void ArgCompletion_VideoName( const idCmdArgs &args, void(*callback)( const char *s ) );
  156. static void ArgCompletion_ConfigName( const idCmdArgs &args, void(*callback)( const char *s ) );
  157. static void ArgCompletion_SaveGame( const idCmdArgs &args, void(*callback)( const char *s ) );
  158. static void ArgCompletion_DemoName( const idCmdArgs &args, void(*callback)( const char *s ) );
  159. };
  160. extern idCmdSystem * cmdSystem;
  161. ID_INLINE void idCmdSystem::ArgCompletion_Boolean( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  162. callback( va( "%s 0", args.Argv( 0 ) ) );
  163. callback( va( "%s 1", args.Argv( 0 ) ) );
  164. }
  165. template<int min,int max> ID_INLINE void idCmdSystem::ArgCompletion_Integer( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  166. for ( int i = min; i <= max; i++ ) {
  167. callback( va( "%s %d", args.Argv( 0 ), i ) );
  168. }
  169. }
  170. template<const char **strings> ID_INLINE void idCmdSystem::ArgCompletion_String( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  171. for ( int i = 0; strings[i]; i++ ) {
  172. callback( va( "%s %s", args.Argv( 0 ), strings[i] ) );
  173. }
  174. }
  175. template<int type> ID_INLINE void idCmdSystem::ArgCompletion_Decl( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  176. cmdSystem->ArgCompletion_DeclName( args, callback, type );
  177. }
  178. ID_INLINE void idCmdSystem::ArgCompletion_FileName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  179. cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", true, "", NULL );
  180. }
  181. ID_INLINE void idCmdSystem::ArgCompletion_MapName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  182. cmdSystem->ArgCompletion_FolderExtension( args, callback, "maps/", true, ".map", NULL );
  183. }
  184. ID_INLINE void idCmdSystem::ArgCompletion_ModelName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  185. cmdSystem->ArgCompletion_FolderExtension( args, callback, "models/", false, ".lwo", ".ase", ".md5mesh", ".ma", NULL );
  186. }
  187. ID_INLINE void idCmdSystem::ArgCompletion_SoundName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  188. cmdSystem->ArgCompletion_FolderExtension( args, callback, "sound/", false, ".wav", NULL );
  189. }
  190. ID_INLINE void idCmdSystem::ArgCompletion_ImageName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  191. cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", false, ".tga", ".dds", ".jpg", ".pcx", NULL );
  192. }
  193. ID_INLINE void idCmdSystem::ArgCompletion_VideoName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  194. cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", false, ".bik", NULL );
  195. }
  196. ID_INLINE void idCmdSystem::ArgCompletion_ConfigName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  197. cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", true, ".cfg", NULL );
  198. }
  199. ID_INLINE void idCmdSystem::ArgCompletion_SaveGame( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  200. cmdSystem->ArgCompletion_FolderExtension( args, callback, "SaveGames/", true, ".save", NULL );
  201. }
  202. ID_INLINE void idCmdSystem::ArgCompletion_DemoName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  203. cmdSystem->ArgCompletion_FolderExtension( args, callback, "demos/", true, ".demo", NULL );
  204. }
  205. #endif /* !__CMDSYSTEM_H__ */