diff --git a/CMakeLists.txt b/CMakeLists.txt index fb6686a6..41c9976e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,12 +73,12 @@ FetchContent_MakeAvailable(kananlib) set(SAFETYHOOK_FETCH_ZYDIS ON) -message(STATUS "Fetching safetyhook (v0.1.0-pre)...") +message(STATUS "Fetching safetyhook (d0b3e864a7246d530d22818b7707af879c0ebd1e)...") FetchContent_Declare(safetyhook GIT_REPOSITORY "https://github.com/cursey/safetyhook" GIT_TAG - v0.1.0-pre + d0b3e864a7246d530d22818b7707af879c0ebd1e ) FetchContent_MakeAvailable(safetyhook) diff --git a/cmake.toml b/cmake.toml index c8ea3df4..7cc09ecf 100644 --- a/cmake.toml +++ b/cmake.toml @@ -50,7 +50,7 @@ tag = "19776b59a52285862cb1fbf483cd113b6b3dd642" [fetch-content.safetyhook] git = "https://github.com/cursey/safetyhook" -tag = "v0.1.0-pre" +tag = "d0b3e864a7246d530d22818b7707af879c0ebd1e" cmake-before=""" set(SAFETYHOOK_FETCH_ZYDIS ON) """ diff --git a/src/hooks/D3D11Hook.cpp b/src/hooks/D3D11Hook.cpp index 9c16d663..b8d12b48 100644 --- a/src/hooks/D3D11Hook.cpp +++ b/src/hooks/D3D11Hook.cpp @@ -77,17 +77,18 @@ bool D3D11Hook::hook() { } try { - safetyhook::ThreadFreezer suspender{}; - m_present_hook.reset(); - m_resize_buffers_hook.reset(); + safetyhook::execute_while_frozen([&] { + m_present_hook.reset(); + m_resize_buffers_hook.reset(); - auto& present_fn = (*(void***)swap_chain)[8]; - auto& resize_buffers_fn = (*(void***)swap_chain)[13]; + auto& present_fn = (*(void***)swap_chain)[8]; + auto& resize_buffers_fn = (*(void***)swap_chain)[13]; - m_present_hook = std::make_unique(&present_fn, (void*)&D3D11Hook::present); - m_resize_buffers_hook = std::make_unique(&resize_buffers_fn, (void*)&D3D11Hook::resize_buffers); + m_present_hook = std::make_unique(&present_fn, (void*)&D3D11Hook::present); + m_resize_buffers_hook = std::make_unique(&resize_buffers_fn, (void*)&D3D11Hook::resize_buffers); - m_hooked = true; + m_hooked = true; + }); } catch (const std::exception& e) { spdlog::error("Failed to hook D3D11: {}", e.what()); m_hooked = false; diff --git a/src/hooks/D3D12Hook.cpp b/src/hooks/D3D12Hook.cpp index c5936a7e..29af6946 100644 --- a/src/hooks/D3D12Hook.cpp +++ b/src/hooks/D3D12Hook.cpp @@ -294,24 +294,25 @@ bool D3D12Hook::hook() { } try { - safetyhook::ThreadFreezer suspender{}; - spdlog::info("Initializing hooks"); + safetyhook::execute_while_frozen([&] { + spdlog::info("Initializing hooks"); - m_present_hook.reset(); - m_resize_buffers_hook.reset(); - m_resize_target_hook.reset(); + m_present_hook.reset(); + m_resize_buffers_hook.reset(); + m_resize_target_hook.reset(); - auto& present_fn = (*(void***)swap_chain)[8]; // Present - auto& resize_buffers_fn = (*(void***)swap_chain)[13]; // ResizeBuffers - auto& resize_target_fn = (*(void***)swap_chain)[14]; // ResizeTarget - auto& create_swap_chain_fn = (*(void***)factory)[15]; // CreateSwapChainForHwnd + auto& present_fn = (*(void***)swap_chain)[8]; // Present + auto& resize_buffers_fn = (*(void***)swap_chain)[13]; // ResizeBuffers + auto& resize_target_fn = (*(void***)swap_chain)[14]; // ResizeTarget + auto& create_swap_chain_fn = (*(void***)factory)[15]; // CreateSwapChainForHwnd - m_present_hook = std::make_unique(&present_fn, (void*)&D3D12Hook::present); - m_resize_buffers_hook = std::make_unique(&resize_buffers_fn, (void*)&D3D12Hook::resize_buffers); - m_resize_target_hook = std::make_unique(&resize_target_fn, (void*)&D3D12Hook::resize_target); - //m_create_swap_chain_hook = std::make_unique(create_swap_chain_fn, (uintptr_t)&D3D12Hook::create_swap_chain); + m_present_hook = std::make_unique(&present_fn, (void*)&D3D12Hook::present); + m_resize_buffers_hook = std::make_unique(&resize_buffers_fn, (void*)&D3D12Hook::resize_buffers); + m_resize_target_hook = std::make_unique(&resize_target_fn, (void*)&D3D12Hook::resize_target); + //m_create_swap_chain_hook = std::make_unique(create_swap_chain_fn, (uintptr_t)&D3D12Hook::create_swap_chain); - m_hooked = true; + m_hooked = true; + }); } catch (const std::exception& e) { spdlog::error("Failed to initialize hooks: {}", e.what()); m_hooked = false; diff --git a/src/hooks/WindowsMessageHook.cpp b/src/hooks/WindowsMessageHook.cpp index e0a61a4e..b13d6bcc 100644 --- a/src/hooks/WindowsMessageHook.cpp +++ b/src/hooks/WindowsMessageHook.cpp @@ -41,27 +41,27 @@ WindowsMessageHook::WindowsMessageHook(HWND wnd) std::lock_guard _{ g_proc_mutex }; spdlog::info("Initializing WindowsMessageHook"); - safetyhook::ThreadFreezer suspender{}; + safetyhook::execute_while_frozen([this] { + g_windows_message_hook = this; - g_windows_message_hook = this; + // Save the original window procedure. + m_original_proc = (WNDPROC)GetWindowLongPtr(m_wnd, GWLP_WNDPROC); - // Save the original window procedure. - m_original_proc = (WNDPROC)GetWindowLongPtr(m_wnd, GWLP_WNDPROC); + // Set it to our "hook" procedure. + SetWindowLongPtr(m_wnd, GWLP_WNDPROC, (LONG_PTR)&window_proc); - // Set it to our "hook" procedure. - SetWindowLongPtr(m_wnd, GWLP_WNDPROC, (LONG_PTR)&window_proc); - - spdlog::info("Hooked Windows message handler"); + spdlog::info("Hooked Windows message handler"); + }); } WindowsMessageHook::~WindowsMessageHook() { std::lock_guard _{ g_proc_mutex }; spdlog::info("Destroying WindowsMessageHook"); - safetyhook::ThreadFreezer suspender{}; - - remove(); - g_windows_message_hook = nullptr; + safetyhook::execute_while_frozen([this] { + remove(); + g_windows_message_hook = nullptr; + }); } bool WindowsMessageHook::remove() { diff --git a/src/mods/vr/FFakeStereoRenderingHook.cpp b/src/mods/vr/FFakeStereoRenderingHook.cpp index ecc84427..0397dd5e 100644 --- a/src/mods/vr/FFakeStereoRenderingHook.cpp +++ b/src/mods/vr/FFakeStereoRenderingHook.cpp @@ -3723,7 +3723,7 @@ void FFakeStereoRenderingHook::post_calculate_stereo_projection_matrix(safetyhoo return; } - auto vfunc = utility::find_virtual_function_start(g_hook->m_calculate_stereo_projection_matrix_post_hook.target()); + auto vfunc = utility::find_virtual_function_start(g_hook->m_calculate_stereo_projection_matrix_post_hook.target_address()); if (!vfunc) { // attempt to hook GetProjectionData instead to get the localplayer @@ -3780,7 +3780,7 @@ void FFakeStereoRenderingHook::post_calculate_stereo_projection_matrix(safetyhoo if (!vfunc) { SPDLOG_INFO("Could not find function via normal means, scanning for int3s..."); - const auto ref = utility::scan_reverse(g_hook->m_calculate_stereo_projection_matrix_post_hook.target(), 0x2000, "CC CC CC"); + const auto ref = utility::scan_reverse(g_hook->m_calculate_stereo_projection_matrix_post_hook.target_address(), 0x2000, "CC CC CC"); if (ref) { vfunc = *ref + 3; @@ -4332,7 +4332,7 @@ void VRRenderTargetManager_Base::pre_texture_hook_callback(safetyhook::Context& SPDLOG_INFO("Emu landed at {:x}", emu_ctx.ctx->Registers.RegRip); func_ptr = emu_ctx.ctx->Registers.RegRip; } else { - const auto target = g_hook->get_render_target_manager()->pre_texture_hook.target(); + const auto target = g_hook->get_render_target_manager()->pre_texture_hook.target_address(); func_ptr = target + 5 + *(int32_t*)&rtm->texture_create_insn_bytes.data()[1]; } diff --git a/src/mods/vr/IXRTrackingSystemHook.cpp b/src/mods/vr/IXRTrackingSystemHook.cpp index 3b58cdde..27794b6b 100644 --- a/src/mods/vr/IXRTrackingSystemHook.cpp +++ b/src/mods/vr/IXRTrackingSystemHook.cpp @@ -796,7 +796,7 @@ void* IXRTrackingSystemHook::process_view_rotation_analyzer(void* a1, size_t a2, if (std::abs((intptr_t)a3 - stack_pointer) > 0x1000 || std::abs((intptr_t)a4 - stack_pointer) > 0x1000 || a3 == a4) { SPDLOG_ERROR("Function we hooked for ProcessViewRotation is not the correct one. Analyzing next function."); - const auto function_addr = g_hook->m_process_view_rotation_hook.target(); + const auto function_addr = g_hook->m_process_view_rotation_hook.target_address(); detail::functions[function_addr].process_view_rotation_analysis_failed = true; g_hook->m_process_view_rotation_hook.reset(); @@ -806,7 +806,7 @@ void* IXRTrackingSystemHook::process_view_rotation_analyzer(void* a1, size_t a2, const auto target = g_hook->m_process_view_rotation_hook.target(); g_hook->m_process_view_rotation_hook.reset(); //g_hook->m_process_view_rotation_hook = std::make_unique((void**)g_hook->m_addr_of_process_view_rotation_ptr, &IXRTrackingSystemHook::process_view_rotation); - g_hook->m_process_view_rotation_hook = safetyhook::create_inline((void*)target, &IXRTrackingSystemHook::process_view_rotation); + g_hook->m_process_view_rotation_hook = safetyhook::create_inline(target, &IXRTrackingSystemHook::process_view_rotation); } return result;