2019-07-26 05:21:03 +08:00
|
|
|
#include "DX9_Hook.h"
|
2019-08-18 22:22:07 +08:00
|
|
|
#include "Windows_Hook.h"
|
2019-09-02 02:53:16 +08:00
|
|
|
#include "../Renderer_Detector.h"
|
|
|
|
#include "../../dll/dll.h"
|
2019-08-14 20:55:31 +08:00
|
|
|
|
2020-01-20 01:55:14 +08:00
|
|
|
#ifdef EMU_OVERLAY
|
2019-08-14 20:55:31 +08:00
|
|
|
|
2019-07-26 05:21:03 +08:00
|
|
|
#include <imgui.h>
|
2019-09-02 02:53:16 +08:00
|
|
|
#include <impls/windows/imgui_impl_dx9.h>
|
2019-08-01 04:20:27 +08:00
|
|
|
|
2019-08-17 01:21:30 +08:00
|
|
|
DX9_Hook* DX9_Hook::_inst = nullptr;
|
2019-07-26 05:21:03 +08:00
|
|
|
|
2019-08-17 01:10:12 +08:00
|
|
|
bool DX9_Hook::start_hook()
|
2019-07-26 05:21:03 +08:00
|
|
|
{
|
2019-08-27 21:38:07 +08:00
|
|
|
if (!hooked)
|
2019-07-26 05:21:03 +08:00
|
|
|
{
|
2019-08-27 21:38:07 +08:00
|
|
|
if (!Windows_Hook::Inst()->start_hook())
|
2019-08-18 23:12:57 +08:00
|
|
|
return false;
|
2019-07-26 05:21:03 +08:00
|
|
|
|
2019-08-26 22:38:01 +08:00
|
|
|
PRINT_DEBUG("Hooked DirectX 9\n");
|
2019-08-27 21:38:07 +08:00
|
|
|
hooked = true;
|
2019-08-26 22:38:01 +08:00
|
|
|
|
2019-08-27 21:38:07 +08:00
|
|
|
Renderer_Detector::Inst().renderer_found(this);
|
|
|
|
|
2019-08-26 22:38:01 +08: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 15:07:53 +08:00
|
|
|
{
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)PresentEx, &DX9_Hook::MyPresentEx)
|
|
|
|
//std::make_pair<void**, void*>(&(PVOID&)EndScene, &DX9_Hook::MyEndScene)
|
|
|
|
);
|
|
|
|
}
|
2019-08-26 22:38:01 +08:00
|
|
|
EndHook();
|
2019-08-02 15:07:53 +08:00
|
|
|
|
2019-08-26 22:38:01 +08:00
|
|
|
get_steam_client()->steam_overlay->HookReady();
|
2019-07-26 05:21:03 +08:00
|
|
|
}
|
2019-08-26 22:38:01 +08:00
|
|
|
return true;
|
2019-07-26 05:21:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DX9_Hook::resetRenderState()
|
|
|
|
{
|
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
initialized = false;
|
|
|
|
ImGui_ImplDX9_Shutdown();
|
2019-08-27 21:38:07 +08:00
|
|
|
Windows_Hook::Inst()->resetRenderState();
|
2019-08-01 04:20:27 +08:00
|
|
|
ImGui::DestroyContext();
|
2019-07-26 05:21:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 20:55:31 +08:00
|
|
|
// Try to make this function and overlay's proc as short as possible or it might affect game's fps.
|
2019-07-26 05:21:03 +08:00
|
|
|
void DX9_Hook::prepareForOverlay(IDirect3DDevice9 *pDevice)
|
|
|
|
{
|
2019-08-01 04:20:27 +08:00
|
|
|
D3DDEVICE_CREATION_PARAMETERS param;
|
|
|
|
pDevice->GetCreationParameters(¶m);
|
|
|
|
|
2019-08-14 20:55:31 +08:00
|
|
|
// Workaround to detect if we changed window.
|
2019-08-27 21:38:07 +08:00
|
|
|
if (param.hFocusWindow != Windows_Hook::Inst()->GetGameHwnd())
|
2019-08-01 04:20:27 +08:00
|
|
|
resetRenderState();
|
|
|
|
|
2019-07-26 05:21:03 +08:00
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.IniFilename = NULL;
|
|
|
|
|
|
|
|
ImGui_ImplDX9_Init(pDevice);
|
2019-09-05 15:00:02 +08:00
|
|
|
|
|
|
|
get_steam_client()->steam_overlay->CreateFonts();
|
|
|
|
|
2019-07-26 05:21:03 +08:00
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:10:33 +08:00
|
|
|
if (ImGui_ImplDX9_NewFrame())
|
|
|
|
{
|
|
|
|
Windows_Hook::Inst()->prepareForOverlay(param.hFocusWindow);
|
2019-07-26 05:21:03 +08:00
|
|
|
|
2020-01-10 15:10:33 +08:00
|
|
|
ImGui::NewFrame();
|
2019-07-26 05:21:03 +08:00
|
|
|
|
2020-01-10 15:10:33 +08:00
|
|
|
get_steam_client()->steam_overlay->OverlayProc();
|
2019-07-26 05:21:03 +08:00
|
|
|
|
2020-01-10 15:10:33 +08:00
|
|
|
ImGui::Render();
|
2019-07-26 05:21:03 +08:00
|
|
|
|
2020-01-10 15:10:33 +08:00
|
|
|
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
}
|
2019-07-26 05:21:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX9_Hook::MyReset(IDirect3DDevice9* _this, D3DPRESENT_PARAMETERS* pPresentationParameters)
|
|
|
|
{
|
2019-08-17 01:21:30 +08:00
|
|
|
DX9_Hook::Inst()->resetRenderState();
|
|
|
|
return (_this->*DX9_Hook::Inst()->Reset)(pPresentationParameters);
|
2019-07-26 05:21:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX9_Hook::MyEndScene(IDirect3DDevice9* _this)
|
|
|
|
{
|
2019-08-17 01:21:30 +08:00
|
|
|
if( !DX9_Hook::Inst()->uses_present )
|
|
|
|
DX9_Hook::Inst()->prepareForOverlay(_this);
|
|
|
|
return (_this->*DX9_Hook::Inst()->EndScene)();
|
2019-07-26 05:21:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX9_Hook::MyPresent(IDirect3DDevice9* _this, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion)
|
|
|
|
{
|
2019-08-17 01:21:30 +08:00
|
|
|
DX9_Hook::Inst()->uses_present = true;
|
|
|
|
DX9_Hook::Inst()->prepareForOverlay(_this);
|
|
|
|
return (_this->*DX9_Hook::Inst()->Present)(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
|
2019-07-26 05:21:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX9_Hook::MyPresentEx(IDirect3DDevice9Ex* _this, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion, DWORD dwFlags)
|
|
|
|
{
|
2019-08-17 01:21:30 +08: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-26 05:21:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DX9_Hook::DX9_Hook():
|
|
|
|
initialized(false),
|
2019-08-27 21:38:07 +08:00
|
|
|
hooked(false),
|
2019-08-01 04:20:27 +08:00
|
|
|
uses_present(false),
|
|
|
|
EndScene(nullptr),
|
|
|
|
Present(nullptr),
|
|
|
|
PresentEx(nullptr),
|
|
|
|
Reset(nullptr)
|
2019-07-26 05:21:03 +08:00
|
|
|
{
|
2020-07-16 17:56:24 +08:00
|
|
|
_library = LoadLibrary(DX9_DLL);
|
2019-07-26 05:21:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DX9_Hook::~DX9_Hook()
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("DX9 Hook removed\n");
|
|
|
|
|
2019-08-18 22:19:28 +08:00
|
|
|
if (initialized)
|
2019-08-01 23:05:41 +08:00
|
|
|
{
|
|
|
|
ImGui_ImplDX9_InvalidateDeviceObjects();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
}
|
2019-07-26 05:21:03 +08:00
|
|
|
|
2019-08-18 22:19:28 +08:00
|
|
|
FreeLibrary(reinterpret_cast<HMODULE>(_library));
|
|
|
|
|
2019-08-17 01:21:30 +08:00
|
|
|
_inst = nullptr;
|
2019-07-26 05:21:03 +08:00
|
|
|
}
|
|
|
|
|
2019-08-17 01:21:30 +08:00
|
|
|
DX9_Hook* DX9_Hook::Inst()
|
2019-07-26 05:21:03 +08:00
|
|
|
{
|
2019-08-17 01:21:30 +08:00
|
|
|
if( _inst == nullptr )
|
|
|
|
_inst = new DX9_Hook;
|
|
|
|
|
|
|
|
return _inst;
|
2019-07-26 05:21:03 +08:00
|
|
|
}
|
|
|
|
|
2019-08-26 22:38:01 +08:00
|
|
|
const char* DX9_Hook::get_lib_name() const
|
|
|
|
{
|
2020-07-16 17:56:24 +08:00
|
|
|
return DX9_DLL;
|
2019-08-26 22:38:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DX9_Hook::loadFunctions(IDirect3DDevice9* pDevice, bool ex)
|
2019-07-26 05:21:03 +08:00
|
|
|
{
|
2019-08-26 22:38:01 +08:00
|
|
|
void** vTable = *reinterpret_cast<void***>(pDevice);
|
2019-07-26 05:21:03 +08:00
|
|
|
|
|
|
|
#define LOAD_FUNC(X) (void*&)X = vTable[(int)IDirect3DDevice9VTable::X]
|
|
|
|
LOAD_FUNC(Reset);
|
|
|
|
LOAD_FUNC(EndScene);
|
|
|
|
LOAD_FUNC(Present);
|
2019-08-26 22:38:01 +08:00
|
|
|
if (ex)
|
|
|
|
LOAD_FUNC(PresentEx);
|
2019-07-26 05:21:03 +08:00
|
|
|
#undef LOAD_FUNC
|
2019-08-14 20:55:31 +08:00
|
|
|
}
|
|
|
|
|
2020-01-20 01:55:14 +08:00
|
|
|
#endif//EMU_OVERLAY
|