-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandbuffer.cpp
407 lines (332 loc) · 12 KB
/
commandbuffer.cpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//===== Copyright © 1996-2006, Valve Corporation, All rights reserved. ======//
//
// Purpose:
//
// $Workfile: $
// $Date: $
// $NoKeywords: $
//===========================================================================//
#include "tier1/commandbuffer.h"
#include "tier1/utlbuffer.h"
#include "tier1/strtools.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define MAX_ALIAS_NAME 32
#define MAX_COMMAND_LENGTH 1024
struct cmdalias_t
{
cmdalias_t *next;
char name[ MAX_ALIAS_NAME ];
char *value;
};
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
CCommandBuffer::CCommandBuffer( ) : m_Commands( 32, 32 )
{
m_hNextCommand = m_Commands.InvalidIndex();
m_nWaitDelayTicks = 1;
m_nCurrentTick = 0;
m_nLastTickToProcess = -1;
m_nArgSBufferSize = 0;
m_bIsProcessingCommands = false;
m_nMaxArgSBufferLength = ARGS_BUFFER_LENGTH;
}
CCommandBuffer::~CCommandBuffer()
{
}
//-----------------------------------------------------------------------------
// Indicates how long to delay when encoutering a 'wait' command
//-----------------------------------------------------------------------------
void CCommandBuffer::SetWaitDelayTime( int nTickDelay )
{
Assert( nTickDelay >= 0 );
m_nWaitDelayTicks = nTickDelay;
}
//-----------------------------------------------------------------------------
// Specifies a max limit of the args buffer. For unittesting. Size == 0 means use default
//-----------------------------------------------------------------------------
void CCommandBuffer::LimitArgumentBufferSize( int nSize )
{
if ( nSize > ARGS_BUFFER_LENGTH )
{
nSize = ARGS_BUFFER_LENGTH;
}
m_nMaxArgSBufferLength = ( nSize == 0 ) ? ARGS_BUFFER_LENGTH : nSize;
}
//-----------------------------------------------------------------------------
// Parses argv0 out of the buffer
//-----------------------------------------------------------------------------
bool CCommandBuffer::ParseArgV0( CUtlBuffer &buf, char *pArgV0, int nMaxLen, const char **pArgS )
{
pArgV0[0] = 0;
*pArgS = NULL;
if ( !buf.IsValid() )
return false;
int nSize = buf.ParseToken( CCommand::DefaultBreakSet(), pArgV0, nMaxLen );
if ( ( nSize <= 0 ) || ( nMaxLen == nSize ) )
return false;
int nArgSLen = buf.TellMaxPut() - buf.TellGet();
*pArgS = (nArgSLen > 0) ? (const char*)buf.PeekGet() : NULL;
return true;
}
//-----------------------------------------------------------------------------
// Insert a command into the command queue
//-----------------------------------------------------------------------------
void CCommandBuffer::InsertCommandAtAppropriateTime( intp hCommand )
{
intp i;
Command_t &command = m_Commands[hCommand];
for ( i = m_Commands.Head(); i != m_Commands.InvalidIndex(); i = m_Commands.Next(i) )
{
if ( m_Commands[i].m_nTick > command.m_nTick )
break;
}
m_Commands.LinkBefore( i, hCommand );
}
//-----------------------------------------------------------------------------
// Insert a command into the command queue at the appropriate time
//-----------------------------------------------------------------------------
void CCommandBuffer::InsertImmediateCommand( intp hCommand )
{
m_Commands.LinkBefore( m_hNextCommand, hCommand );
}
//-----------------------------------------------------------------------------
// Insert a command into the command queue
//-----------------------------------------------------------------------------
bool CCommandBuffer::InsertCommand( const char *pArgS, int nCommandSize, int nTick, cmd_source_t cmdSource )
{
if ( nCommandSize >= CCommand::MaxCommandLength() )
{
Warning( "WARNING: Command too long... ignoring!\n%s\n", pArgS );
return false;
}
// Add one for null termination
if ( m_nArgSBufferSize + nCommandSize + 1 > m_nMaxArgSBufferLength )
{
Compact();
if ( m_nArgSBufferSize + nCommandSize + 1 > m_nMaxArgSBufferLength )
return false;
}
memcpy( &m_pArgSBuffer[m_nArgSBufferSize], pArgS, nCommandSize );
m_pArgSBuffer[m_nArgSBufferSize + nCommandSize] = 0;
++nCommandSize;
intp hCommand = m_Commands.Alloc();
Command_t &command = m_Commands[hCommand];
command.m_nTick = nTick;
command.m_nFirstArgS = m_nArgSBufferSize;
command.m_nBufferSize = nCommandSize;
command.m_source = cmdSource;
m_nArgSBufferSize += nCommandSize;
if ( !m_bIsProcessingCommands || ( nTick > m_nCurrentTick ) )
{
InsertCommandAtAppropriateTime( hCommand );
}
else
{
InsertImmediateCommand( hCommand );
}
return true;
}
//-----------------------------------------------------------------------------
// Returns the length of the next command
//-----------------------------------------------------------------------------
void CCommandBuffer::GetNextCommandLength( const char *pText, int nMaxLen, int *pCommandLength, int *pNextCommandOffset )
{
int nCommandLength = 0;
int nNextCommandOffset;
bool bIsQuoted = false;
bool bIsCommented = false;
for ( nNextCommandOffset=0; nNextCommandOffset < nMaxLen; ++nNextCommandOffset, nCommandLength += bIsCommented ? 0 : 1 )
{
char c = pText[nNextCommandOffset];
if ( !bIsCommented )
{
if ( c == '"' )
{
bIsQuoted = !bIsQuoted;
continue;
}
// don't break if inside a C++ style comment
if ( !bIsQuoted && c == '/' )
{
bIsCommented = ( nNextCommandOffset < nMaxLen-1 ) && pText[nNextCommandOffset+1] == '/';
if ( bIsCommented )
{
++nNextCommandOffset;
continue;
}
}
// don't break if inside a quoted string
if ( !bIsQuoted && c == ';' )
break;
}
// FIXME: This is legacy behavior; should we not break if a \n is inside a quoted string?
if ( c == '\n' )
break;
}
*pCommandLength = nCommandLength;
*pNextCommandOffset = nNextCommandOffset;
}
//-----------------------------------------------------------------------------
// Add text to command buffer, return false if it couldn't owing to overflow
//-----------------------------------------------------------------------------
bool CCommandBuffer::AddText( const char *pText, cmd_source_t cmdSource, int nTickDelay )
{
Assert( nTickDelay >= 0 );
int nLen = Q_strlen( pText );
int nTick = m_nCurrentTick + nTickDelay;
// Parse the text into distinct commands
const char *pCurrentCommand = pText;
int nOffsetToNextCommand;
for( ; nLen > 0; nLen -= nOffsetToNextCommand+1, pCurrentCommand += nOffsetToNextCommand+1 )
{
// find a \n or ; line break
int nCommandLength;
GetNextCommandLength( pCurrentCommand, nLen, &nCommandLength, &nOffsetToNextCommand );
if ( nCommandLength <= 0 )
continue;
const char *pArgS;
char *pArgV0 = (char*)stackalloc( nCommandLength+1 );
CUtlBuffer bufParse( pCurrentCommand, nCommandLength, CUtlBuffer::TEXT_BUFFER | CUtlBuffer::READ_ONLY );
ParseArgV0( bufParse, pArgV0, nCommandLength+1, &pArgS );
if ( pArgV0[0] == 0 )
continue;
// Deal with the special 'wait' command
if ( !Q_stricmp( pArgV0, "wait" ) && IsWaitEnabled() )
{
int nDelay = pArgS ? atoi( pArgS ) : m_nWaitDelayTicks;
nTick += nDelay;
continue;
}
if ( !InsertCommand( pCurrentCommand, nCommandLength, nTick, cmdSource ) )
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// Are we in the middle of processing commands?
//-----------------------------------------------------------------------------
bool CCommandBuffer::IsProcessingCommands()
{
return m_bIsProcessingCommands;
}
//-----------------------------------------------------------------------------
// Delays all queued commands to execute at a later time
//-----------------------------------------------------------------------------
void CCommandBuffer::DelayAllQueuedCommands( int nDelay )
{
if ( nDelay <= 0 )
return;
for ( intp i = m_Commands.Head(); i != m_Commands.InvalidIndex(); i = m_Commands.Next(i) )
{
m_Commands[i].m_nTick += nDelay;
}
}
//-----------------------------------------------------------------------------
// Call this to begin iterating over all commands up to flCurrentTime
//-----------------------------------------------------------------------------
void CCommandBuffer::BeginProcessingCommands( int nDeltaTicks )
{
if ( nDeltaTicks == 0 )
return;
Assert( !m_bIsProcessingCommands );
m_bIsProcessingCommands = true;
m_nLastTickToProcess = m_nCurrentTick + nDeltaTicks - 1;
// Necessary to insert commands while commands are being processed
m_hNextCommand = m_Commands.Head();
}
//-----------------------------------------------------------------------------
// Returns the next command
//-----------------------------------------------------------------------------
bool CCommandBuffer::DequeueNextCommand( CCommand* pCommand )
{
pCommand->Reset();
Assert( m_bIsProcessingCommands );
if ( m_Commands.Count() == 0 )
return false;
intp nHead = m_Commands.Head();
Command_t &command = m_Commands[ nHead ];
if ( command.m_nTick > m_nLastTickToProcess )
return false;
m_nCurrentTick = command.m_nTick;
// Copy the current command into a temp buffer
// 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?
if ( command.m_nBufferSize > 0 )
{
pCommand->Tokenize( &m_pArgSBuffer[command.m_nFirstArgS], command.m_source );
}
m_Commands.Remove( nHead );
// Necessary to insert commands while commands are being processed
m_hNextCommand = m_Commands.Head();
// Msg("Dequeue : ");
// for ( int i = 0; i < nArgc; ++i )
// {
// Msg("%s ", m_pCurrentArgv[i] );
// }
// Msg("\n");
return true;
}
//-----------------------------------------------------------------------------
// Compacts the command buffer
//-----------------------------------------------------------------------------
void CCommandBuffer::Compact()
{
// Compress argvbuffer + argv
// NOTE: I'm using this choice instead of calling malloc + free
// per command to allocate arguments because I expect to post a
// bunch of commands but not have many delayed commands;
// avoiding the allocation cost seems more important that the memcpy
// cost here since I expect to not have much to copy.
m_nArgSBufferSize = 0;
char pTempBuffer[ ARGS_BUFFER_LENGTH ];
for ( intp i = m_Commands.Head(); i != m_Commands.InvalidIndex(); i = m_Commands.Next(i) )
{
Command_t &command = m_Commands[ i ];
memcpy( &pTempBuffer[m_nArgSBufferSize], &m_pArgSBuffer[command.m_nFirstArgS], command.m_nBufferSize );
command.m_nFirstArgS = m_nArgSBufferSize;
m_nArgSBufferSize += command.m_nBufferSize;
}
// NOTE: We could also store 2 buffers in the command buffer and switch
// between the two to avoid the 2nd memcpy; but again I'm guessing the memory
// tradeoff isn't worth it
memcpy( m_pArgSBuffer, pTempBuffer, m_nArgSBufferSize );
}
//-----------------------------------------------------------------------------
// Call this to finish iterating over all commands
//-----------------------------------------------------------------------------
void CCommandBuffer::EndProcessingCommands()
{
Assert( m_bIsProcessingCommands );
m_bIsProcessingCommands = false;
m_nCurrentTick = m_nLastTickToProcess + 1;
m_hNextCommand = m_Commands.InvalidIndex();
// Extract commands that are before the end time
// NOTE: This is a bug for this to
intp i = m_Commands.Head();
if ( i == m_Commands.InvalidIndex() )
{
m_nArgSBufferSize = 0;
return;
}
while ( i != m_Commands.InvalidIndex() )
{
if ( m_Commands[i].m_nTick >= m_nCurrentTick )
break;
AssertMsgOnce( false, "CCommandBuffer::EndProcessingCommands() called before all appropriate commands were dequeued.\n" );
int nNext = i;
Msg( "Warning: Skipping command %s\n", &m_pArgSBuffer[ m_Commands[i].m_nFirstArgS ] );
m_Commands.Remove( i );
i = nNext;
}
Compact();
}
//-----------------------------------------------------------------------------
// Returns a handle to the next command to process
//-----------------------------------------------------------------------------
CommandHandle_t CCommandBuffer::GetNextCommandHandle()
{
Assert( m_bIsProcessingCommands );
return m_Commands.Head();
}