2019-07-25 23:21:03 +02:00
|
|
|
#include "DX9_Hook.h"
|
2019-08-18 16:22:07 +02:00
|
|
|
#include "Windows_Hook.h"
|
|
|
|
#include "Hook_Manager.h"
|
|
|
|
#include "../dll/dll.h"
|
2019-08-14 14:55:31 +02:00
|
|
|
|
2019-08-14 15:09:57 +02:00
|
|
|
#ifndef NO_OVERLAY
|
2019-08-14 14:55:31 +02:00
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
#include <imgui.h>
|
|
|
|
#include <impls/imgui_impl_dx9.h>
|
|
|
|
|
2019-07-31 22:20:27 +02:00
|
|
|
#include "steam_overlay.h"
|
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
DX9_Hook* DX9_Hook::_inst = nullptr;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-16 19:10:12 +02:00
|
|
|
bool DX9_Hook::start_hook()
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
|
|
|
if (!_hooked)
|
|
|
|
{
|
2019-08-18 17:12:57 +02:00
|
|
|
if (!Windows_Hook::Inst().start_hook())
|
|
|
|
return false;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-20 23:27:17 +02:00
|
|
|
HWND hWnd = GetGameWindow();
|
|
|
|
if (!hWnd)
|
|
|
|
return false;
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
IDirect3D9Ex* pD3D;
|
2019-07-31 22:20:27 +02:00
|
|
|
IDirect3DDevice9Ex* pDeviceEx;
|
2019-08-18 16:19:28 +02:00
|
|
|
decltype(Direct3DCreate9Ex)* Direct3DCreate9Ex = (decltype(Direct3DCreate9Ex))GetProcAddress(reinterpret_cast<HMODULE>(_library), "Direct3DCreate9Ex");
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-16 10:31:29 +02:00
|
|
|
Direct3DCreate9Ex(D3D_SDK_VERSION, &pD3D);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
D3DPRESENT_PARAMETERS params = {};
|
|
|
|
params.BackBufferWidth = 1;
|
|
|
|
params.BackBufferHeight = 1;
|
2019-08-20 23:27:17 +02:00
|
|
|
params.hDeviceWindow = hWnd;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-07-31 22:20:27 +02:00
|
|
|
pD3D->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, ¶ms, NULL, &pDeviceEx);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-02 09:07:53 +02:00
|
|
|
if (pDeviceEx != nullptr)
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("Hooked DirectX 9\n");
|
2019-08-18 17:12:57 +02:00
|
|
|
|
|
|
|
_hooked = true;
|
|
|
|
Hook_Manager::Inst().FoundRenderer(this);
|
|
|
|
|
2019-08-02 09:07:53 +02:00
|
|
|
loadFunctions(pDeviceEx);
|
|
|
|
|
|
|
|
UnhookAll();
|
|
|
|
BeginHook();
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)Reset, &DX9_Hook::MyReset),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)Present, &DX9_Hook::MyPresent),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)PresentEx, &DX9_Hook::MyPresentEx)
|
|
|
|
//std::make_pair<void**, void*>(&(PVOID&)EndScene, &DX9_Hook::MyEndScene)
|
|
|
|
);
|
|
|
|
EndHook();
|
2019-08-18 16:22:07 +02:00
|
|
|
|
2019-08-18 17:12:57 +02:00
|
|
|
get_steam_client()->steam_overlay->HookReady();
|
2019-08-02 09:07:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("Failed to DirectX 9\n");
|
2019-08-16 19:10:12 +02:00
|
|
|
return false;
|
2019-08-02 09:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(pDeviceEx)pDeviceEx->Release();
|
|
|
|
if(pD3D)pD3D->Release();
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
2019-08-16 19:10:12 +02:00
|
|
|
return true;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DX9_Hook::resetRenderState()
|
|
|
|
{
|
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
initialized = false;
|
|
|
|
ImGui_ImplDX9_Shutdown();
|
2019-08-18 16:22:07 +02:00
|
|
|
Windows_Hook::Inst().resetRenderState();
|
2019-07-31 22:20:27 +02:00
|
|
|
ImGui::DestroyContext();
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 14:55:31 +02:00
|
|
|
// Try to make this function and overlay's proc as short as possible or it might affect game's fps.
|
2019-07-25 23:21:03 +02:00
|
|
|
void DX9_Hook::prepareForOverlay(IDirect3DDevice9 *pDevice)
|
|
|
|
{
|
|
|
|
IDirect3DSwapChain9* pSwapChain;
|
|
|
|
pDevice->GetSwapChain(0, &pSwapChain);
|
|
|
|
D3DPRESENT_PARAMETERS PresentParameters;
|
|
|
|
pSwapChain->GetPresentParameters(&PresentParameters);
|
|
|
|
pSwapChain->Release();
|
|
|
|
|
2019-07-31 22:20:27 +02:00
|
|
|
D3DDEVICE_CREATION_PARAMETERS param;
|
|
|
|
pDevice->GetCreationParameters(¶m);
|
|
|
|
|
2019-08-14 14:55:31 +02:00
|
|
|
// Workaround to detect if we changed window.
|
2019-08-18 16:22:07 +02:00
|
|
|
if (param.hFocusWindow != Windows_Hook::Inst().GetGameHwnd())
|
2019-07-31 22:20:27 +02:00
|
|
|
resetRenderState();
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.IniFilename = NULL;
|
|
|
|
|
|
|
|
ImGui_ImplDX9_Init(pDevice);
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui_ImplDX9_NewFrame();
|
2019-08-18 16:22:07 +02:00
|
|
|
Windows_Hook::Inst().prepareForOverlay(param.hFocusWindow);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
2019-08-18 16:22:07 +02:00
|
|
|
get_steam_client()->steam_overlay->OverlayProc(PresentParameters.BackBufferWidth, PresentParameters.BackBufferHeight);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
ImGui::EndFrame();
|
|
|
|
|
|
|
|
ImGui::Render();
|
|
|
|
|
|
|
|
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// DirectX 9 Initialization functions
|
2019-08-16 10:36:44 +02:00
|
|
|
//IDirect3D9* DX9_Hook::MyDirect3DCreate9(UINT SDKVersion)
|
|
|
|
//{
|
|
|
|
// auto res = hook->Direct3DCreate9(SDKVersion);
|
|
|
|
//
|
|
|
|
// if( res != nullptr )
|
|
|
|
// hook->hook_dx9(SDKVersion);
|
|
|
|
//
|
|
|
|
// return res;
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//HRESULT DX9_Hook::MyDirect3DCreate9Ex(UINT SDKVersion, IDirect3D9Ex** ppDevice)
|
|
|
|
//{
|
|
|
|
// auto res = hook->Direct3DCreate9Ex(SDKVersion, ppDevice);
|
|
|
|
//
|
|
|
|
// if (SUCCEEDED(res))
|
|
|
|
// hook->hook_dx9(SDKVersion);
|
|
|
|
//
|
|
|
|
// return res;
|
|
|
|
//}
|
2019-07-25 23:21:03 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX9_Hook::MyReset(IDirect3DDevice9* _this, D3DPRESENT_PARAMETERS* pPresentationParameters)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
DX9_Hook::Inst()->resetRenderState();
|
|
|
|
return (_this->*DX9_Hook::Inst()->Reset)(pPresentationParameters);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX9_Hook::MyEndScene(IDirect3DDevice9* _this)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
if( !DX9_Hook::Inst()->uses_present )
|
|
|
|
DX9_Hook::Inst()->prepareForOverlay(_this);
|
|
|
|
return (_this->*DX9_Hook::Inst()->EndScene)();
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX9_Hook::MyPresent(IDirect3DDevice9* _this, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
DX9_Hook::Inst()->uses_present = true;
|
|
|
|
DX9_Hook::Inst()->prepareForOverlay(_this);
|
|
|
|
return (_this->*DX9_Hook::Inst()->Present)(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX9_Hook::MyPresentEx(IDirect3DDevice9Ex* _this, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion, DWORD dwFlags)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
DX9_Hook::Inst()->uses_present = true;
|
|
|
|
DX9_Hook::Inst()->prepareForOverlay(_this);
|
|
|
|
return (_this->*DX9_Hook::Inst()->PresentEx)(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DX9_Hook::DX9_Hook():
|
|
|
|
initialized(false),
|
2019-07-31 22:20:27 +02:00
|
|
|
uses_present(false),
|
|
|
|
EndScene(nullptr),
|
|
|
|
Present(nullptr),
|
|
|
|
PresentEx(nullptr),
|
|
|
|
Reset(nullptr)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-18 16:19:28 +02:00
|
|
|
_library = LoadLibrary(DLL_NAME);
|
2019-07-25 23:21:03 +02:00
|
|
|
// Hook to Direct3DCreate9 and Direct3DCreate9Ex so we know when it gets called.
|
|
|
|
// If its called, then DX9 will be used to render the overlay.
|
2019-08-16 10:36:44 +02:00
|
|
|
//Direct3DCreate9 = (decltype(Direct3DCreate9))GetProcAddress(_dll, "Direct3DCreate9");
|
|
|
|
//Direct3DCreate9Ex = (decltype(Direct3DCreate9Ex))GetProcAddress(_dll, "Direct3DCreate9Ex");
|
|
|
|
//
|
|
|
|
//BeginHook();
|
|
|
|
//HookFuncs(
|
|
|
|
// std::make_pair<void**, void*>(&(PVOID&)Direct3DCreate9, &DX9_Hook::MyDirect3DCreate9),
|
|
|
|
// std::make_pair<void**, void*>(&(PVOID&)Direct3DCreate9Ex, &DX9_Hook::MyDirect3DCreate9Ex)
|
|
|
|
//);
|
|
|
|
//EndHook();
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DX9_Hook::~DX9_Hook()
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("DX9 Hook removed\n");
|
|
|
|
|
2019-08-18 16:19:28 +02:00
|
|
|
//resetRenderState();
|
|
|
|
if (initialized)
|
2019-08-01 17:05:41 +02:00
|
|
|
{
|
2019-08-01 19:07:19 +02:00
|
|
|
//ImGui_ImplDX9_Shutdown(); This makes some games hang when Releasing the D3D9 device (pDevice->Release())
|
|
|
|
// maybe because D3D is already shut down when we try to free the device?
|
2019-08-01 17:05:41 +02:00
|
|
|
ImGui_ImplDX9_InvalidateDeviceObjects();
|
2019-08-18 16:22:07 +02:00
|
|
|
Windows_Hook::Inst().resetRenderState();
|
2019-08-01 17:05:41 +02:00
|
|
|
ImGui::DestroyContext();
|
|
|
|
}
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-18 16:19:28 +02:00
|
|
|
FreeLibrary(reinterpret_cast<HMODULE>(_library));
|
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
_inst = nullptr;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
DX9_Hook* DX9_Hook::Inst()
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
if( _inst == nullptr )
|
|
|
|
_inst = new DX9_Hook;
|
|
|
|
|
|
|
|
return _inst;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2019-07-31 22:20:27 +02:00
|
|
|
void DX9_Hook::loadFunctions(IDirect3DDevice9Ex* pDeviceEx)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-07-31 22:20:27 +02:00
|
|
|
void** vTable = *reinterpret_cast<void***>(pDeviceEx);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
#define LOAD_FUNC(X) (void*&)X = vTable[(int)IDirect3DDevice9VTable::X]
|
|
|
|
LOAD_FUNC(Reset);
|
|
|
|
LOAD_FUNC(EndScene);
|
|
|
|
LOAD_FUNC(Present);
|
|
|
|
LOAD_FUNC(PresentEx);
|
|
|
|
#undef LOAD_FUNC
|
2019-08-14 14:55:31 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 15:09:57 +02:00
|
|
|
#endif//NO_OVERLAY
|