From b7dd5e11c13b58e0869b0c2128c7824184291bc4 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Tue, 31 Jan 2023 17:09:56 +0100 Subject: [PATCH] Fix bug causing init errors not getting logged Initialize console and spdlog before anything else that is using the console to log errors to (in this case, winsock init errors were never getting logged). --- r5dev/core/dllmain.cpp | 8 ++++---- r5dev/core/init.cpp | 12 +++++++----- r5dev/windows/system.cpp | 9 ++------- r5dev/windows/system.h | 4 ++-- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/r5dev/core/dllmain.cpp b/r5dev/core/dllmain.cpp index 2b13f53e..9e01af20 100644 --- a/r5dev/core/dllmain.cpp +++ b/r5dev/core/dllmain.cpp @@ -21,8 +21,6 @@ void SDK_Init() CheckCPU(); // Check CPU as early as possible, SpdLog also uses SSE intrinsics. MathLib_Init(); // Initialize Mathlib. - WinSock_Init(); // Initialize Winsock. - curl_global_init(CURL_GLOBAL_ALL); if (strstr(GetCommandLineA(), "-launcher")) @@ -42,6 +40,8 @@ void SDK_Init() Console_Init(); #endif // !DEDICATED SpdLog_Init(); + WinSock_Init(); // Initialize Winsock. + for (size_t i = 0; i < SDK_ARRAYSIZE(R5R_EMBLEM); i++) { std::string svEscaped = StringEscape(R5R_EMBLEM[i]); @@ -50,7 +50,7 @@ void SDK_Init() spdlog::info("\n"); Systems_Init(); - WinSys_Attach(); + WinSys_Init(); #ifndef DEDICATED Input_Init(); @@ -77,7 +77,7 @@ void SDK_Shutdown() WinSock_Shutdown(); Systems_Shutdown(); - WinSys_Detach(); + WinSys_Shutdown(); #ifndef DEDICATED Input_Shutdown(); diff --git a/r5dev/core/init.cpp b/r5dev/core/init.cpp index ec4409e0..20351b89 100644 --- a/r5dev/core/init.cpp +++ b/r5dev/core/init.cpp @@ -153,7 +153,7 @@ void Systems_Init() initTimer.End(); spdlog::info("+-------------------------------------------------------------+\n"); - spdlog::info("Detour->Init() '{:10.6f}' seconds ('{:12d}' clocks)\n", initTimer.GetDuration().GetSeconds(), initTimer.GetDuration().GetCycles()); + spdlog::info("{:16s} '{:10.6f}' seconds ('{:12d}' clocks)\n", "Detour->Init()", initTimer.GetDuration().GetSeconds(), initTimer.GetDuration().GetCycles()); initTimer.Start(); @@ -179,7 +179,7 @@ void Systems_Init() } initTimer.End(); - spdlog::info("Detour->Attach() '{:10.6f}' seconds ('{:12d}' clocks)\n", initTimer.GetDuration().GetSeconds(), initTimer.GetDuration().GetCycles()); + spdlog::info("{:16s} '{:10.6f}' seconds ('{:12d}' clocks)\n", "Detour->Attach()", initTimer.GetDuration().GetSeconds(), initTimer.GetDuration().GetCycles()); spdlog::info("+-------------------------------------------------------------+\n"); ConVar::Init(); @@ -219,7 +219,7 @@ void Systems_Shutdown() DetourTransactionCommit(); shutdownTimer.End(); - spdlog::info("Detour->Detach() '{:10.6f}' seconds ('{:12d}' clocks)\n", shutdownTimer.GetDuration().GetSeconds(), shutdownTimer.GetDuration().GetCycles()); + spdlog::info("{:16s} '{:10.6f}' seconds ('{:12d}' clocks)\n", "Detour->Detach()", shutdownTimer.GetDuration().GetSeconds(), shutdownTimer.GetDuration().GetCycles()); spdlog::info("+-------------------------------------------------------------+\n"); } @@ -240,7 +240,7 @@ void WinSock_Init() int nError = ::WSAStartup(MAKEWORD(2, 2), &wsaData); if (nError != 0) { - std::cerr << "Failed to start Winsock via WSAStartup: (" << NET_ErrorString(WSAGetLastError()) << ")" << std::endl; + spdlog::error("{:s}: Failed to start Winsock: ({:s})\n", __FUNCTION__, NET_ErrorString(WSAGetLastError())); } } void WinSock_Shutdown() @@ -248,7 +248,7 @@ void WinSock_Shutdown() int nError = ::WSACleanup(); if (nError != 0) { - std::cerr << "Failed to stop Winsock via WSACleanup: (" << NET_ErrorString(WSAGetLastError()) << ")" << std::endl; + spdlog::error("{:s}: Failed to stop Winsock: ({:s})\n", __FUNCTION__, NET_ErrorString(WSAGetLastError())); } } void QuerySystemInfo() @@ -404,6 +404,8 @@ void DetourRegister() // Register detour classes to be searched and hooked. // Launcher REGISTER(VPRX); REGISTER(VLauncher); + + REGISTER(VAppSystemGroup); REGISTER(VApplication); // FileSystem diff --git a/r5dev/windows/system.cpp b/r5dev/windows/system.cpp index 705a0850..b7c7d023 100644 --- a/r5dev/windows/system.cpp +++ b/r5dev/windows/system.cpp @@ -77,15 +77,10 @@ ConsoleHandlerRoutine( void WinSys_Init() { +#ifdef DEDICATED VGetVersionExA = (IGetVersionExA)DetourFindFunction("KERNEL32.dll", "GetVersionExA"); VPeekMessageA = (IPeekMessage)DetourFindFunction("USER32.dll", "PeekMessageA"); VPeekMessageW = (IPeekMessage)DetourFindFunction("USER32.dll", "PeekMessageW"); -} - -void WinSys_Attach() -{ -#ifdef DEDICATED - WinSys_Init(); /////////////////////////////////////////////////////////////////////////// DetourTransactionBegin(); @@ -106,7 +101,7 @@ void WinSys_Attach() #endif // DEDICATED } -void WinSys_Detach() +void WinSys_Shutdown() { #ifdef DEDICATED /////////////////////////////////////////////////////////////////////////// diff --git a/r5dev/windows/system.h b/r5dev/windows/system.h index 21926f85..8731c969 100644 --- a/r5dev/windows/system.h +++ b/r5dev/windows/system.h @@ -5,5 +5,5 @@ WINAPI ConsoleHandlerRoutine( DWORD eventCode); -void WinSys_Attach(); -void WinSys_Detach(); +void WinSys_Init(); +void WinSys_Shutdown();