From 87131e27e2d8943e4f53bbf12be53ec23bd343e0 Mon Sep 17 00:00:00 2001 From: asmodai Date: Fri, 5 May 2017 18:45:24 +0300 Subject: [PATCH] Make launcher code cleaner --- rehlds/dedicated/src/conproc.cpp | 19 +++----- rehlds/dedicated/src/dbg.cpp | 21 +++------ rehlds/dedicated/src/dedicated_exports.cpp | 2 +- rehlds/dedicated/src/sys_ded.cpp | 2 +- rehlds/dedicated/src/sys_linux.cpp | 55 +++++++++++----------- rehlds/dedicated/src/sys_window.cpp | 9 ++-- 6 files changed, 47 insertions(+), 61 deletions(-) diff --git a/rehlds/dedicated/src/conproc.cpp b/rehlds/dedicated/src/conproc.cpp index 0429cf9..3f7af5e 100644 --- a/rehlds/dedicated/src/conproc.cpp +++ b/rehlds/dedicated/src/conproc.cpp @@ -10,9 +10,7 @@ static HANDLE hStdin; BOOL SetConsoleCXCY(HANDLE hStdout, int cx, int cy) { CONSOLE_SCREEN_BUFFER_INFO info; - COORD coordMax; - - coordMax = GetLargestConsoleWindowSize(hStdout); + COORD coordMax = GetLargestConsoleWindowSize(hStdout); if (cy > coordMax.Y) cy = coordMax.Y; @@ -113,12 +111,11 @@ BOOL ReadText(LPTSTR pszText, int iBeginLine, int iEndLine) { COORD coord; DWORD dwRead; - BOOL bRet; coord.X = 0; coord.Y = iBeginLine; - bRet = ReadConsoleOutputCharacter(hStdout, pszText, 80 * (iEndLine - iBeginLine + 1), coord, &dwRead); + BOOL bRet = ReadConsoleOutputCharacter(hStdout, pszText, 80 * (iEndLine - iBeginLine + 1), coord, &dwRead); // Make sure it's null terminated. if (bRet) @@ -183,23 +180,21 @@ BOOL WriteText(LPCTSTR szText) unsigned __stdcall RequestProc(void *arg) { - int *pBuffer; - DWORD dwRet; HANDLE heventWait[2]; int iBeginLine, iEndLine; heventWait[0] = heventParentSend; heventWait[1] = heventDone; - while (1) + for (;;) { - dwRet = WaitForMultipleObjects(2, heventWait, FALSE, INFINITE); + DWORD dwRet = WaitForMultipleObjects(2, heventWait, FALSE, INFINITE); // heventDone fired, so we're exiting. if (dwRet == WAIT_OBJECT_0 + 1) break; - pBuffer = (int *)GetMappedBuffer(hfileBuffer); + int* pBuffer = (int *)GetMappedBuffer(hfileBuffer); // hfileBuffer is invalid. Just leave. if (!pBuffer) @@ -284,14 +279,14 @@ void InitConProc() heventChildSend = heventChild; // So we'll know when to go away. - heventDone = CreateEvent(NULL, FALSE, FALSE, NULL); + heventDone = CreateEvent(nullptr, FALSE, FALSE, nullptr); if (!heventDone) { sys->Printf("InitConProc: Couldn't create heventDone\n"); return; } - if (!_beginthreadex(NULL, 0, RequestProc, NULL, 0, &threadAddr)) + if (!_beginthreadex(nullptr, 0, RequestProc, nullptr, 0, &threadAddr)) { CloseHandle(heventDone); sys->Printf("InitConProc: Couldn't create third party thread\n"); diff --git a/rehlds/dedicated/src/dbg.cpp b/rehlds/dedicated/src/dbg.cpp index f0b6b43..9adfde0 100644 --- a/rehlds/dedicated/src/dbg.cpp +++ b/rehlds/dedicated/src/dbg.cpp @@ -33,9 +33,8 @@ inline void CPerformanceCounter::InitializePerformanceCounter() // get 32 out of the 64 time bits such that we have around // 1 microsecond resolution - unsigned int lowpart, highpart; - lowpart = (unsigned int)performanceFreq.LowPart; - highpart = (unsigned int)performanceFreq.HighPart; + auto lowpart = performanceFreq.LowPart; + auto highpart = performanceFreq.HighPart; m_iLowShift = 0; while (highpart || (lowpart > 2000000.0)) @@ -46,7 +45,7 @@ inline void CPerformanceCounter::InitializePerformanceCounter() highpart >>= 1; } - m_flPerfCounterFreq = 1.0 / (double)lowpart; + m_flPerfCounterFreq = 1.0 / lowpart; #endif // _WIN32 } @@ -59,18 +58,16 @@ inline double CPerformanceCounter::GetCurTime() static unsigned int oldtime; static int first = 1; LARGE_INTEGER PerformanceCount; - unsigned int temp, t2; - double time; + unsigned int temp; QueryPerformanceCounter(&PerformanceCount); if (m_iLowShift == 0) { - temp = (unsigned int)PerformanceCount.LowPart; + temp = PerformanceCount.LowPart; } else { - temp = ((unsigned int)PerformanceCount.LowPart >> m_iLowShift) | - ((unsigned int)PerformanceCount.HighPart << (32 - m_iLowShift)); + temp = (PerformanceCount.LowPart >> m_iLowShift) | (PerformanceCount.HighPart << (32 - m_iLowShift)); } if (first) @@ -88,12 +85,8 @@ inline double CPerformanceCounter::GetCurTime() } else { - t2 = temp - oldtime; - - time = (double)t2 * m_flPerfCounterFreq; oldtime = temp; - - m_flCurrentTime += time; + m_flCurrentTime += (temp - oldtime) * m_flPerfCounterFreq; if (m_flCurrentTime == m_flLastCurrentTime) { diff --git a/rehlds/dedicated/src/dedicated_exports.cpp b/rehlds/dedicated/src/dedicated_exports.cpp index 956fb15..4d21062 100644 --- a/rehlds/dedicated/src/dedicated_exports.cpp +++ b/rehlds/dedicated/src/dedicated_exports.cpp @@ -2,7 +2,7 @@ class CDedicatedExports: IDedicatedExports { public: - void Sys_Printf(char *text); + void Sys_Printf(char *text) override; }; EXPOSE_SINGLE_INTERFACE(CDedicatedExports, IDedicatedExports, VENGINE_DEDICATEDEXPORTS_API_VERSION); diff --git a/rehlds/dedicated/src/sys_ded.cpp b/rehlds/dedicated/src/sys_ded.cpp index caa2463..043c443 100644 --- a/rehlds/dedicated/src/sys_ded.cpp +++ b/rehlds/dedicated/src/sys_ded.cpp @@ -83,7 +83,7 @@ int RunEngine() { engineAPI = (IDedicatedServerAPI *)engineFactory(VENGINE_HLDS_API_VERSION, nullptr); #ifdef LAUNCHER_FIXES - rehldsApi = (IRehldsApi *)engineFactory(VREHLDS_HLDS_API_VERSION, NULL); + rehldsApi = (IRehldsApi *)engineFactory(VREHLDS_HLDS_API_VERSION, nullptr); if (rehldsApi) { if (rehldsApi->GetMajorVersion() != REHLDS_API_VERSION_MAJOR || rehldsApi->GetMinorVersion() < REHLDS_API_VERSION_MINOR) diff --git a/rehlds/dedicated/src/sys_linux.cpp b/rehlds/dedicated/src/sys_linux.cpp index acb3303..5900c2d 100644 --- a/rehlds/dedicated/src/sys_linux.cpp +++ b/rehlds/dedicated/src/sys_linux.cpp @@ -5,22 +5,22 @@ public: CSys(); virtual ~CSys(); - void Sleep(int msec); - bool GetExecutableName(char *out); - NORETURN void ErrorMessage(int level, const char *msg); + void Sleep(int msec) override; + bool GetExecutableName(char *out) override; + NORETURN 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; @@ -71,7 +71,7 @@ void Sleep_Select(int msec) tv.tv_sec = 0; tv.tv_usec = 1000 * msec; - select(1, NULL, NULL, NULL, &tv); + select(1, nullptr, nullptr, nullptr, &tv); } void Sleep_Net(int msec) @@ -102,7 +102,7 @@ void Sleep_Timer(int msec) g_bPaused = false; // set the timer to trigger - if (!setitimer(ITIMER_REAL, &tm, NULL)) { + if (!setitimer(ITIMER_REAL, &tm, nullptr)) { // wait for the signal pause(); } @@ -133,28 +133,27 @@ void Sys_InitPingboost() Sys_Sleep = Sleep_Old; char *pPingType; - int type; if (CommandLine()->CheckParm("-pingboost", &pPingType) && pPingType) { - type = atoi(pPingType); + int type = atoi(pPingType); switch (type) { case 1: - signal(SIGALRM, alarmFunc); - Sys_Sleep = Sleep_Timer; - break; + signal(SIGALRM, alarmFunc); + Sys_Sleep = Sleep_Timer; + break; case 2: - Sys_Sleep = Sleep_Select; - break; + Sys_Sleep = Sleep_Select; + break; case 3: - Sys_Sleep = Sleep_Net; + 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; + // 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; + Sys_Sleep = Sleep_Old; + break; } } } diff --git a/rehlds/dedicated/src/sys_window.cpp b/rehlds/dedicated/src/sys_window.cpp index 27f4816..3f76e5c 100644 --- a/rehlds/dedicated/src/sys_window.cpp +++ b/rehlds/dedicated/src/sys_window.cpp @@ -47,7 +47,7 @@ void CSys::Sleep(int msec) bool CSys::GetExecutableName(char *out) { - if (!::GetModuleFileName((HINSTANCE)GetModuleHandle(NULL), out, 256)) + if (!::GetModuleFileName((HINSTANCE)GetModuleHandle(nullptr), out, 256)) return false; return true; @@ -67,7 +67,6 @@ void CSys::WriteStatusText(char *szText) void CSys::UpdateStatus(int force) { static double tLast = 0.0; - double tCurrent; char szStatus[256]; int n, nMax; char szMap[32]; @@ -76,7 +75,7 @@ void CSys::UpdateStatus(int force) if (!engineAPI) return; - tCurrent = (double)timeGetTime() * 0.001; + double tCurrent = timeGetTime() * 0.001; engineAPI->UpdateStatus(&fps, &n, &nMax, szMap); if (!force) @@ -190,8 +189,8 @@ bool Sys_SetupConsole() void Sys_PrepareConsoleInput() { MSG msg; - while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { - if (!GetMessage(&msg, NULL, 0, 0)) { + while (PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE)) { + if (!GetMessage(&msg, nullptr, 0, 0)) { break; }