Skip to content

Commit

Permalink
Update safetyhook dependency
Browse files Browse the repository at this point in the history
* Fix uses of safetyhook::ThreadFreezer to use safetyhook::execute_while_frozen(...)
* Fix uses of <hook>.target() to use <hook>.target_address()
  • Loading branch information
cursey authored and praydog committed Dec 17, 2023
1 parent 65f643c commit 72e0e1b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 37 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,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)

Expand Down
2 changes: 1 addition & 1 deletion cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ tag = "ba66ffaeecfd201ac66310ddf0a4a98140f1441f"

[fetch-content.safetyhook]
git = "https://github.com/cursey/safetyhook"
tag = "v0.1.0-pre"
tag = "d0b3e864a7246d530d22818b7707af879c0ebd1e"
cmake-before="""
set(SAFETYHOOK_FETCH_ZYDIS ON)
"""
Expand Down
17 changes: 9 additions & 8 deletions src/hooks/D3D11Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PointerHook>(&present_fn, (void*)&D3D11Hook::present);
m_resize_buffers_hook = std::make_unique<PointerHook>(&resize_buffers_fn, (void*)&D3D11Hook::resize_buffers);
m_present_hook = std::make_unique<PointerHook>(&present_fn, (void*)&D3D11Hook::present);
m_resize_buffers_hook = std::make_unique<PointerHook>(&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;
Expand Down
18 changes: 9 additions & 9 deletions src/hooks/D3D12Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PointerHook>(&present_fn, (void*)&D3D12Hook::present);
m_hooked = true;
auto& present_fn = (*(void***)swap_chain)[8]; // Present
m_present_hook = std::make_unique<PointerHook>(&present_fn, (void*)&D3D12Hook::present);
m_hooked = true;
});
} catch (const std::exception& e) {
spdlog::error("Failed to initialize hooks: {}", e.what());
m_hooked = false;
Expand Down
24 changes: 12 additions & 12 deletions src/hooks/WindowsMessageHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions src/mods/vr/FFakeStereoRenderingHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}

Expand Down
4 changes: 2 additions & 2 deletions src/mods/vr/IXRTrackingSystemHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<PointerHook>((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;
Expand Down

0 comments on commit 72e0e1b

Please sign in to comment.