Fix rare ImGui crash on shutdown

The 'DirectX_Init()' call was performed late in code, shortly after the window has been created (at this point all device objects and window handles are valid), but the 'DirectX_Shutdown()' call was performed on DLL_DETACH, which was way too late, as the objects were already destroyed at this point. This wasn't an issue before, as we created our own objects in the old DX code. But due to optimizations, we were using the same pointers as the game (noticeable performance boost), but did not adjust the shutdown to accommodate the changes. The shutdown is now performed while the device objects and window handles are valid. Code has been tested on Nvidia and AMD systems, and has confirmed to fix the aforementioned issues.
This commit is contained in:
Kawe Mazidjatari 2023-06-18 22:16:43 +02:00
parent d5039de251
commit 165d80c541
5 changed files with 52 additions and 31 deletions

View File

@ -88,7 +88,6 @@ class VOpcodes : public IDetour
// LogFunAdr("Sys_InitGame", Sys_InitGame.GetPtr());
// LogFunAdr("Host_Init_1", gHost_Init_1.GetPtr());
// LogFunAdr("Host_Init_2", gHost_Init_2.GetPtr());
// LogFunAdr("Host_Disconnect", Host_Disconnect.GetPtr());
#ifndef CLIENT_DLL
LogFunAdr("Server_S2C_CONNECT", Server_S2C_CONNECT_1.GetPtr());
#endif // !CLIENT_DLL
@ -149,17 +148,6 @@ class VOpcodes : public IDetour
// // 0x140236640 // 88 4C 24 08 53 55 56 57 48 83 EC 68 //
//
// //-------------------------------------------------------------------------
// Host_Shutdown = g_GameDll.FindPatternSIMD("48 8B C4 48 83 EC ?? 80 3D ?? ?? ?? ?? ?? 0F 85 ?? ?? ?? ?? 8B 15 ?? ?? ?? ??");
// // 0x140239620 // 48 8B C4 48 83 EC ?? 80 3D ? ? ? ? ? 0F 85 ? ? ? ? 8B 15 ? ? ? ? //
//
// //-------------------------------------------------------------------------
//#if defined (GAMEDLL_S0) || defined (GAMEDLL_S1)
// Host_Disconnect = g_GameDll.FindPatternSIMD("48 83 EC 38 48 89 7C 24 ?? 0F B6 F9");
//#elif defined (GAMEDLL_S2) || defined (GAMEDLL_S3)
// Host_Disconnect = g_GameDll.FindPatternSIMD("40 53 48 83 EC 30 0F B6 D9");
//#endif // 0x14023CCA0 // 40 53 48 83 EC 30 0F B6 D9 //
//
// //-------------------------------------------------------------------------
#ifndef CLIENT_DLL
Server_S2C_CONNECT_1 = g_GameDll.FindPatternSIMD("48 3B 05 ?? ?? ?? ?? 74 0C");
#endif // !CLIENT_DLL

View File

@ -132,7 +132,6 @@ void SDK_Shutdown()
#ifndef DEDICATED
Input_Shutdown();
DirectX_Shutdown();
#endif // !DEDICATED
Console_Shutdown();

View File

