2019-07-25 21:21:03 +00:00
|
|
|
#include "DX10_Hook.h"
|
2019-08-18 14:22:07 +00:00
|
|
|
#include "Windows_Hook.h"
|
2019-07-25 21:21:03 +00:00
|
|
|
#include "Hook_Manager.h"
|
2019-08-18 14:22:07 +00:00
|
|
|
#include "../dll/dll.h"
|
|
|
|
|
|
|
|
#ifndef NO_OVERLAY
|
2019-07-25 21:21:03 +00:00
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <impls/imgui_impl_dx10.h>
|
|
|
|
|
2019-08-16 17:21:30 +00:00
|
|
|
DX10_Hook* DX10_Hook::_inst = nullptr;
|
2019-07-25 21:21:03 +00:00
|
|
|
|
2019-08-16 17:10:12 +00:00
|
|
|
bool DX10_Hook::start_hook()
|
2019-07-25 21:21:03 +00:00
|
|
|
{
|
2019-08-25 19:22:25 +00:00
|
|
|
bool res = true;
|
2019-07-25 21:21:03 +00:00
|
|
|
if (!_hooked)
|
|
|
|
{
|
2019-08-18 15:12:57 +00:00
|
|
|
if (!Windows_Hook::Inst().start_hook())
|
|
|
|
return false;
|
2019-07-25 21:21:03 +00:00
|
|
|
|
2019-08-26 14:38:01 +00:00
|
|
|
PRINT_DEBUG("Hooked DirectX 10\n");
|
|
|
|
_hooked = true;
|
|
|
|
|
|
|
|
Hook_Manager::Inst().FoundRenderer(this);
|
|
|
|
|
|
|
|
UnhookAll();
|
|
|
|
BeginHook();
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX10_Hook::Present, &DX10_Hook::MyPresent),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX10_Hook::ResizeTarget, &DX10_Hook::MyResizeTarget),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)DX10_Hook::ResizeBuffers, &DX10_Hook::MyResizeBuffers)
|
|
|
|
);
|
|
|
|
EndHook();
|
2019-08-20 21:27:17 +00:00
|
|
|
|
2019-08-26 14:38:01 +00:00
|
|
|
get_steam_client()->steam_overlay->HookReady();
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
2019-08-25 19:22:25 +00:00
|
|
|
return res;
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DX10_Hook::resetRenderState()
|
|
|
|
{
|
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
mainRenderTargetView->Release();
|
|
|
|
|
|
|
|
ImGui_ImplDX10_Shutdown();
|
2019-08-18 14:22:07 +00:00
|
|
|
Windows_Hook::Inst().resetRenderState();
|
2019-07-31 20:20:27 +00:00
|
|
|
ImGui::DestroyContext();
|
2019-07-25 21:21:03 +00:00
|
|
|
|
|
|
|
initialized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 12:55:31 +00:00
|
|
|
// Try to make this function and overlay's proc as short as possible or it might affect game's fps.
|
2019-07-25 21:21:03 +00:00
|
|
|
void DX10_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
|
|
|
|
{
|
|
|
|
DXGI_SWAP_CHAIN_DESC desc;
|
|
|
|
pSwapChain->GetDesc(&desc);
|
|
|
|
|
|
|
|
if (!initialized)
|
|
|
|
{
|
2019-08-19 17:51:35 +00:00
|
|
|
if (FAILED(pSwapChain->GetDevice(IID_PPV_ARGS(&pDevice))))
|
2019-07-25 21:21:03 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.IniFilename = NULL;
|
|
|
|
|
|
|
|
ID3D10Texture2D* pBackBuffer;
|
|
|
|
|
2019-08-19 17:51:35 +00:00
|
|
|
pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
|
2019-07-25 21:21:03 +00:00
|
|
|
pDevice->CreateRenderTargetView(pBackBuffer, NULL, &mainRenderTargetView);
|
|
|
|
pBackBuffer->Release();
|
|
|
|
|
|
|
|
ImGui_ImplDX10_Init(pDevice);
|
2019-08-16 16:30:55 +00:00
|
|
|
|
2019-08-19 17:51:35 +00:00
|
|
|
pDevice->Release();
|
|
|
|
|
2019-07-25 21:21:03 +00:00
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui_ImplDX10_NewFrame();
|
2019-08-18 14:22:07 +00:00
|
|
|
Windows_Hook::Inst().prepareForOverlay(desc.OutputWindow);
|
2019-07-25 21:21:03 +00:00
|
|
|
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
2019-08-18 14:22:07 +00:00
|
|
|
get_steam_client()->steam_overlay->OverlayProc(desc.BufferDesc.Width, desc.BufferDesc.Height);
|
2019-07-25 21:21:03 +00:00
|
|
|
|
|
|
|
ImGui::EndFrame();
|
|
|
|
|
|
|
|
ImGui::Render();
|
|
|
|
|
|
|
|
pDevice->OMSetRenderTargets(1, &mainRenderTargetView, NULL);
|
|
|
|
ImGui_ImplDX10_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// DirectX 10 Initialization functions
|
|
|
|
//HRESULT WINAPI DX10_Hook::MyD3D10CreateDevice(IDXGIAdapter* pAdapter, D3D10_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, UINT SDKVersion, ID3D10Device** ppDevice)
|
|
|
|
//{
|
|
|
|
// auto res = _D3D10CreateDevice(pAdapter, DriverType, Software, Flags, SDKVersion, ppDevice);
|
|
|
|
//
|
|
|
|
// if (SUCCEEDED(res))
|
|
|
|
// hook->hook_dx10(SDKVersion);
|
|
|
|
//
|
|
|
|
// return res;
|
|
|
|
//}
|
|
|
|
|
2019-08-16 08:36:44 +00:00
|
|
|
//HRESULT WINAPI DX10_Hook::MyD3D10CreateDeviceAndSwapChain(IDXGIAdapter* pAdapter, D3D10_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, UINT SDKVersion,
|
|
|
|
// DXGI_SWAP_CHAIN_DESC* pSwapChainDesc, IDXGISwapChain** ppSwapChain, ID3D10Device** ppDevice)
|
|
|
|
//{
|
|
|
|
// auto res = hook->D3D10CreateDeviceAndSwapChain(pAdapter, DriverType, Software, Flags, SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice);
|
|
|
|
//
|
|
|
|
// if (SUCCEEDED(res))
|
|
|
|
// hook->hook_dx10(SDKVersion);
|
|
|
|
//
|
|
|
|
// return res;
|
|
|
|
//}
|
2019-07-25 21:21:03 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX10_Hook::MyPresent(IDXGISwapChain *_this, UINT SyncInterval, UINT Flags)
|
|
|
|
{
|
2019-08-16 17:21:30 +00:00
|
|
|
DX10_Hook::Inst()->prepareForOverlay(_this);
|
|
|
|
return (_this->*DX10_Hook::Inst()->Present)(SyncInterval, Flags);
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX10_Hook::MyResizeTarget(IDXGISwapChain* _this, const DXGI_MODE_DESC* pNewTargetParameters)
|
|
|
|
{
|
2019-08-16 17:21:30 +00:00
|
|
|
DX10_Hook::Inst()->resetRenderState();
|
|
|
|
return (_this->*DX10_Hook::Inst()->ResizeTarget)(pNewTargetParameters);
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX10_Hook::MyResizeBuffers(IDXGISwapChain* _this, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags)
|
|
|
|
{
|
2019-08-16 17:21:30 +00:00
|
|
|
DX10_Hook::Inst()->resetRenderState();
|
|
|
|
return (_this->*DX10_Hook::Inst()->ResizeBuffers)(BufferCount, Width, Height, NewFormat, SwapChainFlags);
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DX10_Hook::DX10_Hook():
|
|
|
|
initialized(false),
|
|
|
|
pDevice(nullptr),
|
2019-08-26 17:36:07 +00:00
|
|
|
mainRenderTargetView(nullptr),
|
|
|
|
Present(nullptr),
|
|
|
|
ResizeBuffers(nullptr),
|
|
|
|
ResizeTarget(nullptr)
|
2019-07-25 21:21:03 +00:00
|
|
|
{
|
2019-08-18 14:19:28 +00:00
|
|
|
_library = LoadLibrary(DLL_NAME);
|
2019-08-16 08:28:23 +00:00
|
|
|
|
2019-07-25 21:21:03 +00:00
|
|
|
// Hook to D3D10CreateDevice and D3D10CreateDeviceAndSwapChain so we know when it gets called.
|
|
|
|
// If its called, then DX10 will be used to render the overlay.
|
|
|
|
//_D3D10CreateDevice = (decltype(_D3D10CreateDevice))GetProcAddress(_dll, "D3D10CreateDevice");
|
2019-08-16 08:36:44 +00:00
|
|
|
//D3D10CreateDeviceAndSwapChain = (decltype(D3D10CreateDeviceAndSwapChain))GetProcAddress(_dll, "D3D10CreateDeviceAndSwapChain");
|
|
|
|
//
|
|
|
|
//BeginHook();
|
|
|
|
//HookFuncs(
|
|
|
|
// //std::make_pair<void**, void*>(&(PVOID&)_D3D10CreateDevice, &DX10_Hook::MyD3D10CreateDevice),
|
|
|
|
// std::make_pair<void**, void*>(&(PVOID&)D3D10CreateDeviceAndSwapChain, &DX10_Hook::MyD3D10CreateDeviceAndSwapChain)
|
|
|
|
//);
|
|
|
|
//EndHook();
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DX10_Hook::~DX10_Hook()
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("DX10 Hook removed\n");
|
|
|
|
|
2019-08-21 19:52:08 +00:00
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
mainRenderTargetView->Release();
|
|
|
|
|
|
|
|
//ImGui_ImplDX10_Shutdown();
|
|
|
|
ImGui_ImplDX10_InvalidateDeviceObjects();
|
|
|
|
Windows_Hook::Inst().resetRenderState();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
|
|
|
|
initialized = false;
|
|
|
|
}
|
2019-08-18 14:19:28 +00:00
|
|
|
|
|
|
|
FreeLibrary(reinterpret_cast<HMODULE>(_library));
|
2019-07-25 21:21:03 +00:00
|
|
|
|
2019-08-16 17:21:30 +00:00
|
|
|
_inst = nullptr;
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 17:21:30 +00:00
|
|
|
DX10_Hook* DX10_Hook::Inst()
|
2019-07-25 21:21:03 +00:00
|
|
|
{
|
2019-08-16 17:21:30 +00:00
|
|
|
if (_inst == nullptr)
|
|
|
|
_inst = new DX10_Hook;
|
|
|
|
|
|
|
|
return _inst;
|
2019-07-25 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 14:38:01 +00:00
|
|
|
const char* DX10_Hook::get_lib_name() const
|
|
|
|
{
|
|
|
|
return DLL_NAME;
|
|
|
|
}
|
|
|
|
|
2019-08-26 17:36:07 +00:00
|
|
|
void DX10_Hook::loadFunctions(IDXGISwapChain *pSwapChain)
|
2019-07-25 21:21:03 +00:00
|
|
|
{
|
2019-08-26 17:36:07 +00:00
|
|
|
void** vTable;
|
2019-07-25 21:21:03 +00:00
|
|
|
|
|
|
|
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 12:55:31 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 13:09:57 +00:00
|
|
|
#endif//NO_OVERLAY
|