2020-12-02 16:19:16 -05:00
|
|
|
#include "Hook.h"
|
2021-01-03 06:48:07 -05:00
|
|
|
#include "vendor/kiero/kiero.h"
|
2021-01-07 16:28:48 -05:00
|
|
|
#include "vendor/kiero/minhook/MinHook.h"
|
2020-12-02 16:19:16 -05:00
|
|
|
|
|
|
|
WNDPROC Hook::oWndProc = NULL;
|
|
|
|
f_Present11 Hook::oPresent11 = NULL;
|
|
|
|
f_Present9 Hook::oPresent9 = NULL;
|
2021-01-11 13:07:10 -05:00
|
|
|
f_Reset Hook::oReset9 = NULL;
|
2020-12-02 16:19:16 -05:00
|
|
|
|
2020-12-09 12:39:01 -05:00
|
|
|
bool Hook::mouse_visibility = false;
|
|
|
|
bool Hook::show_mouse = false;
|
|
|
|
|
2020-12-02 16:19:16 -05:00
|
|
|
std::function<void()> Hook::window_func = NULL;
|
|
|
|
|
|
|
|
LRESULT Hook::InputProc(const HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam);
|
|
|
|
|
2021-01-07 16:07:45 -05:00
|
|
|
if (Hook::show_mouse)
|
|
|
|
{
|
|
|
|
patch::Nop(0x4EB9F4, 5); // disable radio scroll
|
|
|
|
Call<0x541BD0>(); // CPad::ClearMouseHistory
|
|
|
|
}
|
|
|
|
else
|
|
|
|
patch::SetRaw(0x4EB9F4, (void*)"\xE8\x67\xFC\xFF\xFF", 5); // enable radio scroll
|
|
|
|
|
2020-12-17 02:12:23 -05:00
|
|
|
if (ImGui::GetIO().WantTextInput)
|
|
|
|
{
|
|
|
|
Call<0x53F1E0>(); // CPad::ClearKeyboardHistory
|
|
|
|
return 1;
|
|
|
|
}
|
2020-12-02 16:19:16 -05:00
|
|
|
else
|
|
|
|
return CallWindowProc(oWndProc, hWnd, uMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
2021-01-11 13:07:10 -05:00
|
|
|
HRESULT Hook::Reset(IDirect3DDevice9 * pDevice, D3DPRESENT_PARAMETERS * pPresentationParameters)
|
2020-12-02 16:19:16 -05:00
|
|
|
{
|
|
|
|
ImGui_ImplDX9_InvalidateDeviceObjects();
|
|
|
|
|
2020-12-05 16:34:27 -05:00
|
|
|
return oReset9(pDevice, pPresentationParameters);
|
2020-12-02 16:19:16 -05:00
|
|
|
}
|
|
|
|
|
2021-01-13 13:25:28 -05:00
|
|
|
void Hook::Present(void *ptr)
|
2020-12-02 16:19:16 -05:00
|
|
|
{
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
|
|
|
|
if (Globals::init_done)
|
|
|
|
{
|
2021-01-22 16:47:22 -05:00
|
|
|
Hook::ShowMouse(show_mouse);
|
2020-12-09 12:39:01 -05:00
|
|
|
|
2020-12-02 16:19:16 -05:00
|
|
|
// Change font size if the game resolution changes
|
2021-01-11 13:07:10 -05:00
|
|
|
if (Globals::screen_size.x != screen::GetScreenWidth()
|
|
|
|
&& Globals::screen_size.y != screen::GetScreenHeight())
|
2020-12-02 16:19:16 -05:00
|
|
|
{
|
|
|
|
int font_size = int(screen::GetScreenHeight() / 54.85); // manually tested
|
|
|
|
|
|
|
|
io.FontDefault = io.Fonts->AddFontFromFileTTF("C:/Windows/Fonts/trebucbd.ttf", font_size);
|
|
|
|
io.Fonts->Build();
|
|
|
|
|
2021-01-11 13:07:10 -05:00
|
|
|
if (Globals::renderer == Render_DirectX9)
|
|
|
|
ImGui_ImplDX9_InvalidateDeviceObjects();
|
|
|
|
else
|
|
|
|
ImGui_ImplDX11_InvalidateDeviceObjects();
|
|
|
|
|
|
|
|
Globals::screen_size = ImVec2(screen::GetScreenWidth(), screen::GetScreenHeight());
|
2020-12-02 16:19:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui_ImplWin32_NewFrame();
|
2021-01-11 13:07:10 -05:00
|
|
|
if (Globals::renderer == Render_DirectX9)
|
|
|
|
ImGui_ImplDX9_NewFrame();
|
|
|
|
else
|
|
|
|
ImGui_ImplDX11_NewFrame();
|
|
|
|
|
2020-12-02 16:19:16 -05:00
|
|
|
ImGui::NewFrame();
|
|
|
|
|
|
|
|
if (window_func != NULL)
|
|
|
|
window_func();
|
|
|
|
|
|
|
|
ImGui::EndFrame();
|
|
|
|
ImGui::Render();
|
2021-01-11 13:07:10 -05:00
|
|
|
|
|
|
|
if (Globals::renderer == Render_DirectX9)
|
|
|
|
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
else
|
|
|
|
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
|
2020-12-02 16:19:16 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Globals::init_done = true;
|
|
|
|
ImGui::CreateContext();
|
|
|
|
|
|
|
|
ImGuiStyle& style = ImGui::GetStyle();
|
|
|
|
|
|
|
|
ImGui_ImplWin32_Init(RsGlobal.ps->window);
|
2021-01-22 16:47:22 -05:00
|
|
|
|
|
|
|
// shift trigger fix
|
|
|
|
patch::Nop(0x00531155,5);
|
2020-12-02 16:19:16 -05:00
|
|
|
|
2021-01-11 13:07:10 -05:00
|
|
|
if (Globals::renderer == Render_DirectX9)
|
2021-01-07 16:07:45 -05:00
|
|
|
{
|
2021-01-11 13:07:10 -05:00
|
|
|
Globals::device = ptr;
|
|
|
|
ImGui_ImplDX9_Init(reinterpret_cast<IDirect3DDevice9*>(Globals::device));
|
2021-01-07 16:07:45 -05:00
|
|
|
}
|
2021-01-11 13:07:10 -05:00
|
|
|
else
|
2020-12-02 16:19:16 -05:00
|
|
|
{
|
2021-01-11 13:07:10 -05:00
|
|
|
// for dx11 device ptr is swapchain
|
|
|
|
reinterpret_cast<IDXGISwapChain*>(ptr)->GetDevice(__uuidof(ID3D11Device), (void**)&Globals::device);
|
|
|
|
ID3D11DeviceContext* context;
|
|
|
|
reinterpret_cast<ID3D11Device*>(Globals::device)->GetImmediateContext(&context);
|
2020-12-02 16:19:16 -05:00
|
|
|
|
2021-01-11 13:07:10 -05:00
|
|
|
ImGui_ImplDX11_Init(reinterpret_cast<ID3D11Device*>(Globals::device), context);
|
2020-12-02 16:19:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui_ImplWin32_EnableDpiAwareness();
|
|
|
|
|
|
|
|
io.IniFilename = NULL;
|
|
|
|
io.LogFilename = NULL;
|
|
|
|
|
|
|
|
style.WindowTitleAlign = ImVec2(0.5, 0.5);
|
2021-01-11 13:07:10 -05:00
|
|
|
oWndProc = (WNDPROC)SetWindowLongPtr(RsGlobal.ps->window, GWL_WNDPROC, (LRESULT)InputProc);
|
2020-12-02 16:19:16 -05:00
|
|
|
}
|
2021-01-13 13:25:28 -05:00
|
|
|
}
|
2020-12-02 16:19:16 -05:00
|
|
|
|
2021-01-13 13:25:28 -05:00
|
|
|
HRESULT Hook::PresentDx9Handler(IDirect3DDevice9 *pDevice, RECT* pSourceRect, RECT* pDestRect, HWND hDestWindowOverride, RGNDATA* pDirtyRegion)
|
|
|
|
{
|
|
|
|
Present(pDevice);
|
|
|
|
return oPresent9(pDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT Hook::PresentDx11Handler(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags)
|
|
|
|
{
|
|
|
|
Present(pSwapChain);
|
|
|
|
return oPresent11(pSwapChain, SyncInterval, Flags);
|
2020-12-02 16:19:16 -05:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:48:07 -05:00
|
|
|
// Thanks imring
|
2020-12-09 12:39:01 -05:00
|
|
|
void Hook::ShowMouse(bool state)
|
|
|
|
{
|
|
|
|
if (state)
|
|
|
|
{
|
2020-12-21 15:24:07 -05:00
|
|
|
patch::SetUChar(0x6194A0, 0xC3);
|
2020-12-09 12:39:01 -05:00
|
|
|
patch::Nop(0x53F417, 5); // don't call CPad__getMouseState
|
2021-01-05 01:29:26 -05:00
|
|
|
patch::SetRaw(0x53F41F, (void*)"\x33\xC0\x0F\x84", 4); // disable camera mouse movement
|
2020-12-09 12:39:01 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-22 16:47:22 -05:00
|
|
|
if (mouse_visibility != show_mouse)
|
|
|
|
{
|
|
|
|
patch::SetRaw(0x541DF5, (void*)"\xE8\x46\xF3\xFE\xFF", 5); // call CControllerConfigManager::AffectPadFromKeyBoard
|
|
|
|
patch::SetRaw(0x53F417, (void*)"\xE8\xB4\x7A\x20\x00", 5); // call CPad__getMouseState
|
|
|
|
patch::SetRaw(0x53F41F, (void*)"\x85\xC0\x0F\x8C", 4); // xor eax, eax -> test eax, eax , enable camera mouse movement
|
|
|
|
// jz loc_53F526 -> jl loc_53F526
|
|
|
|
patch::SetUChar(0x6194A0, 0xE9); // jmp setup
|
|
|
|
mouse_visibility = show_mouse;
|
|
|
|
}
|
2020-12-09 12:39:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::GetIO().MouseDrawCursor = state;
|
|
|
|
CPad::NewMouseControllerState.X = 0;
|
|
|
|
CPad::NewMouseControllerState.Y = 0;
|
2020-12-10 14:36:18 -05:00
|
|
|
|
|
|
|
// Broken in psdk
|
2020-12-09 12:39:01 -05:00
|
|
|
Call<0x541BD0>(); // CPad::ClearMouseHistory
|
|
|
|
Call<0x541DD0>(); // CPad::UpdatePads
|
|
|
|
}
|
|
|
|
|
2020-12-02 16:19:16 -05:00
|
|
|
Hook::Hook()
|
|
|
|
{
|
|
|
|
ImGui::CreateContext();
|
|
|
|
Events::initRwEvent += []()
|
|
|
|
{
|
|
|
|
if (kiero::init(kiero::RenderType::D3D9) == kiero::Status::Success)
|
|
|
|
{
|
2021-01-11 13:07:10 -05:00
|
|
|
Globals::renderer = Render_DirectX9;
|
|
|
|
if (kiero::bind(16, (void**)&oReset9, Reset) == kiero::Status::Success
|
2021-01-13 13:25:28 -05:00
|
|
|
&& kiero::bind(17, (void**)&oPresent9, PresentDx9Handler) == kiero::Status::Success)
|
2020-12-17 12:33:23 -05:00
|
|
|
flog << "Successfully hooked dx9 device." << std::endl;
|
2020-12-02 16:19:16 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// gtaRenderHook
|
|
|
|
if (kiero::init(kiero::RenderType::D3D11) == kiero::Status::Success)
|
|
|
|
{
|
2021-01-11 13:07:10 -05:00
|
|
|
Globals::renderer = Render_DirectX11;
|
2021-01-13 13:25:28 -05:00
|
|
|
if (kiero::bind(8, (void**)&oPresent11, PresentDx11Handler) == kiero::Status::Success)
|
2020-12-17 12:33:23 -05:00
|
|
|
flog << "Successfully hooked dx11 device." << std::endl;
|
2020-12-02 16:19:16 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flog << "Failed to hook device" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Hook::~Hook()
|
|
|
|
{
|
|
|
|
ImGui_ImplDX9_Shutdown();
|
|
|
|
ImGui_ImplWin32_Shutdown();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
kiero::shutdown();
|
|
|
|
}
|