aboutsummaryrefslogtreecommitdiff
path: root/sp/src/public/tier1/CommandBuffer.h
blob: d9bfd7dea3757309dde46dbdb54186d66f058ca2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $Workfile:     $
// $Date:         $
// $NoKeywords: $
//===========================================================================//


#ifndef COMMANDBUFFER_H
#define COMMANDBUFFER_H

#ifdef _WIN32
#pragma once
#endif

#include "tier1/utllinkedlist.h"
#include "tier1/convar.h"


//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class CUtlBuffer;


//-----------------------------------------------------------------------------
// Invalid command handle
//-----------------------------------------------------------------------------
typedef int CommandHandle_t;
enum
{
	COMMAND_BUFFER_INVALID_COMMAND_HANDLE = 0
};


//-----------------------------------------------------------------------------
// A command buffer class- a queue of argc/argv based commands associated
// with a particular time
//-----------------------------------------------------------------------------
class CCommandBuffer
{
public:
	// Constructor, destructor
	CCommandBuffer( );
	~CCommandBuffer();

    // Inserts text into the command buffer
	bool AddText( const char *pText, int nTickDelay = 0 );

	// Used to iterate over all commands appropriate for the current time
	void BeginProcessingCommands( int nDeltaTicks );
	bool DequeueNextCommand( );
	int DequeueNextCommand( const char **& ppArgv );
	int ArgC() const;
	const char **ArgV() const;
	const char *ArgS() const;		// All args that occur after the 0th arg, in string form
	const char *GetCommandString() const;	// The entire command in string form, including the 0th arg
	const CCommand& GetCommand() const;
	void EndProcessingCommands();

	// Are we in the middle of processing commands?
	bool IsProcessingCommands();

	// Delays all queued commands to execute at a later time
	void DelayAllQueuedCommands( int nTickDelay );

	// Indicates how long to delay when encoutering a 'wait' command
	void SetWaitDelayTime( int nTickDelay );

	// Returns a handle to the next command to process
	// (useful when inserting commands into the buffer during processing
	// of commands to force immediate execution of those commands,
	// most relevantly, to implement a feature where you stream a file
	// worth of commands into the buffer, where the file size is too large
	// to entirely contain in the buffer).
    CommandHandle_t GetNextCommandHandle();

	// Specifies a max limit of the args buffer. For unittesting. Size == 0 means use default
	void LimitArgumentBufferSize( int nSize );

	void SetWaitEnabled( bool bEnable )		{ m_bWaitEnabled = bEnable; }
	bool IsWaitEnabled( void )				{ return m_bWaitEnabled; }

	int GetArgumentBufferSize() { return m_nArgSBufferSize; }
	int GetMaxArgumentBufferSize() { return m_nMaxArgSBufferLength; }

private:
	enum
	{
		ARGS_BUFFER_LENGTH = 8192,
	};

	struct Command_t
	{
		int m_nTick;
		int m_nFirstArgS;
		int m_nBufferSize;
	};

	// Insert a command into the command queue at the appropriate time
	void InsertCommandAtAppropriateTime( int hCommand );
						   
	// Insert a command into the command queue
	// Only happens if it's inserted while processing other commands
	void InsertImmediateCommand( int hCommand );

	// Insert a command into the command queue
	bool InsertCommand( const char *pArgS, int nCommandSize, int nTick );

	// Returns the length of the next command, as well as the offset to the next command
	void GetNextCommandLength( const char *pText, int nMaxLen, int *pCommandLength, int *pNextCommandOffset );

	// Compacts the command buffer
	void Compact();

	// Parses argv0 out of the buffer
	bool ParseArgV0( CUtlBuffer &buf, char *pArgv0, int nMaxLen, const char **pArgs );

	char	m_pArgSBuffer[ ARGS_BUFFER_LENGTH ];
	int		m_nLastUsedArgSSize;
	int		m_nArgSBufferSize;
	CUtlFixedLinkedList< Command_t >	m_Commands;
	int		m_nCurrentTick;
	int		m_nLastTickToProcess;
	int		m_nWaitDelayTicks;
	int		m_hNextCommand;
	int		m_nMaxArgSBufferLength;
	bool	m_bIsProcessingCommands;
	bool	m_bWaitEnabled;

	// NOTE: This is here to avoid the pointers returned by DequeueNextCommand
	// to become invalid by calling AddText. Is there a way we can avoid the memcpy?
	CCommand m_CurrentCommand;
};


//-----------------------------------------------------------------------------
// Returns the next command
//-----------------------------------------------------------------------------
inline int CCommandBuffer::ArgC() const
{
	return m_CurrentCommand.ArgC();
}

inline const char **CCommandBuffer::ArgV() const
{
	return m_CurrentCommand.ArgV();
}

inline const char *CCommandBuffer::ArgS() const
{
	return m_CurrentCommand.ArgS();
}

inline const char *CCommandBuffer::GetCommandString() const
{
	return m_CurrentCommand.GetCommandString();
}

inline const CCommand& CCommandBuffer::GetCommand() const
{
	return m_CurrentCommand;
}

#endif // COMMANDBUFFER_H