From b846809ac34f872fd584101be919c3742ec533ea Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Tue, 26 Jul 2022 00:36:35 +0200 Subject: [PATCH] Give process time to shutdown properly Game gets max 10 seconds to shutdown properly when closed directly by the external console window (g_pHostState->m_iNextState = HostStates_t::HS_SHUTDOWN). --- r5dev/windows/console.cpp | 2 ++ r5dev/windows/system.cpp | 23 +++++++++++++++++++++++ r5dev/windows/system.h | 5 +++++ 3 files changed, 30 insertions(+) diff --git a/r5dev/windows/console.cpp b/r5dev/windows/console.cpp index 89ad2ba1..8e2e3bcb 100644 --- a/r5dev/windows/console.cpp +++ b/r5dev/windows/console.cpp @@ -11,6 +11,7 @@ #ifndef DEDICATED #include "windows/id3dx.h" #endif // !DEDICATED +#include "windows/system.h" #include "windows/console.h" #include "common/opcodes.h" @@ -111,6 +112,7 @@ void Console_Init() SetConsoleBackgroundColor(0x0000); AnsiColors_Init(); } + SetConsoleCtrlHandler(ConsoleHandlerRoutine, true); } //############################################################################# diff --git a/r5dev/windows/system.cpp b/r5dev/windows/system.cpp index 283f8ab4..0e180b72 100644 --- a/r5dev/windows/system.cpp +++ b/r5dev/windows/system.cpp @@ -1,5 +1,6 @@ #include "core/stdafx.h" #include "windows/system.h" +#include "engine/host_state.h" /////////////////////////////////////////////////////////////////////////////// typedef BOOL(WINAPI* IGetVersionExA)( @@ -48,6 +49,28 @@ HPeekMessage( #endif // DEDICATED } +BOOL +WINAPI +ConsoleHandlerRoutine( + DWORD eventCode) +{ + switch (eventCode) + { + case CTRL_CLOSE_EVENT: + case CTRL_LOGOFF_EVENT: + case CTRL_SHUTDOWN_EVENT: + if (g_pHostState) + { + g_pHostState->m_iNextState = HostStates_t::HS_SHUTDOWN; + } + + Sleep(10000); + return TRUE; + } + + return FALSE; +} + //############################################################################# // MANAGEMENT //############################################################################# diff --git a/r5dev/windows/system.h b/r5dev/windows/system.h index a1622c31..21926f85 100644 --- a/r5dev/windows/system.h +++ b/r5dev/windows/system.h @@ -1,4 +1,9 @@ #pragma once +BOOL +WINAPI +ConsoleHandlerRoutine( + DWORD eventCode); + void WinSys_Attach(); void WinSys_Detach();