forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys_ded.cpp
More file actions
530 lines (447 loc) · 12.7 KB
/
sys_ded.cpp
File metadata and controls
530 lines (447 loc) · 12.7 KB
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//===========================================================================//
#include <stdio.h>
#include <stdlib.h>
#include "isys.h"
#include "console/conproc.h"
#include "dedicated.h"
#include "engine_hlds_api.h"
#include "checksum_md5.h"
#include "mathlib/mathlib.h"
#include "tier0/vcrmode.h"
#include "tier0/dbg.h"
#include "tier1/strtools.h"
#include "tier0/icommandline.h"
#include "idedicatedexports.h"
#include "vgui/vguihelpers.h"
#include "appframework/AppFramework.h"
#include "filesystem_init.h"
#include "tier2/tier2.h"
#include "dedicated.h"
#include "vstdlib/cvar.h"
#include "inputsystem/iinputsystem.h"
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#include "KeyValues.h"
#else
#define _chdir chdir
#include <unistd.h>
#endif
void* FileSystemFactory( const char *pName, int *pReturnCode );
bool InitInstance( );
int ProcessConsoleInput( void );
bool NET_Init( void );
void NET_Shutdown( void );
const char *UTIL_GetBaseDir( void );
#ifdef _WIN32
bool g_bVGui = false;
#endif
#if defined ( _WIN32 )
#include "console/TextConsoleWin32.h"
CTextConsoleWin32 console;
#else
#include "console/TextConsoleUnix.h"
CTextConsoleUnix console;
#endif
#ifdef _WIN32
extern char *gpszCvars;
#endif
IDedicatedServerAPI *engine = NULL;
//-----------------------------------------------------------------------------
// Implementation of IVCRHelpers.
//-----------------------------------------------------------------------------
class CVCRHelpers : public IVCRHelpers
{
public:
virtual void ErrorMessage( const char *pMsg )
{
printf( "ERROR: %s\n", pMsg );
}
virtual void* GetMainWindow()
{
return 0;
}
};
CVCRHelpers g_VCRHelpers;
SpewRetval_t DedicatedSpewOutputFunc( SpewType_t spewType, char const *pMsg ); // in sys_common.cpp
//-----------------------------------------------------------------------------
// Run a single VGUI frame. if bFinished is true, run VGUIFinishedConfig() first.
//-----------------------------------------------------------------------------
static bool DoRunVGUIFrame( bool bFinished = false )
{
#ifdef _WIN32
if ( g_bVGui )
{
if ( bFinished )
VGUIFinishedConfig();
RunVGUIFrame();
return true;
}
#endif
return false;
}
//-----------------------------------------------------------------------------
// Handle the VCRHook PeekMessage loop.
// Return true if WM_QUIT received.
//-----------------------------------------------------------------------------
static bool HandleVCRHook()
{
#if defined ( _WIN32 )
MSG msg;
bool bDone = false;
while( VCRHook_PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
//if (!GetMessage( &msg, NULL, 0, 0))
if ( msg.message == WM_QUIT )
{
bDone = true;
break;
}
TranslateMessage( &msg );
DispatchMessage( &msg );
}
if ( IsPC() )
{
// NOTE: Under some implementations of Win9x,
// dispatching messages can cause the FPU control word to change
SetupFPUControlWord();
}
if ( bDone /*|| gbAppHasBeenTerminated*/ )
return true;
#endif // _WIN32
return false;
}
//-----------------------------------------------------------------------------
//
// Server loop
//
//-----------------------------------------------------------------------------
void RunServer( void )
{
#ifdef _WIN32
if(gpszCvars)
{
engine->AddConsoleText(gpszCvars);
}
#endif
// Run 2 engine frames first to get the engine to load its resources.
for ( int i = 0; i < 2; i++ )
{
DoRunVGUIFrame();
if ( !engine->RunFrame() )
return;
}
// Run final VGUI frame.
DoRunVGUIFrame( true );
int bDone = false;
while ( !bDone )
{
// Check on VCRHook_PeekMessage...
if ( HandleVCRHook() )
break;
if ( !DoRunVGUIFrame() )
ProcessConsoleInput();
if ( !engine->RunFrame() )
bDone = true;
sys->UpdateStatus( 0 /* don't force */ );
}
}
//-----------------------------------------------------------------------------
//
// initialize the console or wait for vgui to start the server
//
//-----------------------------------------------------------------------------
static bool ConsoleStartup( )
{
#ifdef _WIN32
if ( g_bVGui )
{
RunVGUIFrame();
// Run the config screen
while (VGUIIsInConfig() && VGUIIsRunning())
RunVGUIFrame();
if ( VGUIIsStopping() )
return false;
return true;
}
else
{
if ( !console.Init() )
{
return false;
}
}
#endif // _WIN32
return true;
}
//-----------------------------------------------------------------------------
// Instantiate all main libraries
//-----------------------------------------------------------------------------
bool CDedicatedAppSystemGroup::Create( )
{
#ifndef _WIN32
if ( !console.Init() )
return false;
#endif
// Hook the debug output stuff (override the spew func in the appframework)
SpewOutputFunc( DedicatedSpewOutputFunc );
// Added the dedicated exports module for the engine to grab
AppModule_t dedicatedModule = LoadModule( Sys_GetFactoryThis() );
IAppSystem *pSystem = AddSystem( dedicatedModule, VENGINE_DEDICATEDEXPORTS_API_VERSION );
if ( !pSystem )
return false;
if ( sys->LoadModules( this ) )
{
// Find the input system and tell it to skip Steam Controller initialization (we have to set this flag before Init gets called on the
// input system). Dedicated server should skip controller initialization to avoid initializing Steam, because we don't want the user to be
// flagged as "playing" the game.
auto inputsystem = ( IInputSystem* )FindSystem( INPUTSYSTEM_INTERFACE_VERSION );
if ( inputsystem )
{
inputsystem->SetSkipControllerInitialization( true );
}
return true;
}
else
{
return false;
}
}
bool CDedicatedAppSystemGroup::PreInit( )
{
// A little hack needed because dedicated links directly to filesystem .cpp files
g_pFullFileSystem = NULL;
if ( !BaseClass::PreInit() )
return false;
CFSSteamSetupInfo steamInfo;
steamInfo.m_pDirectoryName = NULL;
steamInfo.m_bOnlyUseDirectoryName = false;
steamInfo.m_bToolsMode = false;
steamInfo.m_bSetSteamDLLPath = false;
steamInfo.m_bSteam = g_pFullFileSystem->IsSteam();
steamInfo.m_bNoGameInfo = steamInfo.m_bSteam;
if ( FileSystem_SetupSteamEnvironment( steamInfo ) != FS_OK )
return false;
CFSMountContentInfo fsInfo;
fsInfo.m_pFileSystem = g_pFullFileSystem;
fsInfo.m_bToolsMode = false;
fsInfo.m_pDirectoryName = steamInfo.m_GameInfoPath;
if ( FileSystem_MountContent( fsInfo ) != FS_OK )
return false;
if ( !NET_Init() )
return false;
#ifdef _WIN32
g_bVGui = CommandLine()->CheckParm( "-vgui" );
#endif
CreateInterfaceFn factory = GetFactory();
IInputSystem *inputsystem = (IInputSystem *)factory( INPUTSYSTEM_INTERFACE_VERSION, NULL );
if ( inputsystem )
{
inputsystem->SetConsoleTextMode( true );
}
#ifdef _WIN32
if ( g_bVGui )
{
StartVGUI( GetFactory() );
}
else
#endif
{
if ( !sys->CreateConsoleWindow() )
return false;
}
return true;
}
int CDedicatedAppSystemGroup::Main( )
{
if ( !ConsoleStartup() )
return -1;
#ifdef _WIN32
if ( g_bVGui )
RunVGUIFrame();
#endif
// Set up mod information
ModInfo_t info;
info.m_pInstance = GetAppInstance();
info.m_pBaseDirectory = UTIL_GetBaseDir();
info.m_pInitialMod = CommandLine()->ParmValue( "-game", "hl2" );
info.m_pInitialGame = CommandLine()->ParmValue( "-defaultgamedir", "hl2" );
info.m_pParentAppSystemGroup = this;
info.m_bTextMode = CommandLine()->CheckParm( "-textmode" );
if ( engine->ModInit( info ) )
{
engine->ModShutdown();
} // if engine->ModInit
return 0;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CDedicatedAppSystemGroup::PostShutdown()
{
#ifdef _WIN32
if ( g_bVGui )
StopVGUI();
#endif
sys->DestroyConsoleWindow();
console.ShutDown();
NET_Shutdown();
BaseClass::PostShutdown();
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CDedicatedAppSystemGroup::Destroy()
{
}
//-----------------------------------------------------------------------------
// Gets the executable name
//-----------------------------------------------------------------------------
bool GetExecutableName( char *out, int nMaxLen )
{
#ifdef _WIN32
if ( !::GetModuleFileName( ( HINSTANCE )GetModuleHandle( NULL ), out, nMaxLen ) )
return false;
return true;
#elif POSIX
Q_strncpy( out, g_szEXEName, nMaxLen );
return true;
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Return the directory where this .exe is running from
// Output : char
//-----------------------------------------------------------------------------
void UTIL_ComputeBaseDir( char *pBaseDir, int nMaxLen )
{
int j;
char *pBuffer = NULL;
pBaseDir[ 0 ] = 0;
if ( GetExecutableName( pBaseDir, nMaxLen ) )
{
pBuffer = strrchr( pBaseDir, CORRECT_PATH_SEPARATOR );
if ( pBuffer && *pBuffer )
{
*(pBuffer+1) = '\0';
}
j = strlen( pBaseDir );
if (j > 0)
{
if ( ( pBaseDir[ j-1 ] == '\\' ) ||
( pBaseDir[ j-1 ] == '/' ) )
{
pBaseDir[ j-1 ] = 0;
}
}
}
char const *pOverrideDir = CommandLine()->CheckParm( "-basedir" );
if ( pOverrideDir )
{
strcpy( pBaseDir, pOverrideDir );
}
Q_strlower( pBaseDir );
Q_FixSlashes( pBaseDir );
}
//-----------------------------------------------------------------------------
// This class is a helper class used for steam-based applications.
// It loads up the file system in preparation for using it to load other
// required modules from steam.
//
// I couldn't use the one in appframework because the dedicated server
// inlines all the filesystem code.
//-----------------------------------------------------------------------------
class CDedicatedSteamApplication : public CSteamApplication
{
public:
CDedicatedSteamApplication( CSteamAppSystemGroup *pAppSystemGroup );
virtual bool Create( );
};
//-----------------------------------------------------------------------------
// This class is a helper class used for steam-based applications.
// It loads up the file system in preparation for using it to load other
// required modules from steam.
//
// I couldn't use the one in appframework because the dedicated server
// inlines all the filesystem code.
//-----------------------------------------------------------------------------
CDedicatedSteamApplication::CDedicatedSteamApplication( CSteamAppSystemGroup *pAppSystemGroup ) : CSteamApplication( pAppSystemGroup )
{
}
//-----------------------------------------------------------------------------
// Implementation of IAppSystemGroup
//-----------------------------------------------------------------------------
bool CDedicatedSteamApplication::Create( )
{
// Add in the cvar factory
AppModule_t cvarModule = LoadModule( VStdLib_GetICVarFactory() );
AddSystem( cvarModule, CVAR_INTERFACE_VERSION );
AppModule_t fileSystemModule = LoadModule( FileSystemFactory );
m_pFileSystem = (IFileSystem*)AddSystem( fileSystemModule, FILESYSTEM_INTERFACE_VERSION );
if ( !m_pFileSystem )
{
Warning( "Unable to load the file system!\n" );
return false;
}
return true;
}
//-----------------------------------------------------------------------------
//
// Main entry point for dedicated server, shared between win32 and linux
//
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
#if !defined( POSIX ) && !defined( PLATFORM_64BITS )
_asm
{
fninit
}
#endif
SetupFPUControlWord();
#ifdef POSIX
Q_strncpy( g_szEXEName, *argv, ARRAYSIZE( g_szEXEName ) );
// Store off command line for argument searching
BuildCmdLine( argc, argv );
#endif
MathLib_Init( 2.2f, 2.2f, 0.0f, 1.0f );
// Store off command line for argument searching
CommandLine()->CreateCmdLine( VCRHook_GetCommandLine() );
#ifndef _WIN32
Plat_SetCommandLine( CommandLine()->GetCmdLine() );
#endif
// Start VCR mode?
const char *filename;
if( CommandLine()->CheckParm( "-vcrrecord", &filename ) )
{
if ( !VCRStart( filename, true, &g_VCRHelpers ) )
{
Error( "-vcrrecord: can't open '%s' for writing.\n", filename );
return -1;
}
}
else if( CommandLine()->CheckParm( "-vcrplayback", &filename ) )
{
if ( !VCRStart( filename, false, &g_VCRHelpers ) )
{
Error( "-vcrplayback: can't open '%s' for reading.\n", filename );
return -1;
}
}
// Figure out the directory the executable is running from
// and make that be the current working directory
char pBasedir[ MAX_PATH ];
UTIL_ComputeBaseDir( pBasedir, MAX_PATH );
_chdir( pBasedir );
// Rehook the command line through VCR mode.
CommandLine()->CreateCmdLine( VCRHook_GetCommandLine() );
if ( !InitInstance() )
return -1;
CDedicatedAppSystemGroup dedicatedSystems;
CDedicatedSteamApplication steamApplication( &dedicatedSystems );
return steamApplication.Run( );
}