#include "common.hpp" #include "function_types.hpp" #include "logger.hpp" #include "gta/array.hpp" #include "gta/player.hpp" #include "gta/script_thread.hpp" #include "gui.hpp" #include "hooking.hpp" #include "memory/module.hpp" #include "natives.hpp" #include "pointers.hpp" #include "renderer.hpp" #include "script_mgr.hpp" #include namespace big { static GtaThread *find_script_thread(rage::joaat_t hash) { for (auto thread : *g_pointers->m_script_threads) { if (thread && thread->m_context.m_thread_id && thread->m_handler && thread->m_script_hash == hash) { return thread; } } return nullptr; } hooking::hooking() : m_swapchain_hook(*g_pointers->m_swapchain, hooks::swapchain_num_funcs), m_set_cursor_pos_hook("SetCursorPos", memory::module("user32.dll").get_export("SetCursorPos").as(), &hooks::set_cursor_pos), m_run_script_threads_hook("Script hook", g_pointers->m_run_script_threads, &hooks::run_script_threads), m_convert_thread_to_fiber_hook("ConvertThreadToFiber", memory::module("kernel32.dll").get_export("ConvertThreadToFiber").as(), &hooks::convert_thread_to_fiber) { m_swapchain_hook.hook(hooks::swapchain_present_index, &hooks::swapchain_present); m_swapchain_hook.hook(hooks::swapchain_resizebuffers_index, &hooks::swapchain_resizebuffers); g_hooking = this; } hooking::~hooking() { if (m_enabled) disable(); g_hooking = nullptr; } void hooking::enable() { m_swapchain_hook.enable(); m_og_wndproc = reinterpret_cast(SetWindowLongPtrW(g_pointers->m_hwnd, GWLP_WNDPROC, reinterpret_cast(&hooks::wndproc))); m_set_cursor_pos_hook.enable(); m_run_script_threads_hook.enable(); m_convert_thread_to_fiber_hook.enable(); ensure_dynamic_hooks(); m_enabled = true; } void hooking::disable() { m_enabled = false; if (m_main_persistent_hook) { m_main_persistent_hook->disable(); } m_convert_thread_to_fiber_hook.disable(); m_run_script_threads_hook.disable(); m_set_cursor_pos_hook.disable(); SetWindowLongPtrW(g_pointers->m_hwnd, GWLP_WNDPROC, reinterpret_cast(m_og_wndproc)); m_swapchain_hook.disable(); } void hooking::ensure_dynamic_hooks() { if (!m_main_persistent_hook) { if (auto main_persistent = find_script_thread(RAGE_JOAAT("main_persistent"))) { m_main_persistent_hook = std::make_unique(main_persistent->m_handler, hooks::main_persistent_num_funcs); m_main_persistent_hook->hook(hooks::main_persistent_dtor_index, &hooks::main_persistent_dtor); m_main_persistent_hook->hook(hooks::main_persistent_is_networked_index, &hooks::main_persistent_is_networked); m_main_persistent_hook->enable(); } } } minhook_keepalive::minhook_keepalive() { MH_Initialize(); } minhook_keepalive::~minhook_keepalive() { MH_Uninitialize(); } bool hooks::run_script_threads(std::uint32_t ops_to_execute) { if (g_running) { g_script_mgr.tick(); } return g_hooking->m_run_script_threads_hook.get_original()(ops_to_execute); } void *hooks::convert_thread_to_fiber(void *param) { if (IsThreadAFiber()) { return GetCurrentFiber(); } return g_hooking->m_convert_thread_to_fiber_hook.get_original()(param); } HRESULT hooks::swapchain_present(IDXGISwapChain *this_, UINT sync_interval, UINT flags) { if (g_running) { g_renderer->on_present(); } return g_hooking->m_swapchain_hook.get_original(swapchain_present_index)(this_, sync_interval, flags); } HRESULT hooks::swapchain_resizebuffers(IDXGISwapChain * this_, UINT buffer_count, UINT width, UINT height, DXGI_FORMAT new_format, UINT swapchain_flags) { if (g_running) { g_renderer->pre_reset(); auto result = g_hooking->m_swapchain_hook.get_original(swapchain_resizebuffers_index) (this_, buffer_count, width, height, new_format, swapchain_flags); if (SUCCEEDED(result)) { g_renderer->post_reset(); } return result; } return g_hooking->m_swapchain_hook.get_original(swapchain_resizebuffers_index) (this_, buffer_count, width, height, new_format, swapchain_flags); } LRESULT hooks::wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { if (g_running) { g_renderer->wndproc(hwnd, msg, wparam, lparam); } return CallWindowProcW(g_hooking->m_og_wndproc, hwnd, msg, wparam, lparam); } BOOL hooks::set_cursor_pos(int x, int y) { if (g_gui.m_opened) { return true; } return g_hooking->m_set_cursor_pos_hook.get_original()(x, y); } void hooks::main_persistent_dtor(CGameScriptHandler *this_, bool free_memory) { auto og_func = g_hooking->m_main_persistent_hook->get_original(main_persistent_dtor_index); g_hooking->m_main_persistent_hook->disable(); g_hooking->m_main_persistent_hook.reset(); return og_func(this_, free_memory); } bool hooks::main_persistent_is_networked(CGameScriptHandler *this_) { return *g_pointers->m_is_session_started; } }