@ -3,31 +3,32 @@
#include "host_cmd.h"
#include "common.h"
#include "client/client.h"
#ifndef DEDICATED
#include "windows/id3dx.h"
#endif // !DEDICATED
#if !defined (GAMEDLL_S0) && !defined (GAMEDLL_S1) && !defined (GAMEDLL_S2)
/*
==================
DFS_InitializeFeatureFlagDefinitions
Host_Shutdown
Initialize feature
flag definitions
shutdown host
systems
==================
*/
bool DFS_InitializeFeatureFlagDefinitions(const char* pszFeatureFlags)
void Host_Shutdown()
{
if (CommandLine()->CheckParm("-nodfs"))
return false;
return v_DFS_InitializeFeatureFlagDefinitions(pszFeatureFlags);
#ifndef DEDICATED
DirectX_Shutdown();
#endif // DEDICATED
v_Host_Shutdown();
}
#endif // !(GAMEDLL_S0) || !(GAMEDLL_S1) || !(GAMEDLL_S2)
/*
==================
Host_Status_PrintClient
Print client info
to console
Print client info
to console
==================
*/
void Host_Status_PrintClient(CClient* client, bool bShowAddress, void (*print) (const char* fmt, ...))
@ -61,21 +62,41 @@ void Host_Status_PrintClient(CClient* client, bool bShowAddress, void (*print) (
//print("\n");
}
#if !defined (GAMEDLL_S0) && !defined (GAMEDLL_S1) && !defined (GAMEDLL_S2)
/*
==================
DFS_InitializeFeatureFlagDefinitions
Initialize feature
flag definitions
==================
*/
bool DFS_InitializeFeatureFlagDefinitions(const char* pszFeatureFlags)
{
if (CommandLine()->CheckParm("-nodfs"))
return false;
return v_DFS_InitializeFeatureFlagDefinitions(pszFeatureFlags);
}
#endif // !(GAMEDLL_S0) || !(GAMEDLL_S1) || !(GAMEDLL_S2)
///////////////////////////////////////////////////////////////////////////////
void VHostCmd::Attach() const
{
DetourAttach(&v_Host_Shutdown, &Host_Shutdown);
DetourAttach(&v_Host_Status_PrintClient, &Host_Status_PrintClient);
#if !defined (GAMEDLL_S0) && !defined (GAMEDLL_S1) && !defined (GAMEDLL_S2)
DetourAttach(&v_DFS_InitializeFeatureFlagDefinitions, &DFS_InitializeFeatureFlagDefinitions);
#endif // !(GAMEDLL_S0) || !(GAMEDLL_S1) || !(GAMEDLL_S2)
DetourAttach(&v_Host_Status_PrintClient, &Host_Status_PrintClient);
}
void VHostCmd::Detach() const
{
DetourDetach(&v_Host_Shutdown, &Host_Shutdown);
DetourDetach(&v_Host_Status_PrintClient, &Host_Status_PrintClient);
#if !defined (GAMEDLL_S0) && !defined (GAMEDLL_S1) && !defined (GAMEDLL_S2)
DetourDetach(&v_DFS_InitializeFeatureFlagDefinitions, &DFS_InitializeFeatureFlagDefinitions);
#endif // !(GAMEDLL_S0) || !(GAMEDLL_S1) || !(GAMEDLL_S2)
DetourDetach(&v_Host_Status_PrintClient, &Host_Status_PrintClient);
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -21,9 +21,15 @@ extern EngineParms_t* g_pEngineParms;
inline CMemory p_Host_Init;
inline auto v_Host_Init = p_Host_Init.RCast<void* (*)(bool* bDedicated)>();
inline CMemory p_Host_Shutdown;
inline auto v_Host_Shutdown = p_Host_Shutdown.RCast<void (*)()>();
inline CMemory p_Host_NewGame;
inline auto v_Host_NewGame = p_Host_NewGame.RCast<bool (*)(char* pszMapName, char* pszMapGroup, bool bLoadGame, char bBackground, LARGE_INTEGER PerformanceCount)>();
inline CMemory p_Host_Disconnect;
inline auto v_Host_Disconnect = p_Host_Disconnect.RCast<void (*)(bool bShowMainMenu)>();
inline CMemory p_Host_ChangeLevel;
inline auto v_Host_ChangeLevel = p_Host_ChangeLevel.RCast<bool (*)(bool bLoadFromSavedGame, const char* pszMapName, const char* pszMapGroup)>();
@ -46,6 +52,8 @@ class VHostCmd : public IDetour
virtual void GetAdr(void) const
{
LogFunAdr("Host_Init", p_Host_Init.GetPtr());
LogFunAdr("Host_Shutdown", p_Host_Shutdown.GetPtr());
LogFunAdr("Host_Disconnect", p_Host_Disconnect.GetPtr());
LogFunAdr("Host_NewGame", p_Host_NewGame.GetPtr());
LogFunAdr("Host_ChangeLevel", p_Host_ChangeLevel.GetPtr());
LogFunAdr("Host_Status_PrintClient", p_Host_Status_PrintClient.GetPtr());
@ -59,22 +67,27 @@ class VHostCmd : public IDetour
{
#if defined (GAMEDLL_S0) || defined (GAMEDLL_S1)
p_Host_Init = g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 48 89 6C 24 ?? 48 89 74 24 ?? 57 41 54 41 55 41 56 41 57 48 81 EC ?? ?? ?? ?? 48 8B D9 FF 15 ?? ?? ?? ??");
p_Host_NewGame = g_GameDll.FindPatternSIMD("48 8B C4 56 41 54 41 57 48 81 EC ?? ?? ?? ?? F2 0F 10 05 ?? ?? ?? ??"); /*48 8B C4 56 41 54 41 57 48 81 EC ? ? ? ? F2 0F 10 05 ? ? ? ?*/
p_Host_NewGame = g_GameDll.FindPatternSIMD("48 8B C4 56 41 54 41 57 48 81 EC ?? ?? ?? ?? F2 0F 10 05 ?? ?? ?? ??");
p_Host_Disconnect = g_GameDll.FindPatternSIMD("48 83 EC 38 48 89 7C 24 ?? 0F B6 F9");
p_Host_ChangeLevel = g_GameDll.FindPatternSIMD("40 53 56 41 56 48 81 EC ?? ?? ?? ?? 49 8B D8");
p_SetLaunchOptions = g_GameDll.FindPatternSIMD("48 89 6C 24 ?? 57 48 83 EC 20 48 8B E9 48 8B 0D ?? ?? ?? ??");
#elif defined (GAMEDLL_S2) || defined (GAMEDLL_S3)
p_Host_Init = g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 48 89 74 24 ?? 48 89 7C 24 ?? 55 41 54 41 55 41 56 41 57 48 8D AC 24 ?? ?? ?? ?? B8 ?? ?? ?? ?? E8 ?? ?? ?? ?? 48 2B E0 48 8B D9");
p_Host_NewGame = g_GameDll.FindPatternSIMD("48 8B C4 ?? 41 54 41 55 48 81 EC 70 04 ?? ?? F2 0F 10 05 ?? ?? ?? 0B");
p_Host_Disconnect = g_GameDll.FindPatternSIMD("40 53 48 83 EC 30 0F B6 D9");
p_Host_ChangeLevel = g_GameDll.FindPatternSIMD("40 56 57 41 56 48 81 EC ?? ?? ?? ??");
p_SetLaunchOptions = g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 48 89 6C 24 ?? 57 48 83 EC 20 48 8B 1D ?? ?? ?? ?? 48 8B E9 48 85 DB");
#endif
p_Host_Shutdown = g_GameDll.FindPatternSIMD("48 8B C4 48 83 EC ?? 80 3D ?? ?? ?? ?? ?? 0F 85 ?? ?? ?? ?? 8B 15 ?? ?? ?? ??");
p_Host_Status_PrintClient = g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 48 89 6C 24 ?? 56 57 41 56 48 83 EC 60 48 8B A9 ?? ?? ?? ??");
#if !defined (GAMEDLL_S0) && !defined (GAMEDLL_S1) && !defined (GAMEDLL_S2)
p_DFS_InitializeFeatureFlagDefinitions = g_GameDll.FindPatternSIMD("E8 ?? ?? ?? ?? 40 38 3D ?? ?? ?? ?? 48 8D 15 ?? ?? ?? ?? 48 8B CE").FollowNearCallSelf();
v_DFS_InitializeFeatureFlagDefinitions = p_DFS_InitializeFeatureFlagDefinitions.RCast<bool (*)(const char*)>(); /*48 8B C4 55 53 48 8D 68 E8*/
#endif // !(GAMEDLL_S0) || !(GAMEDLL_S1) || !(GAMEDLL_S2)
v_Host_Init = p_Host_Init.RCast<void* (*)(bool*)>(); /*48 89 5C 24 ?? 48 89 74 24 ?? 48 89 7C 24 ?? 55 41 54 41 55 41 56 41 57 48 8D AC 24 ?? ?? ?? ?? B8 ?? ?? ?? ?? E8 ?? ?? ?? ?? 48 2B E0 48 8B D9*/
v_Host_Shutdown = p_Host_Shutdown.RCast<void (*)()>();
v_Host_NewGame = p_Host_NewGame.RCast<bool (*)(char*, char*, bool, char, LARGE_INTEGER)>(); /*48 8B C4 ?? 41 54 41 55 48 81 EC 70 04 00 00 F2 0F 10 05 ?? ?? ?? 0B*/
v_Host_Disconnect = p_Host_Disconnect.RCast<void (*)(bool)>();
v_Host_ChangeLevel = p_Host_ChangeLevel.RCast<bool (*)(bool, const char*, const char*)>(); /*40 56 57 41 56 48 81 EC ?? ?? ?? ??*/
v_Host_Status_PrintClient = p_Host_Status_PrintClient.RCast<void (*)(CClient*, bool, void (*) (const char*, ...))>();
v_SetLaunchOptions = p_SetLaunchOptions.RCast<int (*)(const CCommand&)>(); /*48 89 5C 24 ?? 48 89 6C 24 ?? 57 48 83 EC 20 48 8B 1D ?? ?? ?? ?? 48 8B E9 48 85 DB*/

View File

@ -83,13 +83,13 @@ void ImGui_Init()
///////////////////////////////////////////////////////////////////////////////
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui_ImplWin32_Init(*g_pGameWindow);
ImGui_ImplDX11_Init(*g_ppGameDevice, *g_ppImmediateContext);
///////////////////////////////////////////////////////////////////////////////
ImGuiIO& io = ImGui::GetIO();
io.ImeWindowHandle = *g_pGameWindow;
io.ConfigFlags |= ImGuiConfigFlags_IsSRGB;
ImGui_ImplWin32_Init(*g_pGameWindow);
ImGui_ImplDX11_Init(*g_ppGameDevice, *g_ppImmediateContext);
}
void ImGui_Shutdown()