forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsys_windows.cpp
More file actions
411 lines (350 loc) · 10.9 KB
/
sys_windows.cpp
File metadata and controls
411 lines (350 loc) · 10.9 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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <eh.h>
#include "isys.h"
#include "console/conproc.h"
#include "dedicated.h"
#include "engine_hlds_api.h"
#include "checksum_md5.h"
#include "tier0/vcrmode.h"
#include "tier0/dbg.h"
#include "tier1/strtools.h"
#include "tier0/icommandline.h"
#include "inputsystem/iinputsystem.h"
#include "SteamAppStartup.h"
#include "console/textconsole.h"
#include "vgui/vguihelpers.h"
#include "appframework/appframework.h"
#include "materialsystem/imaterialsystem.h"
#include "istudiorender.h"
#include "vgui/ivgui.h"
#include "console/TextConsoleWin32.h"
#include "icvar.h"
#include "datacache/idatacache.h"
#include "datacache/imdlcache.h"
#include "vphysics_interface.h"
#include "filesystem.h"
#include "steam/steam_api.h"
#include "filesystem/IQueuedLoader.h"
extern CTextConsoleWin32 console;
extern bool g_bVGui;
//-----------------------------------------------------------------------------
// Purpose: Implements OS Specific layer ( loosely )
//-----------------------------------------------------------------------------
class CSys : public ISys
{
public:
virtual ~CSys( void );
virtual bool LoadModules( CDedicatedAppSystemGroup *pAppSystemGroup );
void Sleep( int msec );
bool GetExecutableName( char *out );
void ErrorMessage( int level, const char *msg );
void WriteStatusText( char *szText );
void UpdateStatus( int force );
long LoadLibrary( char *lib );
void FreeLibrary( long library );
bool CreateConsoleWindow( void );
void DestroyConsoleWindow( void );
void ConsoleOutput ( char *string );
char *ConsoleInput ( int index, char *buf, int buflen );
void Printf( PRINTF_FORMAT_STRING const char *fmt, ... );
};
static CSys g_Sys;
ISys *sys = &g_Sys;
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CSys::~CSys()
{
sys = NULL;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : msec -
//-----------------------------------------------------------------------------
void CSys::Sleep( int msec )
{
// Call ThreadSleep because it has the necessary magic to set the system
// timer resolution so that Sleep( 1 ) will sleep for one millisecond
// instead of for 10-16 ms.
ThreadSleep( msec );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *lib -
// Output : long
//-----------------------------------------------------------------------------
long CSys::LoadLibrary( char *lib )
{
void *hDll = ::LoadLibrary( lib );
return (long)hDll;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : library -
//-----------------------------------------------------------------------------
void CSys::FreeLibrary( long library )
{
if ( !library )
return;
::FreeLibrary( (HMODULE)library );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *out -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CSys::GetExecutableName( char *out )
{
if ( !::GetModuleFileName( ( HINSTANCE )GetModuleHandle( NULL ), out, 256 ) )
{
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : level -
// *msg -
//-----------------------------------------------------------------------------
void CSys::ErrorMessage( int level, const char *msg )
{
MessageBox( NULL, msg, "Half-Life", MB_OK );
PostQuitMessage(0);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : force -
//-----------------------------------------------------------------------------
void CSys::UpdateStatus( int force )
{
static double tLast = 0.0;
double tCurrent;
char szPrompt[256];
int n, nMax;
char szMap[64];
char szHostname[128];
float fps;
if ( !engine )
return;
tCurrent = Sys_FloatTime();
if ( !force )
{
if ( ( tCurrent - tLast ) < 0.5f )
return;
}
tLast = tCurrent;
engine->UpdateStatus( &fps, &n, &nMax, szMap, sizeof( szMap ) );
engine->UpdateHostname( szHostname, sizeof( szHostname ) );
console.SetTitle( szHostname );
Q_snprintf( szPrompt, sizeof( szPrompt ), "%.1f fps %2i/%2i on map %16s", (float)fps, n, nMax, szMap);
console.SetStatusLine(szPrompt);
console.UpdateStatus();
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *string -
// Output : void CSys::ConsoleOutput
//-----------------------------------------------------------------------------
void CSys::ConsoleOutput (char *string)
{
if ( g_bVGui )
{
VGUIPrintf( string );
}
else
{
console.Print(string);
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *fmt -
// ... -
//-----------------------------------------------------------------------------
void CSys::Printf( PRINTF_FORMAT_STRING const char *fmt, ... )
{
// Dump text to debugging console.
va_list argptr;
char szText[1024];
va_start (argptr, fmt);
Q_vsnprintf (szText, sizeof( szText ), fmt, argptr);
va_end (argptr);
// Get Current text and append it.
ConsoleOutput( szText );
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : char *
//-----------------------------------------------------------------------------
char *CSys::ConsoleInput ( int index, char *buf, int buflen )
{
return console.GetLine( index, buf, buflen );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *szText -
//-----------------------------------------------------------------------------
void CSys::WriteStatusText( char *szText )
{
SetConsoleTitle( szText );
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CSys::CreateConsoleWindow( void )
{
if ( !AllocConsole () )
{
return false;
}
InitConProc();
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CSys::DestroyConsoleWindow( void )
{
FreeConsole ();
// shut down QHOST hooks if necessary
DeinitConProc ();
}
//-----------------------------------------------------------------------------
// Loading modules used by the dedicated server.
//-----------------------------------------------------------------------------
bool CSys::LoadModules( CDedicatedAppSystemGroup *pAppSystemGroup )
{
AppSystemInfo_t appSystems[] =
{
{ "engine.dll", CVAR_QUERY_INTERFACE_VERSION }, // NOTE: This one must be first!!
{ "inputsystem.dll", INPUTSYSTEM_INTERFACE_VERSION },
{ "materialsystem.dll", MATERIAL_SYSTEM_INTERFACE_VERSION },
{ "studiorender.dll", STUDIO_RENDER_INTERFACE_VERSION },
{ "vphysics.dll", VPHYSICS_INTERFACE_VERSION },
{ "datacache.dll", DATACACHE_INTERFACE_VERSION },
{ "datacache.dll", MDLCACHE_INTERFACE_VERSION },
{ "datacache.dll", STUDIO_DATA_CACHE_INTERFACE_VERSION },
{ "vgui2.dll", VGUI_IVGUI_INTERFACE_VERSION },
{ "engine.dll", VENGINE_HLDS_API_VERSION },
{ "dedicated.dll", QUEUEDLOADER_INTERFACE_VERSION },
{ "", "" } // Required to terminate the list
};
if ( !pAppSystemGroup->AddSystems( appSystems ) )
return false;
engine = (IDedicatedServerAPI *)pAppSystemGroup->FindSystem( VENGINE_HLDS_API_VERSION );
IMaterialSystem* pMaterialSystem = (IMaterialSystem*)pAppSystemGroup->FindSystem( MATERIAL_SYSTEM_INTERFACE_VERSION );
pMaterialSystem->SetShaderAPI( "shaderapiempty.dll" );
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool NET_Init( void )
{
// Startup winock
WORD version = MAKEWORD( 1, 1 );
WSADATA wsaData;
int err = WSAStartup( version, &wsaData );
if ( err != 0 )
{
char msg[ 256 ];
Q_snprintf( msg, sizeof( msg ), "Winsock 1.1 unavailable...\n" );
sys->Printf( "%s", msg );
Plat_DebugString( msg );
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void NET_Shutdown( void )
{
// Kill winsock
WSACleanup();
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : hInstance -
// hPrevInstance -
// lpszCmdLine -
// nCmdShow -
// Output : int PASCAL
//-----------------------------------------------------------------------------
int main(int argc, char **argv); // in sys_ded.cpp
static char *GetBaseDir( const char *pszBuffer )
{
static char basedir[ MAX_PATH ];
char szBuffer[ MAX_PATH ];
int j;
char *pBuffer = NULL;
V_strcpy_safe( szBuffer, pszBuffer );
pBuffer = strrchr( szBuffer,'\\' );
if ( pBuffer )
{
*(pBuffer+1) = '\0';
}
strcpy( basedir, szBuffer );
j = strlen( basedir );
if (j > 0)
{
if ( ( basedir[ j-1 ] == '\\' ) ||
( basedir[ j-1 ] == '/' ) )
{
basedir[ j-1 ] = 0;
}
}
return basedir;
}
void MiniDumpFunction( unsigned int nExceptionCode, EXCEPTION_POINTERS *pException )
{
SteamAPI_WriteMiniDump( nExceptionCode, pException, 0 );
}
extern "C" __declspec(dllexport) int DedicatedMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
SetAppInstance( hInstance );
// Check that we are running on Win32
OSVERSIONINFO vinfo;
vinfo.dwOSVersionInfoSize = sizeof(vinfo);
if ( !GetVersionEx ( &vinfo ) )
return -1;
if ( vinfo.dwPlatformId == VER_PLATFORM_WIN32s )
return -1;
int argc, iret = -1;
LPWSTR * argv= CommandLineToArgvW(GetCommandLineW(),&argc);
CommandLine()->CreateCmdLine( VCRHook_GetCommandLine() );
if ( !Plat_IsInDebugSession() && !CommandLine()->FindParm( "-nominidumps") )
{
// This warning only applies if you want to catch structured exceptions (crashes)
// using C++ exceptions. We do not want to do that so we can build with C++ exceptions
// completely disabled, and just suppress this warning.
// warning C4535: calling _set_se_translator() requires /EHa
#pragma warning( suppress : 4535 )
_set_se_translator( MiniDumpFunction );
try // this try block allows the SE translator to work
{
iret = main(argc,(char **)argv);
}
catch( ... )
{
return -1;
}
}
else
{
iret = main(argc,(char **)argv);
}
GlobalFree( argv );
return iret;
}