Skip to content

Commit 2e8f65c

Browse files
committed
Replace polyhook with safetyhook
This greatly simplifies code and reduces boilerplate
1 parent 16cb35a commit 2e8f65c

File tree

13 files changed

+61
-120
lines changed

13 files changed

+61
-120
lines changed

UE4SS/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ target_link_libraries(${TARGET} PUBLIC
160160
MProgram ScopedTimer)
161161

162162
target_link_libraries(${TARGET} PUBLIC
163-
ImGui PolyHook_2 d3d11 GLFW opengl32 dbghelp)
163+
ImGui safetyhook d3d11 GLFW opengl32 dbghelp)
164164

165165
set_property(TARGET ${TARGET} PROPERTY INTERPROCEDURAL_OPTIMIZATION ON)
166166

UE4SS/include/CrashDumper.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <memory>
44

5-
#include <polyhook2/PE/IatHook.hpp>
5+
#include <safetyhook.hpp>
66

77
namespace RC
88
{
@@ -11,8 +11,7 @@ namespace RC
1111
private:
1212
bool enabled = false;
1313
void* m_previous_exception_filter = nullptr;
14-
std::unique_ptr<PLH::IatHook> m_set_unhandled_exception_filter_hook;
15-
uint64_t m_hook_trampoline_set_unhandled_exception_filter_hook;
14+
SafetyHookInline m_set_unhandled_exception_filter_hook;
1615

1716
public:
1817
CrashDumper();

UE4SS/include/UE4SSProgram.hpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <Unreal/TArray.hpp>
2222
#include <Unreal/UnrealVersion.hpp>
2323

24+
#include <safetyhook.hpp>
25+
2426
// Used to set up ImGui context and allocator in DLL mods
2527
#define UE4SS_ENABLE_IMGUI() \
2628
{ \
@@ -109,17 +111,10 @@ namespace RC
109111
std::mutex m_event_queue_mutex{};
110112

111113
private:
112-
std::unique_ptr<PLH::IatHook> m_load_library_a_hook;
113-
uint64_t m_hook_trampoline_load_library_a;
114-
115-
std::unique_ptr<PLH::IatHook> m_load_library_ex_a_hook;
116-
uint64_t m_hook_trampoline_load_library_ex_a;
117-
118-
std::unique_ptr<PLH::IatHook> m_load_library_w_hook;
119-
uint64_t m_hook_trampoline_load_library_w;
120-
121-
std::unique_ptr<PLH::IatHook> m_load_library_ex_w_hook;
122-
uint64_t m_hook_trampoline_load_library_ex_w;
114+
SafetyHookInline m_load_library_a_hook;
115+
SafetyHookInline m_load_library_ex_a_hook;
116+
SafetyHookInline m_load_library_w_hook;
117+
SafetyHookInline m_load_library_ex_w_hook;
123118

124119
public:
125120
static inline std::vector<std::unique_ptr<Mod>> m_mods;

UE4SS/src/CrashDumper.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,15 @@ namespace RC
7575

7676
CrashDumper::~CrashDumper()
7777
{
78-
m_set_unhandled_exception_filter_hook->unHook();
78+
m_set_unhandled_exception_filter_hook = {};
7979
SetUnhandledExceptionFilter(reinterpret_cast<LPTOP_LEVEL_EXCEPTION_FILTER>(m_previous_exception_filter));
8080
}
8181

8282
void CrashDumper::enable()
8383
{
8484
SetErrorMode(SEM_FAILCRITICALERRORS);
8585
m_previous_exception_filter = SetUnhandledExceptionFilter(ExceptionHandler);
86-
87-
m_set_unhandled_exception_filter_hook = std::make_unique<PLH::IatHook>("kernel32.dll",
88-
"SetUnhandledExceptionFilter",
89-
std::bit_cast<uint64_t>(&HookedSetUnhandledExceptionFilter),
90-
&m_hook_trampoline_set_unhandled_exception_filter_hook,
91-
L"");
92-
m_set_unhandled_exception_filter_hook->hook();
86+
m_set_unhandled_exception_filter_hook = safetyhook::create_inline(SetUnhandledExceptionFilter, HookedSetUnhandledExceptionFilter);
9387
this->enabled = true;
9488
}
9589

UE4SS/src/UE4SSProgram.cpp

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,31 @@ namespace RC
129129
void* HookedLoadLibraryA(const char* dll_name)
130130
{
131131
UE4SSProgram& program = UE4SSProgram::get_program();
132-
HMODULE lib = PLH::FnCast(program.m_hook_trampoline_load_library_a, &LoadLibraryA)(dll_name);
132+
HMODULE lib = program.m_load_library_a_hook.call<HMODULE>(dll_name);
133133
program.fire_dll_load_for_cpp_mods(to_wstring(dll_name));
134134
return lib;
135135
}
136136

137137
void* HookedLoadLibraryExA(const char* dll_name, void* file, int32_t flags)
138138
{
139139
UE4SSProgram& program = UE4SSProgram::get_program();
140-
HMODULE lib = PLH::FnCast(program.m_hook_trampoline_load_library_ex_a, &LoadLibraryExA)(dll_name, file, flags);
140+
HMODULE lib = program.m_load_library_ex_a_hook.call<HMODULE>(dll_name, file, flags);
141141
program.fire_dll_load_for_cpp_mods(to_wstring(dll_name));
142142
return lib;
143143
}
144144

145145
void* HookedLoadLibraryW(const wchar_t* dll_name)
146146
{
147147
UE4SSProgram& program = UE4SSProgram::get_program();
148-
HMODULE lib = PLH::FnCast(program.m_hook_trampoline_load_library_w, &LoadLibraryW)(dll_name);
148+
HMODULE lib = program.m_load_library_w_hook.call<HMODULE>(dll_name);
149149
program.fire_dll_load_for_cpp_mods(dll_name);
150150
return lib;
151151
}
152152

153153
void* HookedLoadLibraryExW(const wchar_t* dll_name, void* file, int32_t flags)
154154
{
155155
UE4SSProgram& program = UE4SSProgram::get_program();
156-
HMODULE lib = PLH::FnCast(program.m_hook_trampoline_load_library_ex_w, &LoadLibraryExW)(dll_name, file, flags);
156+
HMODULE lib = program.m_load_library_ex_w_hook.call<HMODULE>(dll_name, file, flags);
157157
program.fire_dll_load_for_cpp_mods(dll_name);
158158
return lib;
159159
}
@@ -229,33 +229,10 @@ namespace RC
229229
Output::send(STR("WITH_CASE_PRESERVING_NAME: No\n\n"));
230230
#endif
231231

232-
m_load_library_a_hook = std::make_unique<PLH::IatHook>("kernel32.dll",
233-
"LoadLibraryA",
234-
std::bit_cast<uint64_t>(&HookedLoadLibraryA),
235-
&m_hook_trampoline_load_library_a,
236-
L"");
237-
m_load_library_a_hook->hook();
238-
239-
m_load_library_ex_a_hook = std::make_unique<PLH::IatHook>("kernel32.dll",
240-
"LoadLibraryExA",
241-
std::bit_cast<uint64_t>(&HookedLoadLibraryExA),
242-
&m_hook_trampoline_load_library_ex_a,
243-
L"");
244-
m_load_library_ex_a_hook->hook();
245-
246-
m_load_library_w_hook = std::make_unique<PLH::IatHook>("kernel32.dll",
247-
"LoadLibraryW",
248-
std::bit_cast<uint64_t>(&HookedLoadLibraryW),
249-
&m_hook_trampoline_load_library_w,
250-
L"");
251-
m_load_library_w_hook->hook();
252-
253-
m_load_library_ex_w_hook = std::make_unique<PLH::IatHook>("kernel32.dll",
254-
"LoadLibraryExW",
255-
std::bit_cast<uint64_t>(&HookedLoadLibraryExW),
256-
&m_hook_trampoline_load_library_ex_w,
257-
L"");
258-
m_load_library_ex_w_hook->hook();
232+
m_load_library_a_hook = safetyhook::create_inline(LoadLibraryA, HookedLoadLibraryA);
233+
m_load_library_ex_a_hook = safetyhook::create_inline(LoadLibraryExA, HookedLoadLibraryExA);
234+
m_load_library_w_hook = safetyhook::create_inline(LoadLibraryW, HookedLoadLibraryW);
235+
m_load_library_ex_w_hook = safetyhook::create_inline(LoadLibraryExW, HookedLoadLibraryExW);
259236

260237
Unreal::UnrealInitializer::SetupUnrealModules();
261238

deps/first/ASMHelper/include/ASMHelper/ASMHelper.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace RC::ASM
99
{
1010
void* address{};
1111
ZydisDecodedInstruction raw{};
12+
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT]{};
1213
};
1314

1415
RC_ASM_API auto is_jmp_instruction(void* instruction_ptr) -> bool;

deps/first/ASMHelper/src/ASMHelper.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ namespace RC::ASM
1010
{
1111
auto instruction_ptr = static_cast<uint8_t*>(in_instruction_ptr);
1212
ZydisDecoder decoder{};
13-
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_ADDRESS_WIDTH_64);
13+
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
1414
ZyanUSize offset = 0;
15-
ZydisDecodedInstruction instruction{};
16-
while (ZYAN_SUCCESS(ZydisDecoderDecodeBuffer(&decoder, instruction_ptr + offset, 16 - offset, &instruction)))
15+
16+
Instruction instruction{in_instruction_ptr, {}, {}};
17+
while (ZYAN_SUCCESS(ZydisDecoderDecodeFull(&decoder, instruction_ptr + offset, 16 - offset, &instruction.raw, instruction.operands)))
1718
{
1819
break;
1920
}
20-
return {in_instruction_ptr, instruction};
21+
return instruction;
2122
}
2223

