CmdSystem.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  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. class idCmdSystem {
  53. public:
  54. virtual ~idCmdSystem( void ) {}
  55. virtual void Init( void ) = 0;
  56. virtual void Shutdown( void ) = 0;
  57. // Registers a command and the function to call for it.
  58. virtual void AddCommand( const char *cmdName, cmdFunction_t function, int flags, const char *description, argCompletion_t argCompletion = NULL ) = 0;
  59. // Removes a command.
  60. virtual void RemoveCommand( const char *cmdName ) = 0;
  61. // Remove all commands with one of the flags set.
  62. virtual void RemoveFlaggedCommands( int flags ) = 0;
  63. // Command and argument completion using callback for each valid string.
  64. virtual void CommandCompletion( void(*callback)( const char *s ) ) = 0;
  65. virtual void ArgCompletion( const char *cmdString, void(*callback)( const char *s ) ) = 0;
  66. // Adds command text to the command buffer, does not add a final \n
  67. virtual void BufferCommandText( cmdExecution_t exec, const char *text ) = 0;
  68. // Pulls off \n \r or ; terminated lines of text from the command buffer and
  69. // executes the commands. Stops when the buffer is empty.
  70. // Normally called once per frame, but may be explicitly invoked.
  71. virtual void ExecuteCommandBuffer( void ) = 0;
  72. // Base for path/file auto-completion.
  73. virtual void ArgCompletion_FolderExtension( const idCmdArgs &args, void(*callback)( const char *s ), const char *folder, bool stripFolder, ... ) = 0;
  74. // Base for decl name auto-completion.
  75. virtual void ArgCompletion_DeclName( const idCmdArgs &args, void(*callback)( const char *s ), int type ) = 0;
  76. // Adds to the command buffer in tokenized form ( CMD_EXEC_NOW or CMD_EXEC_APPEND only )
  77. virtual void BufferCommandArgs( cmdExecution_t exec, const idCmdArgs &args ) = 0;
  78. // Setup a reloadEngine to happen on next command run, and give a command to execute after reload
  79. virtual void SetupReloadEngine( const idCmdArgs &args ) = 0;
  80. virtual bool PostReloadEngine( void ) = 0;
  81. // Default argument completion functions.
  82. static void ArgCompletion_Boolean( const idCmdArgs &args, void(*callback)( const char *s ) );
  83. template<int min,int max>
  84. static void ArgCompletion_Integer( const idCmdArgs &args, void(*callback)( const char *s ) );
  85. template<const char **strings>
  86. static void ArgCompletion_String( const idCmdArgs &args, void(*callback)( const char *s ) );
  87. template<int type>
  88. static void ArgCompletion_Decl( const idCmdArgs &args, void(*callback)( const char *s ) );
  89. static void ArgCompletion_FileName( const idCmdArgs &args, void(*callback)( const char *s ) );
  90. static void ArgCompletion_MapName( const idCmdArgs &args, void(*callback)( const char *s ) );
  91. static void ArgCompletion_ModelName( const idCmdArgs &args, void(*callback)( const char *s ) );
  92. static void ArgCompletion_SoundName( const idCmdArgs &args, void(*callback)( const char *s ) );
  93. static void ArgCompletion_ImageName( const idCmdArgs &args, void(*callback)( const char *s ) );
  94. static void ArgCompletion_VideoName( const idCmdArgs &args, void(*callback)( const char *s ) );
  95. static void ArgCompletion_ConfigName( const idCmdArgs &args, void(*callback)( const char *s ) );
  96. static void ArgCompletion_SaveGame( const idCmdArgs &args, void(*callback)( const char *s ) );
  97. static void ArgCompletion_DemoName( const idCmdArgs &args, void(*callback)( const char *s ) );
  98. };
  99. extern idCmdSystem * cmdSystem;
  100. ID_INLINE void idCmdSystem::ArgCompletion_Boolean( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  101. callback( va( "%s 0", args.Argv( 0 ) ) );
  102. callback( va( "%s 1", args.Argv( 0 ) ) );
  103. }
  104. template<int min,int max> ID_STATIC_TEMPLATE ID_INLINE void idCmdSystem::ArgCompletion_Integer( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  105. for ( int i = min; i <= max; i++ ) {
  106. callback( va( "%s %d", args.Argv( 0 ), i ) );
  107. }
  108. }
  109. template<const char **strings> ID_STATIC_TEMPLATE ID_INLINE void idCmdSystem::ArgCompletion_String( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  110. for ( int i = 0; strings[i]; i++ ) {
  111. callback( va( "%s %s", args.Argv( 0 ), strings[i] ) );
  112. }
  113. }
  114. template<int type> ID_STATIC_TEMPLATE ID_INLINE void idCmdSystem::ArgCompletion_Decl( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  115. cmdSystem->ArgCompletion_DeclName( args, callback, type );
  116. }
  117. ID_INLINE void idCmdSystem::ArgCompletion_FileName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  118. cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", true, "", NULL );
  119. }
  120. ID_INLINE void idCmdSystem::ArgCompletion_MapName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  121. cmdSystem->ArgCompletion_FolderExtension( args, callback, "maps/", true, ".map", NULL );
  122. }
  123. ID_INLINE void idCmdSystem::ArgCompletion_ModelName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  124. cmdSystem->ArgCompletion_FolderExtension( args, callback, "models/", false, ".lwo", ".ase", ".md5mesh", ".ma", NULL );
  125. }
  126. ID_INLINE void idCmdSystem::ArgCompletion_SoundName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  127. cmdSystem->ArgCompletion_FolderExtension( args, callback, "sound/", false, ".wav", ".ogg", NULL );
  128. }
  129. ID_INLINE void idCmdSystem::ArgCompletion_ImageName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  130. cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", false, ".tga", ".dds", ".jpg", ".pcx", NULL );
  131. }
  132. ID_INLINE void idCmdSystem::ArgCompletion_VideoName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  133. cmdSystem->ArgCompletion_FolderExtension( args, callback, "video/", false, ".roq", NULL );
  134. }
  135. ID_INLINE void idCmdSystem::ArgCompletion_ConfigName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  136. cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", true, ".cfg", NULL );
  137. }
  138. ID_INLINE void idCmdSystem::ArgCompletion_SaveGame( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  139. cmdSystem->ArgCompletion_FolderExtension( args, callback, "SaveGames/", true, ".save", NULL );
  140. }
  141. ID_INLINE void idCmdSystem::ArgCompletion_DemoName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
  142. cmdSystem->ArgCompletion_FolderExtension( args, callback, "demos/", true, ".demo", NULL );
  143. }
  144. #endif /* !__CMDSYSTEM_H__ */