2022-08-05 02:06:42 -04: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 "DX9_Hook.h"
|
2019-08-18 16:22:07 +02:00
|
|
|
#include "Windows_Hook.h"
|
2022-08-05 02:06:42 -04:00
|
|
|
#include "DirectX_VTables.h"
|
2019-08-14 14:55:31 +02:00
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
#include <imgui.h>
|
2022-08-05 02:06:42 -04:00
|
|
|
#include <backends/imgui_impl_dx9.h>
|
2019-07-31 22:20:27 +02:00
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
DX9_Hook* DX9_Hook::_inst = nullptr;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
template<typename T>
|
|
|
|
inline void SafeRelease(T*& pUnk)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
if (pUnk != nullptr)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
pUnk->Release();
|
|
|
|
pUnk = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DX9_Hook::StartHook(std::function<bool(bool)> key_combination_callback)
|
|
|
|
{
|
|
|
|
if (!_Hooked)
|
|
|
|
{
|
|
|
|
if (Reset == nullptr || Present == nullptr)
|
|
|
|
{
|
|
|
|
SPDLOG_WARN("Failed to hook DirectX 9: Rendering functions missing.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Windows_Hook::Inst()->StartHook(key_combination_callback))
|
2019-08-18 17:12:57 +02:00
|
|
|
return false;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
_WindowsHooked = true;
|
|
|
|
|
|
|
|
SPDLOG_INFO("Hooked DirectX 9");
|
|
|
|
_Hooked = true;
|
2019-08-27 15:38:07 +02:00
|
|
|
|
2019-08-26 16:38:01 +02:00
|
|
|
BeginHook();
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)Reset, &DX9_Hook::MyReset),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)Present, &DX9_Hook::MyPresent)
|
|
|
|
);
|
|
|
|
if (PresentEx != nullptr)
|
2019-08-02 09:07:53 +02:00
|
|
|
{
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)PresentEx, &DX9_Hook::MyPresentEx)
|
2022-08-05 02:06:42 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (SwapChainPresent != nullptr)
|
|
|
|
{
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)SwapChainPresent, &DX9_Hook::MySwapChainPresent)
|
2019-08-02 09:07:53 +02:00
|
|
|
);
|
|
|
|
}
|
2019-08-26 16:38:01 +02:00
|
|
|
EndHook();
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
2019-08-26 16:38:01 +02:00
|
|
|
return true;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
bool DX9_Hook::IsStarted()
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
return _Hooked;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DX9_Hook::_ResetRenderState()
|
|
|
|
{
|
|
|
|
if (_Initialized)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
OverlayHookReady(false);
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
ImGui_ImplDX9_Shutdown();
|
2022-08-05 02:06:42 -04:00
|
|
|
Windows_Hook::Inst()->_ResetRenderState();
|
2019-07-31 22:20:27 +02:00
|
|
|
ImGui::DestroyContext();
|
2022-08-05 02:06:42 -04:00
|
|
|
|
|
|
|
SafeRelease(_pDevice);
|
|
|
|
|
|
|
|
_LastWindow = nullptr;
|
|
|
|
_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 02:06:42 -04:00
|
|
|
void DX9_Hook::_PrepareForOverlay(IDirect3DDevice9 *pDevice, HWND destWindow)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
if (!destWindow)
|
|
|
|
{
|
|
|
|
IDirect3DSwapChain9 *pSwapChain = nullptr;
|
|
|
|
if (pDevice->GetSwapChain(0, &pSwapChain) == D3D_OK)
|
|
|
|
{
|
|
|
|
D3DPRESENT_PARAMETERS params;
|
|
|
|
if (pSwapChain->GetPresentParameters(¶ms) == D3D_OK)
|
|
|
|
{
|
|
|
|
destWindow = params.hDeviceWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
pSwapChain->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Is this necessary anymore?
|
|
|
|
if (!destWindow)
|
|
|
|
{
|
|
|
|
D3DDEVICE_CREATION_PARAMETERS param;
|
|
|
|
pDevice->GetCreationParameters(¶m);
|
|
|
|
destWindow = param.hFocusWindow;
|
|
|
|
}
|
2019-07-31 22:20:27 +02:00
|
|
|
|
2019-08-14 14:55:31 +02:00
|
|
|
// Workaround to detect if we changed window.
|
2022-08-05 02:06:42 -04:00
|
|
|
if (destWindow != _LastWindow || _pDevice != pDevice)
|
|
|
|
_ResetRenderState();
|
2019-07-31 22:20:27 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
if (!_Initialized)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
pDevice->AddRef();
|
|
|
|
_pDevice = pDevice;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
ImGui::CreateContext();
|
2019-07-25 23:21:03 +02:00
|
|
|
ImGui_ImplDX9_Init(pDevice);
|
2019-09-05 09:00:02 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
_LastWindow = destWindow;
|
|
|
|
_Initialized = true;
|
|
|
|
OverlayHookReady(true);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
if (ImGui_ImplDX9_NewFrame() && Windows_Hook::Inst()->_PrepareForOverlay(destWindow))
|
|
|
|
{
|
2020-01-10 08:10:33 +01:00
|
|
|
ImGui::NewFrame();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 02:06:42 -04: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
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
}
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX9_Hook::MyReset(IDirect3DDevice9* _this, D3DPRESENT_PARAMETERS* pPresentationParameters)
|
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
auto inst = DX9_Hook::Inst();
|
|
|
|
inst->_ResetRenderState();
|
|
|
|
return (_this->*inst->Reset)(pPresentationParameters);
|
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)
|
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
auto inst = DX9_Hook::Inst();
|
|
|
|
inst->_PrepareForOverlay(_this, hDestWindowOverride);
|
|
|
|
return (_this->*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)
|
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
auto inst = DX9_Hook::Inst();
|
|
|
|
inst->_PrepareForOverlay(_this, hDestWindowOverride);
|
|
|
|
return (_this->*inst->PresentEx)(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX9_Hook::MySwapChainPresent(IDirect3DSwapChain9* _this, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion, DWORD dwFlags)
|
|
|
|
{
|
|
|
|
IDirect3DDevice9* pDevice;
|
|
|
|
auto inst = DX9_Hook::Inst();
|
|
|
|
|
|
|
|
if (SUCCEEDED(_this->GetDevice(&pDevice)))
|
|
|
|
{
|
|
|
|
HWND destWindow = hDestWindowOverride;
|
|
|
|
if (!destWindow)
|
|
|
|
{
|
|
|
|
D3DPRESENT_PARAMETERS param;
|
|
|
|
if (_this->GetPresentParameters(¶m) == D3D_OK)
|
|
|
|
{
|
|
|
|
destWindow = param.hDeviceWindow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inst->_PrepareForOverlay(pDevice, destWindow);
|
|
|
|
pDevice->Release();
|
|
|
|
}
|
|
|
|
return (_this->*inst->SwapChainPresent)(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DX9_Hook::DX9_Hook():
|
2022-08-05 02:06:42 -04:00
|
|
|
_Initialized(false),
|
|
|
|
_Hooked(false),
|
|
|
|
_WindowsHooked(false),
|
|
|
|
_LastWindow(nullptr),
|
2019-07-31 22:20:27 +02:00
|
|
|
Present(nullptr),
|
|
|
|
PresentEx(nullptr),
|
|
|
|
Reset(nullptr)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DX9_Hook::~DX9_Hook()
|
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
SPDLOG_INFO("DX9 Hook removed");
|
|
|
|
|
|
|
|
if (_WindowsHooked)
|
|
|
|
delete Windows_Hook::Inst();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
if (_Initialized)
|
2019-08-01 17:05:41 +02:00
|
|
|
{
|
|
|
|
ImGui_ImplDX9_InvalidateDeviceObjects();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
}
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
std::string DX9_Hook::GetLibraryName() const
|
2019-08-26 16:38:01 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
return LibraryName;
|
2019-08-26 16:38:01 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
void DX9_Hook::LoadFunctions(decltype(Present) PresentFcn, decltype(Reset) ResetFcn, decltype(PresentEx) PresentExFcn, decltype(&IDirect3DSwapChain9::Present) SwapChainPresentFcn)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
Present = PresentFcn;
|
|
|
|
Reset = ResetFcn;
|
|
|
|
|
|
|
|
PresentEx = PresentExFcn;
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
SwapChainPresent = SwapChainPresentFcn;
|
2019-08-14 14:55:31 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
std::weak_ptr<uint64_t> DX9_Hook::CreateImageResource(const void* image_data, uint32_t width, uint32_t height)
|
|
|
|
{
|
|
|
|
IDirect3DTexture9** pTexture = new IDirect3DTexture9*(nullptr);
|
|
|
|
|
|
|
|
_pDevice->CreateTexture(
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
1,
|
|
|
|
D3DUSAGE_DYNAMIC,
|
|
|
|
D3DFMT_A8R8G8B8,
|
|
|
|
D3DPOOL_DEFAULT,
|
|
|
|
pTexture,
|
|
|
|
nullptr
|
|
|
|
);
|
|
|
|
|
|
|
|
if (*pTexture != nullptr)
|
|
|
|
{
|
|
|
|
D3DLOCKED_RECT rect;
|
|
|
|
if (SUCCEEDED((*pTexture)->LockRect(0, &rect, nullptr, D3DLOCK_DISCARD)))
|
|
|
|
{
|
|
|
|
const uint32_t* pixels = reinterpret_cast<const uint32_t*>(image_data);
|
|
|
|
uint8_t* texture_bits = reinterpret_cast<uint8_t*>(rect.pBits);
|
|
|
|
for (int32_t i = 0; i < height; ++i)
|
|
|
|
{
|
|
|
|
for (int32_t j = 0; j < width; ++j)
|
|
|
|
{
|
|
|
|
// RGBA to ARGB Conversion, DX9 doesn't have a RGBA loader
|
|
|
|
uint32_t color = *pixels++;
|
|
|
|
reinterpret_cast<uint32_t*>(texture_bits)[j] = ((color & 0xff) << 16) | (color & 0xff00) | ((color & 0xff0000) >> 16) | (color & 0xff000000);
|
|
|
|
}
|
|
|
|
texture_bits += rect.Pitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED((*pTexture)->UnlockRect(0)))
|
|
|
|
{
|
|
|
|
(*pTexture)->Release();
|
|
|
|
delete pTexture;
|
|
|
|
pTexture = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(*pTexture)->Release();
|
|
|
|
delete pTexture;
|
|
|
|
pTexture = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pTexture == nullptr)
|
|
|
|
return std::shared_ptr<uint64_t>();
|
|
|
|
|
|
|
|
auto ptr = std::shared_ptr<uint64_t>((uint64_t*)pTexture, [](uint64_t* handle)
|
|
|
|
{
|
|
|
|
if (handle != nullptr)
|
|
|
|
{
|
|
|
|
IDirect3DTexture9** resource = reinterpret_cast<IDirect3DTexture9**>(handle);
|
|
|
|
(*resource)->Release();
|
|
|
|
delete resource;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_ImageResources.emplace(ptr);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DX9_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);
|
|
|
|
}
|
|
|
|
}
|