2324
auto is_jmp_instruction(void* in_instruction_ptr) -> bool
@@ -36,7 +37,7 @@ namespace RC::ASM
3637
{
3738
auto instruction = get_first_instruction_at_address(in_instruction_ptr);
3839
ZyanU64 resolved_address{};
39-
if (ZYAN_SUCCESS(ZydisCalcAbsoluteAddress(&instruction.raw, &instruction.raw.operands[0], std::bit_cast<ZyanU64>(in_instruction_ptr), &resolved_address)))
40+
if (ZYAN_SUCCESS(ZydisCalcAbsoluteAddress(&instruction.raw, &instruction.operands[0], std::bit_cast<ZyanU64>(in_instruction_ptr), &resolved_address)))
4041
{
4142
return std::bit_cast<void*>(resolved_address);
4243
}

deps/first/Helpers/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ add_library(${TARGET} INTERFACE)
1010
target_compile_features(${TARGET} INTERFACE cxx_std_20)
1111

1212
target_include_directories(${TARGET} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
13+
14+
target_link_libraries(${TARGET} INTERFACE File Function safetyhook)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <Function/Function.hpp>
4+
5+
#include <safetyhook.hpp>
6+
7+
namespace RC::Helper::Hook
8+
{
9+
template <typename>
10+
class call_hook;
11+
12+
template <typename ReturnType, typename... Params>
13+
class call_hook<Function<ReturnType(Params...)>>
14+
{
15+
public:
16+
ReturnType operator()(SafetyHookInline& hook, Params... args)
17+
{
18+
return hook.call<ReturnType, Params...>(std::forward<Params>(args)...);
19+
}
20+
};
21+
} // namespace RC::Helper::Hook

deps/third/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
add_subdirectory("GLFW")
33
add_subdirectory("imgui")
44
add_subdirectory("zydis")
5-
add_subdirectory("PolyHook_2_0")
5+
add_subdirectory("safetyhook")
66

77
add_subdirectory("raw_pdb")
88
# Third Party Depenedencies

0 commit comments

Comments
 (0)