Team Fortress 2 Source Code as on 22/4/2020
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.

167 lines
4.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //===========================================================================//
  9. #ifndef COMMANDBUFFER_H
  10. #define COMMANDBUFFER_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include "tier1/utllinkedlist.h"
  15. #include "tier1/convar.h"
  16. //-----------------------------------------------------------------------------
  17. // Forward declarations
  18. //-----------------------------------------------------------------------------
  19. class CUtlBuffer;
  20. //-----------------------------------------------------------------------------
  21. // Invalid command handle
  22. //-----------------------------------------------------------------------------
  23. typedef int CommandHandle_t;
  24. enum
  25. {
  26. COMMAND_BUFFER_INVALID_COMMAND_HANDLE = 0
  27. };
  28. //-----------------------------------------------------------------------------
  29. // A command buffer class- a queue of argc/argv based commands associated
  30. // with a particular time
  31. //-----------------------------------------------------------------------------
  32. class CCommandBuffer
  33. {
  34. public:
  35. // Constructor, destructor
  36. CCommandBuffer( );
  37. ~CCommandBuffer();
  38. // Inserts text into the command buffer
  39. bool AddText( const char *pText, int nTickDelay = 0 );
  40. // Used to iterate over all commands appropriate for the current time
  41. void BeginProcessingCommands( int nDeltaTicks );
  42. bool DequeueNextCommand( );
  43. int DequeueNextCommand( const char **& ppArgv );
  44. int ArgC() const;
  45. const char **ArgV() const;
  46. const char *ArgS() const; // All args that occur after the 0th arg, in string form
  47. const char *GetCommandString() const; // The entire command in string form, including the 0th arg
  48. const CCommand& GetCommand() const;
  49. void EndProcessingCommands();
  50. // Are we in the middle of processing commands?
  51. bool IsProcessingCommands();
  52. // Delays all queued commands to execute at a later time
  53. void DelayAllQueuedCommands( int nTickDelay );
  54. // Indicates how long to delay when encoutering a 'wait' command
  55. void SetWaitDelayTime( int nTickDelay );
  56. // Returns a handle to the next command to process
  57. // (useful when inserting commands into the buffer during processing
  58. // of commands to force immediate execution of those commands,
  59. // most relevantly, to implement a feature where you stream a file
  60. // worth of commands into the buffer, where the file size is too large
  61. // to entirely contain in the buffer).
  62. CommandHandle_t GetNextCommandHandle();
  63. // Specifies a max limit of the args buffer. For unittesting. Size == 0 means use default
  64. void LimitArgumentBufferSize( int nSize );
  65. void SetWaitEnabled( bool bEnable ) { m_bWaitEnabled = bEnable; }
  66. bool IsWaitEnabled( void ) { return m_bWaitEnabled; }
  67. int GetArgumentBufferSize() { return m_nArgSBufferSize; }
  68. int GetMaxArgumentBufferSize() { return m_nMaxArgSBufferLength; }
  69. private:
  70. enum
  71. {
  72. ARGS_BUFFER_LENGTH = 8192,
  73. };
  74. struct Command_t
  75. {
  76. int m_nTick;
  77. int m_nFirstArgS;
  78. int m_nBufferSize;
  79. };
  80. // Insert a command into the command queue at the appropriate time
  81. void InsertCommandAtAppropriateTime( int hCommand );
  82. // Insert a command into the command queue
  83. // Only happens if it's inserted while processing other commands
  84. void InsertImmediateCommand( int hCommand );
  85. // Insert a command into the command queue
  86. bool InsertCommand( const char *pArgS, int nCommandSize, int nTick );
  87. // Returns the length of the next command, as well as the offset to the next command
  88. void GetNextCommandLength( const char *pText, int nMaxLen, int *pCommandLength, int *pNextCommandOffset );
  89. // Compacts the command buffer
  90. void Compact();
  91. // Parses argv0 out of the buffer
  92. bool ParseArgV0( CUtlBuffer &buf, char *pArgv0, int nMaxLen, const char **pArgs );
  93. char m_pArgSBuffer[ ARGS_BUFFER_LENGTH ];
  94. int m_nLastUsedArgSSize;
  95. int m_nArgSBufferSize;
  96. CUtlFixedLinkedList< Command_t > m_Commands;
  97. int m_nCurrentTick;
  98. int m_nLastTickToProcess;
  99. int m_nWaitDelayTicks;
  100. int m_hNextCommand;
  101. int m_nMaxArgSBufferLength;
  102. bool m_bIsProcessingCommands;
  103. bool m_bWaitEnabled;
  104. // NOTE: This is here to avoid the pointers returned by DequeueNextCommand
  105. // to become invalid by calling AddText. Is there a way we can avoid the memcpy?
  106. CCommand m_CurrentCommand;
  107. };
  108. //-----------------------------------------------------------------------------
  109. // Returns the next command
  110. //-----------------------------------------------------------------------------
  111. inline int CCommandBuffer::ArgC() const
  112. {
  113. return m_CurrentCommand.ArgC();
  114. }
  115. inline const char **CCommandBuffer::ArgV() const
  116. {
  117. return m_CurrentCommand.ArgV();
  118. }
  119. inline const char *CCommandBuffer::ArgS() const
  120. {
  121. return m_CurrentCommand.ArgS();
  122. }
  123. inline const char *CCommandBuffer::GetCommandString() const
  124. {
  125. return m_CurrentCommand.GetCommandString();
  126. }
  127. inline const CCommand& CCommandBuffer::GetCommand() const
  128. {
  129. return m_CurrentCommand;
  130. }
  131. #endif // COMMANDBUFFER_H