CheatMenuSA/src/hook.cpp

302 lines
6.5 KiB
C++
Raw Normal View History

2021-07-21 13:31:02 -04:00
#include "pch.h"
2021-09-20 08:41:40 -04:00
#include "hook.h"
2021-09-14 08:31:29 -04:00
#include "../depend/kiero/kiero.h"
#include "../depend/kiero/minhook/MinHook.h"
2021-09-20 08:41:40 -04:00
#include "../depend/imgui/imgui_impl_dx9.h"
#include "../depend/imgui/imgui_impl_dx11.h"
#include "../depend/imgui/imgui_impl_win32.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);
if (ImGui::GetIO().WantTextInput)
{
2021-08-06 11:53:18 -04:00
#ifdef GTASA
Call<0x53F1E0>(); // CPad::ClearKeyboardHistory
2021-08-06 11:53:18 -04:00
#endif
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;
}
2021-02-25 17:45:41 -05:00
2020-12-02 16:19:16 -05:00
ImGuiIO& io = ImGui::GetIO();
2021-09-20 08:41:40 -04:00
static bool bInit = false;
2020-12-02 16:19:16 -05:00
2021-09-20 08:41:40 -04:00
if (bInit)
2021-06-18 12:49:11 -04:00
{
ShowMouse(m_bShowMouse);
2021-02-19 02:41:50 -05:00
// handle window scaling here
2021-09-20 08:41:40 -04:00
static ImVec2 fScreenSize = ImVec2(-1, -1);
2021-02-24 16:54:45 -05:00
ImVec2 size(screen::GetScreenWidth(), screen::GetScreenHeight());
2021-09-20 08:41:40 -04:00
if (fScreenSize.x != size.x && fScreenSize.y != size.y)
2020-12-02 16:19:16 -05:00
{
int fontSize = 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", fontSize);
2020-12-02 16:19:16 -05:00
io.Fonts->Build();
2021-09-20 08:41:40 -04:00
if (gRenderer == Render_DirectX9)
{
2021-01-11 13:07:10 -05:00
ImGui_ImplDX9_InvalidateDeviceObjects();
}
2021-01-11 13:07:10 -05:00
else
{
2021-01-11 13:07:10 -05:00
ImGui_ImplDX11_InvalidateDeviceObjects();
}
2021-01-11 13:07:10 -05:00
2021-02-19 02:41:50 -05:00
ImGuiStyle* style = &ImGui::GetStyle();
float scaleX = size.x / 1366.0f;
float scaleY = size.y / 768.0f;
2021-02-19 02:41:50 -05:00
style->FramePadding = ImVec2(5 * scaleX, 3 * scaleY);
style->ItemSpacing = ImVec2(8 * scaleX, 4 * scaleY);
style->ScrollbarSize = 12 * scaleX;
style->IndentSpacing = 20 * scaleX;
style->ItemInnerSpacing = ImVec2(4 * scaleX, 4 * scaleY);
2021-02-19 02:41:50 -05:00
2021-09-20 08:41:40 -04:00
fScreenSize = size;
2020-12-02 16:19:16 -05:00
}
ImGui_ImplWin32_NewFrame();
2021-09-20 08:41:40 -04:00
if (gRenderer == Render_DirectX9)
{
2021-01-11 13:07:10 -05:00
ImGui_ImplDX9_NewFrame();
}
2021-01-11 13:07:10 -05:00
else
{
2021-01-11 13:07:10 -05:00
ImGui_ImplDX11_NewFrame();
}
2021-01-11 13:07:10 -05:00
2020-12-02 16:19:16 -05:00
ImGui::NewFrame();
if (windowCallback != nullptr)
{
windowCallback();
}
2020-12-02 16:19:16 -05:00
ImGui::EndFrame();
ImGui::Render();
2021-01-11 13:07:10 -05:00
2021-09-20 08:41:40 -04:00
if (gRenderer == Render_DirectX9)
{
2021-01-11 13:07:10 -05:00
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
}
2021-01-11 13:07:10 -05:00
else
{
2021-01-11 13:07:10 -05:00
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
}
2020-12-02 16:19:16 -05:00
}
else
{
2021-09-20 08:41:40 -04:00
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-08-06 11:53:18 -04:00
#ifdef GTASA
2021-09-20 08:41:40 -04:00
// shift trigger fix
2021-02-24 16:54:45 -05:00
patch::Nop(0x00531155, 5);
2021-08-06 11:53:18 -04:00
#endif
2020-12-02 16:19:16 -05:00
2021-09-20 08:41:40 -04:00
if (gRenderer == Render_DirectX9)
{
2021-09-20 08:41:40 -04:00
ImGui_ImplDX9_Init(reinterpret_cast<IDirect3DDevice9*>(ptr));
}
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-09-20 08:41:40 -04:00
reinterpret_cast<IDXGISwapChain*>(ptr)->GetDevice(__uuidof(ID3D11Device), &ptr);
2021-01-11 13:07:10 -05:00
ID3D11DeviceContext* context;
2021-09-20 08:41:40 -04:00
reinterpret_cast<ID3D11Device*>(ptr)->GetImmediateContext(&context);
2020-12-02 16:19:16 -05:00
2021-09-20 08:41:40 -04:00
ImGui_ImplDX11_Init(reinterpret_cast<ID3D11Device*>(ptr), 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;
2021-08-20 22:54:11 -04:00
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
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
}
void Hook::ShowMouse(bool state)
{
2021-06-18 12:49:11 -04:00
if (m_bMouseVisibility != m_bShowMouse)
2021-01-22 17:30:43 -05:00
{
ImGui::GetIO().MouseDrawCursor = state;
2021-02-24 16:54:45 -05:00
2021-08-06 11:53:18 -04:00
#ifdef GTASA
2021-08-01 21:41:48 -04:00
Hook::ApplyMouseFix(); // Reapply the patches
2021-08-06 11:53:18 -04:00
#elif GTAVC
2021-08-01 21:41:48 -04:00
if (m_bShowMouse)
{
patch::SetUChar(0x6020A0, 0xC3); // psSetMousePos
patch::Nop(0x4AB6CA, 5); // don't call CPad::UpdateMouse()
}
else
{
patch::SetUChar(0x6020A0, 0x53);
patch::SetRaw(0x4AB6CA, (char*)"\xE8\x51\x21\x00\x00", 5);
}
2021-08-06 11:53:18 -04:00
#endif
2021-08-01 21:41:48 -04:00
2021-01-29 11:52:39 -05:00
CPad::NewMouseControllerState.X = 0;
CPad::NewMouseControllerState.Y = 0;
2021-08-01 21:41:48 -04:00
CPad::ClearMouseHistory();
CPad::UpdatePads();
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
// Nvidia Overlay crash fix
2021-07-09 05:02:54 -04:00
if (init(kiero::RenderType::D3D9) == kiero::Status::Success)
2020-12-02 16:19:16 -05:00
{
2021-09-20 08:41:40 -04:00
gRenderer = Render_DirectX9;
2021-07-09 05:02:54 -04:00
kiero::bind(16, (void**)&oReset, Reset);
kiero::bind(42, (void**)&oEndScene, Dx9Handler);
2021-02-24 16:54:45 -05:00
}
else
{
2021-07-09 05:02:54 -04:00
// gtaRenderHook
if (init(kiero::RenderType::D3D11) == kiero::Status::Success)
2020-12-02 16:19:16 -05:00
{
2021-09-20 08:41:40 -04:00
gRenderer = Render_DirectX11;
2021-07-09 05:02:54 -04:00
kiero::bind(8, (void**)&oPresent11, Dx11Handler);
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
}
2021-08-01 21:41:48 -04:00
#ifdef GTASA
struct Mouse
{
unsigned int x, y;
unsigned int wheelDelta;
2021-09-14 08:31:29 -04:00
unsigned char buttons[8];
};
struct MouseInfo
{
int x, y, wheelDelta;
} mouseInfo;
static BOOL __stdcall _SetCursorPos(int X, int Y)
{
if (Hook::m_bShowMouse || GetActiveWindow() != RsGlobal.ps->window)
2021-09-14 08:31:29 -04:00
{
return 1;
2021-09-14 08:31:29 -04:00
}
mouseInfo.x = X;
mouseInfo.y = Y;
return SetCursorPos(X, Y);
}
static LRESULT __stdcall _DispatchMessage(MSG* lpMsg)
{
if (lpMsg->message == WM_MOUSEWHEEL && !Hook::m_bShowMouse)
{
2021-07-31 14:29:50 -04:00
mouseInfo.wheelDelta += GET_WHEEL_DELTA_WPARAM(lpMsg->wParam);
}
return DispatchMessageA(lpMsg);
}
static int _cdecl _GetMouseState(Mouse* pMouse)
{
if (Hook::m_bShowMouse)
2021-07-31 14:29:50 -04:00
{
return -1;
2021-07-31 14:29:50 -04:00
}
struct tagPOINT Point;
pMouse->x = 0;
pMouse->y = 0;
pMouse->wheelDelta = mouseInfo.wheelDelta;
GetCursorPos(&Point);
if (mouseInfo.x >= 0)
{
2021-09-14 08:31:29 -04:00
pMouse->x = int(1.6f*(Point.x - mouseInfo.x)); // hacky fix
}
if (mouseInfo.y >= 0)
{
pMouse->y = int(Point.y - mouseInfo.y);
}
mouseInfo.wheelDelta = 0;
2021-09-14 08:31:29 -04:00
pMouse->buttons[0] = (GetAsyncKeyState(1) >> 8);
pMouse->buttons[1] = (GetAsyncKeyState(2) >> 8);
pMouse->buttons[2] = (GetAsyncKeyState(4) >> 8);
pMouse->buttons[3] = (GetAsyncKeyState(5) >> 8);
pMouse->buttons[4] = (GetAsyncKeyState(6) >> 8);
return 0;
}
void Hook::ApplyMouseFix()
{
patch::ReplaceFunctionCall(0x53F417, _GetMouseState);
patch::Nop(0x57C59B, 1);
patch::ReplaceFunctionCall(0x57C59C, _SetCursorPos);
patch::Nop(0x81E5D4, 1);
patch::ReplaceFunctionCall(0x81E5D5, _SetCursorPos);
2021-07-01 18:03:46 -04:00
patch::Nop(0x74542D, 1);
patch::ReplaceFunctionCall(0x74542E, _SetCursorPos);
patch::Nop(0x748A7C, 1);
patch::ReplaceFunctionCall(0x748A7D, _DispatchMessage);
2021-09-14 08:31:29 -04:00
patch::SetChar(0x746A08, 32); // diMouseOffset
patch::SetChar(0x746A58, 32); // diDeviceoffset
}
2021-08-01 21:41:48 -04:00
#endif