forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsys_dll.cpp
1599 lines (1354 loc) · 42.8 KB
/
sys_dll.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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#if defined(_WIN32) && !defined(_X360)
#include "winlite.h"
#elif defined(OSX)
#include <Carbon/Carbon.h>
#include <sys/sysctl.h>
#elif defined(PLATFORM_BSD)
#include <sys/types.h>
#include <sys/sysctl.h>
#define HW_MEMSIZE HW_PHYSMEM
#endif
#if defined(LINUX)
#include <unistd.h>
#include <fcntl.h>
#endif
#if defined( USE_SDL )
#include "SDL.h"
#endif
#include "quakedef.h"
#include "igame.h"
#include "errno.h"
#include "host.h"
#include "profiling.h"
#include "server.h"
#include "vengineserver_impl.h"
#include "filesystem_engine.h"
#include "sys.h"
#include "sys_dll.h"
#include "ivideomode.h"
#include "host_cmd.h"
#include "crtmemdebug.h"
#include "sv_log.h"
#include "sv_main.h"
#include "traceinit.h"
#include "dt_test.h"
#include "keys.h"
#include "gl_matsysiface.h"
#include "tier0/vcrmode.h"
#include "tier0/icommandline.h"
#include "cmd.h"
#include <ihltvdirector.h>
#if defined( REPLAY_ENABLED )
#include "replay/ireplaysystem.h"
#endif
#include "MapReslistGenerator.h"
#include "DevShotGenerator.h"
#include "cdll_engine_int.h"
#include "dt_send.h"
#include "idedicatedexports.h"
#include "eifacev21.h"
#include "cl_steamauth.h"
#include "tier0/etwprof.h"
#include "vgui_baseui_interface.h"
#include "tier0/systeminformation.h"
#ifdef _WIN32
#if !defined( _X360 )
#include <io.h>
#endif
#endif
#include "toolframework/itoolframework.h"
#if defined( _X360 )
#include "xbox/xbox_win32stubs.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define ONE_HUNDRED_TWENTY_EIGHT_MB (128 * 1024 * 1024)
ConVar mem_min_heapsize( "mem_min_heapsize", "48", FCVAR_INTERNAL_USE, "Minimum amount of memory to dedicate to engine hunk and datacache (in mb)" );
ConVar mem_max_heapsize( "mem_max_heapsize", "256", FCVAR_INTERNAL_USE, "Maximum amount of memory to dedicate to engine hunk and datacache (in mb)" );
ConVar mem_max_heapsize_dedicated( "mem_max_heapsize_dedicated", "64", FCVAR_INTERNAL_USE, "Maximum amount of memory to dedicate to engine hunk and datacache, for dedicated server (in mb)" );
#define MINIMUM_WIN_MEMORY (unsigned)(mem_min_heapsize.GetInt()*1024*1024)
#define MAXIMUM_WIN_MEMORY max( (unsigned)(mem_max_heapsize.GetInt()*1024*1024), MINIMUM_WIN_MEMORY )
#define MAXIMUM_DEDICATED_MEMORY (unsigned)(mem_max_heapsize_dedicated.GetInt()*1024*1024)
char *CheckParm(const char *psz, char **ppszValue = NULL);
void SeedRandomNumberGenerator( bool random_invariant );
void Con_ColorPrintf( const Color& clr, PRINTF_FORMAT_STRING const char *fmt, ... ) FMTFUNCTION( 2, 3 );
void COM_ShutdownFileSystem( void );
void COM_InitFilesystem( const char *pFullModPath );
modinfo_t gmodinfo;
#ifdef PLATFORM_WINDOWS
HWND *pmainwindow = NULL;
#endif
char gszDisconnectReason[256];
char gszExtendedDisconnectReason[256];
bool gfExtendedError = false;
uint8 g_eSteamLoginFailure = 0;
bool g_bV3SteamInterface = false;
CreateInterfaceFn g_AppSystemFactory = NULL;
static bool s_bIsDedicated = false;
ConVar *sv_noclipduringpause = NULL;
// Special mode where the client uses a console window and has no graphics. Useful for stress-testing a server
// without having to round up 32 people.
bool g_bTextMode = false;
// Set to true when we exit from an error.
bool g_bInErrorExit = false;
static FileFindHandle_t g_hfind = FILESYSTEM_INVALID_FIND_HANDLE;
// The extension DLL directory--one entry per loaded DLL
CSysModule *g_GameDLL = NULL;
// Prototype of an global method function
typedef void (DLLEXPORT * PFN_GlobalMethod)( edict_t *pEntity );
IServerGameDLL *serverGameDLL = NULL;
int g_iServerGameDLLVersion = 0;
IServerGameEnts *serverGameEnts = NULL;
IServerGameClients *serverGameClients = NULL;
int g_iServerGameClientsVersion = 0; // This matches the number at the end of the interface name (so for "ServerGameClients004", this would be 4).
IHLTVDirector *serverGameDirector = NULL;
IServerGameTags *serverGameTags = NULL;
void Sys_InitArgv( char *lpCmdLine );
void Sys_ShutdownArgv( void );
//-----------------------------------------------------------------------------
// Purpose: Compare file times
// Input : ft1 -
// ft2 -
// Output : int
//-----------------------------------------------------------------------------
int Sys_CompareFileTime( long ft1, long ft2 )
{
if ( ft1 < ft2 )
{
return -1;
}
else if ( ft1 > ft2 )
{
return 1;
}
return 0;
}
//-----------------------------------------------------------------------------
// Is slash?
//-----------------------------------------------------------------------------
inline bool IsSlash( char c )
{
return ( c == '\\') || ( c == '/' );
}
//-----------------------------------------------------------------------------
// Purpose: Create specified directory
// Input : *path -
// Output : void Sys_mkdir
//-----------------------------------------------------------------------------
void Sys_mkdir( const char *path )
{
char testpath[ MAX_OSPATH ];
// Remove any terminal backslash or /
Q_strncpy( testpath, path, sizeof( testpath ) );
int nLen = Q_strlen( testpath );
if ( (nLen > 0) && IsSlash( testpath[ nLen - 1 ] ) )
{
testpath[ nLen - 1 ] = 0;
}
// Look for URL
const char *pPathID = "MOD";
if ( IsSlash( testpath[0] ) && IsSlash( testpath[1] ) )
{
pPathID = NULL;
}
if ( g_pFileSystem->FileExists( testpath, pPathID ) )
{
// if there is a file of the same name as the directory we want to make, just kill it
if ( !g_pFileSystem->IsDirectory( testpath, pPathID ) )
{
g_pFileSystem->RemoveFile( testpath, pPathID );
}
}
g_pFileSystem->CreateDirHierarchy( path, pPathID );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *path -
// *basename -
// Output : char *Sys_FindFirst
//-----------------------------------------------------------------------------
const char *Sys_FindFirst(const char *path, char *basename, int namelength )
{
if (g_hfind != FILESYSTEM_INVALID_FIND_HANDLE)
{
Sys_Error ("Sys_FindFirst without close");
g_pFileSystem->FindClose(g_hfind);
}
const char* psz = g_pFileSystem->FindFirst(path, &g_hfind);
if (basename && psz)
{
Q_FileBase(psz, basename, namelength );
}
return psz;
}
//-----------------------------------------------------------------------------
// Purpose: Sys_FindFirst with a path ID filter.
//-----------------------------------------------------------------------------
const char *Sys_FindFirstEx( const char *pWildcard, const char *pPathID, char *basename, int namelength )
{
if (g_hfind != FILESYSTEM_INVALID_FIND_HANDLE)
{
Sys_Error ("Sys_FindFirst without close");
g_pFileSystem->FindClose(g_hfind);
}
const char* psz = g_pFileSystem->FindFirstEx( pWildcard, pPathID, &g_hfind);
if (basename && psz)
{
Q_FileBase(psz, basename, namelength );
}
return psz;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *basename -
// Output : char *Sys_FindNext
//-----------------------------------------------------------------------------
const char* Sys_FindNext(char *basename, int namelength)
{
const char *psz = g_pFileSystem->FindNext(g_hfind);
if ( basename && psz )
{
Q_FileBase(psz, basename, namelength );
}
return psz;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : void Sys_FindClose
//-----------------------------------------------------------------------------
void Sys_FindClose(void)
{
if ( FILESYSTEM_INVALID_FIND_HANDLE != g_hfind )
{
g_pFileSystem->FindClose(g_hfind);
g_hfind = FILESYSTEM_INVALID_FIND_HANDLE;
}
}
//-----------------------------------------------------------------------------
// Purpose: OS Specific initializations
//-----------------------------------------------------------------------------
void Sys_Init( void )
{
// Set default FPU control word to truncate (chop) mode for optimized _ftol()
// This does not "stick", the mode is restored somewhere down the line.
// Sys_TruncateFPU();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Sys_Shutdown( void )
{
}
//-----------------------------------------------------------------------------
// Purpose: Print to system console
// Input : *fmt -
// ... -
// Output : void Sys_Printf
//-----------------------------------------------------------------------------
void Sys_Printf(char *fmt, ...)
{
va_list argptr;
char text[1024];
va_start (argptr,fmt);
Q_vsnprintf (text, sizeof( text ), fmt, argptr);
va_end (argptr);
if ( developer.GetInt() )
{
#ifdef _WIN32
wchar_t unicode[2048];
::MultiByteToWideChar(CP_UTF8, 0, text, -1, unicode, sizeof( unicode ) / sizeof(wchar_t));
unicode[(sizeof( unicode ) / sizeof(wchar_t)) - 1] = L'\0';
OutputDebugStringW( unicode );
Sleep( 0 );
#else
fprintf( stderr, "%s", text );
#endif
}
if ( s_bIsDedicated )
{
printf( "%s", text );
}
}
bool Sys_MessageBox(const char *title, const char *info, bool bShowOkAndCancel)
{
#ifdef _WIN32
if ( IDOK == ::MessageBox( NULL, title, info, MB_ICONEXCLAMATION | ( bShowOkAndCancel ? MB_OKCANCEL : MB_OK ) ) )
{
return true;
}
return false;
#elif defined( USE_SDL )
int buttonid = 0;
SDL_MessageBoxData messageboxdata = { 0 };
SDL_MessageBoxButtonData buttondata[] =
{
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "OK" },
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 0, "Cancel" },
};
messageboxdata.window = GetAssertDialogParent();
messageboxdata.title = title;
messageboxdata.message = info;
messageboxdata.numbuttons = bShowOkAndCancel ? 2 : 1;
messageboxdata.buttons = buttondata;
SDL_ShowMessageBox( &messageboxdata, &buttonid );
return ( buttonid == 1 );
#elif defined( POSIX )
Warning( "%s\n", info );
return true;
#else
#error "implement me"
#endif
}
bool g_bUpdateMinidumpComment = true;
void BuildMinidumpComment( char const *pchSysErrorText, bool bRealCrash );
void Sys_Error_Internal( bool bMinidump, const char *error, va_list argsList )
{
char text[1024];
static bool bReentry = false; // Don't meltdown
Q_vsnprintf( text, sizeof( text ), error, argsList );
if ( bReentry )
{
fprintf( stderr, "%s\n", text );
return;
}
bReentry = true;
if ( s_bIsDedicated )
{
printf( "%s\n", text );
}
else
{
Sys_Printf( "%s\n", text );
}
// Write the error to the log and ensure the log contents get written to disk
g_Log.Printf( "Engine error: %s\n", text );
g_Log.Flush();
g_bInErrorExit = true;
#if !defined( SWDS )
if ( IsPC() && videomode )
videomode->Shutdown();
#endif
if ( IsPC() &&
!CommandLine()->FindParm( "-makereslists" ) &&
!CommandLine()->FindParm( "-nomessagebox" ) &&
!CommandLine()->FindParm( "-nocrashdialog" ) )
{
#ifdef _WIN32
::MessageBox( NULL, text, "Engine Error", MB_OK | MB_TOPMOST );
#elif defined( USE_SDL )
Sys_MessageBox( "Engine Error", text, false );
#endif
}
if ( IsPC() )
{
DebuggerBreakIfDebugging();
}
else if ( !IsRetail() )
{
DebuggerBreak();
}
#if !defined( _X360 )
BuildMinidumpComment( text, true );
g_bUpdateMinidumpComment = false;
if ( bMinidump && !Plat_IsInDebugSession() && !CommandLine()->FindParm( "-nominidumps") )
{
#if defined( WIN32 )
// MiniDumpWrite() has problems capturing the calling thread's context
// unless it is called with an exception context. So fake an exception.
__try
{
RaiseException
(
0, // dwExceptionCode
EXCEPTION_NONCONTINUABLE, // dwExceptionFlags
0, // nNumberOfArguments,
NULL // const ULONG_PTR* lpArguments
);
// Never get here (non-continuable exception)
}
// Write the minidump from inside the filter (GetExceptionInformation() is only
// valid in the filter)
__except ( SteamAPI_WriteMiniDump( 0, GetExceptionInformation(), build_number() ), EXCEPTION_EXECUTE_HANDLER )
{
// We always get here because the above filter evaluates to EXCEPTION_EXECUTE_HANDLER
}
#elif defined(POSIX)
// Doing this doesn't quite work the way we want because there is no "crashing" thread
// and we see "No thread was identified as the cause of the crash; No signature could be created because we do not know which thread crashed" on the back end
//SteamAPI_WriteMiniDump( 0, NULL, build_number() );
printf("\n ##### Sys_Error: %s", text );
fflush(stdout );
raise(SIGTRAP);
#else
#warning "need minidump impl on sys_error"
#endif
}
#endif // _X360
host_initialized = false;
#if defined(_WIN32) && !defined( _X360 )
// We don't want global destructors in our process OR in any DLL to get executed.
// _exit() avoids calling global destructors in our module, but not in other DLLs.
TerminateProcess( GetCurrentProcess(), 100 );
#else
_exit( 100 );
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Exit engine with error
// Input : *error -
// ... -
// Output : void Sys_Error
//-----------------------------------------------------------------------------
void Sys_Error(const char *error, ...)
{
va_list argptr;
va_start( argptr, error );
Sys_Error_Internal( true, error, argptr );
va_end( argptr );
}
//-----------------------------------------------------------------------------
// Purpose: Exit engine with error
// Input : *error -
// ... -
// Output : void Sys_Error
//-----------------------------------------------------------------------------
void Sys_Exit(const char *error, ...)
{
va_list argptr;
va_start( argptr, error );
Sys_Error_Internal( false, error, argptr );
va_end( argptr );
}
bool IsInErrorExit()
{
return g_bInErrorExit;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : msec -
// Output : void Sys_Sleep
//-----------------------------------------------------------------------------
void Sys_Sleep( int msec )
{
#ifdef _WIN32
Sleep ( msec );
#elif POSIX
usleep( msec * 1000 );
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : hInst -
// ulInit -
// lpReserved -
// Output : BOOL WINAPI DllMain
//-----------------------------------------------------------------------------
#if defined(_WIN32) && !defined( _X360 )
BOOL WINAPI DllMain(HANDLE hInst, ULONG ulInit, LPVOID lpReserved)
{
InitCRTMemDebug();
if (ulInit == DLL_PROCESS_ATTACH)
{
}
else if (ulInit == DLL_PROCESS_DETACH)
{
}
return TRUE;
}
#endif
//-----------------------------------------------------------------------------
// Purpose: Allocate memory for engine hunk
// Input : *parms -
//-----------------------------------------------------------------------------
void Sys_InitMemory( void )
{
// Allow overrides
if ( CommandLine()->FindParm( "-minmemory" ) )
{
host_parms.memsize = MINIMUM_WIN_MEMORY;
return;
}
host_parms.memsize = 0;
#ifdef _WIN32
#if (_MSC_VER > 1200)
// MSVC 6.0 doesn't support GlobalMemoryStatusEx()
if ( IsPC() )
{
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if ( GetVersionEx ((OSVERSIONINFO *)&osvi) )
{
if ( osvi.dwPlatformId >= VER_PLATFORM_WIN32_NT && osvi.dwMajorVersion >= 5 )
{
MEMORYSTATUSEX memStat;
ZeroMemory(&memStat, sizeof(MEMORYSTATUSEX));
memStat.dwLength = sizeof(MEMORYSTATUSEX);
if ( GlobalMemoryStatusEx( &memStat ) )
{
if ( memStat.ullTotalPhys > 0xFFFFFFFFUL )
{
host_parms.memsize = 0xFFFFFFFFUL;
}
else
{
host_parms.memsize = memStat.ullTotalPhys;
}
}
}
}
}
#endif // (_MSC_VER > 1200)
if ( !IsX360() )
{
if ( host_parms.memsize == 0 )
{
MEMORYSTATUS lpBuffer;
// Get OS Memory status
lpBuffer.dwLength = sizeof(MEMORYSTATUS);
GlobalMemoryStatus( &lpBuffer );
if ( lpBuffer.dwTotalPhys <= 0 )
{
host_parms.memsize = MAXIMUM_WIN_MEMORY;
}
else
{
host_parms.memsize = lpBuffer.dwTotalPhys;
}
}
if ( host_parms.memsize < ONE_HUNDRED_TWENTY_EIGHT_MB )
{
Sys_Error( "Available memory less than 128MB!!! %i\n", host_parms.memsize );
}
// take one quarter the physical memory
if ( host_parms.memsize <= 512*1024*1024)
{
host_parms.memsize >>= 2;
// Apply cap of 64MB for 512MB systems
// this keeps the code the same as HL2 gold
// but allows us to use more memory on 1GB+ systems
if (host_parms.memsize > MAXIMUM_DEDICATED_MEMORY)
{
host_parms.memsize = MAXIMUM_DEDICATED_MEMORY;
}
}
else
{
// just take one quarter, no cap
host_parms.memsize >>= 2;
}
// At least MINIMUM_WIN_MEMORY mb, even if we have to swap a lot.
if (host_parms.memsize < MINIMUM_WIN_MEMORY)
{
host_parms.memsize = MINIMUM_WIN_MEMORY;
}
// Apply cap
if (host_parms.memsize > MAXIMUM_WIN_MEMORY)
{
host_parms.memsize = MAXIMUM_WIN_MEMORY;
}
}
else
{
host_parms.memsize = 128*1024*1024;
}
#elif defined(POSIX)
uint64_t memsize = ONE_HUNDRED_TWENTY_EIGHT_MB;
#if defined(OSX) || defined(PLATFORM_BSD)
int mib[2] = { CTL_HW, HW_MEMSIZE };
u_int namelen = sizeof(mib) / sizeof(mib[0]);
size_t len = sizeof(memsize);
if (sysctl(mib, namelen, &memsize, &len, NULL, 0) < 0)
{
memsize = ONE_HUNDRED_TWENTY_EIGHT_MB;
}
#elif defined(LINUX)
const int fd = open("/proc/meminfo", O_RDONLY);
if (fd < 0)
{
Sys_Error( "Can't open /proc/meminfo (%s)!\n", strerror(errno) );
}
char buf[1024 * 16];
const ssize_t br = read(fd, buf, sizeof (buf));
close(fd);
if (br < 0)
{
Sys_Error( "Can't read /proc/meminfo (%s)!\n", strerror(errno) );
}
buf[br] = '\0';
// Split up the buffer by lines...
char *line = buf;
for (char *ptr = buf; *ptr; ptr++)
{
if (*ptr == '\n')
{
// we've got a complete line.
*ptr = '\0';
unsigned long long ull = 0;
if (sscanf(line, "MemTotal: %llu kB", &ull) == 1)
{
// found it!
memsize = ((uint64_t) ull) * 1024;
break;
}
line = ptr;
}
}
#else
#error Write me.
#endif
if ( memsize > 0xFFFFFFFFUL )
{
host_parms.memsize = 0xFFFFFFFFUL;
}
else
{
host_parms.memsize = memsize;
}
if ( host_parms.memsize < ONE_HUNDRED_TWENTY_EIGHT_MB )
{
Sys_Error( "Available memory less than 128MB!!! %i\n", host_parms.memsize );
}
// take one quarter the physical memory
if ( host_parms.memsize <= 512*1024*1024)
{
host_parms.memsize >>= 2;
// Apply cap of 64MB for 512MB systems
// this keeps the code the same as HL2 gold
// but allows us to use more memory on 1GB+ systems
if (host_parms.memsize > MAXIMUM_DEDICATED_MEMORY)
{
host_parms.memsize = MAXIMUM_DEDICATED_MEMORY;
}
}
else
{
// just take one quarter, no cap
host_parms.memsize >>= 2;
}
// At least MINIMUM_WIN_MEMORY mb, even if we have to swap a lot.
if (host_parms.memsize < MINIMUM_WIN_MEMORY)
{
host_parms.memsize = MINIMUM_WIN_MEMORY;
}
// Apply cap
if (host_parms.memsize > MAXIMUM_WIN_MEMORY)
{
host_parms.memsize = MAXIMUM_WIN_MEMORY;
}
#else
#error Write me.
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *parms -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
void Sys_ShutdownMemory( void )
{
host_parms.memsize = 0;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Sys_InitAuthentication( void )
{
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Sys_ShutdownAuthentication( void )
{
}
//-----------------------------------------------------------------------------
// Debug library spew output
//-----------------------------------------------------------------------------
CTHREADLOCALINT g_bInSpew;
#include "tier1/fmtstr.h"
static ConVar sys_minidumpspewlines( "sys_minidumpspewlines", "500", 0, "Lines of crash dump console spew to keep." );
static CUtlLinkedList< CUtlString > g_SpewHistory;
static int g_nSpewLines = 1;
static CThreadFastMutex g_SpewMutex;
static void AddSpewRecord( char const *pMsg )
{
#if !defined( _X360 )
AUTO_LOCK( g_SpewMutex );
static bool s_bReentrancyGuard = false;
if ( s_bReentrancyGuard )
return;
s_bReentrancyGuard = true;
if ( g_SpewHistory.Count() > sys_minidumpspewlines.GetInt() )
{
g_SpewHistory.Remove( g_SpewHistory.Head() );
}
int i = g_SpewHistory.AddToTail();
g_SpewHistory[ i ].Format( "%d(%f): %s", g_nSpewLines++, Plat_FloatTime(), pMsg );
s_bReentrancyGuard = false;
#endif
}
void GetSpew( char *buf, size_t buflen )
{
AUTO_LOCK( g_SpewMutex );
// Walk list backward
char *pcur = buf;
int remainder = (int)buflen - 1;
// Walk backward(
for ( int i = g_SpewHistory.Tail(); i != g_SpewHistory.InvalidIndex(); i = g_SpewHistory.Previous( i ) )
{
const CUtlString &rec = g_SpewHistory[ i ];
int len = rec.Length();
int tocopy = MIN( len, remainder );
if ( tocopy <= 0 )
break;
Q_memcpy( pcur, rec.String(), tocopy );
remainder -= tocopy;
pcur += tocopy;
if ( remainder <= 0 )
break;
}
*pcur = 0;
}
ConVar spew_consolelog_to_debugstring( "spew_consolelog_to_debugstring", "0", 0, "Send console log to PLAT_DebugString()" );
SpewRetval_t Sys_SpewFunc( SpewType_t spewType, const char *pMsg )
{
bool suppress = g_bInSpew;
g_bInSpew = true;
AddSpewRecord( pMsg );
// Text output shows up on dedicated server profiles, both as consuming CPU
// time and causing IPC delays. Sending the messages to ETW will help us
// understand why, and save us time when server operators are triggering
// excessive spew. Having the output in traces is also generically useful
// for understanding slowdowns.
ETWMark1I( pMsg, spewType );
if ( !suppress )
{
// If this is a dedicated server, then we have taken over its spew function, but we still
// want its vgui console to show the spew, so pass it into the dedicated server.
if ( dedicated )
dedicated->Sys_Printf( (char*)pMsg );
if( spew_consolelog_to_debugstring.GetBool() )
{
Plat_DebugString( pMsg );
}
if ( g_bTextMode )
{
printf( "%s", pMsg );
}
if ((spewType != SPEW_LOG) || (sv.GetMaxClients() == 1))
{
Color color;
switch ( spewType )
{
#ifndef SWDS
case SPEW_WARNING:
{
color.SetColor( 255, 90, 90, 255 );
}
break;
case SPEW_ASSERT:
{
color.SetColor( 255, 20, 20, 255 );
}
break;
case SPEW_ERROR:
{
color.SetColor( 20, 70, 255, 255 );
}
break;
#endif
default:
{
color = *GetSpewOutputColor();
}
break;
}
Con_ColorPrintf( color, "%s", pMsg );
}
else
{
g_Log.Printf( "%s", pMsg );
}
}
g_bInSpew = false;
if (spewType == SPEW_ERROR)
{
Sys_Error( "%s", pMsg );
return SPEW_ABORT;
}
if (spewType == SPEW_ASSERT)
{
if ( CommandLine()->FindParm( "-noassert" ) == 0 )
return SPEW_DEBUGGER;
else
return SPEW_CONTINUE;
}
return SPEW_CONTINUE;
}
void DeveloperChangeCallback( IConVar *pConVar, const char *pOldString, float flOldValue )
{
// Set the "developer" spew group to the value...
ConVarRef var( pConVar );
int val = var.GetInt();
SpewActivate( "developer", val );
// Activate console spew (spew value 2 == developer console spew)
SpewActivate( "console", val ? 2 : 1 );
}
//-----------------------------------------------------------------------------
// Purpose: factory comglomerator, gets the client, server, and gameui dlls together
//-----------------------------------------------------------------------------
void *GameFactory( const char *pName, int *pReturnCode )
{
void *pRetVal = NULL;
// first ask the app factory
pRetVal = g_AppSystemFactory( pName, pReturnCode );
if (pRetVal)
return pRetVal;
#ifndef SWDS
// now ask the client dll
if (ClientDLL_GetFactory())
{
pRetVal = ClientDLL_GetFactory()( pName, pReturnCode );
if (pRetVal)
return pRetVal;
}
// gameui.dll
if (EngineVGui()->GetGameUIFactory())
{
pRetVal = EngineVGui()->GetGameUIFactory()( pName, pReturnCode );
if (pRetVal)
return pRetVal;
}
#endif
// server dll factory access would go here when needed
return NULL;
}
// factory instance
CreateInterfaceFn g_GameSystemFactory = GameFactory;
//-----------------------------------------------------------------------------
// Purpose:
// Input : *lpOrgCmdLine -
// launcherFactory -
// *pwnd -
// bIsDedicated -
// Output : int