Skip to content

Commit d377423

Browse files
committed
Polyhook -> safetyhook
fix: safetyhook compilation (localcc) Add call_hook and call_hook_unsafe helper wrappers for Safetyhook Add deprecation warning to polyhook_2 xmake file.
1 parent 4fc8691 commit d377423

File tree

13 files changed

+83
-70
lines changed

13 files changed

+83
-70
lines changed

UE4SS/include/CrashDumper.hpp

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

33
#include <memory>
44

5-
namespace PLH
6-
{
7-
class IatHook;
8-
}
5+
#include <safetyhook.hpp>
96

107
namespace RC
118
{
@@ -14,8 +11,7 @@ namespace RC
1411
private:
1512
bool enabled = false;
1613
void* m_previous_exception_filter = nullptr;
17-
std::unique_ptr<PLH::IatHook> m_set_unhandled_exception_filter_hook;
18-
uint64_t m_hook_trampoline_set_unhandled_exception_filter_hook;
14+
SafetyHookInline m_set_unhandled_exception_filter_hook;
1915

2016
public:
2117
CrashDumper();

UE4SS/include/UE4SSProgram.hpp

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

24+
#include <safetyhook.hpp>
2425
#include <String/StringType.hpp>
2526

2627
// Used to set up ImGui context and allocator in DLL mods
@@ -133,17 +134,10 @@ namespace RC
133134
std::mutex m_event_queue_mutex{};
134135

135136
private:
136-
std::unique_ptr<PLH::IatHook> m_load_library_a_hook;
137-
uint64_t m_hook_trampoline_load_library_a;
138-
139-
std::unique_ptr<PLH::IatHook> m_load_library_ex_a_hook;
140-
uint64_t m_hook_trampoline_load_library_ex_a;
141-
142-
std::unique_ptr<PLH::IatHook> m_load_library_w_hook;
143-
uint64_t m_hook_trampoline_load_library_w;
144-
145-
std::unique_ptr<PLH::IatHook> m_load_library_ex_w_hook;
146-
uint64_t m_hook_trampoline_load_library_ex_w;
137+
SafetyHookInline m_load_library_a_hook;
138+
SafetyHookInline m_load_library_ex_a_hook;
139+
SafetyHookInline m_load_library_w_hook;
140+
SafetyHookInline m_load_library_ex_w_hook;
147141

148142
public:
149143
std::vector<std::unique_ptr<Mod>> m_mods;

UE4SS/src/CrashDumper.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <UE4SSProgram.hpp>
77
#include <Unreal/Core/Windows/WindowsHWrapper.hpp>
88

9-
#include <polyhook2/PE/IatHook.hpp>
109
#include <dbghelp.h>
1110

1211
#include <String/StringType.hpp>
@@ -78,21 +77,15 @@ namespace RC
7877

7978
CrashDumper::~CrashDumper()
8079
{
81-
m_set_unhandled_exception_filter_hook->unHook();
80+
m_set_unhandled_exception_filter_hook = {};
8281
SetUnhandledExceptionFilter(reinterpret_cast<LPTOP_LEVEL_EXCEPTION_FILTER>(m_previous_exception_filter));
8382
}
8483

8584
void CrashDumper::enable()
8685
{
8786
SetErrorMode(SEM_FAILCRITICALERRORS);
8887
m_previous_exception_filter = SetUnhandledExceptionFilter(ExceptionHandler);
89-
90-
m_set_unhandled_exception_filter_hook = std::make_unique<PLH::IatHook>("kernel32.dll",
91-
"SetUnhandledExceptionFilter",
92-
std::bit_cast<uint64_t>(&HookedSetUnhandledExceptionFilter),
93-
&m_hook_trampoline_set_unhandled_exception_filter_hook,
94-
L"");
95-
m_set_unhandled_exception_filter_hook->hook();
88+
m_set_unhandled_exception_filter_hook = safetyhook::create_inline(SetUnhandledExceptionFilter, HookedSetUnhandledExceptionFilter);
9689
this->enabled = true;
9790
}
9891

UE4SS/src/UE4SSProgram.cpp

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@
5555
#include <Unreal/World.hpp>
5656
#include <UnrealDef.hpp>
5757

58-
#include <polyhook2/PE/IatHook.hpp>
59-
6058
namespace RC
6159
{
6260
// Commented out because this system (turn off hotkeys when in-game console is open) it doesn't work properly.
@@ -132,31 +130,31 @@ namespace RC
132130
void* HookedLoadLibraryA(const char* dll_name)
133131
{
134132
UE4SSProgram& program = UE4SSProgram::get_program();
135-
HMODULE lib = PLH::FnCast(program.m_hook_trampoline_load_library_a, &LoadLibraryA)(dll_name);
133+
HMODULE lib = program.m_load_library_a_hook.call<HMODULE>(dll_name);
136134
program.fire_dll_load_for_cpp_mods(ensure_str(dll_name));
137135
return lib;
138136
}
139137

140138
void* HookedLoadLibraryExA(const char* dll_name, void* file, int32_t flags)
141139
{
142140
UE4SSProgram& program = UE4SSProgram::get_program();
143-
HMODULE lib = PLH::FnCast(program.m_hook_trampoline_load_library_ex_a, &LoadLibraryExA)(dll_name, file, flags);
141+
HMODULE lib = program.m_load_library_ex_a_hook.call<HMODULE>(dll_name, file, flags);
144142
program.fire_dll_load_for_cpp_mods(ensure_str(dll_name));
145143
return lib;
146144
}
147145

148146
void* HookedLoadLibraryW(const wchar_t* dll_name)
149147
{
150148
UE4SSProgram& program = UE4SSProgram::get_program();
151-
HMODULE lib = PLH::FnCast(program.m_hook_trampoline_load_library_w, &LoadLibraryW)(dll_name);
149+
HMODULE lib = program.m_load_library_w_hook.call<HMODULE>(dll_name);
152150
program.fire_dll_load_for_cpp_mods(ToCharTypePtr(dll_name));
153151
return lib;
154152
}
155153

156154
void* HookedLoadLibraryExW(const wchar_t* dll_name, void* file, int32_t flags)
157155
{
158156
UE4SSProgram& program = UE4SSProgram::get_program();
159-
HMODULE lib = PLH::FnCast(program.m_hook_trampoline_load_library_ex_w, &LoadLibraryExW)(dll_name, file, flags);
157+
HMODULE lib = program.m_load_library_ex_w_hook.call<HMODULE>(dll_name, file, flags);
160158
program.fire_dll_load_for_cpp_mods(ToCharTypePtr(dll_name));
161159
return lib;
162160
}
@@ -234,35 +232,10 @@ namespace RC
234232
#define UE4SS_COMPILER STR("MSVC")
235233
#endif
236234

237-
Output::send(STR("UE4SS Build Configuration: {} ({})\n"), ensure_str(UE4SS_CONFIGURATION), UE4SS_COMPILER);
238-
239-
m_load_library_a_hook = std::make_unique<PLH::IatHook>("kernel32.dll",
240-
"LoadLibraryA",
241-
std::bit_cast<uint64_t>(&HookedLoadLibraryA),
242-
&m_hook_trampoline_load_library_a,
243-
L"");
244-
m_load_library_a_hook->hook();
245-
246-
m_load_library_ex_a_hook = std::make_unique<PLH::IatHook>("kernel32.dll",
247-
"LoadLibraryExA",
248-
std::bit_cast<uint64_t>(&HookedLoadLibraryExA),
249-
&m_hook_trampoline_load_library_ex_a,
250-
L"");
251-
m_load_library_ex_a_hook->hook();
252-
253-
m_load_library_w_hook = std::make_unique<PLH::IatHook>("kernel32.dll",
254-
"LoadLibraryW",
255-
std::bit_cast<uint64_t>(&HookedLoadLibraryW),
256-
&m_hook_trampoline_load_library_w,
257-
L"");
258-
m_load_library_w_hook->hook();
259-
260-
m_load_library_ex_w_hook = std::make_unique<PLH::IatHook>("kernel32.dll",
261-
"LoadLibraryExW",
262-
std::bit_cast<uint64_t>(&HookedLoadLibraryExW),
263-
&m_hook_trampoline_load_library_ex_w,
264-
L"");
265-
m_load_library_ex_w_hook->hook();
235+
m_load_library_a_hook = safetyhook::create_inline(LoadLibraryA, HookedLoadLibraryA);
236+
m_load_library_ex_a_hook = safetyhook::create_inline(LoadLibraryExA, HookedLoadLibraryExA);
237+
m_load_library_w_hook = safetyhook::create_inline(LoadLibraryW, HookedLoadLibraryW);
238+
m_load_library_ex_w_hook = safetyhook::create_inline(LoadLibraryExW, HookedLoadLibraryExW);
266239

267240
Unreal::UnrealInitializer::SetupUnrealModules();
268241

UE4SS/xmake.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_requires("glfw 3.3.9", { debug = is_mode_debug() , configs = {runtimes = get
99
add_requires("opengl", { debug = is_mode_debug(), configs = {runtimes = get_mode_runtimes()} })
1010
add_requires("glaze v2.9.5", { debug = is_mode_debug(), configs = {runtimes = get_mode_runtimes()} })
1111
add_requires("fmt 10.2.1", { debug = is_mode_debug(), configs = {runtimes = get_mode_runtimes()} })
12+
add_requires("safetyhook v0.4.1", { debug = is_mode_debug(), configs = {runtimes = get_mode_runtimes()} })
1213

1314
option("ue4ssBetaIsStarted")
1415
set_default(true)
@@ -79,7 +80,7 @@ target(projectName)
7980

8081
add_packages("imgui", "ImGuiTextEdit", "IconFontCppHeaders", "glfw", "opengl", { public = true })
8182

82-
add_packages("glaze", "polyhook_2", { public = true })
83+
add_packages("glaze", "safetyhook", "polyhook_2", { public = true })
8384

8485
add_links("dbghelp", "psapi", "d3d11", { public = true })
8586

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

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

1515
RC_ASM_API auto resolve_jmp(void* instruction_ptr) -> void*;

deps/first/ASMHelper/src/ASMHelper.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ namespace RC::ASM
1212
ZydisDecoder decoder{};
1313
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
1414
ZyanUSize offset = 0;
15-
ZydisDecodedInstruction instruction{};
16-
ZydisDecodedOperand operands[10]{};
17-
while (ZYAN_SUCCESS(ZydisDecoderDecodeFull(&decoder, instruction_ptr + offset, 16 - offset, &instruction, operands)))
15+
16+
Instruction instruction{in_instruction_ptr, {}, {}};
17+
while (ZYAN_SUCCESS(ZydisDecoderDecodeFull(&decoder, instruction_ptr + offset, 16 - offset, &instruction.raw, instruction.operands)))
1818
{
1919
break;
2020
}
21-
return {in_instruction_ptr, instruction, operands};
21+
return instruction;
2222
}
2323

2424
auto resolve_absolute_address(void* in_instruction_ptr) -> void*
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
22+
template <typename>
23+
class call_hook_unsafe;
24+
25+
template <typename ReturnType, typename... Params>
26+
class call_hook_unsafe<Function<ReturnType(Params...)>>
27+
{
28+
public:
29+
ReturnType operator()(SafetyHookInline& hook, Params... args)
30+
{
31+
return hook.unsafe_call<ReturnType, Params...>(std::forward<Params>(args)...);
32+
}
33+
};
34+
} // namespace RC::Helper::Hook

deps/first/Helpers/xmake.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ target(projectName)
77
add_rules("ue4ss.dependency")
88

99
add_deps("String")
10+
add_packages("safetyhook")
1011

1112
add_includedirs("include", { public = true })
1213
add_headerfiles("include/**.hpp")

deps/first/Unreal

0 commit comments

Comments
 (0)