diff --git a/overlay_experimental/Base_Hook.cpp b/overlay_experimental/Base_Hook.cpp index 24d39b5..59d4762 100644 --- a/overlay_experimental/Base_Hook.cpp +++ b/overlay_experimental/Base_Hook.cpp @@ -36,5 +36,6 @@ void Base_Hook::UnhookAll() void Base_Hook::HookFuncs(std::pair hook) { - DetourAttach(hook.first, hook.second); + if( DetourAttach(hook.first, hook.second) == 0 ) + _hooked_funcs.emplace_back(hook); } \ No newline at end of file diff --git a/overlay_experimental/DX10_Hook.cpp b/overlay_experimental/DX10_Hook.cpp index 47d9bf8..e1f8e01 100644 --- a/overlay_experimental/DX10_Hook.cpp +++ b/overlay_experimental/DX10_Hook.cpp @@ -2,8 +2,6 @@ #include "DX10_Hook.h" #include "Hook_Manager.h" -#include "../detours/detours.h" - #include #include #include @@ -59,6 +57,8 @@ void DX10_Hook::resetRenderState() mainRenderTargetView->Release(); ImGui_ImplDX10_Shutdown(); + ImGui_ImplWin32_Shutdown(); + ImGui::DestroyContext(); initialized = false; } @@ -172,11 +172,7 @@ DX10_Hook::~DX10_Hook() PRINT_DEBUG("DX10 Hook removed\n"); if (_hooked) - { - ImGui_ImplDX10_Shutdown(); - ImGui_ImplWin32_Shutdown(); - ImGui::DestroyContext(); - } + resetRenderState(); hook = nullptr; } diff --git a/overlay_experimental/DX11_Hook.cpp b/overlay_experimental/DX11_Hook.cpp index 7c044d5..4492046 100644 --- a/overlay_experimental/DX11_Hook.cpp +++ b/overlay_experimental/DX11_Hook.cpp @@ -68,6 +68,8 @@ void DX11_Hook::resetRenderState() pContext->Release(); ImGui_ImplDX11_Shutdown(); + ImGui_ImplWin32_Shutdown(); + ImGui::DestroyContext(); initialized = false; } @@ -188,11 +190,7 @@ DX11_Hook::~DX11_Hook() PRINT_DEBUG("DX11 Hook removed\n"); if (_hooked) - { - ImGui_ImplDX11_Shutdown(); - ImGui_ImplWin32_Shutdown(); - ImGui::DestroyContext(); - } + resetRenderState(); hook = nullptr; } diff --git a/overlay_experimental/DX12_Hook.cpp b/overlay_experimental/DX12_Hook.cpp index 7e74c5b..ac3317b 100644 --- a/overlay_experimental/DX12_Hook.cpp +++ b/overlay_experimental/DX12_Hook.cpp @@ -26,6 +26,8 @@ void DX12_Hook::resetRenderState() pDescriptorHeap->Release(); ImGui_ImplDX12_Shutdown(); + ImGui_ImplWin32_Shutdown(); + ImGui::DestroyContext(); initialized = false; } @@ -35,7 +37,7 @@ void DX12_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain) { DXGI_SWAP_CHAIN_DESC desc; pSwapChain->GetDesc(&desc); - + if (!initialized) { D3D12_DESCRIPTOR_HEAP_DESC d3d12_desc = {}; @@ -149,11 +151,7 @@ DX12_Hook::~DX12_Hook() PRINT_DEBUG("DX11 Hook removed\n"); if (_hooked) - { - ImGui_ImplDX12_Shutdown(); - ImGui_ImplWin32_Shutdown(); - ImGui::DestroyContext(); - } + resetRenderState(); hook = nullptr; } diff --git a/overlay_experimental/DX9_Hook.cpp b/overlay_experimental/DX9_Hook.cpp index 210e301..15e6dc3 100644 --- a/overlay_experimental/DX9_Hook.cpp +++ b/overlay_experimental/DX9_Hook.cpp @@ -6,6 +6,8 @@ #include #include +#include "steam_overlay.h" + static DX9_Hook* hook; ////////////////////////////////////////////////////////////////// @@ -23,7 +25,7 @@ void DX9_Hook::hook_dx9(UINT SDKVersion) Hook_Manager::Inst().FoundHook(this); IDirect3D9Ex* pD3D; - IDirect3DDevice9Ex* pDevice; + IDirect3DDevice9Ex* pDeviceEx; Direct3DCreate9Ex(SDKVersion, &pD3D); @@ -32,21 +34,21 @@ void DX9_Hook::hook_dx9(UINT SDKVersion) params.BackBufferHeight = 1; params.hDeviceWindow = GetForegroundWindow(); - pD3D->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, ¶ms, NULL, &pDevice); + pD3D->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, ¶ms, NULL, &pDeviceEx); - loadFunctions(pDevice); + loadFunctions(pDeviceEx); - UnhookAll(); + //UnhookAll(); BeginHook(); HookFuncs( - //std::make_pair(&(PVOID&)Reset, &DX9_Hook::MyReset), + std::make_pair(&(PVOID&)Reset, &DX9_Hook::MyReset), std::make_pair(&(PVOID&)Present, &DX9_Hook::MyPresent), std::make_pair(&(PVOID&)PresentEx, &DX9_Hook::MyPresentEx) //std::make_pair(&(PVOID&)EndScene, &DX9_Hook::MyEndScene) ); EndHook(); - pDevice->Release(); + pDeviceEx->Release(); pD3D->Release(); } } @@ -56,8 +58,9 @@ void DX9_Hook::resetRenderState() if (initialized) { initialized = false; - ImGui_ImplDX9_Shutdown(); + ImGui_ImplWin32_Shutdown(); + ImGui::DestroyContext(); } } @@ -67,18 +70,20 @@ void DX9_Hook::prepareForOverlay(IDirect3DDevice9 *pDevice) pDevice->GetSwapChain(0, &pSwapChain); D3DPRESENT_PARAMETERS PresentParameters; pSwapChain->GetPresentParameters(&PresentParameters); - pSwapChain->Release(); + D3DDEVICE_CREATION_PARAMETERS param; + pDevice->GetCreationParameters(¶m); + + if (param.hFocusWindow != Hook_Manager::Inst().GetOverlay()->GetGameHwnd()) + resetRenderState(); + if (!initialized) { ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); io.IniFilename = NULL; - D3DDEVICE_CREATION_PARAMETERS param; - pDevice->GetCreationParameters(¶m); - Hook_Manager::Inst().ChangeGameWindow(param.hFocusWindow); ImGui_ImplWin32_Init(param.hFocusWindow); ImGui_ImplDX9_Init(pDevice); @@ -130,27 +135,32 @@ HRESULT STDMETHODCALLTYPE DX9_Hook::MyReset(IDirect3DDevice9* _this, D3DPRESENT_ HRESULT STDMETHODCALLTYPE DX9_Hook::MyEndScene(IDirect3DDevice9* _this) { - //if( !hook->uses_present ) + if( !hook->uses_present ) hook->prepareForOverlay(_this); return (_this->*hook->EndScene)(); } HRESULT STDMETHODCALLTYPE DX9_Hook::MyPresent(IDirect3DDevice9* _this, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion) { + hook->uses_present = true; hook->prepareForOverlay(_this); return (_this->*hook->Present)(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion); } HRESULT STDMETHODCALLTYPE DX9_Hook::MyPresentEx(IDirect3DDevice9Ex* _this, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion, DWORD dwFlags) { + hook->uses_present = true; hook->prepareForOverlay(_this); return (_this->*hook->PresentEx)(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags); } - DX9_Hook::DX9_Hook(): initialized(false), - uses_present(false) + uses_present(false), + EndScene(nullptr), + Present(nullptr), + PresentEx(nullptr), + Reset(nullptr) { _dll = GetModuleHandle(DLL_NAME); _hooked = false; @@ -172,11 +182,7 @@ DX9_Hook::~DX9_Hook() PRINT_DEBUG("DX9 Hook removed\n"); if (_hooked) - { - ImGui_ImplDX9_Shutdown(); - ImGui_ImplWin32_Shutdown(); - ImGui::DestroyContext(); - } + resetRenderState(); hook = nullptr; } @@ -191,9 +197,9 @@ void DX9_Hook::Create() } } -void DX9_Hook::loadFunctions(IDirect3DDevice9Ex* obj) +void DX9_Hook::loadFunctions(IDirect3DDevice9Ex* pDeviceEx) { - void** vTable = *reinterpret_cast(obj); + void** vTable = *reinterpret_cast(pDeviceEx); #define LOAD_FUNC(X) (void*&)X = vTable[(int)IDirect3DDevice9VTable::X] LOAD_FUNC(Reset); diff --git a/overlay_experimental/DX9_Hook.h b/overlay_experimental/DX9_Hook.h index 18b8ffb..8a98404 100644 --- a/overlay_experimental/DX9_Hook.h +++ b/overlay_experimental/DX9_Hook.h @@ -44,7 +44,7 @@ private: public: static void Create(); // Initialize DX9 Hook. - void loadFunctions(IDirect3DDevice9Ex *obj); + void loadFunctions(IDirect3DDevice9Ex *pDeviceEx); }; #endif//__INCLUDED_DX9_HOOK_H__ \ No newline at end of file