-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathsv_main.cpp
2983 lines (2417 loc) · 82.4 KB
/
sv_main.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
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
//
// Purpose:
//
// $Workfile: $
// $NoKeywords: $
//===========================================================================//
#include "server_pch.h"
#include "decal.h"
#include "host_cmd.h"
#include "cmodel_engine.h"
#include "sv_log.h"
#include "zone.h"
#include "sound.h"
#include "vox.h"
#include "EngineSoundInternal.h"
#include "checksum_engine.h"
#include "host.h"
#include "keys.h"
#include "vengineserver_impl.h"
#include "sv_filter.h"
#include "pr_edict.h"
#include "screen.h"
#include "sys_dll.h"
#include "world.h"
#include "sv_main.h"
#include "networkstringtableserver.h"
#include "datamap.h"
#include "filesystem_engine.h"
#include "string_t.h"
#include "vstdlib/random.h"
#include "networkstringtable.h"
#include "dt_send_eng.h"
#include "sv_packedentities.h"
#include "testscriptmgr.h"
#include "PlayerState.h"
#include "saverestoretypes.h"
#include "tier0/vprof.h"
#include "proto_oob.h"
#include "staticpropmgr.h"
#include "checksum_crc.h"
#include "console.h"
#include "tier0/icommandline.h"
#include "gl_matsysiface.h"
#include "GameEventManager.h"
#ifndef SWDS
#include "vgui_baseui_interface.h"
#endif
#include "cbenchmark.h"
#include "client.h"
#include "hltvserver.h"
#include "replay_internal.h"
#include "replayserver.h"
#include "KeyValues.h"
#include "sv_logofile.h"
#include "cl_steamauth.h"
#include "sv_steamauth.h"
#include "sv_plugin.h"
#include "DownloadListGenerator.h"
#include "sv_steamauth.h"
#include "LocalNetworkBackdoor.h"
#include "cvar.h"
#include "enginethreads.h"
#include "tier1/functors.h"
#include "vstdlib/jobthread.h"
#include "pure_server.h"
#include "datacache/idatacache.h"
#include "filesystem/IQueuedLoader.h"
#include "vstdlib/jobthread.h"
#include "SourceAppInfo.h"
#include "cl_rcon.h"
#include "host_state.h"
#include "voice.h"
#include "cbenchmark.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#include "tier0/memalloc.h"
extern CNetworkStringTableContainer *networkStringTableContainerServer;
extern CNetworkStringTableContainer *networkStringTableContainerClient;
//void OnHibernateWhenEmptyChanged( IConVar *var, const char *pOldValue, float flOldValue );
//ConVar sv_hibernate_when_empty( "sv_hibernate_when_empty", "1", 0, "Puts the server into extremely low CPU usage mode when no clients connected", OnHibernateWhenEmptyChanged );
//ConVar sv_hibernate_ms( "sv_hibernate_ms", "20", 0, "# of milliseconds to sleep per frame while hibernating" );
//ConVar sv_hibernate_ms_vgui( "sv_hibernate_ms_vgui", "20", 0, "# of milliseconds to sleep per frame while hibernating but running the vgui dedicated server frontend" );
//static ConVar sv_hibernate_postgame_delay( "sv_hibernate_postgame_delay", "5", 0, "# of seconds to wait after final client leaves before hibernating.");
ConVar sv_shutdown_timeout_minutes( "sv_shutdown_timeout_minutes", "360", FCVAR_REPLICATED, "If sv_shutdown is pending, wait at most N minutes for server to drain before forcing shutdown." );
static double s_timeForceShutdown = 0.0;
extern ConVar deathmatch;
extern ConVar sv_sendtables;
// Server default maxplayers value
#define DEFAULT_SERVER_CLIENTS 6
// This many players on a Lan with same key, is ok.
#define MAX_IDENTICAL_CDKEYS 5
CGameServer sv;
CGlobalVars g_ServerGlobalVariables( false );
static int current_skill;
static void SV_CheatsChanged_f( IConVar *pConVar, const char *pOldString, float flOldValue )
{
ConVarRef var( pConVar );
if ( var.GetInt() == 0 )
{
// cheats were disabled, revert all cheat cvars to their default values
g_pCVar->RevertFlaggedConVars( FCVAR_CHEAT );
DevMsg( "FCVAR_CHEAT cvars reverted to defaults.\n" );
}
}
static bool g_sv_pure_waiting_on_reload = false;
static int g_sv_pure_mode = 0;
int GetSvPureMode()
{
return g_sv_pure_mode;
}
static void SV_Pure_f( const CCommand &args )
{
int pure_mode = -2;
if ( args.ArgC() == 2 )
{
pure_mode = atoi( args[1] );
}
Msg( "--------------------------------------------------------\n" );
if ( pure_mode >= -1 && pure_mode <= 2 )
{
// Not changing?
if ( pure_mode == GetSvPureMode() )
{
Msg( "sv_pure value unchanged (current value is %d).\n", GetSvPureMode() );
}
else
{
// Set the value.
g_sv_pure_mode = pure_mode;
Msg( "sv_pure set to %d.\n", g_sv_pure_mode );
if ( sv.IsActive() )
{
g_sv_pure_waiting_on_reload = true;
}
}
}
else
{
Msg( "sv_pure: Only allow client to use certain files.\n"
"\n"
" -1 - Do not apply any rules or restrict which files the client may load.\n"
" 0 - Apply rules in cfg/pure_server_minimal.txt only.\n"
" 1 - Apply rules in cfg/pure_server_full.txt and then cfg/pure_server_whitelist.txt.\n"
" 2 - Apply rules in cfg/pure_server_full.txt.\n"
"\n"
" See cfg/pure_server_whitelist_example.txt for more details.\n"
);
}
if ( pure_mode == -2 )
{
// If we're a client on a server with sv_pure = 1, display the current whitelist.
#ifndef DEDICATED
if ( cl.IsConnected() )
{
Msg( "\n\n" );
extern void CL_PrintWhitelistInfo(); // from cl_main.cpp
CL_PrintWhitelistInfo();
}
else
#endif
{
Msg( "\nCurrent sv_pure value is %d.\n", GetSvPureMode() );
}
}
if ( sv.IsActive() && g_sv_pure_waiting_on_reload )
{
Msg( "Note: Waiting for the next changelevel to apply the current value.\n" );
}
Msg( "--------------------------------------------------------\n" );
}
static ConCommand sv_pure( "sv_pure", SV_Pure_f, "Show user data." );
ConVar sv_pure_kick_clients( "sv_pure_kick_clients", "1", 0, "If set to 1, the server will kick clients with mismatching files. Otherwise, it will issue a warning to the client." );
ConVar sv_pure_trace( "sv_pure_trace", "0", 0, "If set to 1, the server will print a message whenever a client is verifying a CRC for a file." );
ConVar sv_pure_consensus( "sv_pure_consensus", "5", 0, "Minimum number of file hashes to agree to form a consensus." );
ConVar sv_pure_retiretime( "sv_pure_retiretime", "900", 0, "Seconds of server idle time to flush the sv_pure file hash cache." );
ConVar sv_cheats( "sv_cheats", "0", FCVAR_NOTIFY|FCVAR_REPLICATED, "Allow cheats on server", SV_CheatsChanged_f );
ConVar sv_lan( "sv_lan", "0", 0, "Server is a lan server ( no heartbeat, no authentication, no non-class C addresses )" );
static ConVar sv_pausable( "sv_pausable","0", FCVAR_NOTIFY, "Is the server pausable." );
static ConVar sv_contact( "sv_contact", "", FCVAR_NOTIFY, "Contact email for server sysop" );
static ConVar sv_cacheencodedents("sv_cacheencodedents", "1", 0, "If set to 1, does an optimization to prevent extra SendTable_Encode calls.");
static ConVar sv_voiceenable( "sv_voiceenable", "1", FCVAR_ARCHIVE|FCVAR_NOTIFY ); // set to 0 to disable all voice forwarding.
ConVar sv_downloadurl( "sv_downloadurl", "", FCVAR_REPLICATED, "Location from which clients can download missing files" );
ConVar sv_maxreplay("sv_maxreplay", "0", 0, "Maximum replay time in seconds", true, 0, true, 15 );
static ConVar sv_consistency( "sv_consistency", "1", FCVAR_REPLICATED, "Legacy variable with no effect! This was deleted and then added as a temporary kludge to prevent players from being banned by servers running old versions of SMAC" );
/// XXX(JohnS): When steam voice gets ugpraded to Opus we will probably default back to steam. At that time we should
/// note that Steam voice is the highest quality codec below.
static ConVar sv_voicecodec( "sv_voicecodec", "vaudio_opus", 0,
"Specifies which voice codec to use. Valid options are:\n"
"vaudio_speex - Legacy Speex codec (lowest quality)\n"
"vaudio_celt - Newer CELT codec\n"
"vaudio_opus - Latest Opus codec (highest quality, comes by default)\n"
"steam - Use Steam voice API" );
ConVar sv_mincmdrate( "sv_mincmdrate", "10", FCVAR_REPLICATED, "This sets the minimum value for cl_cmdrate. 0 == unlimited." );
ConVar sv_maxcmdrate( "sv_maxcmdrate", "66", FCVAR_REPLICATED, "(If sv_mincmdrate is > 0), this sets the maximum value for cl_cmdrate." );
ConVar sv_client_cmdrate_difference( "sv_client_cmdrate_difference", "20", FCVAR_REPLICATED,
"cl_cmdrate is moved to within sv_client_cmdrate_difference units of cl_updaterate before it "
"is clamped between sv_mincmdrate and sv_maxcmdrate." );
ConVar sv_client_min_interp_ratio( "sv_client_min_interp_ratio", "1", FCVAR_REPLICATED,
"This can be used to limit the value of cl_interp_ratio for connected clients "
"(only while they are connected).\n"
" -1 = let clients set cl_interp_ratio to anything\n"
" any other value = set minimum value for cl_interp_ratio"
);
ConVar sv_client_max_interp_ratio( "sv_client_max_interp_ratio", "5", FCVAR_REPLICATED,
"This can be used to limit the value of cl_interp_ratio for connected clients "
"(only while they are connected). If sv_client_min_interp_ratio is -1, "
"then this cvar has no effect."
);
ConVar sv_client_predict( "sv_client_predict", "-1", FCVAR_REPLICATED,
"This can be used to force the value of cl_predict for connected clients "
"(only while they are connected).\n"
" -1 = let clients set cl_predict to anything\n"
" 0 = force cl_predict to 0\n"
" 1 = force cl_predict to 1"
);
ConVar sv_restrict_aspect_ratio_fov( "sv_restrict_aspect_ratio_fov", "1", FCVAR_REPLICATED,
"This can be used to limit the effective FOV of users using wide-screen\n"
"resolutions with aspect ratios wider than 1.85:1 (slightly wider than 16:9).\n"
" 0 = do not cap effective FOV\n"
" 1 = limit the effective FOV on windowed mode users using resolutions\n"
" greater than 1.85:1\n"
" 2 = limit the effective FOV on both windowed mode and full-screen users\n",
true, 0, true, 2);
void OnTVEnablehanged( IConVar *pConVar, const char *pOldString, float flOldValue )
{
ConVarRef var( pConVar );
/*
ConVarRef replay_enable( "replay_enable" );
if ( var.GetBool() && replay_enable.IsValid() && replay_enable.GetBool() )
{
var.SetValue( 0 );
Warning( "Error: Replay is enabled. Please disable Replay if you wish to enable SourceTV.\n" );
return;
}
*/
//Let's check maxclients and make sure we have room for SourceTV
if ( var.GetBool() == true )
{
sv.InitMaxClients();
}
}
ConVar tv_enable( "tv_enable", "0", FCVAR_NOTIFY, "Activates SourceTV on server.", OnTVEnablehanged );
extern ConVar *sv_noclipduringpause;
static bool s_bForceSend = false;
void SV_ForceSend()
{
s_bForceSend = true;
}
bool g_FlushMemoryOnNextServer;
int g_FlushMemoryOnNextServerCounter;
void SV_FlushMemoryOnNextServer()
{
g_FlushMemoryOnNextServer = true;
g_FlushMemoryOnNextServerCounter++;
}
// Prints important entity creation/deletion events to console
#if defined( _DEBUG )
ConVar sv_deltatrace( "sv_deltatrace", "0", 0, "For debugging, print entity creation/deletion info to console." );
#define TRACE_DELTA( text ) if ( sv_deltatrace.GetInt() ) { ConMsg( text ); };
#else
#define TRACE_DELTA( funcs )
#endif
#if defined( DEBUG_NETWORKING )
//-----------------------------------------------------------------------------
// Opens the recording file
//-----------------------------------------------------------------------------
static FILE* OpenRecordingFile()
{
FILE* fp = 0;
static bool s_CantOpenFile = false;
static bool s_NeverOpened = true;
if (!s_CantOpenFile)
{
fp = fopen( "svtrace.txt", s_NeverOpened ? "wt" : "at" );
if (!fp)
{
s_CantOpenFile = true;
}
s_NeverOpened = false;
}
return fp;
}
//-----------------------------------------------------------------------------
// Records an argument for a command, flushes when the command is done
//-----------------------------------------------------------------------------
/*
void SpewToFile( char const* pFmt, ... )
static void SpewToFile( const char* pFmt, ... )
{
static CUtlVector<unsigned char> s_RecordingBuffer;
char temp[2048];
va_list args;
va_start( args, pFmt );
int len = Q_vsnprintf( temp, sizeof( temp ), pFmt, args );
va_end( args );
Assert( len < 2048 );
int idx = s_RecordingBuffer.AddMultipleToTail( len );
memcpy( &s_RecordingBuffer[idx], temp, len );
if ( 1 ) //s_RecordingBuffer.Size() > 8192)
{
FILE* fp = OpenRecordingFile();
fwrite( s_RecordingBuffer.Base(), 1, s_RecordingBuffer.Size(), fp );
fclose( fp );
s_RecordingBuffer.RemoveAll();
}
}
*/
#endif // #if defined( DEBUG_NETWORKING )
/*void SV_Init(bool isDedicated)
{
sv.Init( isDedicated );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void SV_Shutdown( void )
{
sv.Shutdown();
}*/
void CGameServer::Clear( void )
{
m_pModelPrecacheTable = NULL;
m_pGenericPrecacheTable = NULL;
m_pSoundPrecacheTable = NULL;
m_pDecalPrecacheTable = NULL;
m_pDynamicModelsTable = NULL;
m_bIsLevelMainMenuBackground = false;
m_bLoadgame = false;
host_state.SetWorldModel( NULL );
Q_memset( m_szStartspot, 0, sizeof( m_szStartspot ) );
num_edicts = 0;
max_edicts = 0;
free_edicts = 0;
edicts = NULL;
// Clear the instance baseline indices in the ServerClasses.
if ( serverGameDLL )
{
for( ServerClass *pCur = serverGameDLL->GetAllServerClasses(); pCur; pCur=pCur->m_pNext )
{
pCur->m_InstanceBaselineIndex = INVALID_STRING_INDEX;
}
}
for ( int i = 0; i < m_TempEntities.Count(); i++ )
{
delete m_TempEntities[i];
}
m_TempEntities.Purge();
CBaseServer::Clear();
}
//-----------------------------------------------------------------------------
// Purpose: Create any client/server string tables needed internally by the engine
//-----------------------------------------------------------------------------
void CGameServer::CreateEngineStringTables( void )
{
int i,j;
m_StringTables->SetTick( m_nTickCount ); // set first tick
bool bUseFilenameTables = false;
char szDownloadableFileTablename[255] = DOWNLOADABLE_FILE_TABLENAME;
char szModelPrecacheTablename[255] = MODEL_PRECACHE_TABLENAME;
char szGenericPrecacheTablename[255] = GENERIC_PRECACHE_TABLENAME;
char szSoundPrecacheTablename[255] = SOUND_PRECACHE_TABLENAME;
char szDecalPrecacheTablename[255] = DECAL_PRECACHE_TABLENAME;
// This was added into staging at some point and is not enabled in main or rel.
if ( 0 )
{
bUseFilenameTables = true;
Q_snprintf( szDownloadableFileTablename, 255, ":%s", DOWNLOADABLE_FILE_TABLENAME );
Q_snprintf( szModelPrecacheTablename, 255, ":%s", MODEL_PRECACHE_TABLENAME );
Q_snprintf( szGenericPrecacheTablename, 255, ":%s", GENERIC_PRECACHE_TABLENAME );
Q_snprintf( szSoundPrecacheTablename, 255, ":%s", SOUND_PRECACHE_TABLENAME );
Q_snprintf( szDecalPrecacheTablename, 255, ":%s", DECAL_PRECACHE_TABLENAME );
}
m_pDownloadableFileTable = m_StringTables->CreateStringTableEx(
szDownloadableFileTablename,
MAX_DOWNLOADABLE_FILES,
0,
0,
bUseFilenameTables );
m_pModelPrecacheTable = m_StringTables->CreateStringTableEx(
szModelPrecacheTablename,
MAX_MODELS,
sizeof ( CPrecacheUserData ),
PRECACHE_USER_DATA_NUMBITS,
bUseFilenameTables );
m_pGenericPrecacheTable = m_StringTables->CreateStringTableEx(
szGenericPrecacheTablename,
MAX_GENERIC,
sizeof ( CPrecacheUserData ),
PRECACHE_USER_DATA_NUMBITS,
bUseFilenameTables );
m_pSoundPrecacheTable = m_StringTables->CreateStringTableEx(
szSoundPrecacheTablename,
MAX_SOUNDS,
sizeof ( CPrecacheUserData ),
PRECACHE_USER_DATA_NUMBITS,
bUseFilenameTables );
m_pDecalPrecacheTable = m_StringTables->CreateStringTableEx(
szDecalPrecacheTablename,
MAX_BASE_DECALS,
sizeof ( CPrecacheUserData ),
PRECACHE_USER_DATA_NUMBITS,
bUseFilenameTables );
m_pInstanceBaselineTable = m_StringTables->CreateStringTable(
INSTANCE_BASELINE_TABLENAME,
MAX_DATATABLES );
m_pLightStyleTable = m_StringTables->CreateStringTable(
LIGHT_STYLES_TABLENAME,
MAX_LIGHTSTYLES );
m_pUserInfoTable = m_StringTables->CreateStringTable(
USER_INFO_TABLENAME,
1<<ABSOLUTE_PLAYER_LIMIT_DW ); // make it a power of 2
// Fixed-size user data; bit value of either 0 or 1.
m_pDynamicModelsTable = m_StringTables->CreateStringTable( "DynamicModels", 2048, true, 1 );
// Send the query info..
m_pServerStartupTable = m_StringTables->CreateStringTable(
SERVER_STARTUP_DATA_TABLENAME,
4 );
SetQueryPortFromSteamServer();
CopyPureServerWhitelistToStringTable();
Assert ( m_pModelPrecacheTable &&
m_pGenericPrecacheTable &&
m_pSoundPrecacheTable &&
m_pDecalPrecacheTable &&
m_pInstanceBaselineTable &&
m_pLightStyleTable &&
m_pUserInfoTable &&
m_pServerStartupTable &&
m_pDownloadableFileTable &&
m_pDynamicModelsTable );
// create an empty lightstyle table with unique index names
for ( i = 0; i<MAX_LIGHTSTYLES; i++ )
{
char name[8]; Q_snprintf( name, 8, "%i", i );
j = m_pLightStyleTable->AddString( true, name );
Assert( j==i ); // indices must match
}
for ( i = 0; i<GetMaxClients(); i++ )
{
char name[8]; Q_snprintf( name, 8, "%i", i );
j = m_pUserInfoTable->AddString( true, name );
Assert( j==i ); // indices must match
}
// set up the downloadable files generator
DownloadListGenerator().SetStringTable( m_pDownloadableFileTable );
}
void CGameServer::SetQueryPortFromSteamServer()
{
if ( !m_pServerStartupTable )
return;
int queryPort = Steam3Server().GetQueryPort();
m_pServerStartupTable->AddString( true, "QueryPort", sizeof( queryPort ), &queryPort );
}
void CGameServer::CopyPureServerWhitelistToStringTable()
{
if ( !m_pPureServerWhitelist )
return;
CUtlBuffer buf;
m_pPureServerWhitelist->Encode( buf );
m_pServerStartupTable->AddString( true, "PureServerWhitelist", buf.TellPut(), buf.Base() );
}
void SV_InstallClientStringTableMirrors( void )
{
#ifndef SWDS
#ifndef SHARED_NET_STRING_TABLES
int numTables = networkStringTableContainerServer->GetNumTables();
for ( int i =0; i<numTables; i++)
{
// iterate through server tables
CNetworkStringTable *serverTable =
(CNetworkStringTable*)networkStringTableContainerServer->GetTable( i );
if ( !serverTable )
continue;
// get mathcing client table
CNetworkStringTable *clientTable =
(CNetworkStringTable*)networkStringTableContainerClient->FindTable( serverTable->GetTableName() );
if ( !clientTable )
{
DevMsg("SV_InstallClientStringTableMirrors! Missing client table \"%s\".\n ", serverTable->GetTableName() );
continue;
}
// link client table to server table
serverTable->SetMirrorTable( clientTable );
}
#endif
#endif
}
//-----------------------------------------------------------------------------
// user <name or userid>
//
// Dump userdata / masterdata for a user
//-----------------------------------------------------------------------------
CON_COMMAND( user, "Show user data." )
{
int uid;
int i;
if ( !sv.IsActive() )
{
ConMsg( "Can't 'user', not running a server\n" );
return;
}
if (args.ArgC() != 2)
{
ConMsg ("Usage: user <username / userid>\n");
return;
}
uid = atoi(args[1]);
for (i=0 ; i< sv.GetClientCount() ; i++)
{
IClient *pClient = sv.GetClient( i );
if ( !pClient->IsConnected() )
continue;
if ( (pClient->GetPlayerSlot()== uid ) || !Q_strcmp( pClient->GetClientName(), args[1]) )
{
ConMsg ("TODO: SV_User_f.\n");
return;
}
}
ConMsg ("User not in server.\n");
}
//-----------------------------------------------------------------------------
// Dump userids for all current players
//-----------------------------------------------------------------------------
CON_COMMAND( users, "Show user info for players on server." )
{
if ( !sv.IsActive() )
{
ConMsg( "Can't 'users', not running a server\n" );
return;
}
int c = 0;
ConMsg ("<slot:userid:\"name\">\n");
for ( int i=0 ; i< sv.GetClientCount() ; i++ )
{
IClient *pClient = sv.GetClient( i );
if ( pClient->IsConnected() )
{
ConMsg ("%i:%i:\"%s\"\n", pClient->GetPlayerSlot(), pClient->GetUserID(), pClient->GetClientName() );
c++;
}
}
ConMsg ( "%i users\n", c );
}
//-----------------------------------------------------------------------------
// Purpose: Determine the value of sv.maxclients
//-----------------------------------------------------------------------------
bool CL_IsHL2Demo(); // from cl_main.cpp
bool CL_IsPortalDemo(); // from cl_main.cpp
extern ConVar tv_enable;
void SetupMaxPlayers( int iDesiredMaxPlayers )
{
int minmaxplayers = 1;
int maxmaxplayers = ABSOLUTE_PLAYER_LIMIT;
int defaultmaxplayers = 1;
if ( serverGameClients )
{
serverGameClients->GetPlayerLimits( minmaxplayers, maxmaxplayers, defaultmaxplayers );
if ( minmaxplayers < 1 )
{
Sys_Error( "GetPlayerLimits: min maxplayers must be >= 1 (%i)", minmaxplayers );
}
else if ( defaultmaxplayers < 1 )
{
Sys_Error( "GetPlayerLimits: default maxplayers must be >= 1 (%i)", minmaxplayers );
}
if ( minmaxplayers > maxmaxplayers || defaultmaxplayers > maxmaxplayers )
{
Sys_Error( "GetPlayerLimits: min maxplayers %i > max %i", minmaxplayers, maxmaxplayers );
}
if ( maxmaxplayers > ABSOLUTE_PLAYER_LIMIT )
{
Sys_Error( "GetPlayerLimits: max players limited to %i", ABSOLUTE_PLAYER_LIMIT );
}
}
// Determine absolute limit
sv.m_nMaxClientsLimit = maxmaxplayers;
// Check for command line override
int newmaxplayers = iDesiredMaxPlayers;
if ( newmaxplayers >= 1 )
{
// Never go above what the game .dll can handle
newmaxplayers = min( newmaxplayers, maxmaxplayers );
sv.m_nMaxClientsLimit = newmaxplayers;
}
else
{
newmaxplayers = defaultmaxplayers;
}
#if defined( REPLAY_ENABLED )
if ( Replay_IsSupportedModAndPlatform() && CommandLine()->CheckParm( "-replay" ) )
{
newmaxplayers += 1;
sv.m_nMaxClientsLimit += 1;
}
#endif
if ( tv_enable.GetBool() )
{
newmaxplayers += 1;
sv.m_nMaxClientsLimit += 1;
}
newmaxplayers = clamp( newmaxplayers, minmaxplayers, sv.m_nMaxClientsLimit );
if ( ( CL_IsHL2Demo() || CL_IsPortalDemo() ) && !sv.IsDedicated() )
{
newmaxplayers = 1;
sv.m_nMaxClientsLimit = 1;
}
if ( sv.GetMaxClients() < newmaxplayers || !tv_enable.GetBool() )
sv.SetMaxClients( newmaxplayers );
}
void CGameServer::InitMaxClients( void )
{
int newmaxplayers = CommandLine()->ParmValue( "-maxplayers", -1 );
if ( newmaxplayers == -1 )
{
newmaxplayers = CommandLine()->ParmValue( "+maxplayers", -1 );
}
SetupMaxPlayers( newmaxplayers );
}
//-----------------------------------------------------------------------------
// Purpose: Changes the maximum # of players allowed on the server.
// Server cannot be running when command is issued.
//-----------------------------------------------------------------------------
CON_COMMAND( maxplayers, "Change the maximum number of players allowed on this server." )
{
if ( args.ArgC () != 2 )
{
ConMsg ("\"maxplayers\" is \"%u\"\n", sv.GetMaxClients() );
return;
}
if ( sv.IsActive() )
{
ConMsg( "Cannot change maxplayers while the server is running\n");
return;
}
SetupMaxPlayers( Q_atoi( args[ 1 ] ) );
}
int SV_BuildSendTablesArray( ServerClass *pClasses, SendTable **pTables, int nMaxTables )
{
int nTables = 0;
for( ServerClass *pCur=pClasses; pCur; pCur=pCur->m_pNext )
{
ErrorIfNot( nTables < nMaxTables, ("SV_BuildSendTablesArray: too many SendTables!") );
pTables[nTables] = pCur->m_pTable;
++nTables;
}
return nTables;
}
// Builds an alternate copy of the datatable for any classes that have datatables with props excluded.
void SV_InitSendTables( ServerClass *pClasses )
{
SendTable *pTables[MAX_DATATABLES];
int nTables = SV_BuildSendTablesArray( pClasses, pTables, ARRAYSIZE( pTables ) );
SendTable_Init( pTables, nTables );
}
void SV_TermSendTables( ServerClass *pClasses )
{
SendTable_Term();
}
//-----------------------------------------------------------------------------
// Purpose: returns which games/mods we're allowed to play
//-----------------------------------------------------------------------------
struct ModDirPermissions_t
{
int m_iAppID;
const char *m_pchGameDir;
};
static ModDirPermissions_t g_ModDirPermissions[] =
{
{ GetAppSteamAppId( k_App_CSS ), GetAppModName( k_App_CSS ) },
{ GetAppSteamAppId( k_App_DODS ), GetAppModName( k_App_DODS ) },
{ GetAppSteamAppId( k_App_HL2MP ), GetAppModName( k_App_HL2MP ) },
{ GetAppSteamAppId( k_App_LOST_COAST ), GetAppModName( k_App_LOST_COAST ) },
{ GetAppSteamAppId( k_App_HL1DM ), GetAppModName( k_App_HL1DM ) },
{ GetAppSteamAppId( k_App_PORTAL ), GetAppModName( k_App_PORTAL ) },
{ GetAppSteamAppId( k_App_HL2 ), GetAppModName( k_App_HL2 ) },
{ GetAppSteamAppId( k_App_HL2_EP1 ), GetAppModName( k_App_HL2_EP1 ) },
{ GetAppSteamAppId( k_App_HL2_EP2 ), GetAppModName( k_App_HL2_EP2 ) },
{ GetAppSteamAppId( k_App_TF2 ), GetAppModName( k_App_TF2 ) },
};
bool ServerDLL_Load( bool bIsServerOnly )
{
// Load in the game .dll
LoadEntityDLLs( GetBaseDirectory(), bIsServerOnly );
return true;
}
void ServerDLL_Unload()
{
UnloadEntityDLLs();
}
#if !defined(DEDICATED)
#if !defined(_X360)
// Put this function declaration at global scope to avoid the ambiguity of the most vexing parse.
bool CL_IsHL2Demo();
#endif
#endif
//-----------------------------------------------------------------------------
// Purpose: Loads the game .dll
//-----------------------------------------------------------------------------
void SV_InitGameDLL( void )
{
// Clear out the command buffer.
Cbuf_Execute();
// Don't initialize a second time
if ( sv.dll_initialized )
{
return;
}
#if !defined(SWDS)
#if !defined(_X360)
if ( CL_IsHL2Demo() && !sv.IsDedicated() && Q_stricmp( COM_GetModDirectory(), "hl2" ) )
{
Error( "The HL2 demo is unable to run Mods.\n" );
return;
}
if ( CL_IsPortalDemo() && !sv.IsDedicated() && Q_stricmp( COM_GetModDirectory(), "portal" ) )
{
Error( "The Portal demo is unable to run Mods.\n" );
return;
}
// check permissions
if ( Steam3Client().SteamApps() && !CL_IsHL2Demo() && !CL_IsPortalDemo() )
{
bool bVerifiedMod = false;
// find the game dir we're running
for ( int i = 0; i < ARRAYSIZE( g_ModDirPermissions ); i++ )
{
if ( !Q_stricmp( COM_GetModDirectory(), g_ModDirPermissions[i].m_pchGameDir ) )
{
// we've found the mod, make sure we own the app
if ( Steam3Client().SteamApps()->BIsSubscribedApp( g_ModDirPermissions[i].m_iAppID ) )
{
bVerifiedMod = true;
}
else
{
Error( "No permissions to run '%s'\n", COM_GetModDirectory() );
return;
}
break;
}
}
if ( !bVerifiedMod )
{
// make sure they can run the Source engine
if ( ! Steam3Client().SteamApps()->BIsSubscribedApp( 215 ) )
{
Error( "A Source engine game is required to run mods\n" );
return;
}
}
}
#endif // _X360
#endif
COM_TimestampedLog( "SV_InitGameDLL" );
if ( !serverGameDLL )
{
Warning( "Failed to load server binary\n" );
return;
}
// Flag that we've started the game .dll
sv.dll_initialized = true;
COM_TimestampedLog( "serverGameDLL->DLLInit" );
// Tell the game DLL to start up
if(!serverGameDLL->DLLInit(g_AppSystemFactory, g_AppSystemFactory, g_AppSystemFactory, &g_ServerGlobalVariables))
{
Host_Error("IDLLFunctions::DLLInit returned false.\n");
}
if ( CommandLine()->FindParm( "-NoLoadPluginsForClient" ) == 0 )
g_pServerPluginHandler->LoadPlugins(); // load 3rd party plugins
// let's not have any servers with no name
if ( host_name.GetString()[0] == 0 )
{
host_name.SetValue( serverGameDLL->GetGameDescription() );
}
sv_noclipduringpause = ( ConVar * )g_pCVar->FindVar( "sv_noclipduringpause" );
COM_TimestampedLog( "SV_InitSendTables" );
// Make extra copies of data tables if they have SendPropExcludes.
SV_InitSendTables( serverGameDLL->GetAllServerClasses() );
host_state.interval_per_tick = serverGameDLL->GetTickInterval();
if ( host_state.interval_per_tick < MINIMUM_TICK_INTERVAL ||
host_state.interval_per_tick > MAXIMUM_TICK_INTERVAL )
{
Sys_Error( "GetTickInterval returned bogus tick interval (%f)[%f to %f is valid range]", host_state.interval_per_tick,
MINIMUM_TICK_INTERVAL, MAXIMUM_TICK_INTERVAL );
}
// set maxclients limit based on Mod or commandline settings
sv.InitMaxClients();
// Execute and server commands the game .dll added at startup
Cbuf_Execute();
#if defined( REPLAY_ENABLED )
extern IReplaySystem *g_pReplay;
if ( Replay_IsSupportedModAndPlatform() && sv.IsDedicated() )
{
if ( !serverGameDLL->ReplayInit( g_fnReplayFactory ) )
{
Sys_Error( "Server replay init failed" );
}
if ( sv.IsDedicated() && !g_pReplay->SV_Init( g_ServerFactory ) )
{
Sys_Error( "Replay system server init failed!" );
}
}
#endif
}
//
// Release resources associated with extension DLLs.
//
void SV_ShutdownGameDLL( void )
{
if ( !sv.dll_initialized )
{
return;
}
if ( g_pReplay )
{
g_pReplay->SV_Shutdown();
}
// Delete any extra SendTable copies we've attached to the game DLL's classes, if any.
SV_TermSendTables( serverGameDLL->GetAllServerClasses() );
g_pServerPluginHandler->UnloadPlugins();
serverGameDLL->DLLShutdown();
UnloadEntityDLLs();
sv.dll_initialized = false;
}