2019-07-25 23:21:03 +02:00
|
|
|
#include "DX11_Hook.h"
|
2019-08-18 16:22:07 +02:00
|
|
|
#include "Windows_Hook.h"
|
2019-07-25 23:21:03 +02:00
|
|
|
#include "Hook_Manager.h"
|
2019-08-18 16:22:07 +02:00
|
|
|
#include "../dll/dll.h"
|
|
|
|
|
|
|
|
#ifndef NO_OVERLAY
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <impls/imgui_impl_dx11.h>
|
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
DX11_Hook* DX11_Hook::_inst = nullptr;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
HRESULT GetDeviceAndCtxFromSwapchain(IDXGISwapChain* pSwapChain, ID3D11Device** ppDevice, ID3D11DeviceContext** ppContext)
|
|
|
|
{
|
2019-08-19 19:51:35 +02:00
|
|
|
HRESULT ret = pSwapChain->GetDevice(IID_PPV_ARGS(ppDevice));
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
if (SUCCEEDED(ret))
|
|
|
|
(*ppDevice)->GetImmediateContext(ppContext);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-08-16 19:10:12 +02:00
|
|
|
bool DX11_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
|
|
|
IDXGISwapChain* pSwapChain;
|
|
|
|
ID3D11Device* pDevice;
|
|
|
|
DXGI_SWAP_CHAIN_DESC SwapChainDesc = {};
|
2019-08-16 10:36:44 +02:00
|
|
|
decltype(D3D11CreateDeviceAndSwapChain)* D3D11CreateDeviceAndSwapChain =
|
2019-08-18 16:22:07 +02:00
|
|
|
(decltype(D3D11CreateDeviceAndSwapChain))GetProcAddress(reinterpret_cast<HMODULE>(_library), "D3D11CreateDeviceAndSwapChain");
|
2019-08-02 09:07:53 +02:00
|
|
|
SwapChainDesc.BufferCount = 1;
|
|
|
|
SwapChainDesc.BufferDesc.Width = 1;
|
|
|
|
SwapChainDesc.BufferDesc.Height = 1;
|
2019-07-25 23:21:03 +02:00
|
|
|
SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
2019-08-02 09:07:53 +02:00
|
|
|
SwapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
|
|
|
|
SwapChainDesc.BufferDesc.RefreshRate.Denominator = 0;
|
2019-07-25 23:21:03 +02:00
|
|
|
SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
2019-08-20 23:27:17 +02:00
|
|
|
SwapChainDesc.OutputWindow = hWnd;
|
2019-07-25 23:21:03 +02:00
|
|
|
SwapChainDesc.SampleDesc.Count = 1;
|
|
|
|
SwapChainDesc.SampleDesc.Quality = 0;
|
|
|
|
SwapChainDesc.Windowed = TRUE;
|
|
|
|
|
2019-08-16 18:29:49 +02:00
|
|
|
D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_NULL, NULL, 0, NULL, NULL, D3D11_SDK_VERSION, &SwapChainDesc, &pSwapChain, &pDevice, NULL, NULL);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-02 09:07:53 +02:00
|
|
|
if (pDevice != nullptr && pSwapChain != nullptr)
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("Hooked DirectX 11\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(pDevice, pSwapChain);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-02 09:07:53 +02:00
|
|
|
UnhookAll();
|
|
|
|
BeginHook();
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX11_Hook::Present, &DX11_Hook::MyPresent),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX11_Hook::ResizeTarget, &DX11_Hook::MyResizeTarget),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX11_Hook::ResizeBuffers, &DX11_Hook::MyResizeBuffers)
|
|
|
|
);
|
|
|
|
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 hook DirectX 11\n");
|
2019-08-16 19:10:12 +02:00
|
|
|
return false;
|
2019-08-02 09:07:53 +02:00
|
|
|
}
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-02 09:07:53 +02:00
|
|
|
if(pDevice) pDevice->Release();
|
|
|
|
if(pSwapChain) pSwapChain->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 DX11_Hook::resetRenderState()
|
|
|
|
{
|
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
mainRenderTargetView->Release();
|
|
|
|
pContext->Release();
|
|
|
|
|
|
|
|
ImGui_ImplDX11_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
|
|
|
|
|
|
|
initialized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 DX11_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
|
|
|
|
{
|
|
|
|
DXGI_SWAP_CHAIN_DESC desc;
|
|
|
|
pSwapChain->GetDesc(&desc);
|
|
|
|
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
ID3D11Device* pDevice = nullptr;
|
|
|
|
if (FAILED(GetDeviceAndCtxFromSwapchain(pSwapChain, &pDevice, &pContext)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.IniFilename = NULL;
|
|
|
|
|
|
|
|
ID3D11Texture2D* pBackBuffer;
|
|
|
|
|
2019-08-19 19:51:35 +02:00
|
|
|
pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
|
2019-07-25 23:21:03 +02:00
|
|
|
pDevice->CreateRenderTargetView(pBackBuffer, NULL, &mainRenderTargetView);
|
|
|
|
pBackBuffer->Release();
|
|
|
|
|
|
|
|
ImGui_ImplDX11_Init(pDevice, pContext);
|
2019-08-16 18:30:55 +02:00
|
|
|
|
2019-08-19 19:51:35 +02:00
|
|
|
pDevice->Release();
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui_ImplDX11_NewFrame();
|
2019-08-18 16:22:07 +02:00
|
|
|
Windows_Hook::Inst().prepareForOverlay(desc.OutputWindow);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
2019-08-18 16:22:07 +02:00
|
|
|
get_steam_client()->steam_overlay->OverlayProc(desc.BufferDesc.Width, desc.BufferDesc.Height);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
ImGui::EndFrame();
|
|
|
|
|
|
|
|
ImGui::Render();
|
|
|
|
|
|
|
|
pContext->OMSetRenderTargets(1, &mainRenderTargetView, NULL);
|
|
|
|
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// DirectX 11 Initialization functions
|
|
|
|
//HRESULT WINAPI DX11_Hook::MyD3D11CreateDevice(__in_opt IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags,
|
|
|
|
// __in_ecount_opt(FeatureLevels) CONST D3D_FEATURE_LEVEL* pFeatureLevels, UINT FeatureLevels, UINT SDKVersion, __out_opt ID3D11Device** ppDevice,
|
|
|
|
// __out_opt D3D_FEATURE_LEVEL* pFeatureLevel, __out_opt ID3D11DeviceContext** ppImmediateContext)
|
|
|
|
//{
|
2019-08-16 00:12:11 +02:00
|
|
|
// auto res = D3D11CreateDevice(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels, SDKVersion, ppDevice, pFeatureLevel, ppImmediateContext);
|
2019-07-25 23:21:03 +02:00
|
|
|
//
|
|
|
|
// if (SUCCEEDED(res))
|
|
|
|
// hook->hook_dx11(SDKVersion);
|
|
|
|
//
|
|
|
|
// return res;
|
|
|
|
//}
|
|
|
|
|
2019-08-16 10:36:44 +02:00
|
|
|
//HRESULT WINAPI DX11_Hook::MyD3D11CreateDeviceAndSwapChain(__in_opt IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags,
|
|
|
|
// __in_ecount_opt(FeatureLevels) CONST D3D_FEATURE_LEVEL* pFeatureLevels, UINT FeatureLevels, UINT SDKVersion,
|
|
|
|
// __in_opt CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc, __out_opt IDXGISwapChain** ppSwapChain, __out_opt ID3D11Device** ppDevice,
|
|
|
|
// __out_opt D3D_FEATURE_LEVEL* pFeatureLevel, __out_opt ID3D11DeviceContext** ppImmediateContext)
|
|
|
|
//{
|
|
|
|
// auto res = hook->D3D11CreateDeviceAndSwapChain(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels, SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
|
|
|
|
//
|
|
|
|
// if (SUCCEEDED(res))
|
|
|
|
// hook->hook_dx11(SDKVersion);
|
|
|
|
//
|
|
|
|
// return res;
|
|
|
|
//}
|
2019-07-25 23:21:03 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX11_Hook::MyPresent(IDXGISwapChain *_this, UINT SyncInterval, UINT Flags)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
DX11_Hook::Inst()->prepareForOverlay(_this);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
return (_this->*DX11_Hook::Inst()->Present)(SyncInterval, Flags);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX11_Hook::MyResizeTarget(IDXGISwapChain* _this, const DXGI_MODE_DESC* pNewTargetParameters)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
DX11_Hook::Inst()->resetRenderState();
|
|
|
|
return (_this->*DX11_Hook::Inst()->ResizeTarget)(pNewTargetParameters);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX11_Hook::MyResizeBuffers(IDXGISwapChain* _this, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags)
|
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
DX11_Hook::Inst()->resetRenderState();
|
|
|
|
return (_this->*DX11_Hook::Inst()->ResizeBuffers)(BufferCount, Width, Height, NewFormat, SwapChainFlags);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DX11_Hook::DX11_Hook():
|
|
|
|
initialized(false),
|
|
|
|
pContext(nullptr),
|
|
|
|
mainRenderTargetView(nullptr)
|
|
|
|
{
|
2019-08-18 16:19:28 +02:00
|
|
|
_library = LoadLibrary(DLL_NAME);
|
2019-08-16 10:28:23 +02:00
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
// Hook to D3D11CreateDevice and D3D11CreateDeviceAndSwapChain so we know when it gets called.
|
|
|
|
// If its called, then DX11 will be used to render the overlay.
|
2019-08-16 00:12:11 +02:00
|
|
|
//D3D11CreateDevice = (decltype(D3D11CreateDevice))GetProcAddress(_dll, "D3D11CreateDevice");
|
2019-08-16 10:36:44 +02:00
|
|
|
//D3D11CreateDeviceAndSwapChain = (decltype(D3D11CreateDeviceAndSwapChain))GetProcAddress(_dll, "D3D11CreateDeviceAndSwapChain");
|
|
|
|
//
|
|
|
|
//BeginHook();
|
|
|
|
//HookFuncs(
|
|
|
|
// //std::make_pair<void**, void*>(&(PVOID&)D3D11CreateDevice, &DX11_Hook::MyD3D11CreateDevice),
|
|
|
|
// std::make_pair<void**, void*>(&(PVOID&)D3D11CreateDeviceAndSwapChain, &DX11_Hook::MyD3D11CreateDeviceAndSwapChain)
|
|
|
|
//);
|
|
|
|
//EndHook();
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DX11_Hook::~DX11_Hook()
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("DX11 Hook removed\n");
|
|
|
|
|
2019-08-18 16:19:28 +02:00
|
|
|
resetRenderState();
|
|
|
|
|
|
|
|
FreeLibrary(reinterpret_cast<HMODULE>(_library));
|
2019-07-25 23:21:03 +02:00
|
|
|
|
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
|
|
|
DX11_Hook* DX11_Hook::Inst()
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
if (_inst == nullptr)
|
|
|
|
_inst = new DX11_Hook;
|
|
|
|
|
|
|
|
return _inst;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DX11_Hook::loadFunctions(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain)
|
|
|
|
{
|
|
|
|
void** vTable = *reinterpret_cast<void***>(pDevice);
|
|
|
|
|
|
|
|
#define LOAD_FUNC(X) (void*&)X = vTable[(int)ID3D11DeviceVTable::X]
|
|
|
|
|
|
|
|
#undef LOAD_FUNC
|
|
|
|
|
|
|
|
vTable = *reinterpret_cast<void***>(pSwapChain);
|
|
|
|
#define LOAD_FUNC(X) (void*&)X = vTable[(int)IDXGISwapChainVTable::X]
|
|
|
|
LOAD_FUNC(Present);
|
|
|
|
LOAD_FUNC(ResizeBuffers);
|
|
|
|
LOAD_FUNC(ResizeTarget);
|
|
|
|
#undef LOAD_FUNC
|
2019-08-14 14:55:31 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 15:09:57 +02:00
|
|
|
#endif//NO_OVERLAY
|