Skip to content

Commit b6d995c

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 58a902c commit b6d995c

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>
@@ -77,21 +76,15 @@ namespace RC
7776

7877
CrashDumper::~CrashDumper()
7978
{
80-
m_set_unhandled_exception_filter_hook->unHook();
79+
m_set_unhandled_exception_filter_hook = {};
8180
SetUnhandledExceptionFilter(reinterpret_cast<LPTOP_LEVEL_EXCEPTION_FILTER>(m_previous_exception_filter));
8281
}
8382

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

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
}
@@ -232,35 +230,10 @@ namespace RC
232230
#define UE4SS_COMPILER STR("MSVC")
233231
#endif
234232

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

265238
Unreal::UnrealInitializer::SetupUnrealModules();
266239

UE4SS/xmake.lua

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

1112
option("ue4ssBetaIsStarted")
1213
set_default(true)
@@ -77,7 +78,7 @@ target(projectName)
7778

7879
add_packages("imgui", "ImGuiTextEdit", "IconFontCppHeaders", "glfw", "opengl", { public = true })
7980

80-
add_packages("glaze", "polyhook_2", { public = true })
81+
add_packages("glaze", "safetyhook", "polyhook_2", { public = true })
8182

8283
add_links("dbghelp", "psapi", "d3d11", { public = true })
8384

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)