diff --git a/CMakeLists.txt b/CMakeLists.txt index a154f357..65794384 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,12 +74,12 @@ FetchContent_MakeAvailable(kananlib) set(SAFETYHOOK_FETCH_ZYDIS ON) -message(STATUS "Fetching safetyhook (v0.1.0-pre)...") +message(STATUS "Fetching safetyhook (5a81074882a84ff870da48bc4955addef5ccc6e7)...") FetchContent_Declare(safetyhook GIT_REPOSITORY - "https://github.com/cursey/safetyhook" + "https://github.com/praydog/safetyhook" GIT_TAG - v0.1.0-pre + 5a81074882a84ff870da48bc4955addef5ccc6e7 ) FetchContent_MakeAvailable(safetyhook) diff --git a/cmake.toml b/cmake.toml index 3a5ed15c..5e80b170 100644 --- a/cmake.toml +++ b/cmake.toml @@ -50,8 +50,8 @@ git = "https://github.com/cursey/kananlib" tag = "ba66ffaeecfd201ac66310ddf0a4a98140f1441f" [fetch-content.safetyhook] -git = "https://github.com/cursey/safetyhook" -tag = "v0.1.0-pre" +git = "https://github.com/praydog/safetyhook" +tag = "5a81074882a84ff870da48bc4955addef5ccc6e7" 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 aed08419..bc57e9fe 100644 --- a/src/hooks/D3D12Hook.cpp +++ b/src/hooks/D3D12Hook.cpp @@ -294,17 +294,17 @@ bool D3D12Hook::hook() { } try { - safetyhook::ThreadFreezer suspender{}; - spdlog::info("Initializing hooks"); + safetyhook::execute_while_frozen([&] { + spdlog::info("Initializing hooks"); + m_present_hook.reset(); + m_swapchain_hook.reset(); - m_present_hook.reset(); - m_swapchain_hook.reset(); + m_is_phase_1 = true; - m_is_phase_1 = true; - - auto& present_fn = (*(void***)swap_chain)[8]; // Present - m_present_hook = std::make_unique(&present_fn, (void*)&D3D12Hook::present); - m_hooked = true; + auto& present_fn = (*(void***)swap_chain)[8]; // Present + m_present_hook = std::make_unique(&present_fn, (void*)&D3D12Hook::present); + 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 863e61bc..ad080ca1 100644 --- a/src/mods/vr/FFakeStereoRenderingHook.cpp +++ b/src/mods/vr/FFakeStereoRenderingHook.cpp @@ -4556,7 +4556,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 @@ -4613,7 +4613,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; @@ -5264,7 +5264,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 e75bdf23..54dc95ad 100644 --- a/src/mods/vr/IXRTrackingSystemHook.cpp +++ b/src/mods/vr/IXRTrackingSystemHook.cpp @@ -1412,7 +1412,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(); @@ -1422,7 +1422,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;