diff --git a/rehlds/dedicated/src/commandline.cpp b/rehlds/common/commandline.cpp similarity index 95% rename from rehlds/dedicated/src/commandline.cpp rename to rehlds/common/commandline.cpp index 86e0b25..9f40d2f 100644 --- a/rehlds/dedicated/src/commandline.cpp +++ b/rehlds/common/commandline.cpp @@ -321,30 +321,28 @@ const char *CCommandLine::CheckParm(const char *psz, char **ppszValue) const return nullptr; char *pret = strstr(m_pszCmdLine, psz); - if (!pret || !ppszValue) - return nullptr; + if (pret && ppszValue) { + *ppszValue = nullptr; - *ppszValue = nullptr; + // find the next whitespace + char *p1 = pret; + do { + ++p1; + } while (*p1 != ' ' && *p1); - // find the next whitespace - char *p1 = pret; - do { - ++p1; - } while (*p1 != ' ' && *p1); + int i = 0; + char *p2 = p1 + 1; - int i = 0; - char *p2 = p1 + 1; + do { + if (p2[i] == '\0' || p2[i] == ' ') + break; - do { - if (p2[i] == '\0' || p2[i] == ' ') - break; + sz[i++] = p2[i]; + } while (i < sizeof(sz)); - sz[i++] = p2[i]; + sz[i] = '\0'; + *ppszValue = sz; } - while (i < sizeof(sz)); - - sz[i] = '\0'; - *ppszValue = sz; return pret; } diff --git a/rehlds/dedicated/src/icommandline.h b/rehlds/common/icommandline.h similarity index 100% rename from rehlds/dedicated/src/icommandline.h rename to rehlds/common/icommandline.h diff --git a/rehlds/dedicated/build.gradle b/rehlds/dedicated/build.gradle index 66faa15..8c8dcee 100644 --- a/rehlds/dedicated/build.gradle +++ b/rehlds/dedicated/build.gradle @@ -115,6 +115,7 @@ model { include "ObjectList.cpp" include "SteamAppStartUp.cpp" include "textconsole.cpp" + include "commandline.cpp" if (GradleCppUtils.windows) { include "TextConsoleWin32.cpp" } diff --git a/rehlds/dedicated/msvc/dedicated.vcxproj b/rehlds/dedicated/msvc/dedicated.vcxproj index 12c2ca4..5200cbb 100644 --- a/rehlds/dedicated/msvc/dedicated.vcxproj +++ b/rehlds/dedicated/msvc/dedicated.vcxproj @@ -154,6 +154,7 @@ + @@ -163,7 +164,6 @@ - diff --git a/rehlds/dedicated/msvc/dedicated.vcxproj.filters b/rehlds/dedicated/msvc/dedicated.vcxproj.filters index 380ebeb..d2638e9 100644 --- a/rehlds/dedicated/msvc/dedicated.vcxproj.filters +++ b/rehlds/dedicated/msvc/dedicated.vcxproj.filters @@ -37,9 +37,6 @@ - - src - src @@ -85,6 +82,9 @@ src\engine + + src + diff --git a/rehlds/dedicated/src/dedicated.h b/rehlds/dedicated/src/dedicated.h index 6e18558..8696402 100644 --- a/rehlds/dedicated/src/dedicated.h +++ b/rehlds/dedicated/src/dedicated.h @@ -1,16 +1,23 @@ -#ifndef DEDICATED_H -#define DEDICATED_H -#ifdef _WIN32 #pragma once + +#ifdef _WIN32 +#define VGUI #endif +#define LAUNCHER_ERROR -1 +#define LAUNCHER_OK 0 + typedef void (*NET_Sleep_t)(); typedef void (*SleepType)(int msec); extern bool g_bVGui; extern IDedicatedServerAPI *engineAPI; + #ifdef LAUNCHER_FIXES extern IRehldsApi *rehldsApi; extern const RehldsFuncs_t* rehldsFuncs; #endif -#endif // DEDICATED_H + +bool Sys_SetupConsole(); +void Sys_PrepareConsoleInput(); +void Sys_InitPingboost(); diff --git a/rehlds/dedicated/src/precompiled.h b/rehlds/dedicated/src/precompiled.h index 91e53c0..48fba5d 100644 --- a/rehlds/dedicated/src/precompiled.h +++ b/rehlds/dedicated/src/precompiled.h @@ -11,6 +11,8 @@ #include "engine_hlds_api.h" #include "idedicatedexports.h" #include "icommandline.h" +#include "isys.h" +#include "dll_state.h" #ifdef LAUNCHER_FIXES struct DLL_FUNCTIONS; @@ -18,6 +20,8 @@ struct DLL_FUNCTIONS; #include "rehlds_api.h" #endif +#include "dedicated.h" + #include "sys_ded.h" #include "icommandline.h" #include "textconsole.h" @@ -28,4 +32,6 @@ struct DLL_FUNCTIONS; #ifdef _WIN32 #include "conproc.h" +#else + #include #endif // _WIN32 diff --git a/rehlds/dedicated/src/sys_ded.cpp b/rehlds/dedicated/src/sys_ded.cpp index e7f5298..8b8dc2f 100644 --- a/rehlds/dedicated/src/sys_ded.cpp +++ b/rehlds/dedicated/src/sys_ded.cpp @@ -1,10 +1,8 @@ #include "precompiled.h" -long hDLLThirdParty = 0L; - bool g_bVGui = false; char *gpszCvars = nullptr; -bool gbAppHasBeenTerminated = false; +bool g_bAppHasBeenTerminated = false; CSysModule *g_pEngineModule = nullptr; IDedicatedServerAPI *engineAPI = nullptr; @@ -37,26 +35,18 @@ void Sys_Printf(char *fmt, ...) va_end(argptr); // Get Current text and append it. +#ifdef VGUI if (g_bVGui) { VGUIPrintf(szText); } else +#endif { console.Print(szText); } } -void Load3rdParty() -{ - // Only do this if the server operator wants the support. - // ( In case of malicious code, too ) - if (CommandLine()->CheckParm("-usegh")) - { - hDLLThirdParty = sys->LoadLibrary("ghostinj.dll"); - } -} - void ProcessConsoleInput() { if (!engineAPI) @@ -66,7 +56,7 @@ void ProcessConsoleInput() if (inputLine) { char szBuf[256]; - _snprintf(szBuf, sizeof(szBuf), "%s\n", inputLine); + _snprintf(szBuf, sizeof szBuf, "%s\n", inputLine); engineAPI->AddConsoleText(szBuf); } } @@ -76,145 +66,213 @@ char *UTIL_GetBaseDir() return "."; } -// Server Frame -int RunServer() +int RunEngine() { -#ifdef _WIN32 +#ifdef VGUI if (g_bVGui) { // TODO: finish VGUI - //vgui::ivgui()->SetSleep(0); - } #endif - int iret = 0; - while (true) + CreateInterfaceFn engineFactory = Sys_GetFactory(g_pEngineModule); + RunVGUIFrame(); + + if (engineFactory) { - if (gbAppHasBeenTerminated) - return iret; - - CreateInterfaceFn engineFactory = Sys_GetFactory(g_pEngineModule); - - RunVGUIFrame(); - - if (engineFactory) - { - engineAPI = (IDedicatedServerAPI *)engineFactory(VENGINE_HLDS_API_VERSION, NULL); + engineAPI = (IDedicatedServerAPI *)engineFactory(VENGINE_HLDS_API_VERSION, nullptr); #ifdef LAUNCHER_FIXES - rehldsApi = (IRehldsApi *)engineFactory(VREHLDS_HLDS_API_VERSION, NULL); - if (rehldsApi) - { - if (rehldsApi->GetMajorVersion() != REHLDS_API_VERSION_MAJOR || rehldsApi->GetMinorVersion() < REHLDS_API_VERSION_MINOR) - { - rehldsApi = nullptr; - } - else - { - rehldsFuncs = rehldsApi->GetFuncs(); - } - } -#endif - } - - RunVGUIFrame(); - - if (!engineAPI) - return -1; - - if (!engineAPI->Init(UTIL_GetBaseDir(), (char *)CommandLine()->GetCmdLine(), Sys_GetFactoryThis(), g_FilesystemFactoryFn)) - return -1; - - RunVGUIFrame(); - -#ifdef _WIN32 - if (g_bVGui) + rehldsApi = (IRehldsApi *)engineFactory(VREHLDS_HLDS_API_VERSION, NULL); + if (rehldsApi) { - // TODO: finish VGUI - /*g_pFileSystemInterface->AddSearchPath("platform", "PLATFORM"); - - // find our configuration directory - char szConfigDir[512]; - const char *steamPath = getenv("SteamInstallPath"); - if (steamPath) + if (rehldsApi->GetMajorVersion() != REHLDS_API_VERSION_MAJOR || rehldsApi->GetMinorVersion() < REHLDS_API_VERSION_MINOR) { - // put the config dir directly under steam - _snprintf(szConfigDir, sizeof(szConfigDir), "%s/config", steamPath); + rehldsApi = nullptr; } else { - // we're not running steam, so just put the config dir under the platform - strncpy(szConfigDir, "platform/config", sizeof(szConfigDir)); - }*/ - } -#endif // _WIN32 - - RunVGUIFrame(); - - if (gpszCvars) - engineAPI->AddConsoleText(gpszCvars); - - VGUIFinishedConfig(); - RunVGUIFrame(); - - while (true) - { -#ifdef _WIN32 - if (g_bVGui) - RunVGUIFrame(); -#endif // _WIN32 - - // Running really fast, yield some time to other apps - sys->Sleep(1); - -#ifdef _WIN32 - if (!g_bVGui) - { - bool bDone = false; - MSG msg; - while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) - { - if (!GetMessage(&msg, NULL, 0, 0)) - { - bDone = true; - break; - } - - TranslateMessage(&msg); - DispatchMessage(&msg); - } - - if (gbAppHasBeenTerminated || bDone) - break; + rehldsFuncs = rehldsApi->GetFuncs(); } - - if (!g_bVGui) -#endif // _WIN32 - { - ProcessConsoleInput(); - } - - if (!engineAPI->RunFrame()) - break; - - sys->UpdateStatus(0 /* don't force */); } +#endif + } + RunVGUIFrame(); + if (!engineAPI || !engineAPI->Init(UTIL_GetBaseDir(), (char *)CommandLine()->GetCmdLine(), Sys_GetFactoryThis(), g_FilesystemFactoryFn)) { + return LAUNCHER_ERROR; + } - if (g_bVGui) - { + RunVGUIFrame(); + +#ifdef VGUI + // TODO: finish me! + /*if (g_bVGui) { + g_pFileSystemInterface->AddSearchPath("platform", "PLATFORM"); + + // find our configuration directory + char szConfigDir[512]; + const char *steamPath = getenv("SteamInstallPath"); + if (steamPath) { + // put the config dir directly under steam + _snprintf(szConfigDir, sizeof(szConfigDir), "%s/config", steamPath); + } + else { + // we're not running steam, so just put the config dir under the platform + strncpy(szConfigDir, "platform/config", sizeof(szConfigDir)); + } + }*/ +#endif // VGUI + + RunVGUIFrame(); + + if (gpszCvars) { + engineAPI->AddConsoleText(gpszCvars); + } + + VGUIFinishedConfig(); + RunVGUIFrame(); + + bool bDone = false; + while (!bDone) { + // Running really fast, yield some time to other apps + sys->Sleep(1); + + Sys_PrepareConsoleInput(); + + + if (g_bAppHasBeenTerminated) + break; + +#ifdef VGUI + if (g_bVGui) { RunVGUIFrame(); - StopVGUI(); } else +#endif { - sys->DestroyConsoleWindow(); - console.ShutDown(); + ProcessConsoleInput(); } - iret = engineAPI->Shutdown(); - Sys_UnloadModule(g_pEngineModule); - VGUIFinishedConfig(); - - if (iret == DLL_CLOSE) - return iret; + bDone = !engineAPI->RunFrame(); + sys->UpdateStatus(FALSE /* don't force */); } + +#ifdef VGUI + if (g_bVGui) { + RunVGUIFrame(); + StopVGUI(); + } + else +#endif + { + sys->DestroyConsoleWindow(); + console.ShutDown(); + } + + int iret = engineAPI->Shutdown(); + VGUIFinishedConfig(); + + if (iret == DLL_CLOSE) + g_bAppHasBeenTerminated = true; + + return LAUNCHER_OK; +} + +int StartServer(char* cmdline) +{ + g_bAppHasBeenTerminated = false; + + do { + CommandLine()->CreateCmdLine(cmdline); + CommandLine()->AppendParm("-steam", nullptr); + + Sys_InitPingboost(); + + // Load engine + g_pEngineModule = Sys_LoadModule(ENGINE_LIB); + if (!g_pEngineModule) { + sys->ErrorMessage(0, "Unable to load engine, image is corrupt."); + return LAUNCHER_ERROR; + } + + // Load filesystem + g_pFileSystemModule = Sys_LoadModule(STDIO_FILESYSTEM_LIB); + if (!g_pFileSystemModule) { + sys->ErrorMessage(0, "Unable to load filesystem, image is corrupt."); + return LAUNCHER_ERROR; + } + + // Get filesystem factory + g_FilesystemFactoryFn = Sys_GetFactory(g_pFileSystemModule); + if (!g_FilesystemFactoryFn) { + sys->ErrorMessage(0, "Unable to get filesystem factory."); + return LAUNCHER_ERROR; + } + + // Retrieve filesystem interface + IFileSystem *pFullFileSystem = (IFileSystem *)g_FilesystemFactoryFn(FILESYSTEM_INTERFACE_VERSION, nullptr); + if (!pFullFileSystem) { + sys->ErrorMessage(0, "Can not retrive filesystem interface version '" FILESYSTEM_INTERFACE_VERSION "'.\n"); + return LAUNCHER_ERROR; + } + + // Mount filesystem + pFullFileSystem->Mount(); + + // Init VGUI or Console mode +#ifdef VGUI + char *pszValue = nullptr; + if (!CommandLine()->CheckParm("-console", &pszValue)) { + g_bVGui = true; + StartVGUI(); + } + else +#endif + { + if (!console.Init()) { + sys->ErrorMessage(0, "Failed to initialize console."); // TODO: add more details + return LAUNCHER_ERROR; + } + + if (!sys->CreateConsoleWindow()) { + sys->ErrorMessage(0, "Failed to create console window."); + return LAUNCHER_ERROR; + } + + if (!Sys_SetupConsole()) + return LAUNCHER_ERROR; + } + +#ifdef VGUI + // TODO: finish me! + /*// run vgui + if (g_bVGui) + { + while (VGUIIsInConfig() && VGUIIsRunning()) + { + RunVGUIFrame(); + } + } + else*/ +#endif + { + int ret = RunEngine(); + + if (ret == LAUNCHER_ERROR) { + sys->ErrorMessage(0, "Failed to launch engine.\n"); + return LAUNCHER_ERROR; + } + } + + if (gpszCvars) + free(gpszCvars); + + // Unmount filesystem + pFullFileSystem->Unmount(); + + // Unload modules + Sys_UnloadModule(g_pFileSystemModule); + Sys_UnloadModule(g_pEngineModule); + + } while (!g_bAppHasBeenTerminated); + + return LAUNCHER_OK; } diff --git a/rehlds/dedicated/src/sys_ded.h b/rehlds/dedicated/src/sys_ded.h index 425089d..c58f6ce 100644 --- a/rehlds/dedicated/src/sys_ded.h +++ b/rehlds/dedicated/src/sys_ded.h @@ -1,26 +1,12 @@ -#ifndef SYS_DED_H -#define SYS_DED_H -#ifdef _WIN32 #pragma once -#endif -#include "isys.h" -#include "dedicated.h" -#include "dll_state.h" - -#if !defined(_WIN32) -#include -#endif // !defined(_WIN32) - -int RunServer(); void Load3rdParty(); void Sys_Printf_Safe(char *text); void Sys_Printf(char *fmt, ...); void _log(const char *fmt, ...); -extern long hDLLThirdParty; extern char *gpszCvars; -extern bool gbAppHasBeenTerminated; +extern bool g_bAppHasBeenTerminated; extern IFileSystem *g_pFileSystemInterface; extern CSysModule *g_pEngineModule; extern CSysModule *g_pFileSystemModule; @@ -30,4 +16,5 @@ extern CreateInterfaceFn g_FilesystemFactoryFn; BOOL WINAPI MyHandlerRoutine(DWORD CtrlType); #endif // _WIN32 -#endif // SYS_DED_H +int RunEngine(); +int StartServer(char* cmdline); diff --git a/rehlds/dedicated/src/sys_linux.cpp b/rehlds/dedicated/src/sys_linux.cpp index b266018..8def318 100644 --- a/rehlds/dedicated/src/sys_linux.cpp +++ b/rehlds/dedicated/src/sys_linux.cpp @@ -2,6 +2,7 @@ class CSys: public ISys { public: + CSys(); virtual ~CSys(); void Sleep(int msec); @@ -29,6 +30,20 @@ char g_szEXEName[MAX_PATH]; SleepType Sys_Sleep; NET_Sleep_t NET_Sleep_Timeout = NULL; +CSys::CSys() +{ + char *fname; + if (CommandLine()->CheckParm("-pidfile", &fname) && fname) { + FILE *pidFile = fopen(fname, "w"); + if (pidFile) { + fprintf(pidFile, "%i\n", getpid()); + fclose(pidFile); + } + else + printf("Warning: unable to open pidfile (%s)\n", fname); + } +} + CSys::~CSys() { sys = nullptr; @@ -61,8 +76,8 @@ void Sleep_Select(int msec) void Sleep_Net(int msec) { - NET_Sleep_Timeout(); -} + NET_Sleep_Timeout(); + } // linux runs on a 100Hz scheduling clock, so the minimum latency from // usleep is 10msec. However, people want lower latency than this.. @@ -113,6 +128,37 @@ void alarmFunc(int num) } +void Sys_InitPingboost() +{ + Sys_Sleep = Sleep_Old; + + char *pPingType; + int type; + if (CommandLine()->CheckParm("-pingboost", &pPingType) && pPingType) { + type = atoi(pPingType); + switch (type) { + case 1: + signal(SIGALRM, alarmFunc); + Sys_Sleep = Sleep_Timer; + break; + case 2: + Sys_Sleep = Sleep_Select; + break; + case 3: + Sys_Sleep = Sleep_Net; + + // we Sys_GetProcAddress NET_Sleep() from + //engine_i486.so later in this function + NET_Sleep_Timeout = (NET_Sleep_t)Sys_GetProcAddress(g_pEngineModule, "NET_Sleep_Timeout"); + break; + // just in case + default: + Sys_Sleep = Sleep_Old; + break; + } + } +} + bool CSys::GetExecutableName(char *out) { strcpy(out, g_szEXEName); @@ -139,7 +185,7 @@ long CSys::LoadLibrary(char *lib) char cwd[1024]; char absolute_lib[1024]; - if (!getcwd(cwd, sizeof(cwd))) + if (!getcwd(cwd, sizeof cwd)) ErrorMessage(1, "Sys_LoadLibrary: Couldn't determine current directory."); if (cwd[strlen(cwd) - 1] == '/') @@ -200,14 +246,13 @@ void CSys::Printf(char *fmt, ...) } #define MAX_LINUX_CMDLINE 2048 -static char linuxCmdline[ MAX_LINUX_CMDLINE ]; +static char linuxCmdline[MAX_LINUX_CMDLINE]; -void BuildCmdLine(int argc, char **argv) +char* BuildCmdLine(int argc, char **argv) { - int len; - int i; + int len = 0; - for (len = 0, i = 1; i < argc; i++) + for (int i = 1; i < argc; i++) { len += strlen(argv[i]) + 1; } @@ -216,112 +261,33 @@ void BuildCmdLine(int argc, char **argv) { printf("command line too long, %i max\n", MAX_LINUX_CMDLINE); exit(-1); - return; } linuxCmdline[0] = '\0'; - for (i = 1; i < argc; i++) + for (int i = 1; i < argc; i++) { if (i > 1) { strcat(linuxCmdline, " "); } - strcat(linuxCmdline, argv[ i ]); + strcat(linuxCmdline, argv[i]); } } -char *GetCommandLine() +bool Sys_SetupConsole() +{ + return true; +} + +void Sys_PrepareConsoleInput() { - return linuxCmdline; } int main(int argc, char *argv[]) { - _snprintf(g_szEXEName, sizeof(g_szEXEName), "%s", argv[0]); - BuildCmdLine(argc, argv); + _snprintf(g_szEXEName, sizeof g_szEXEName, "%s", argv[0]); + char* cmdline = BuildCmdLine(argc, argv); + StartServer(cmdline); - CommandLine()->CreateCmdLine(::GetCommandLine()); - CommandLine()->AppendParm("-steam", NULL); - - // Load engine - g_pEngineModule = Sys_LoadModule(ENGINE_LIB); - if (!g_pEngineModule) - { - sys->ErrorMessage(1, "Unable to load engine, image is corrupt."); - return -1; - } - - Sys_Sleep = Sleep_Old; - - char *pPingType; - int type; - if (CommandLine()->CheckParm("-pingboost", &pPingType) && pPingType) - { - type = atoi(pPingType); - switch (type) - { - case 1: - signal(SIGALRM, alarmFunc); - Sys_Sleep = Sleep_Timer; - break; - case 2: - Sys_Sleep = Sleep_Select; - break; - case 3: - // we Sys_GetProcAddress NET_Sleep() from - //engine_i486.so later in this function - NET_Sleep_Timeout = (NET_Sleep_t)Sys_GetProcAddress(g_pEngineModule, "NET_Sleep_Timeout"); - if (NET_Sleep_Timeout != nullptr) - Sys_Sleep = Sleep_Net; - break; - // just in case - default: - Sys_Sleep = Sleep_Old; - break; - } - } - - char *fsmodule; - if (CommandLine()->CheckParm("-pidfile", &fsmodule) && fsmodule) - { - FILE *pidFile = fopen(fsmodule, "w"); - if (pidFile) { - fprintf(pidFile, "%i\n", getpid()); - fclose(pidFile); - } - else - printf("Warning: unable to open pidfile (%s)\n", pPingType); - } - - g_pFileSystemModule = Sys_LoadModule(STDIO_FILESYSTEM_LIB); - - // Get FileSystem interface - g_FilesystemFactoryFn = Sys_GetFactory(g_pFileSystemModule); - if (!g_FilesystemFactoryFn) - return -1; - - IFileSystem *pFullFileSystem = (IFileSystem *)g_FilesystemFactoryFn(FILESYSTEM_INTERFACE_VERSION, NULL); - if (!pFullFileSystem) - return -1; - - pFullFileSystem->Mount(); - - if (!console.Init()) { - puts("Failed to initilise console."); - return 0; - } - - gbAppHasBeenTerminated = false; - RunServer(); - - if (gpszCvars) - free(gpszCvars); - - if (pFullFileSystem) - pFullFileSystem->Unmount(); - - Sys_UnloadModule(g_pFileSystemModule); - - exit(0); return 0; } diff --git a/rehlds/dedicated/src/sys_window.cpp b/rehlds/dedicated/src/sys_window.cpp index 77f90ce..27f4816 100644 --- a/rehlds/dedicated/src/sys_window.cpp +++ b/rehlds/dedicated/src/sys_window.cpp @@ -2,31 +2,41 @@ class CSys: public ISys { public: + CSys(); virtual ~CSys(); - void Sleep(int msec); - bool GetExecutableName(char *out); - void ErrorMessage(int level, const char *msg); + void Sleep(int msec) override; + bool GetExecutableName(char *out) override; + void ErrorMessage(int level, const char *msg) override; - void WriteStatusText(char *szText); - void UpdateStatus(int force); + void WriteStatusText(char *szText) override; + void UpdateStatus(int force) override; - long LoadLibrary(char *lib); - void FreeLibrary(long library); + long LoadLibrary(char *lib) override; + void FreeLibrary(long library) override; - bool CreateConsoleWindow(); - void DestroyConsoleWindow(); + bool CreateConsoleWindow() override; + void DestroyConsoleWindow() override; - void ConsoleOutput(char *string); - char *ConsoleInput(); - void Printf(char *fmt, ...); + void ConsoleOutput(char *string) override; + char *ConsoleInput() override; + void Printf(char *fmt, ...) override; }; CSys g_Sys; ISys *sys = &g_Sys; +CSys::CSys() +{ + // Startup winock + WORD version = MAKEWORD(2, 0); + WSADATA wsaData; + WSAStartup(version, &wsaData); +} + CSys::~CSys() { + WSACleanup(); sys = nullptr; } @@ -148,94 +158,51 @@ void CSys::Printf(char *fmt, ...) ConsoleOutput(szText); } -int StartServer() +BOOL WINAPI ConsoleCtrlHandler(DWORD CtrlType) { - // Startup winock - WORD version = MAKEWORD(2, 0); - WSADATA wsaData; - WSAStartup(version, &wsaData); - - CommandLine()->CreateCmdLine(GetCommandLine()); - - // Load engine - g_pEngineModule = Sys_LoadModule(ENGINE_LIB); - if (!g_pEngineModule) - { - MessageBox(NULL, "Unable to load engine, image is corrupt.", "Half-Life Dedicated Server Error", MB_OK); - return -1; + switch (CtrlType) { + case CTRL_C_EVENT: + case CTRL_BREAK_EVENT: + case CTRL_CLOSE_EVENT: + case CTRL_LOGOFF_EVENT: + case CTRL_SHUTDOWN_EVENT: + g_bAppHasBeenTerminated = true; + return TRUE; + default: + break; } - g_pFileSystemModule = Sys_LoadModule(STDIO_FILESYSTEM_LIB); + return FALSE; +} - // Get FileSystem interface - g_FilesystemFactoryFn = Sys_GetFactory(g_pFileSystemModule); - if (!g_FilesystemFactoryFn) - return -1; +bool Sys_SetupConsole() +{ + console.SetColor(FOREGROUND_RED | FOREGROUND_INTENSITY); - IFileSystem *pFullFileSystem = (IFileSystem *)g_FilesystemFactoryFn(FILESYSTEM_INTERFACE_VERSION, NULL); - if (!pFullFileSystem) - return -1; - - pFullFileSystem->Mount(); - - char *pszValue = nullptr; - if (CommandLine()->CheckParm("-steam") - || (CommandLine()->CheckParm("-console", &pszValue) && !pszValue)) { - g_bVGui = true; - StartVGUI(); + if (!SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE)) { + sys->ErrorMessage(0, "Unable to set control handler."); + return false; } - else - { - if (!console.Init()) { - MessageBox(NULL, "Failed to initialize console.", "Half-Life Dedicated Server Error", MB_OK); - return 0; + + return true; +} + +void Sys_PrepareConsoleInput() +{ + MSG msg; + while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { + if (!GetMessage(&msg, NULL, 0, 0)) { + break; } - if (!sys->CreateConsoleWindow()) { - return 0; - } - - console.SetColor(FOREGROUND_RED | FOREGROUND_INTENSITY); - - if (!SetConsoleCtrlHandler(MyHandlerRoutine, TRUE)) { - MessageBox(NULL, "Unable to set control handler", "Half-Life Dedicated Server Error", MB_OK); - return 0; - } + TranslateMessage(&msg); + DispatchMessage(&msg); } +} - gbAppHasBeenTerminated = false; - Load3rdParty(); - - // TODO: finish me! - /*// run vgui - if (g_bVGui) - { - while (VGUIIsInConfig() && VGUIIsRunning()) - { - RunVGUIFrame(); - } - } - else*/ - { - RunServer(); - } - - if (gpszCvars) - free(gpszCvars); - - if (pFullFileSystem) - pFullFileSystem->Unmount(); - - Sys_UnloadModule(g_pFileSystemModule); - - if (hDLLThirdParty) - { - Sys_UnloadModule((CSysModule *)hDLLThirdParty); - hDLLThirdParty = 0L; - } - - WSACleanup(); - return 0; +void Sys_InitPingboost() +{ + } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) @@ -243,24 +210,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine if (ShouldLaunchAppViaSteam(lpCmdLine, STDIO_FILESYSTEM_LIB, STDIO_FILESYSTEM_LIB)) return 0; - auto command = CommandLineToArgvW(GetCommandLineW(), (int *)&lpCmdLine); - auto ret = StartServer(); - LocalFree(command); + //auto command = CommandLineToArgvW(GetCommandLineW(), (int *)&lpCmdLine); + auto ret = StartServer(lpCmdLine); + //LocalFree(command); return ret; } - -BOOL WINAPI MyHandlerRoutine(DWORD CtrlType) -{ - switch (CtrlType) - { - case CTRL_C_EVENT: - case CTRL_BREAK_EVENT: - case CTRL_CLOSE_EVENT: - case CTRL_LOGOFF_EVENT: - case CTRL_SHUTDOWN_EVENT: - gbAppHasBeenTerminated = true; - return TRUE; - } - - return FALSE; -}