CheatMenuSA/CheatMenu/Hook.cpp

219 lines
5.5 KiB
C++
Raw Normal View History

2020-12-02 16:19:16 -05:00
#include "Hook.h"
2021-04-07 16:35:21 -04:00
#include "../Depend/kiero/kiero.h"
#include "../Depend/kiero/minhook/MinHook.h"
2020-12-02 16:19:16 -05:00
LRESULT Hook::WndProc(const HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2020-12-02 16:19:16 -05:00
{
ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam);
2021-06-18 12:49:11 -04:00
if (m_bShowMouse)
{
patch::Nop(0x4EB9F4, 5); // disable radio scroll
2021-02-13 04:08:37 -05:00
CPad::ClearMouseHistory();
if (uMsg == WM_MOUSEWHEEL)
2021-02-13 04:08:37 -05:00
return 1;
}
else
patch::SetRaw(0x4EB9F4, (void*)"\xE8\x67\xFC\xFF\xFF", 5); // enable radio scroll
if (ImGui::GetIO().WantTextInput)
{
Call<0x53F1E0>(); // CPad::ClearKeyboardHistory
2021-02-24 16:54:45 -05:00
return 1;
}
2021-02-24 16:54:45 -05:00
return CallWindowProc(oWndProc, hWnd, uMsg, wParam, lParam);
2020-12-02 16:19:16 -05:00
}
2021-02-24 16:54:45 -05:00
HRESULT Hook::Reset(IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters)
2020-12-02 16:19:16 -05:00
{
ImGui_ImplDX9_InvalidateDeviceObjects();
return oReset(pDevice, pPresentationParameters);
2020-12-02 16:19:16 -05:00
}
void Hook::RenderFrame(void* ptr)
2020-12-02 16:19:16 -05:00
{
2021-06-18 12:49:11 -04:00
if (!ImGui::GetCurrentContext())
2021-02-25 17:45:41 -05:00
return;
2020-12-02 16:19:16 -05:00
ImGuiIO& io = ImGui::GetIO();
2021-06-18 12:49:11 -04:00
if (Globals::m_bInit)
{
ShowMouse(m_bShowMouse);
2021-02-19 02:41:50 -05:00
// handle window scaling here
2021-02-24 16:54:45 -05:00
ImVec2 size(screen::GetScreenWidth(), screen::GetScreenHeight());
2021-06-18 12:49:11 -04:00
if (Globals::m_fScreenSize.x != size.x && Globals::m_fScreenSize.y != size.y)
2020-12-02 16:19:16 -05:00
{
2021-06-18 12:49:11 -04:00
int font_size = static_cast<int>(size.y / 54.85f); // manually tested
2020-12-02 16:19:16 -05:00
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();
2021-06-18 12:49:11 -04:00
if (Globals::m_fScreenSize.x != -1 && Globals::m_fScreenSize.y != -1)
2021-02-19 02:41:50 -05:00
{
2021-06-18 12:49:11 -04:00
Globals::m_fMenuSize.x += (size.x - Globals::m_fScreenSize.x) / 4.0f;
Globals::m_fMenuSize.y += (size.y - Globals::m_fScreenSize.y) / 1.2f;
2021-02-19 02:41:50 -05:00
}
ImGuiStyle* style = &ImGui::GetStyle();
float scale_x = size.x / 1366.0f;
float scale_y = size.y / 768.0f;
style->FramePadding = ImVec2(5 * scale_x, 3 * scale_y);
style->ItemSpacing = ImVec2(8 * scale_x, 4 * scale_y);
style->ScrollbarSize = 12 * scale_x;
style->IndentSpacing = 20 * scale_x;
2021-02-24 16:54:45 -05:00
style->ItemInnerSpacing = ImVec2(4 * scale_x, 4 * scale_y);
2021-02-19 02:41:50 -05:00
2021-06-18 12:49:11 -04:00
Globals::m_fScreenSize = size;
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();
2021-06-18 12:49:11 -04:00
if (window_callback != nullptr)
2021-02-19 02:41:50 -05:00
window_callback();
2020-12-02 16:19:16 -05:00
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
{
2021-06-18 12:49:11 -04:00
Globals::m_bInit = true;
2020-12-02 16:19:16 -05:00
ImGui::CreateContext();
ImGuiStyle& style = ImGui::GetStyle();
ImGui_ImplWin32_Init(RsGlobal.ps->window);
2021-02-24 16:54:45 -05:00
2021-01-22 16:47:22 -05:00
// shift trigger fix
2021-02-24 16:54:45 -05:00
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-11 13:07:10 -05:00
Globals::device = ptr;
ImGui_ImplDX9_Init(reinterpret_cast<IDirect3DDevice9*>(Globals::device));
}
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
2021-06-18 12:49:11 -04:00
reinterpret_cast<IDXGISwapChain*>(ptr)->GetDevice(__uuidof(ID3D11Device), &Globals::device);
2021-01-11 13:07:10 -05:00
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();
2021-06-18 12:49:11 -04:00
io.IniFilename = nullptr;
io.LogFilename = nullptr;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad && ImGuiConfigFlags_NavEnableKeyboard;
2020-12-02 16:19:16 -05:00
style.WindowTitleAlign = ImVec2(0.5, 0.5);
oWndProc = (WNDPROC)SetWindowLongPtr(RsGlobal.ps->window, GWL_WNDPROC, (LRESULT)WndProc);
2020-12-02 16:19:16 -05:00
}
}
2020-12-02 16:19:16 -05:00
HRESULT Hook::Dx9Handler(IDirect3DDevice9* pDevice)
{
RenderFrame(pDevice);
return oEndScene(pDevice);
}
HRESULT Hook::Dx11Handler(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags)
{
RenderFrame(pSwapChain);
return oPresent11(pSwapChain, SyncInterval, Flags);
2020-12-02 16:19:16 -05:00
}
// Thanks imring
void Hook::ShowMouse(bool state)
{
if (state)
{
2021-01-29 11:52:39 -05:00
CPad::NewMouseControllerState.X = 0;
CPad::NewMouseControllerState.Y = 0;
patch::SetUChar(0x6194A0, 0xC3);
2021-01-29 17:09:48 -05:00
2021-02-25 17:45:41 -05:00
// Don't nop this, WindowedMode uses it
2021-01-29 17:09:48 -05:00
// patch::Nop(0x53F417, 5); // don't call CPad__getMouseState
patch::SetUChar(0x746ED0, 0xC3);
patch::SetRaw(0x53F41F, (void*)"\x33\xC0\x0F\x84", 4); // disable camera mouse movement
}
else
{
2021-06-18 12:49:11 -04:00
if (m_bMouseVisibility != m_bShowMouse)
2021-01-22 16:47:22 -05:00
{
2021-02-24 16:54:45 -05:00
patch::SetUChar(0x6194A0, 0xE9); // jmp setup
2021-01-29 17:09:48 -05:00
patch::SetUChar(0x746ED0, 0xA1);
2021-06-18 12:49:11 -04:00
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
2021-01-22 16:47:22 -05:00
}
}
2021-06-18 12:49:11 -04:00
if (m_bMouseVisibility != m_bShowMouse)
2021-01-22 17:30:43 -05:00
{
2021-02-13 04:08:37 -05:00
CPad::ClearMouseHistory();
CPad::UpdatePads();
2020-12-10 14:36:18 -05:00
2021-02-24 16:54:45 -05:00
// TODO: Replace this with windows cursor
2021-01-22 17:30:43 -05:00
ImGui::GetIO().MouseDrawCursor = state;
2021-02-24 16:54:45 -05:00
2021-01-29 11:52:39 -05:00
CPad::NewMouseControllerState.X = 0;
CPad::NewMouseControllerState.Y = 0;
2021-06-18 12:49:11 -04:00
m_bMouseVisibility = m_bShowMouse;
2021-01-22 17:30:43 -05:00
}
}
2020-12-02 16:19:16 -05:00
Hook::Hook()
{
ImGui::CreateContext();
2021-02-24 16:54:45 -05:00
2021-02-25 17:45:41 -05:00
// gtaRenderHook
2021-06-18 12:49:11 -04:00
if (init(kiero::RenderType::D3D11) == kiero::Status::Success)
2020-12-02 16:19:16 -05:00
{
2021-02-25 17:45:41 -05:00
Globals::renderer = Render_DirectX11;
kiero::bind(8, (void**)&oPresent11, Dx11Handler);
2021-02-24 16:54:45 -05:00
}
else
{
2021-06-18 12:49:11 -04:00
if (init(kiero::RenderType::D3D9) == kiero::Status::Success)
2020-12-02 16:19:16 -05:00
{
2021-02-25 17:45:41 -05:00
Globals::renderer = Render_DirectX9;
kiero::bind(16, (void**)&oReset, Reset);
kiero::bind(42, (void**)&oEndScene, Dx9Handler);
2020-12-02 16:19:16 -05:00
}
2021-02-24 16:54:45 -05:00
}
2020-12-02 16:19:16 -05:00
}
Hook::~Hook()
{
2021-02-25 17:45:41 -05:00
SetWindowLongPtr(RsGlobal.ps->window, GWL_WNDPROC, (LRESULT)oWndProc);
2020-12-02 16:19:16 -05:00
ImGui_ImplDX9_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
kiero::shutdown();
2021-06-18 12:49:11 -04:00
}