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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $Workfile: $
// $Date: $
// $NoKeywords: $
//===========================================================================//
// support QueryPerformanceCounter
#if defined(_WIN32) && !defined(_X360)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include "quakedef.h"
#include "zone.h"
#include "tier0/vcrmode.h"
#include "demo.h"
#include "filesystem.h"
#include "filesystem_engine.h"
#include "eiface.h"
#include "server.h"
#include "sys.h"
#include "baseautocompletefilelist.h"
#include "tier0/icommandline.h"
#include "tier1/utlbuffer.h"
#include "gl_cvars.h"
#include "tier0/memalloc.h"
#include "netmessages.h"
#include "client.h"
#include "sv_plugin.h"
#include "tier1/CommandBuffer.h"
#include "cvar.h"
#include "vstdlib/random.h"
#include "tier1/utldict.h"
#include "tier0/etwprof.h"
#include "tier0/vprof.h"
#include "gl_matsysiface.h" // update materialsystem config
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
// This denotes an execution marker in the command stream.
#define CMDSTR_ADD_EXECUTION_MARKER "[$&*,`]"
#ifdef _DEBUG
ConVar cl_debug_respect_cheat_vars( "cl_debug_respect_cheat_vars", "0", 0, "(debug builds only) - when set to 0, the client can change cheat vars." );
#endif
extern ConVar sv_allow_wait_command;
#define MAX_ALIAS_NAME 32
#define MAX_COMMAND_LENGTH 1024
struct cmdalias_t
{
cmdalias_t *next;
char name[ MAX_ALIAS_NAME ];
char *value;
};
static cmdalias_t *cmd_alias = NULL;
static CCommandBuffer s_CommandBuffer;
static CThreadFastMutex s_CommandBufferMutex;
#define LOCK_COMMAND_BUFFER() AUTO_LOCK(s_CommandBufferMutex)
static FileAssociationInfo g_FileAssociations[] =
{
{ ".dem", "playdemo" },
{ ".sav", "load" },
{ ".bsp", "map" },
};
int g_iFilterCommandsByServerCanExecute = 0; // If this is nonzero, then they want us to only run commands marked with FCVAR_SERVER_CAN_EXECUTE.
int g_iFilterCommandsByClientCmdCanExecute = 0; // If this is nonzero, then they want us to only run commands marked with FCVAR_CLIENTCMD_CAN_EXECUTE.
// This is a list of cvars that are in the client DLL that we want FCVAR_CLIENTCMD_CAN_EXECUTE set on.
// In order to avoid patching the client DLL, we setup this list. Whenever the client DLL has gone out with the
// FCVAR_CLIENTCMD_CAN_EXECUTE flag set, we can get rid of this list.
CUtlDict<int,int> g_ExtraClientCmdCanExecuteCvars;
void Cmd_AddClientCmdCanExecuteVar( const char *pName )
{
if ( g_ExtraClientCmdCanExecuteCvars.Find( pName ) == g_ExtraClientCmdCanExecuteCvars.InvalidIndex() )
g_ExtraClientCmdCanExecuteCvars.Insert( pName );
}
//=============================================================================
// These functions manage a list of execution markers that we use to verify
// special commands in the command buffer.
//=============================================================================
static CUtlVector<int> g_ExecutionMarkers;
static CUniformRandomStream g_ExecutionMarkerStream;
static bool g_bExecutionMarkerStreamInitialized = false;
static int CreateExecutionMarker()
{
if ( !g_bExecutionMarkerStreamInitialized )
{
int iSeed;
if ( IsPosix() )
{
float flFloatTime = (float)Plat_FloatTime();
iSeed = *(int*)(&flFloatTime);
}
else
{
#if defined(_WIN32) && !defined(_X360)
LARGE_INTEGER CurrentTime;
QueryPerformanceCounter( &CurrentTime );
iSeed = CurrentTime.LowPart ^ CurrentTime.HighPart;
#else
float flFloatTime = (float)Plat_FloatTime();
iSeed = *(int*)(&flFloatTime);
#endif
}
g_ExecutionMarkerStream.SetSeed( iSeed );
g_bExecutionMarkerStreamInitialized = true;
}
if ( g_ExecutionMarkers.Count() >= MAX_EXECUTION_MARKERS )
{
// Callers to this function should call Cbuf_HasRoomForExecutionMarkers first.
Host_Error( "CreateExecutionMarker called, but the max has already been reached." );
}
int nRandomNumber;
int nIndex = g_ExecutionMarkers.InvalidIndex();
// Pick a random number that doesn't already exist in the list
do
{
// We don't have CCrypto :(
// if ( CCrypto::GenerateRandomBlock( (uint8 *)&nRandomNumber, sizeof(nRandomNumber) ) )
// {
// nRandomNumber = nRandomNumber & 0x7FFFFFFF;
// }
// else
// {
nRandomNumber = g_ExecutionMarkerStream.RandomInt( 0, 1<<30 );
// }
nIndex = g_ExecutionMarkers.Find( nRandomNumber );
} while ( nIndex != g_ExecutionMarkers.InvalidIndex() );
int i = g_ExecutionMarkers.AddToTail( nRandomNumber );
return g_ExecutionMarkers[i];
}
static bool FindAndRemoveExecutionMarker( int iCode )
{
int i = g_ExecutionMarkers.Find( iCode );
if ( i == g_ExecutionMarkers.InvalidIndex() )
return false;
g_ExecutionMarkers.Remove( i );
return true;
}
//-----------------------------------------------------------------------------
// Used to allow cheats even if cheats aren't theoretically allowed
//-----------------------------------------------------------------------------
static bool g_bRPTActive = false;
void Cmd_SetRptActive( bool bActive )
{
g_bRPTActive = bActive;
}
bool Cmd_IsRptActive()
{
return g_bRPTActive;
}
//=============================================================================
//-----------------------------------------------------------------------------
// Just translates BindToggle <key> <cvar> into: bind <key> "increment var <cvar> 0 1 1"
//-----------------------------------------------------------------------------
CON_COMMAND( BindToggle, "Performs a bind <key> \"increment var <cvar> 0 1 1\"" )
{
if( args.ArgC() <= 2 )
{
ConMsg( "BindToggle <key> <cvar>: invalid syntax specified\n" );
return;
}
char newCmd[MAX_COMMAND_LENGTH];
Q_snprintf( newCmd, sizeof(newCmd), "bind %s \"incrementvar %s 0 1 1\"\n", args[1], args[2] );
Cbuf_InsertText( newCmd );
}
CON_COMMAND_F( PerfMark, "inserts a telemetry marker into the stream. If args are provided, they will be included.", FCVAR_NONE )
{
// Nothing to do, we had our message written out by Cbuf_ExecuteCommand.
}
//-----------------------------------------------------------------------------
// Init, shutdown
//-----------------------------------------------------------------------------
void Cbuf_Init()
{
// Wait for 1 execute time
s_CommandBuffer.SetWaitDelayTime( 1 );
}
void Cbuf_Shutdown()
{
}
//-----------------------------------------------------------------------------
// Clears the command buffer
//-----------------------------------------------------------------------------
void Cbuf_Clear()
{
Cbuf_Init();
}
//-----------------------------------------------------------------------------
// Adds command text at the end of the buffer
//-----------------------------------------------------------------------------
void Cbuf_AddText( const char *pText )
{
LOCK_COMMAND_BUFFER();
if ( !s_CommandBuffer.AddText( pText ) )
{
ConMsg( "Cbuf_AddText: buffer overflow\n" );
}
}
//-----------------------------------------------------------------------------
// Escape an argument for a command. This *can* fail as many characters cannot
// actually be passed through the old command syntax...
//-----------------------------------------------------------------------------
bool Cbuf_EscapeCommandArg( const char *pText, char *pOut, unsigned int nOut )
{
// Okay, so, to be honest, all we can do with the super limited syntax is ensure we don't have quotes or control
// characters, and then wrap the string in quotes. Hence escape *argument*. Anything more advanced than that is just
// not allowable.
if ( !pText || !*pText )
return false;
for (const char *pChar = pText; pChar && *pChar; pChar++ )
{
// ASCII control characters (codepoints <= 31) are just not sane to pass to this system. This includes \n and
// \r.
if ( *pChar <= (char)31 )
return false;
// Can't quote these with current syntax.
if ( *pChar == '"' )
return false;
}
// Room for quotes+null in out?
if ( !pOut || nOut < (unsigned int)V_strlen( pText ) + 3 )
return false;
V_snprintf( pOut, nOut, "\"%s\"", pText );
return true;
}
//-----------------------------------------------------------------------------
// Adds command text at the beginning of the buffer
//-----------------------------------------------------------------------------
void Cbuf_InsertText( const char *pText )
{
LOCK_COMMAND_BUFFER();
// NOTE: This operation is only allowed when the command buffer
// is in the middle of processing. If this assertion never triggers,
// it's safe to eliminate Cbuf_InsertText altogether.
// Otherwise, I have to add a feature to CCommandBuffer
Assert( s_CommandBuffer.IsProcessingCommands() );
Cbuf_AddText( pText );
}
bool Cbuf_AddExecutionMarker( ECmdExecutionMarker marker, const char *pszMarkerCode )
{
if ( marker == eCmdExecutionMarker_Enable_FCVAR_SERVER_CAN_EXECUTE )
{
s_CommandBuffer.SetWaitEnabled( false );
}
else if ( marker == eCmdExecutionMarker_Disable_FCVAR_SERVER_CAN_EXECUTE )
{
s_CommandBuffer.SetWaitEnabled( true );
}
return s_CommandBuffer.AddText( pszMarkerCode );
}
bool Cbuf_AddTextWithMarkers( ECmdExecutionMarker markerLeft, const char *text, ECmdExecutionMarker markerRight )
{
if ( !Cbuf_HasRoomForExecutionMarkers( 2 ) )
{
ConMsg( "Cbuf_AddTextWithMarkers: execution marker overflow\n" );
return false;
}
int iMarkerCodeLeft = CreateExecutionMarker();
int iMarkerCodeRight = CreateExecutionMarker();
// CMDCHAR_ADD_EXECUTION_MARKER tells us there's a special execution thing here.
// (char)marker tells it what to turn on
// iRandomCode is for security, so only our code can stuff this command into the buffer.
char szMarkerLeft[512], szMarkerRight[512];
V_sprintf_safe( szMarkerLeft, ";%s %c %d;", CMDSTR_ADD_EXECUTION_MARKER, (char)markerLeft, iMarkerCodeLeft );
V_sprintf_safe( szMarkerRight, ";%s %c %d;", CMDSTR_ADD_EXECUTION_MARKER, (char)markerRight, iMarkerCodeRight );
// Due to the behavior of the command buffer, we may be over-estimating the amount of space required.
int cTextToBeAdded = strlen( szMarkerLeft ) + strlen( szMarkerRight ) + strlen( text ) + 3;
LOCK_COMMAND_BUFFER();
if ( s_CommandBuffer.GetArgumentBufferSize() + cTextToBeAdded + 1 > s_CommandBuffer.GetMaxArgumentBufferSize() )
{
// cleanup
FindAndRemoveExecutionMarker( iMarkerCodeLeft );
FindAndRemoveExecutionMarker( iMarkerCodeRight );
ConMsg( "Cbuf_AddTextWithMarkers: buffer overflow\n" );
return false;
}
bool bSuccess = Cbuf_AddExecutionMarker( markerLeft, szMarkerLeft ) &&
s_CommandBuffer.AddText( text ) &&
Cbuf_AddExecutionMarker( markerRight, szMarkerRight );
if ( !bSuccess )
{
// If we screwed up the validation, don't allow us to get stuck in a bad state.
Host_Error( "Cbuf_AddTextWithMarkers: buffer overflow\n" );
}
return true;
}
bool Cbuf_HasRoomForExecutionMarkers( int cExecutionMarkers )
{
return ( g_ExecutionMarkers.Count() + cExecutionMarkers ) < MAX_EXECUTION_MARKERS;
}
//-----------------------------------------------------------------------------
// Executes commands in the buffer
//-----------------------------------------------------------------------------
static void Cbuf_ExecuteCommand( const CCommand &args, cmd_source_t source )
{
// Note: If you remove this, PerfMark needs to do the same logic--so don't do that.
tmMessage( TELEMETRY_LEVEL0, TMMF_SEVERITY_LOG | TMMF_ICON_NOTE, "(source/command) %s", tmDynamicString( TELEMETRY_LEVEL0, args.GetCommandString() ) );
// Add the command text to the ETW stream to give better context to traces.
ETWMark( args.GetCommandString() );
// execute the command line
const ConCommandBase *pCmd = Cmd_ExecuteCommand( args, source );
#if !defined(SWDS) && !defined(_XBOX)
if ( pCmd && !pCmd->IsFlagSet( FCVAR_DONTRECORD ) )
{
demorecorder->RecordCommand( args.GetCommandString() );
}
#endif
}
//-----------------------------------------------------------------------------
// Executes commands in the buffer
//-----------------------------------------------------------------------------
void Cbuf_Execute()
{
VPROF("Cbuf_Execute");
tmZoneFiltered( TELEMETRY_LEVEL0, 50, TMZF_NONE, "%s", __FUNCTION__ );
if ( !ThreadInMainThread() )
{
Warning( "Executing command outside main loop thread\n" );
ExecuteOnce( DebuggerBreakIfDebugging() );
}
LOCK_COMMAND_BUFFER();
// If text was added with Cbuf_AddText and then Cbuf_Execute gets called from within handler, we're going
// to execute the new commands anyway, so we can ignore this extra execute call here.
if ( s_CommandBuffer.IsProcessingCommands() )
return;
// Allow/Don't allow wait commands.
if ( sv_allow_wait_command.GetBool() != s_CommandBuffer.IsWaitEnabled() )
{
s_CommandBuffer.SetWaitEnabled( sv_allow_wait_command.GetBool() );
}
// NOTE: The command buffer knows about execution time related to commands,
// but since HL2 doesn't, we're going to spoof the command time to simply
// be the the number of times Cbuf_Execute is called.
s_CommandBuffer.BeginProcessingCommands( 1 );
while ( s_CommandBuffer.DequeueNextCommand( ) )
{
Cbuf_ExecuteCommand( s_CommandBuffer.GetCommand(), src_command );
}
s_CommandBuffer.EndProcessingCommands( );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *param -
// Output : static char const
//-----------------------------------------------------------------------------
static char const *Cmd_TranslateFileAssociation(char const *param )
{
static char sz[ 512 ];
char *retval = NULL;
char temp[ 512 ];
Q_strncpy( temp, param, sizeof( temp ) );
Q_FixSlashes( temp );
Q_strlower( temp );
const char *extension = V_GetFileExtension(temp);
// must have an extension to map
if (!extension)
return retval;
int c = ARRAYSIZE( g_FileAssociations );
for ( int i = 0; i < c; i++ )
{
FileAssociationInfo& info = g_FileAssociations[ i ];
if ( ! Q_strcmp( extension, info.extension+1 ) &&
! CommandLine()->FindParm(va( "+%s", info.command_to_issue ) ) )
{
// Translate if haven't already got one of these commands
Q_strncpy( sz, temp, sizeof( sz ) );
Q_FileBase( sz, temp, sizeof( sz ) );
Q_snprintf( sz, sizeof( sz ), "%s %s", info.command_to_issue, temp );
retval = sz;
break;
}
}
// return null if no translation, otherwise return commands
return retval;
}
//-----------------------------------------------------------------------------
// Purpose: Adds command line parameters as script statements
// Commands lead with a +, and continue until a - or another +
// Also automatically converts .dem, .bsp, and .sav files to +playdemo etc command line options
// hl2 +cmd amlev1
// hl2 -nosound +cmd amlev1
// Output : void Cmd_StuffCmds_f
//-----------------------------------------------------------------------------
CON_COMMAND( stuffcmds, "Parses and stuffs command line + commands to command buffer." )
{
if ( args.ArgC() != 1 )
{
ConMsg( "stuffcmds : execute command line parameters\n" );
return;
}
MEM_ALLOC_CREDIT();
CUtlBuffer build( 0, 0, CUtlBuffer::TEXT_BUFFER );
// arg[0] is the executable name
for ( int i=1; i < CommandLine()->ParmCount(); i++ )
{
const char *szParm = CommandLine()->GetParm(i);
if (!szParm) continue;
if (szParm[0] == '-')
{
// skip -XXX options and eat their args
const char *szValue = CommandLine()->ParmValueByIndex( i );
if ( szValue )
i++;
continue;
}
if (szParm[0] == '+')
{
// convert +XXX options and stuff them into the build buffer
const char *szValue = CommandLine()->ParmValueByIndex( i );
if (szValue)
{
build.PutString(va("%s %s\n", szParm+1, szValue));
i++;
}
else
{
build.PutString(szParm+1);
build.PutChar('\n');
}
}
else
{
// singleton values, convert to command
char const *translated = Cmd_TranslateFileAssociation( CommandLine()->GetParm( i ) );
if (translated)
{
build.PutString(translated);
build.PutChar('\n');
}
}
}
build.PutChar( '\0' );
if ( build.TellPut() > 1 )
{
Cbuf_InsertText( (char *)build.Base() );
}
}
bool IsValidFileExtension( const char *pszFilename )
{
if ( !pszFilename )
{
return false;
}
if ( Q_strstr( pszFilename, ".exe" ) ||
Q_strstr( pszFilename, ".vbs" ) ||
Q_strstr( pszFilename, ".com" ) ||
Q_strstr( pszFilename, ".bat" ) ||
Q_strstr( pszFilename, ".dll" ) ||
Q_strstr( pszFilename, ".ini" ) ||
Q_strstr( pszFilename, ".gcf" ) ||
Q_strstr( pszFilename, ".sys" ) ||
Q_strstr( pszFilename, ".blob" ) )
{
return false;
}
return true;
}
/*
===============
Cmd_Exec_f
===============
*/
void Cmd_Exec_f( const CCommand &args )
{
LOCK_COMMAND_BUFFER();
char fileName[MAX_OSPATH];
int argc = args.ArgC();
if ( argc != 2 )
{
ConMsg( "exec <filename>: execute a script file\n" );
return;
}
const char *szFile = args[1];
const char *pPathID = "MOD";
Q_snprintf( fileName, sizeof( fileName ), "//%s/cfg/%s", pPathID, szFile );
Q_DefaultExtension( fileName, ".cfg", sizeof( fileName ) );
// check path validity
if ( !COM_IsValidPath( fileName ) )
{
ConMsg( "exec %s: invalid path.\n", fileName );
return;
}
// check for invalid file extensions
if ( !IsValidFileExtension( fileName ) )
{
ConMsg( "exec %s: invalid file type.\n", fileName );
return;
}
// 360 doesn't need to do costly existence checks
if ( IsPC() )
{
if ( g_pFileSystem->FileExists( fileName ) )
{
// don't want to exec files larger than 1 MB
// probably not a valid file to exec
unsigned int size = g_pFileSystem->Size( fileName );
if ( size > 1*1024*1024 )
{
ConMsg( "exec %s: file size larger than 1 MB!\n", szFile );
return;
}
}
else
{
// Many exec files are optional. make the error message slightly
// more informative and less like there is a problem.
if ( !V_stristr( szFile, "autoexec.cfg" ) && !V_stristr( szFile, "joystick.cfg" ) && !V_stristr( szFile, "game.cfg" ) )
{
ConMsg( "'%s' not present; not executing.\n", szFile );
}
return;
}
}
char buf[16384] = { 0 };
int len = 0;
char *f = (char *)COM_LoadStackFile( fileName, buf, sizeof( buf ), len );
if ( !f )
{
ConMsg( "exec: couldn't exec %s\n", szFile );
return;
}
char *original_f = f;
VCRHook_Cmd_Exec(&f);
// In case f was allocated and VCR mode spoofed f, free the old one we allocated earlier.
if ( original_f != buf && original_f != f )
{
free( original_f );
}
ConDMsg( "execing %s\n", szFile );
// check to make sure we're not going to overflow the cmd_text buffer
int hCommand = s_CommandBuffer.GetNextCommandHandle();
// Execute each command immediately
const char *pszDataPtr = f;
while( true )
{
// parse a line out of the source
pszDataPtr = COM_ParseLine( pszDataPtr );
// no more tokens
if ( Q_strlen( com_token ) <= 0 )
break;
Cbuf_InsertText( com_token );
// Execute all commands provoked by the current line read from the file
while ( s_CommandBuffer.GetNextCommandHandle() != hCommand )
{
if( s_CommandBuffer.DequeueNextCommand( ) )
{
Cbuf_ExecuteCommand( s_CommandBuffer.GetCommand(), src_command );
}
else
{
Assert( 0 );
break;
}
}
}
if ( f != buf )
{
// Hack for VCR playback. vcrmode allocates the memory but doesn't use the debug memory allocator,
// so we don't want to free what it allocated.
if ( f == original_f )
{
free( f );
}
}
// force any queued convar changes to flush before reading/writing them
UpdateMaterialSystemConfig();
}
/*
===============
Cmd_Echo_f
Just prints the rest of the line to the console
===============
*/
CON_COMMAND_F( echo, "Echo text to console.", FCVAR_SERVER_CAN_EXECUTE )
{
int argc = args.ArgC();
for ( int i=1; i<argc; i++ )
{
ConMsg ("%s ", args[i] );
}
ConMsg ("\n");
}
// Users can alias these commands to remove their functionality because the game systems use convars to implement these features
// The blacklist prevents that exploit
static const char *g_pBlacklistedCommands[] =
{
"dsp_player",
"room_type",
};
/*
===============
Cmd_Alias_f
Creates a new command that executes a command string (possibly ; seperated)
===============
*/
CON_COMMAND( alias, "Alias a command." )
{
cmdalias_t *a;
char cmd[MAX_COMMAND_LENGTH];
int c;
const char *s;
int argc = args.ArgC();
if ( argc == 1 )
{
ConMsg ("Current alias commands:\n");
for (a = cmd_alias ; a ; a=a->next)
{
ConMsg ("%s : %s\n", a->name, a->value);
}
return;
}
s = args[1];
if ( Q_strlen(s) >= MAX_ALIAS_NAME )
{
ConMsg ("Alias name is too long\n");
return;
}
for ( int i = 0; i < ARRAYSIZE(g_pBlacklistedCommands); i++ )
{
if ( !V_stricmp( g_pBlacklistedCommands[i], s) )
{
ConMsg("Can't alias %s\n", g_pBlacklistedCommands[i] );
return;
}
}
// copy the rest of the command line
cmd[0] = 0; // start out with a null string
c = argc;
for ( int i=2 ; i< c ; i++)
{
Q_strncat(cmd, args[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 (!Q_strcmp(s, a->name))
{
if ( !Q_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
=============================================================================
*/
cmd_source_t cmd_source;
int cmd_clientslot = -1;
//-----------------------------------------------------------------------------
// Purpose:
// Output : void Cmd_Init
//-----------------------------------------------------------------------------
CON_COMMAND( cmd, "Forward command to server." )
{
Cmd_ForwardToServer( args );
}
CON_COMMAND_AUTOCOMPLETEFILE( exec, Cmd_Exec_f, "Execute script file.", "cfg", cfg );
void Cmd_Init( void )
{
Sys_CreateFileAssociations( ARRAYSIZE( g_FileAssociations ), g_FileAssociations );
}
//-----------------------------------------------------------------------------
// 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;
}
}
//-----------------------------------------------------------------------------
// FIXME: Remove this! This is a temporary hack to deal with backward compat
//-----------------------------------------------------------------------------
void Cmd_Dispatch( const ConCommandBase *pCommand, const CCommand &command )
{
ConCommand *pConCommand = const_cast<ConCommand*>( static_cast<const ConCommand*>( pCommand ) );
pConCommand->Dispatch( command );
}
static void HandleExecutionMarker( const char *pCommand, const char *pMarkerCode )
{
char cCommand = pCommand[0];
int iMarkerCode = atoi( pMarkerCode );
// Validate..
if ( FindAndRemoveExecutionMarker( iMarkerCode ) )
{
// Ok, now it's validated, so do the command.
if ( cCommand == eCmdExecutionMarker_Enable_FCVAR_SERVER_CAN_EXECUTE )
++g_iFilterCommandsByServerCanExecute;
else if ( cCommand == eCmdExecutionMarker_Disable_FCVAR_SERVER_CAN_EXECUTE )
--g_iFilterCommandsByServerCanExecute;
else if ( cCommand == eCmdExecutionMarker_Enable_FCVAR_CLIENTCMD_CAN_EXECUTE )
++g_iFilterCommandsByClientCmdCanExecute;
else if ( cCommand == eCmdExecutionMarker_Disable_FCVAR_CLIENTCMD_CAN_EXECUTE )
--g_iFilterCommandsByClientCmdCanExecute;
}
else
{
static int cnt = 0;
if ( ++cnt < 3 )
Warning( "Invalid execution marker code.\n" );
}
}
static bool ShouldPreventServerCommand( const ConCommandBase *pCommand )
{
if ( !Host_IsSinglePlayerGame() )
{
if ( g_iFilterCommandsByServerCanExecute > 0 )
{
// If we don't understand the command, and it came from a server command, then forward it back to the server.
// Lots of server plugins use this. They use engine->ClientCommand() to have a client execute a command that
// they have hooked on the server. We disabled it once and they freaked. It IS redundant since they could just
// code the call to their command on the server, but they complained loudly enough that we're adding it back in
// since there's no exploit that we know of by allowing it.
if ( !pCommand )
return false;
if ( !pCommand->IsFlagSet( FCVAR_SERVER_CAN_EXECUTE ) )
{
Warning( "FCVAR_SERVER_CAN_EXECUTE prevented server running command: %s\n", pCommand->GetName() );
return true;
}
}
}
return false;
}
static bool ShouldPreventClientCommand( const ConCommandBase *pCommand )
{
if ( g_iFilterCommandsByClientCmdCanExecute > 0 &&
!pCommand->IsFlagSet( FCVAR_CLIENTCMD_CAN_EXECUTE ) &&
g_ExtraClientCmdCanExecuteCvars.Find( pCommand->GetName() ) == g_ExtraClientCmdCanExecuteCvars.InvalidIndex() )
{
// If this command is in the game DLL, don't mention it because we're going to forward this
// request to the server and let the server handle it.
if ( !pCommand->IsFlagSet( FCVAR_GAMEDLL ) )
{
Warning( "FCVAR_CLIENTCMD_CAN_EXECUTE prevented running command: %s\n", pCommand->GetName() );
}
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// A complete command line has been parsed, so try to execute it
// FIXME: lookupnoadd the token to speed search?
//-----------------------------------------------------------------------------
const ConCommandBase *Cmd_ExecuteCommand( const CCommand &command, cmd_source_t src, int nClientSlot )
{
// execute the command line
if ( !command.ArgC() )
return NULL; // no tokens
// First, check for execution markers.
if ( Q_strcmp( command[0], CMDSTR_ADD_EXECUTION_MARKER ) == 0 )
{
if ( command.ArgC() == 3 )
{
HandleExecutionMarker( command[1], command[2] );
}
else
{
Warning( "WARNING: INVALID EXECUTION MARKER.\n" );
}
return NULL;
}
// check alias
cmdalias_t *a;
for ( a=cmd_alias; a; a=a->next )
{
if ( !Q_strcasecmp( command[0], a->name ) )
{
Cbuf_InsertText( a->value );
return NULL;
}
}
cmd_source = src;
cmd_clientslot = nClientSlot;
// check ConCommands
const ConCommandBase *pCommand = g_pCVar->FindCommandBase( command[ 0 ] );
// If we prevent a server command due to FCVAR_SERVER_CAN_EXECUTE not being set, then we get out immediately.
if ( ShouldPreventServerCommand( pCommand ) )
return NULL;
if ( pCommand && pCommand->IsCommand() )
{
if ( !ShouldPreventClientCommand( pCommand ) && pCommand->IsCommand() )
{
bool isServerCommand = ( pCommand->IsFlagSet( 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( cmd_source == src_client ? nClientSlot : -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( command );
return NULL;
}
// 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
if ( pCommand->IsFlagSet( FCVAR_CHEAT ) )
{
if ( !Host_IsSinglePlayerGame() && !CanCheat() )
{
// But.. if the server is allowed to run this command and the server DID run this command, then let it through.
// (used by soundscape_flush)
if ( g_iFilterCommandsByServerCanExecute == 0 || !pCommand->IsFlagSet( FCVAR_SERVER_CAN_EXECUTE ) )
{
Msg( "Can't use cheat command %s in multiplayer, unless the server has sv_cheats set to 1.\n", pCommand->GetName() );
return NULL;
}
}
}
if ( pCommand->IsFlagSet( FCVAR_SPONLY ) )
{
if ( !Host_IsSinglePlayerGame() )
{
Msg( "Can't use command %s in multiplayer.\n", pCommand->GetName() );
return NULL;
}
}
if ( pCommand->IsFlagSet( FCVAR_DEVELOPMENTONLY ) )
{
Msg( "Unknown command \"%s\"\n", pCommand->GetName() );
return NULL;
}
Cmd_Dispatch( pCommand, command );
return pCommand;
}
}
// Bail out before we update convars if we're runnign in default mode.
if ( pCommand && src == src_command && CommandLine()->CheckParm( "-default" ) && !pCommand->IsFlagSet( FCVAR_EXEC_DESPITE_DEFAULT ) )
{
Msg( "Ignoring cvar \"%s\" due to -default on command line\n", pCommand->GetName() );
return NULL;
}
// check cvars
if ( cv->IsCommand( command ) )
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( command );
return NULL;
}
}
Msg( "Unknown command \"%s\"\n", command[0] );
return NULL;
}
//-----------------------------------------------------------------------------
// Sends the entire command line over to the server
//-----------------------------------------------------------------------------
void Cmd_ForwardToServer( const CCommand &args, bool bReliable )
{
// YWB 6/3/98 Don't forward if this is a dedicated server
#ifndef SWDS
char str[1024];
#ifndef _XBOX
if ( demoplayer->IsPlayingBack() )
return; // not really connected
#endif
str[0] = 0;
if ( Q_strcasecmp( args[0], "cmd") != 0 )
{
Q_strncat( str, args[0], sizeof( str ), COPY_ALL_CHARACTERS );
Q_strncat( str, " ", sizeof( str ), COPY_ALL_CHARACTERS );
}
if ( args.ArgC() > 1)
{
Q_strncat( str, args.ArgS(), sizeof( str ), COPY_ALL_CHARACTERS );
}
cl.SendStringCmd( str );
#endif
}
|