forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandbuffer.cpp
636 lines (525 loc) · 16.9 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
//========= Copyright 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( int hCommand )
{
int 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( int hCommand )
{
m_Commands.LinkBefore( m_hNextCommand, hCommand );
}
//-----------------------------------------------------------------------------
// Insert a command into the command queue
//-----------------------------------------------------------------------------
bool CCommandBuffer::InsertCommand( const char *pArgS, int nCommandSize, int nTick )
{
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;
int hCommand = m_Commands.Alloc();
Command_t &command = m_Commands[hCommand];
command.m_nTick = nTick;
command.m_nFirstArgS = m_nArgSBufferSize;
command.m_nBufferSize = nCommandSize;
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, 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*)_alloca( 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 ) )
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 ( int 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( )
{
m_CurrentCommand.Reset();
Assert( m_bIsProcessingCommands );
if ( m_Commands.Count() == 0 )
return false;
int 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 )
{
m_CurrentCommand.Tokenize( &m_pArgSBuffer[command.m_nFirstArgS] );
}
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;
}
//-----------------------------------------------------------------------------
// Returns the next command
//-----------------------------------------------------------------------------
int CCommandBuffer::DequeueNextCommand( const char **& ppArgv )
{
DequeueNextCommand();
ppArgv = ArgV();
return ArgC();
}
//-----------------------------------------------------------------------------
// 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 ( int 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
int 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();
}
#if 0
/*
===============
Cmd_Alias_f
Creates a new command that executes a command string (possibly ; seperated)
===============
*/
void Cmd_Alias_f (void)
{
cmdalias_t *a;
char cmd[MAX_COMMAND_LENGTH];
int i, c;
char *s;
if (Cmd_Argc() == 1)
{
Con_Printf ("Current alias commands:\n");
for (a = cmd_alias ; a ; a=a->next)
Con_Printf ("%s : %s\n", a->name, a->value);
return;
}
s = Cmd_Argv(1);
if (strlen(s) >= MAX_ALIAS_NAME)
{
Con_Printf ("Alias name is too long\n");
return;
}
// copy the rest of the command line
cmd[0] = 0; // start out with a null string
c = Cmd_Argc();
for (i=2 ; i< c ; i++)
{
Q_strncat(cmd, Cmd_Argv(i), sizeof( cmd ), COPY_ALL_CHARACTERS);
if (i != c)
{
Q_strncat (cmd, " ", sizeof( cmd ), COPY_ALL_CHARACTERS );
}
}
Q_strncat (cmd, "\n", sizeof( cmd ), COPY_ALL_CHARACTERS);
// if the alias already exists, reuse it
for (a = cmd_alias ; a ; a=a->next)
{
if (!strcmp(s, a->name))
{
if ( !strcmp( a->value, cmd ) ) // Re-alias the same thing
return;
delete[] a->value;
break;
}
}
if (!a)
{
a = (cmdalias_t *)new cmdalias_t;
a->next = cmd_alias;
cmd_alias = a;
}
Q_strncpy (a->name, s, sizeof( a->name ) );
a->value = COM_StringCopy(cmd);
}
/*
=============================================================================
COMMAND EXECUTION
=============================================================================
*/
#define MAX_ARGS 80
static int cmd_argc;
static char *cmd_argv[MAX_ARGS];
static char *cmd_null_string = "";
static const char *cmd_args = NULL;
cmd_source_t cmd_source;
//-----------------------------------------------------------------------------
// Purpose:
// Output : void Cmd_Init
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Cmd_Shutdown( void )
{
// TODO, cleanup
while ( cmd_alias )
{
cmdalias_t *next = cmd_alias->next;
delete cmd_alias->value; // created by StringCopy()
delete cmd_alias;
cmd_alias = next;
}
}
/*
============
Cmd_ExecuteString
A complete command line has been parsed, so try to execute it
FIXME: lookupnoadd the token to speed search?
============
*/
const ConCommandBase *Cmd_ExecuteString (const char *text, cmd_source_t src)
{
cmdalias_t *a;
cmd_source = src;
Cmd_TokenizeString (text);
// execute the command line
if (!Cmd_Argc())
return NULL; // no tokens
// check alias
for (a=cmd_alias ; a ; a=a->next)
{
if (!Q_strcasecmp (cmd_argv[0], a->name))
{
Cbuf_InsertText (a->value);
return NULL;
}
}
// check ConCommands
ConCommandBase const *pCommand = ConCommandBase::FindCommand( cmd_argv[ 0 ] );
if ( pCommand && pCommand->IsCommand() )
{
bool isServerCommand = ( pCommand->IsBitSet( FCVAR_GAMEDLL ) &&
// Typed at console
cmd_source == src_command &&
// Not HLDS
!sv.IsDedicated() );
// Hook to allow game .dll to figure out who type the message on a listen server
if ( serverGameClients )
{
// We're actually the server, so set it up locally
if ( sv.IsActive() )
{
g_pServerPluginHandler->SetCommandClient( -1 );
#ifndef SWDS
// Special processing for listen server player
if ( isServerCommand )
{
g_pServerPluginHandler->SetCommandClient( cl.m_nPlayerSlot );
}
#endif
}
// We're not the server, but we've been a listen server (game .dll loaded)
// forward this command tot he server instead of running it locally if we're still
// connected
// Otherwise, things like "say" won't work unless you quit and restart
else if ( isServerCommand )
{
if ( cl.IsConnected() )
{
Cmd_ForwardToServer();
return NULL;
}
else
{
// It's a server command, but we're not connected to a server. Don't try to execute it.
return NULL;
}
}
}
// Allow cheat commands in singleplayer, debug, or multiplayer with sv_cheats on
#ifndef _DEBUG
if ( pCommand->IsBitSet( FCVAR_CHEAT ) )
{
if ( !Host_IsSinglePlayerGame() && sv_cheats.GetInt() == 0 )
{
Msg( "Can't use cheat command %s in multiplayer, unless the server has sv_cheats set to 1.\n", pCommand->GetName() );
return NULL;
}
}
#endif
(( ConCommand * )pCommand )->Dispatch();
return pCommand;
}
// check cvars
if ( cv->IsCommand() )
{
return pCommand;
}
// forward the command line to the server, so the entity DLL can parse it
if ( cmd_source == src_command )
{
if ( cl.IsConnected() )
{
Cmd_ForwardToServer();
return NULL;
}
}
Msg("Unknown command \"%s\"\n", Cmd_Argv(0));
return NULL;
}
#endif