2022-08-05 08:06:42 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) Nemirtingas
|
|
|
|
* This file is part of the ingame overlay project
|
|
|
|
*
|
|
|
|
* The ingame overlay project is free software; you can redistribute it
|
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* The ingame overlay project is distributed in the hope that it will be
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with the ingame overlay project; if not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
#include "DX10_Hook.h"
|
2019-08-18 16:22:07 +02:00
|
|
|
#include "Windows_Hook.h"
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
#include <imgui.h>
|
2022-08-05 08:06:42 +02:00
|
|
|
#include <backends/imgui_impl_dx10.h>
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
DX10_Hook* DX10_Hook::_inst = nullptr;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
template<typename T>
|
|
|
|
inline void SafeRelease(T*& pUnk)
|
|
|
|
{
|
|
|
|
if (pUnk != nullptr)
|
|
|
|
{
|
|
|
|
pUnk->Release();
|
|
|
|
pUnk = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-13 20:32:43 +02:00
|
|
|
bool DX10_Hook::StartHook(std::function<bool(bool)> key_combination_callback, std::set<ingame_overlay::ToggleKey> toggle_keys)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 08:06:42 +02:00
|
|
|
if (!_Hooked)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 08:06:42 +02:00
|
|
|
if (Present == nullptr || ResizeTarget == nullptr || ResizeBuffers == nullptr)
|
|
|
|
{
|
|
|
|
SPDLOG_WARN("Failed to hook DirectX 11: Rendering functions missing.");
|
2019-08-18 17:12:57 +02:00
|
|
|
return false;
|
2022-08-05 08:06:42 +02:00
|
|
|
}
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-13 20:32:43 +02:00
|
|
|
if (!Windows_Hook::Inst()->StartHook(key_combination_callback, toggle_keys))
|
2022-08-05 08:06:42 +02:00
|
|
|
return false;
|
2019-08-26 16:38:01 +02:00
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
_WindowsHooked = true;
|
|
|
|
|
|
|
|
SPDLOG_INFO("Hooked DirectX 10");
|
|
|
|
_Hooked = true;
|
2019-08-26 16:38:01 +02:00
|
|
|
|
|
|
|
BeginHook();
|
|
|
|
HookFuncs(
|
2022-08-05 08:06:42 +02:00
|
|
|
std::make_pair<void**, void*>(&(PVOID&)Present , &DX10_Hook::MyPresent),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)ResizeTarget , &DX10_Hook::MyResizeTarget),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)ResizeBuffers, &DX10_Hook::MyResizeBuffers)
|
2019-08-26 16:38:01 +02:00
|
|
|
);
|
2022-08-05 08:06:42 +02:00
|
|
|
if (Present1 != nullptr)
|
|
|
|
{
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)Present1, &DX10_Hook::MyPresent1)
|
|
|
|
);
|
|
|
|
}
|
2019-08-26 16:38:01 +02:00
|
|
|
EndHook();
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
2022-08-05 08:06:42 +02:00
|
|
|
return true;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
bool DX10_Hook::IsStarted()
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 08:06:42 +02:00
|
|
|
return _Hooked;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DX10_Hook::_ResetRenderState()
|
|
|
|
{
|
|
|
|
if (_Initialized)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 08:06:42 +02:00
|
|
|
OverlayHookReady(false);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
ImGui_ImplDX10_Shutdown();
|
2022-08-10 09:22:23 +02:00
|
|
|
Windows_Hook::Inst()->ResetRenderState();
|
2019-07-31 22:20:27 +02:00
|
|
|
ImGui::DestroyContext();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
SafeRelease(mainRenderTargetView);
|
|
|
|
SafeRelease(pDevice);
|
|
|
|
|
|
|
|
_Initialized = false;
|
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.
|
2022-08-05 08:06:42 +02:00
|
|
|
void DX10_Hook::_PrepareForOverlay(IDXGISwapChain* pSwapChain)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
|
|
|
DXGI_SWAP_CHAIN_DESC desc;
|
|
|
|
pSwapChain->GetDesc(&desc);
|
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
if (!_Initialized)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-19 19:51:35 +02:00
|
|
|
if (FAILED(pSwapChain->GetDevice(IID_PPV_ARGS(&pDevice))))
|
2019-07-25 23:21:03 +02:00
|
|
|
return;
|
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
ID3D10Texture2D* pBackBuffer = nullptr;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2019-08-19 19:51:35 +02:00
|
|
|
pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
|
2022-08-05 08:06:42 +02:00
|
|
|
if (pBackBuffer == nullptr)
|
|
|
|
{
|
|
|
|
pDevice->Release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pDevice->CreateRenderTargetView(pBackBuffer, nullptr, &mainRenderTargetView);
|
2019-07-25 23:21:03 +02:00
|
|
|
pBackBuffer->Release();
|
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
ImGui::CreateContext();
|
2019-07-25 23:21:03 +02:00
|
|
|
ImGui_ImplDX10_Init(pDevice);
|
2019-08-16 18:30:55 +02:00
|
|
|
|
2022-08-10 09:22:23 +02:00
|
|
|
Windows_Hook::Inst()->SetInitialWindowSize(desc.OutputWindow);
|
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
_Initialized = true;
|
|
|
|
OverlayHookReady(true);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 09:22:23 +02:00
|
|
|
if (ImGui_ImplDX10_NewFrame() && Windows_Hook::Inst()->PrepareForOverlay(desc.OutputWindow))
|
2020-01-10 08:10:33 +01:00
|
|
|
{
|
|
|
|
ImGui::NewFrame();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
OverlayProc();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
ImGui::Render();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
pDevice->OMSetRenderTargets(1, &mainRenderTargetView, nullptr);
|
2020-01-10 08:10:33 +01:00
|
|
|
ImGui_ImplDX10_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
}
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX10_Hook::MyPresent(IDXGISwapChain *_this, UINT SyncInterval, UINT Flags)
|
|
|
|
{
|
2022-08-05 08:06:42 +02:00
|
|
|
auto inst= DX10_Hook::Inst();
|
|
|
|
inst->_PrepareForOverlay(_this);
|
|
|
|
return (_this->*inst->Present)(SyncInterval, Flags);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX10_Hook::MyResizeTarget(IDXGISwapChain* _this, const DXGI_MODE_DESC* pNewTargetParameters)
|
|
|
|
{
|
2022-08-05 08:06:42 +02:00
|
|
|
auto inst= DX10_Hook::Inst();
|
|
|
|
inst->_ResetRenderState();
|
|
|
|
return (_this->*inst->ResizeTarget)(pNewTargetParameters);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX10_Hook::MyResizeBuffers(IDXGISwapChain* _this, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags)
|
|
|
|
{
|
2022-08-05 08:06:42 +02:00
|
|
|
auto inst= DX10_Hook::Inst();
|
|
|
|
inst->_ResetRenderState();
|
|
|
|
return (_this->*inst->ResizeBuffers)(BufferCount, Width, Height, NewFormat, SwapChainFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX10_Hook::MyPresent1(IDXGISwapChain1* _this, UINT SyncInterval, UINT Flags, const DXGI_PRESENT_PARAMETERS* pPresentParameters)
|
|
|
|
{
|
|
|
|
auto inst = DX10_Hook::Inst();
|
|
|
|
inst->_PrepareForOverlay(_this);
|
|
|
|
return (_this->*inst->Present1)(SyncInterval, Flags, pPresentParameters);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DX10_Hook::DX10_Hook():
|
2022-08-05 08:06:42 +02:00
|
|
|
_Initialized(false),
|
|
|
|
_Hooked(false),
|
|
|
|
_WindowsHooked(false),
|
2019-07-25 23:21:03 +02:00
|
|
|
pDevice(nullptr),
|
2019-08-26 19:36:07 +02:00
|
|
|
mainRenderTargetView(nullptr),
|
|
|
|
Present(nullptr),
|
|
|
|
ResizeBuffers(nullptr),
|
2022-08-05 08:06:42 +02:00
|
|
|
ResizeTarget(nullptr),
|
|
|
|
Present1(nullptr)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DX10_Hook::~DX10_Hook()
|
|
|
|
{
|
2022-08-05 08:06:42 +02:00
|
|
|
//SPDLOG_INFO("DX10 Hook removed");
|
|
|
|
|
|
|
|
if (_WindowsHooked)
|
|
|
|
delete Windows_Hook::Inst();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
if (_Initialized)
|
2019-08-21 21:52:08 +02:00
|
|
|
{
|
|
|
|
mainRenderTargetView->Release();
|
|
|
|
|
|
|
|
ImGui_ImplDX10_InvalidateDeviceObjects();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
_Initialized = false;
|
2019-08-21 21:52:08 +02:00
|
|
|
}
|
2019-08-18 16:19:28 +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
|
|
|
DX10_Hook* DX10_Hook::Inst()
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
if (_inst == nullptr)
|
|
|
|
_inst = new DX10_Hook;
|
|
|
|
|
|
|
|
return _inst;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
std::string DX10_Hook::GetLibraryName() const
|
|
|
|
{
|
|
|
|
return LibraryName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DX10_Hook::LoadFunctions(
|
|
|
|
decltype(Present) PresentFcn,
|
|
|
|
decltype(ResizeBuffers) ResizeBuffersFcn,
|
|
|
|
decltype(ResizeTarget) ResizeTargetFcn,
|
|
|
|
decltype(Present1) Present1Fcn)
|
2019-08-26 16:38:01 +02:00
|
|
|
{
|
2022-08-05 08:06:42 +02:00
|
|
|
Present = PresentFcn;
|
|
|
|
ResizeBuffers = ResizeBuffersFcn;
|
|
|
|
ResizeTarget = ResizeTargetFcn;
|
|
|
|
Present1 = Present1Fcn;
|
2019-08-26 16:38:01 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
std::weak_ptr<uint64_t> DX10_Hook::CreateImageResource(const void* image_data, uint32_t width, uint32_t height)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 08:06:42 +02:00
|
|
|
ID3D10ShaderResourceView** resource = new ID3D10ShaderResourceView*(nullptr);
|
|
|
|
|
|
|
|
// Create texture
|
|
|
|
D3D10_TEXTURE2D_DESC desc = {};
|
|
|
|
desc.Width = static_cast<UINT>(width);
|
|
|
|
desc.Height = static_cast<UINT>(height);
|
|
|
|
desc.MipLevels = 1;
|
|
|
|
desc.ArraySize = 1;
|
|
|
|
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
desc.SampleDesc.Count = 1;
|
|
|
|
desc.Usage = D3D10_USAGE_DEFAULT;
|
|
|
|
desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
|
|
|
|
desc.CPUAccessFlags = 0;
|
|
|
|
|
|
|
|
ID3D10Texture2D* pTexture = nullptr;
|
|
|
|
D3D10_SUBRESOURCE_DATA subResource;
|
|
|
|
subResource.pSysMem = image_data;
|
|
|
|
subResource.SysMemPitch = desc.Width * 4;
|
|
|
|
subResource.SysMemSlicePitch = 0;
|
|
|
|
pDevice->CreateTexture2D(&desc, &subResource, &pTexture);
|
|
|
|
|
|
|
|
if (pTexture != nullptr)
|
|
|
|
{
|
|
|
|
// Create texture view
|
|
|
|
D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
|
|
|
|
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
|
|
|
|
srvDesc.Texture2D.MipLevels = desc.MipLevels;
|
|
|
|
srvDesc.Texture2D.MostDetailedMip = 0;
|
|
|
|
|
|
|
|
pDevice->CreateShaderResourceView(pTexture, &srvDesc, resource);
|
|
|
|
// Release Texure, the shader resource increases the reference count.
|
|
|
|
pTexture->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*resource == nullptr)
|
|
|
|
return std::shared_ptr<uint64_t>();
|
|
|
|
|
|
|
|
auto ptr = std::shared_ptr<uint64_t>((uint64_t*)resource, [](uint64_t* handle)
|
|
|
|
{
|
|
|
|
if (handle != nullptr)
|
|
|
|
{
|
|
|
|
ID3D10ShaderResourceView** resource = reinterpret_cast<ID3D10ShaderResourceView**>(handle);
|
|
|
|
(*resource)->Release();
|
|
|
|
delete resource;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_ImageResources.emplace(ptr);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
return ptr;
|
2019-08-14 14:55:31 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 08:06:42 +02:00
|
|
|
void DX10_Hook::ReleaseImageResource(std::weak_ptr<uint64_t> resource)
|
|
|
|
{
|
|
|
|
auto ptr = resource.lock();
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
auto it = _ImageResources.find(ptr);
|
|
|
|
if (it != _ImageResources.end())
|
|
|
|
_ImageResources.erase(it);
|
|
|
|
}
|
|
|
|
}
|