Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
12 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // cmd.h -- Command buffer and command execution
  4. // Any number of commands can be added in a frame, from several different sources.
  5. // Most commands come from either keybindings or console line input, but remote
  6. // servers can also send across commands and entire text files can be execed.
  7. //
  8. // The + command line options are also added to the command buffer.
  9. //
  10. // The game starts with a Cbuf_AddText ("exec quake.rc\n"); Cbuf_Execute ();
  11. //
  12. // $NoKeywords: $
  13. //
  14. //===========================================================================//
  15. #ifndef CMD_H
  16. #define CMD_H
  17. #ifdef _WIN32
  18. #pragma once
  19. #endif
  20. #include "tier1/commandbuffer.h" // cmd_source_t
  21. // REI 7/18/2016:
  22. //
  23. // The main execution point for console commands is in Cmd_ExecuteCommand(). There is also a
  24. // backdoor via Cmd_Dispatch which skips the access controls and network forwarding in
  25. // Cmd_ExecuteCommand().
  26. //
  27. // In addition, there is some special game/server specific code for executing commands from the
  28. // client that skips the normal CON_COMMAND parsing / access control / execution path. See
  29. // section (2) below.
  30. //
  31. // There are 4 main paths to executing console commands, and most CON_COMMANDs fit into one
  32. // or more of these paths based on what flags they have set. ConVars are similar to ConCommands
  33. // but have some additional access restrictions; in particular, FCVAR_GAMEDLL vars cannot
  34. // be executed on the server from the client, and FCVAR_REPLICATED variables are transferred from
  35. // the server to the client automatically, not via networked console commands. (REI FIXME: Should
  36. // probably unify access controls between console vars and console commands.)
  37. //
  38. // 1) No access-control flags set. Generally can be executed locally on either client or server,
  39. // but see exceptions below.
  40. //
  41. // Mostly handled in cmd.cpp
  42. // Usually created via Cbuf_AddText/CBuf_InsertText in cmd.cpp
  43. // Executed in a batch via Cbuf_Execute -> Cmd_ExecuteCommand, also in cmd.cpp
  44. //
  45. // 2) FCVAR_GAMEDLL / "special" non-CON_COMMAND commands. The client cannot execute these locally,
  46. // and always forwards these commands to the server which will execute those commands on the client's
  47. // behalf. The "special" commands are also not executable at the server console AFAICT.
  48. //
  49. // Sent by any CNetMsg_StringCommand; the canonical way to send one is
  50. // via Cmd_ForwardToServer() in cmd.cpp, or via engine->ServerCmd() in cdll_engine_int.cpp
  51. //
  52. // Received by CBaseClient::NETMsg_StringCmd in baseclient.cpp and forwarded
  53. // to CGameClient::ExecuteStringCommand in sv_client.cpp. This either dispatches them to
  54. // Cmd_Dispatch (direct execution, kind of scary? shouldn't it use Cmd_ExecuteCommand?)
  55. // for normal CON_COMMANDs, or sends them to ::ClientCommand in server/client.cpp for
  56. // other commands which are just matched via giant if(strcmp) branches.
  57. //
  58. // An example from CS:GO cs_gamerules.cpp:
  59. // if ( FStrEq( args[0], "tr_map_show_exit_door_msg" ) )
  60. // {
  61. // IGameEvent * event = gameeventmanager->CreateEvent( "tr_exit_hint_trigger" );
  62. // if ( event )
  63. // gameeventmanager->FireEvent( event );
  64. // return true;
  65. // }
  66. // (Note that this command could be executed by any client at any time by just typing
  67. // "tr_map_show_exit_door_msg" into the console)
  68. //
  69. // There's also a brief excursion to server plugins in case they want to override any commands
  70. // as well.
  71. //
  72. // WARNING: FCVAR_GAMEDLL is automatically set on *every* console var/command defined in the
  73. // sever DLL. This means that in the absence of further intervention, *every* client
  74. // can execute those commands, regardless of your intent. The common workaround to
  75. // this is to include this check in your console commands:
  76. // if ( !UTIL_IsCommandIssuedByServerAdmin() ) return;
  77. // Note than convars do not have this vulnerability; FCVAR_GAMEDLL convars are not modifyable
  78. // by clients by default. I am not sure about the reasoning behind this discrepancy. I
  79. // believe we should have another flag FCVAR_CLIENT_CAN_EXECUTE as a parallel for
  80. // FCVAR_SERVER_CAN_EXECUTE, but that would be a pretty drastic change at this moment and
  81. // certainly would fork CS:GO from other Source games.
  82. //
  83. // 3) FCVAR_SERVER_CAN_EXECUTE. Commands which the client will execute on behalf of the server
  84. // if requested. Note that the server is allowed to call FCVAR_CHEAT commands that the
  85. // client could not execute themselves (that is, commands with both FCVAR_SERVER_CAN_EXECUTE
  86. // *and* FCVAR_CHEAT are executable on the client *only* when requested by the server).
  87. //
  88. // Sent by any CNETMsg_StringCmd; Two canonical ways to send this message:
  89. // - SV_ExecuteRemoteCommand in sv_main.cpp
  90. // - g_pVEngineServer->ClientCommand in vengineserver_impl.cpp (important
  91. // note: "ClientCommand" here is different from "ClientCmd" below!!)
  92. //
  93. // Received by CBaseClientState::NETMsg_StringCmd in baseclientstate.cpp
  94. // -> CBaseClientState::InternalProcessStringCommand in baseclientstate.cpp
  95. // -> CBuf_AddText() (the normal way to put things into the console)
  96. //
  97. // Note that in the absence of other access controls, these will also be in class (1),
  98. // executable from the console on both server and client.
  99. //
  100. // 4) FCVAR_CLIENTCMD_CAN_EXECUTE. Commands sent from code in the client, to other code in
  101. // the client. I am not sure of the purpose of this restriction, since the client code
  102. // has full access to all console commands if it wants.
  103. //
  104. // Generally sent via engine->ClientCmd() on the client. Can call *any* command by using
  105. // engine->ClientCmd_Unrestricted() or the immediate form engine->ExecuteClientCmd() instead.
  106. //
  107. // Received via the normal Cmd_ExecuteCommand path in cmd.cpp. In particular this means
  108. // that calls to these commands will be delated until command processing happens.
  109. //
  110. // Note that in the absence of other access controls, these will also be in class (1),
  111. // executable from the console on both server and client. An interesting combination is
  112. // FCVAR_CLIENTCMD_CAN_EXECUTE -> FCVAR_GAMEDLL, which will cause calls to
  113. // engine->ClientCmd() to redirect to the server when those commands are processed.
  114. //
  115. // There are a few other access-control mechanisms (most notably FCVAR_CHEAT) which disable
  116. // access to certain commands on almost all paths unless specific engine states are met. I
  117. // am not covering those here.
  118. //
  119. // Note that despite the name, not all of these commands are executed via the console. Many
  120. // items in classes (1) and (2) are executed via keybinds on the client, or via automatically
  121. // executed config files on either client or server. Paths (3) and (4) are really only
  122. // accessible via code.
  123. //
  124. //
  125. //-----------------------------------------------------------------------------
  126. // Forward declarations
  127. //-----------------------------------------------------------------------------
  128. class CCommand;
  129. class ConCommandBase;
  130. typedef enum
  131. {
  132. // These should be single character printable things, like this:
  133. // eCmdExecutionMarker_Enable_FCVAR_SERVER_CAN_EXECUTE = 'a',
  134. } ECmdExecutionMarker;
  135. //-----------------------------------------------------------------------------
  136. // Initialization, shutdown of the command buffer
  137. //-----------------------------------------------------------------------------
  138. void Cbuf_Init (void);
  139. void Cbuf_Shutdown( void );
  140. typedef enum
  141. {
  142. CBUF_FIRST_PLAYER = 0,
  143. CBUF_LAST_PLAYER = MAX_SPLITSCREEN_CLIENTS - 1,
  144. CBUF_SERVER = CBUF_LAST_PLAYER + 1,
  145. CBUF_COUNT,
  146. } ECommandTarget_t;
  147. //-----------------------------------------------------------------------------
  148. // Clears the command buffer
  149. //-----------------------------------------------------------------------------
  150. void Cbuf_Clear( ECommandTarget_t eTarget );
  151. //-----------------------------------------------------------------------------
  152. // as new commands are generated from the console or keybindings,
  153. // the text is added to the end of the command buffer.
  154. //-----------------------------------------------------------------------------
  155. void Cbuf_AddText ( ECommandTarget_t eTarget, const char *text, cmd_source_t source = kCommandSrcCode, int nTickDelay = 0 );
  156. //-----------------------------------------------------------------------------
  157. // when a command wants to issue other commands immediately, the text is
  158. // inserted at the beginning of the buffer, before any remaining unexecuted
  159. // commands.
  160. //-----------------------------------------------------------------------------
  161. void Cbuf_InsertText( ECommandTarget_t eTarget, const char *text, cmd_source_t source = kCommandSrcCode, int nTickDelay = 0 );
  162. // These allow you to create blocks in the command stream where certain rules apply.
  163. // ONLY use Cbuf_AddText in between execution markers. If you use Cbuf_InsertText,
  164. // it will put that stuff before the execution marker and the execution marker won't apply.
  165. //
  166. // cl_restrict_server_commands uses this. It inserts a marker that says, "don't run
  167. // anything unless it's marked with FCVAR_SERVER_CAN_EXECUTE", then inserts some commands,
  168. // then removes the execution marker. That way, ANYTIME Cbuf_Execute() is called,
  169. // it will apply the cl_restrict_server_commands rules correctly.
  170. void Cbuf_AddExecutionMarker( ECommandTarget_t eTarget, ECmdExecutionMarker marker );
  171. // Pulls off \n terminated lines of text from the command buffer and sends
  172. // them through Cmd_ExecuteString. Stops when the buffer is empty.
  173. // Normally called once per frame, but may be explicitly invoked.
  174. // Do not call inside a command function!
  175. //-----------------------------------------------------------------------------
  176. void Cbuf_Execute();
  177. ECommandTarget_t Cbuf_GetCurrentPlayer();
  178. //===========================================================================
  179. /*
  180. Command execution takes a null terminated string, breaks it into tokens,
  181. then searches for a command or variable that matches the first token.
  182. Commands can come from three sources, but the handler functions may choose
  183. to dissallow the action or forward it to a remote server if the source is
  184. not apropriate.
  185. */
  186. // FIXME: Move these into a field of CCommand?
  187. extern int cmd_clientslot;
  188. //-----------------------------------------------------------------------------
  189. // Initialization, shutdown
  190. //-----------------------------------------------------------------------------
  191. void Cmd_Init (void);
  192. void Cmd_Shutdown( void );
  193. //-----------------------------------------------------------------------------
  194. // Executes a command given a CCommand argument structure
  195. //-----------------------------------------------------------------------------
  196. const ConCommandBase *Cmd_ExecuteCommand( ECommandTarget_t eTarget, const CCommand &command, int nClientSlot = -1 );
  197. //-----------------------------------------------------------------------------
  198. // Dispatches a command with the requested arguments
  199. //-----------------------------------------------------------------------------
  200. void Cmd_Dispatch( const ConCommandBase *pCommand, const CCommand &args );
  201. //-----------------------------------------------------------------------------
  202. // adds the current command line as a clc_stringcmd to the client message.
  203. // things like godmode, noclip, etc, are commands directed to the server,
  204. // so when they are typed in at the console, they will need to be forwarded.
  205. // If bReliable is true, it goes into cls.netchan.message.
  206. // If bReliable is false, it goes into cls.datagram.
  207. //-----------------------------------------------------------------------------
  208. void Cmd_ForwardToServer( const CCommand &args, bool bReliable = true );
  209. void Cmd_ForwardToServerWithWhitelist( const CCommand &args, bool bReliable = true );
  210. // Used to allow cheats even if cheats aren't theoretically allowed
  211. void Cmd_SetRptActive( bool bActive );
  212. bool Cmd_IsRptActive();
  213. const char* Cmd_AliasToCommandString( const char* szAliasName );
  214. bool Cbuf_IsProcessingCommands( ECommandTarget_t eTarget );
  215. #endif // CMD_H