-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathlauncher.cpp
607 lines (482 loc) · 18.2 KB
/
launcher.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
#include <windows.h>
#include "HLSDK/common/interface.h"
#include "ICommandLine.h"
#include "IFileSystem.h"
#include "sys.h"
#include "hook.h"
#include <stdio.h>
#include <thread>
//DLL State Flags
#define DLL_INACTIVE 0 // no dll
#define DLL_ACTIVE 1 // dll is running
#define DLL_PAUSED 2 // dll is paused
#define DLL_CLOSE 3 // closing down dll
#define DLL_TRANS 4 // Level Transition
// DLL Pause reasons
#define DLL_NORMAL 0 // User hit Esc or something.
#define DLL_QUIT 4 // Quit now
#define DLL_RESTART 5 // Switch to launcher for linux, does a quit but returns 1
// DLL Substate info ( not relevant )
#define ENG_NORMAL (1<<0)
#define LAUNCHER_ERROR -1
#define LAUNCHER_OK 0
class IDedicatedServerAPI : public IBaseInterface
{
public:
virtual bool Init(const char* basedir, const char* cmdline, CreateInterfaceFn launcherFactory, CreateInterfaceFn filesystemFactory) = 0;
virtual int Shutdown() = 0;
virtual bool RunFrame() = 0;
virtual void AddConsoleText(char* text) = 0;
virtual void UpdateStatus(float* fps, int* nActive, int* nMaxPlayers, char* pszMap) = 0;
};
#define VENGINE_HLDS_API_VERSION "VENGINE_HLDS_API_VERSION002"
class IDedicatedExports : public IBaseInterface
{
public:
virtual ~IDedicatedExports() {};
virtual void Sys_Printf(const char* text) = 0;
};
#define VENGINE_DEDICATEDEXPORTS_API_VERSION "VENGINE_DEDICATEDEXPORTS_API_VERSION001"
class CDedicatedExports : public IDedicatedExports {
public:
void Sys_Printf(const char* text);
};
EXPOSE_SINGLE_INTERFACE(CDedicatedExports, IDedicatedExports, VENGINE_DEDICATEDEXPORTS_API_VERSION);
void CDedicatedExports::Sys_Printf(const char* text)
{
printf(text);
}
using SleepFunc = void (*)();
SleepFunc sleep_thread = nullptr;
char g_pLogFile[MAX_PATH];
int g_iPort = 27015;
int g_iPingBoost = 0;
bool g_bTerminated = false;
IFileSystem* g_pFileSystem;
IDedicatedServerAPI* engineAPI = NULL;
HANDLE hConsoleInput;
HANDLE hConsoleOutput;
int m_nConsoleTextLen;
int m_nCursorPosition;
char m_szConsoleText[256];
char m_szSavedConsoleText[256];
int m_nSavedConsoleTextLen;
char m_aszLineBuffer[10][256];
int m_nInputLine;
int m_nBrowseLine;
int m_nTotalLines;
#define DEFAULT_IP "127.0.0.1"
#define DEFAULT_LOBBYPORT "30002"
#define DEFAULT_PORT "27015"
#define DEFAULT_LOGFILE "csods"
HINTERFACEMODULE LoadFilesystemModule(void)
{
HINTERFACEMODULE hModule = Sys_LoadModule("filesystem_nar.dll");
if (!hModule)
{
MessageBox(NULL, "Could not load filesystem dll.\nFileSystem crashed during construction.", "Fatal Error", MB_ICONERROR);
return NULL;
}
return hModule;
}
BOOL WINAPI ConsoleCtrlHandler(DWORD CtrlType)
{
switch (CtrlType) {
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
g_bTerminated = true;
return TRUE;
default:
break;
}
return FALSE;
}
void UpdateStatus(int force)
{
static double tLast = 0.0;
char szStatus[256];
int n, nMax;
char szMap[32];
float fps;
if (!engineAPI)
return;
double tCurrent = timeGetTime() * 0.001;
engineAPI->UpdateStatus(&fps, &n, &nMax, szMap);
if (!force)
{
if ((tCurrent - tLast) < 0.5f)
return;
}
tLast = tCurrent;
snprintf(szStatus, sizeof(szStatus), "%s - %.1f fps %2i/%2i on %16s", g_pLogFile, fps, n, nMax, szMap);
SetConsoleTitle(szStatus);
}
void Console_Init()
{
hConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
memset(m_szConsoleText, 0, sizeof(m_szConsoleText));
m_nConsoleTextLen = 0;
m_nCursorPosition = 0;
memset(m_szSavedConsoleText, 0, sizeof(m_szSavedConsoleText));
m_nSavedConsoleTextLen = 0;
memset(m_aszLineBuffer, 0, sizeof(m_aszLineBuffer));
m_nTotalLines = 0;
m_nInputLine = 0;
m_nBrowseLine = 0;
}
void Console_PrintRaw(const char* pszMsg, int nChars)
{
char outputStr[2048];
WCHAR unicodeStr[1024];
DWORD nSize = MultiByteToWideChar(CP_UTF8, 0, pszMsg, -1, NULL, 0);
if (nSize > sizeof(unicodeStr))
return;
MultiByteToWideChar(CP_UTF8, 0, pszMsg, -1, unicodeStr, nSize);
DWORD nLength = WideCharToMultiByte(CP_OEMCP, 0, unicodeStr, -1, 0, 0, NULL, NULL);
if (nLength > sizeof(outputStr))
return;
WideCharToMultiByte(CP_OEMCP, 0, unicodeStr, -1, outputStr, nLength, NULL, NULL);
WriteFile(hConsoleOutput, outputStr, nChars ? nChars : strlen(outputStr), NULL, NULL);
}
void Console_Echo(const char* pszMsg, int nChars = 0)
{
Console_PrintRaw(pszMsg, nChars);
}
const char* Console_GetLine()
{
while (true)
{
INPUT_RECORD recs[1024];
unsigned long numread;
unsigned long numevents;
if (!GetNumberOfConsoleInputEvents(hConsoleInput, &numevents))
return nullptr;
if (numevents <= 0)
break;
if (!ReadConsoleInput(hConsoleInput, recs, ARRAYSIZE(recs), &numread))
return nullptr;
if (numread == 0)
return nullptr;
for (int i = 0; i < (int)numread; i++)
{
INPUT_RECORD* pRec = &recs[i];
if (pRec->EventType != KEY_EVENT)
continue;
if (pRec->Event.KeyEvent.bKeyDown)
{
// check for cursor keys
if (pRec->Event.KeyEvent.wVirtualKeyCode == VK_UP)
{
int nLastCommandInHistory = m_nInputLine + 1;
if (nLastCommandInHistory > m_nTotalLines)
nLastCommandInHistory = 0;
if (m_nBrowseLine == nLastCommandInHistory)
break;
if (m_nBrowseLine == m_nInputLine)
{
if (m_nConsoleTextLen > 0)
strncpy(m_szSavedConsoleText, m_szConsoleText, m_nConsoleTextLen);
m_nSavedConsoleTextLen = m_nConsoleTextLen;
}
m_nBrowseLine--;
if (m_nBrowseLine < 0)
m_nBrowseLine = m_nTotalLines - 1;
// delete old line
while (m_nConsoleTextLen--)
Console_Echo("\b \b");
// copy buffered line
Console_Echo(m_aszLineBuffer[m_nBrowseLine]);
strncpy(m_szConsoleText, m_aszLineBuffer[m_nBrowseLine], 256);
m_nConsoleTextLen = strlen(m_aszLineBuffer[m_nBrowseLine]);
m_nCursorPosition = m_nConsoleTextLen;
}
else if (pRec->Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
{
if (m_nBrowseLine == m_nInputLine)
break;
if (++m_nBrowseLine > m_nTotalLines)
m_nBrowseLine = 0;
while (m_nConsoleTextLen--)
Console_Echo("\b \b");
if (m_nBrowseLine == m_nInputLine)
{
if (m_nSavedConsoleTextLen > 0)
{
strncpy(m_szConsoleText, m_szSavedConsoleText, m_nSavedConsoleTextLen);
Console_Echo(m_szConsoleText, m_nSavedConsoleTextLen);
}
m_nConsoleTextLen = m_nSavedConsoleTextLen;
}
else
{
Console_Echo(m_aszLineBuffer[m_nBrowseLine]);
strncpy(m_szConsoleText, m_aszLineBuffer[m_nBrowseLine], 256);
m_nConsoleTextLen = strlen(m_aszLineBuffer[m_nBrowseLine]);
}
m_nCursorPosition = m_nConsoleTextLen;
}
else if (pRec->Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
{
if (m_nCursorPosition == 0)
break;
Console_Echo("\b");
m_nCursorPosition--;
}
else if (pRec->Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
{
if (m_nCursorPosition == m_nConsoleTextLen)
break;
Console_Echo(m_szConsoleText + m_nCursorPosition, 1);
m_nCursorPosition++;
}
else
{
int nLen;
char ch = pRec->Event.KeyEvent.uChar.AsciiChar;
switch (ch)
{
case '\r': // Enter
{
int nLen = 0;
Console_Echo("\n");
if (m_nConsoleTextLen)
{
nLen = m_nConsoleTextLen;
m_szConsoleText[m_nConsoleTextLen] = '\0';
m_nConsoleTextLen = 0;
m_nCursorPosition = 0;
// cache line in buffer, but only if it's not a duplicate of the previous line
if ((m_nInputLine == 0) || (strcmp(m_aszLineBuffer[m_nInputLine - 1], m_szConsoleText)))
{
strncpy(m_aszLineBuffer[m_nInputLine], m_szConsoleText, 256);
m_nInputLine++;
if (m_nInputLine > m_nTotalLines)
m_nTotalLines = m_nInputLine;
if (m_nInputLine >= 10)
m_nInputLine = 0;
}
m_nBrowseLine = m_nInputLine;
}
if (nLen)
return m_szConsoleText;
break;
}
case '\b': // Backspace
{
int nCount;
if (m_nCursorPosition == 0)
break;
m_nConsoleTextLen--;
m_nCursorPosition--;
Console_Echo("\b");
for (nCount = m_nCursorPosition; nCount < m_nConsoleTextLen; ++nCount)
{
m_szConsoleText[nCount] = m_szConsoleText[nCount + 1];
Console_Echo(m_szConsoleText + nCount, 1);
}
Console_Echo(" ");
nCount = m_nConsoleTextLen;
while (nCount >= m_nCursorPosition)
{
Console_Echo("\b");
nCount--;
}
m_nBrowseLine = m_nInputLine;
break;
}
case '\t': // TAB
//ReceiveTab(); // not available in console
break;
default: // dont' accept nonprintable chars
if ((ch >= ' ') && (ch <= '~'))
{
int nCount;
// If the line buffer is maxed out, ignore this char
if ((unsigned)m_nConsoleTextLen >= (sizeof(m_szConsoleText) - 2))
break;
nCount = m_nConsoleTextLen;
while (nCount > m_nCursorPosition)
{
m_szConsoleText[nCount] = m_szConsoleText[nCount - 1];
nCount--;
}
m_szConsoleText[m_nCursorPosition] = ch;
Console_Echo(m_szConsoleText + m_nCursorPosition, m_nConsoleTextLen - m_nCursorPosition + 1);
m_nConsoleTextLen++;
m_nCursorPosition++;
nCount = m_nConsoleTextLen;
while (nCount > m_nCursorPosition)
{
Console_Echo("\b");
nCount--;
}
m_nBrowseLine = m_nInputLine;
}
break;
}
}
}
}
}
return nullptr;
}
void PrepareConsoleInput()
{
MSG msg;
while (PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE)) {
if (!GetMessage(&msg, nullptr, 0, 0)) {
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void ProcessConsoleInput()
{
if (!engineAPI)
return;
const char* inputLine = Console_GetLine();
if (inputLine)
{
char szBuf[256];
snprintf(szBuf, sizeof(szBuf), "%s\n", inputLine);
engineAPI->AddConsoleText(szBuf);
}
}
// pingboost 0
void sleep_1ms() noexcept
{
using namespace std::chrono_literals;
std::this_thread::sleep_for(1ms);
}
static NTSTATUS(__stdcall* NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval) = (NTSTATUS(__stdcall*)(BOOL, PLARGE_INTEGER)) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtDelayExecution");
static NTSTATUS(__stdcall* ZwSetTimerResolution)(IN ULONG RequestedResolution, IN BOOLEAN Set, OUT PULONG ActualResolution) = (NTSTATUS(__stdcall*)(ULONG, BOOLEAN, PULONG)) GetProcAddress(GetModuleHandle("ntdll.dll"), "ZwSetTimerResolution");
// pingboost 4
void sleep_timer() noexcept
{
::LARGE_INTEGER interval;
interval.QuadPart = -1LL;
NtDelayExecution(FALSE, &interval);
}
// pingboost 5
void yield_thread() noexcept
{
std::this_thread::yield();
}
int main(int argc, char* argv)
{
Console_Init();
SetConsoleTitleA("CSO HLDS");
SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
g_bTerminated = false;
do {
CommandLine()->CreateCmdLine(GetCommandLine());
CommandLine()->RemoveParm("-steam");
CommandLine()->AppendParm("-console", nullptr);
WSAData WSAData;
WSAStartup(0x202, &WSAData);
if (CommandLine()->CheckParm("-lang") == NULL)
CommandLine()->AppendParm("-lang", "na_"); // the dedicated server won't load without this line
if (CommandLine()->CheckParm("-ip") == NULL)
CommandLine()->AppendParm("-ip", DEFAULT_IP);
if (CommandLine()->CheckParm("-lobbyport") == NULL)
CommandLine()->AppendParm("-lobbyport", DEFAULT_LOBBYPORT);
const char* port;
if (CommandLine()->CheckParm("-port", &port) == NULL)
{
CommandLine()->AppendParm("-port", DEFAULT_PORT);
g_iPort = atoi(DEFAULT_PORT);
}
else if (port)
g_iPort = atoi(port);
const char* logfile;
if (CommandLine()->CheckParm("-logfile", &logfile) == NULL)
{
CommandLine()->AppendParm("-logfile", DEFAULT_LOGFILE);
memcpy(g_pLogFile, DEFAULT_LOGFILE, sizeof(g_pLogFile));
}
else if (logfile)
memcpy(g_pLogFile, logfile, sizeof(g_pLogFile));
time_t currentTime = time(NULL);
tm* currentLocalTime = localtime(¤tTime);
int currentProcessId = GetCurrentProcessId();
snprintf(g_pLogFile, sizeof(g_pLogFile), "%s_%04d%02d%02d_%02d%02d%02d_%u_%d",
g_pLogFile,
currentLocalTime->tm_year + 1900,
currentLocalTime->tm_mon + 1,
currentLocalTime->tm_mday,
currentLocalTime->tm_hour,
currentLocalTime->tm_min,
currentLocalTime->tm_sec,
currentProcessId,
g_iPort);
if (CommandLine()->CheckParm("-vxlpath") == NULL)
{
TCHAR lpTempPathBuffer[MAX_PATH];
GetTempPath(MAX_PATH, lpTempPathBuffer);
CommandLine()->AppendParm("-vxlpath", lpTempPathBuffer);
}
const char* pingboost;
if (CommandLine()->CheckParm("-pingboost", &pingboost) && pingboost)
g_iPingBoost = atoi(pingboost);
HINTERFACEMODULE hFileSystem = LoadFilesystemModule();
if (!hFileSystem)
return LAUNCHER_ERROR;
CreateInterfaceFn fsCreateInterface = (CreateInterfaceFn)Sys_GetFactory(hFileSystem);
g_pFileSystem = (IFileSystem*)fsCreateInterface(FILESYSTEM_INTERFACE_VERSION, NULL);
g_pFileSystem->Mount();
g_pFileSystem->AddSearchPath(Sys_GetLongPathName(), "BIN");
const char* pszEngineDLL = "hw.dll";
HINTERFACEMODULE hEngine;
hEngine = Sys_LoadModule(pszEngineDLL);
if (!hEngine)
{
static char msg[512];
wsprintf(msg, "Could not load engine : %s.", pszEngineDLL);
MessageBox(NULL, msg, "Fatal Error", MB_ICONERROR);
return LAUNCHER_ERROR;
}
CreateInterfaceFn engineCreateInterface = (CreateInterfaceFn)Sys_GetFactory(hEngine);
engineAPI = (IDedicatedServerAPI*)engineCreateInterface(VENGINE_HLDS_API_VERSION, NULL);
if (!engineCreateInterface || !engineAPI)
return LAUNCHER_ERROR;
Hook((HMODULE)hEngine);
if (!engineAPI->Init(Sys_GetLongPathNameWithoutBin(), CommandLine()->GetCmdLine(), Sys_GetFactoryThis(), fsCreateInterface))
return LAUNCHER_ERROR;
if (g_iPingBoost == 4)
{
ULONG actualResolution;
ZwSetTimerResolution(1, true, &actualResolution);
}
switch (g_iPingBoost)
{
case 4: sleep_thread = sleep_timer; break;
case 5: sleep_thread = yield_thread; break;
default: sleep_thread = sleep_1ms;
}
bool done = false;
while (!done)
{
sleep_thread();
PrepareConsoleInput();
if (g_bTerminated)
break;
ProcessConsoleInput();
done = !engineAPI->RunFrame();
UpdateStatus(FALSE);
}
int ret = engineAPI->Shutdown();
if (ret == DLL_CLOSE)
g_bTerminated = true;
Unhook();
g_pFileSystem->Unmount();
Sys_FreeModule(hFileSystem);
Sys_FreeModule(hEngine);
WSACleanup();
} while (!g_bTerminated);
return LAUNCHER_OK;
}