Skip to content

Commit ef303f5

Browse files
authored
Merge pull request #43 from apple1417/master
support delegate properties
2 parents da8c5e7 + f05a497 commit ef303f5

25 files changed

+716
-141
lines changed

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
`UObject` header layout.
66

77
[aca70889](https://github.com/bl-sdk/unrealsdk/commit/aca70889)
8+
9+
- Added support for Delegate and Multicast Delegate properties.
10+
11+
[4e17d06d](https://github.com/bl-sdk/unrealsdk/commit/4e17d06d),
12+
[270ef4bf](https://github.com/bl-sdk/unrealsdk/commit/270ef4bf)
813

914
## v1.3.0
1015
- Added a `WeakPointer` wrapper class with better ergonomics, including an emulated implementation

src/shared/dllmain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ DWORD WINAPI startup_thread(LPVOID /*unused*/) {
3232
* @param ul_reason_for_call Reason this is being called.
3333
* @return True if loaded successfully, false otherwise.
3434
*/
35-
// NOLINTNEXTLINE(readability-identifier-naming) - for `DllMain`
35+
// NOLINTNEXTLINE(misc-use-internal-linkage, readability-identifier-naming)
3636
BOOL APIENTRY DllMain(HMODULE h_module, DWORD ul_reason_for_call, LPVOID /*unused*/) {
3737
switch (ul_reason_for_call) {
3838
case DLL_PROCESS_ATTACH:

src/unrealsdk/commands.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ std::pair<DLLSafeCallback*, size_t> find_matching_command(std::wstring_view line
8888
return {callback, 0};
8989
}
9090

91-
auto non_space = std::find_if_not(line.begin(), line.end(), &std::iswspace);
91+
auto non_space = std::ranges::find_if_not(line, &std::iswspace);
9292
if (non_space == line.end()) {
9393
return {nullptr, 0};
9494
}

src/unrealsdk/game/bl2/hooks.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ void __fastcall process_event_hook(UObject* obj,
7272
// Copy args so that hooks can't modify them, for parity with call function
7373
const WrappedStruct args_base{func, params};
7474
WrappedStruct args = args_base.copy_params_only();
75-
hook_manager::Details hook{obj, &args, {func->find_return_param()}, {func, obj}};
75+
hook_manager::Details hook{.obj = obj,
76+
.args = &args,
77+
.ret = {func->find_return_param()},
78+
.func = {.func = func, .object = obj}};
7679

7780
const bool block_execution =
7881
hook_manager::impl::run_hooks_of_type(*data, hook_manager::Type::PRE, hook);
@@ -184,7 +187,10 @@ void __fastcall call_function_hook(UObject* obj,
184187
WrappedStruct args{func};
185188
auto original_code = stack->extract_current_args(args);
186189

187-
hook_manager::Details hook{obj, &args, {func->find_return_param()}, {func, obj}};
190+
hook_manager::Details hook{.obj = obj,
191+
.args = &args,
192+
.ret = {func->find_return_param()},
193+
.func = {.func = func, .object = obj}};
188194

189195
const bool block_execution =
190196
hook_manager::impl::run_hooks_of_type(*data, hook_manager::Type::PRE, hook);

src/unrealsdk/game/bl2/object.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ UObject* BL2Hook::construct_object(UClass* cls,
7878
// function is used to tell if to call a hook, it's called all the time.
7979
// Stick with a native function call for speed.
8080

81+
namespace {
82+
8183
#if defined(__MINGW32__)
8284
#pragma GCC diagnostic push
8385
#pragma GCC diagnostic ignored "-Wattributes" // thiscall on non-class
@@ -105,6 +107,8 @@ const constinit Pattern<15> GET_PATH_NAME_PATTERN{
105107
"85 F6" // test esi, esi
106108
};
107109

110+
} // namespace
111+
108112
void BL2Hook::find_get_path_name(void) {
109113
get_path_name_ptr = GET_PATH_NAME_PATTERN.sigscan_nullable<get_path_name_func>();
110114
LOG(MISC, "GetPathName: {:p}", reinterpret_cast<void*>(get_path_name_ptr));

src/unrealsdk/game/bl3/hooks.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ const constinit Pattern<19> PROCESS_EVENT_SIG{
3434
"48 81 EC F0000000" // sub rsp, 000000F0
3535
};
3636

37-
} // namespace
38-
3937
void process_event_hook(UObject* obj, UFunction* func, void* params) {
4038
try {
4139
auto data = hook_manager::impl::preprocess_hook("ProcessEvent", func, obj);
4240
if (data != nullptr) {
4341
// Copy args so that hooks can't modify them, for parity with call function
4442
const WrappedStruct args_base{func, params};
4543
WrappedStruct args = args_base.copy_params_only();
46-
hook_manager::Details hook{obj, &args, {func->find_return_param()}, {func, obj}};
44+
hook_manager::Details hook{.obj = obj,
45+
.args = &args,
46+
.ret = {func->find_return_param()},
47+
.func = {.func = func, .object = obj}};
4748

4849
const bool block_execution =
4950
hook_manager::impl::run_hooks_of_type(*data, hook_manager::Type::PRE, hook);
@@ -104,6 +105,8 @@ bool locking(void) {
104105
return locking;
105106
}
106107

108+
} // namespace
109+
107110
void BL3Hook::hook_process_event(void) {
108111
detour(PROCESS_EVENT_SIG, locking() ? locking_process_event_hook : process_event_hook,
109112
&process_event_ptr, "ProcessEvent");
@@ -135,8 +138,6 @@ const constinit Pattern<20> CALL_FUNCTION_SIG{
135138
"48 81 EC 28010000" // sub rsp, 00000128
136139
};
137140

138-
} // namespace
139-
140141
void call_function_hook(UObject* obj, FFrame* stack, void* result, UFunction* func) {
141142
try {
142143
/*
@@ -167,7 +168,10 @@ void call_function_hook(UObject* obj, FFrame* stack, void* result, UFunction* fu
167168
WrappedStruct args{func};
168169
auto original_code = stack->extract_current_args(args);
169170

170-
hook_manager::Details hook{obj, &args, {func->find_return_param()}, {func, obj}};
171+
hook_manager::Details hook{.obj = obj,
172+
.args = &args,
173+
.ret = {func->find_return_param()},
174+
.func = {.func = func, .object = obj}};
171175

172176
const bool block_execution =
173177
hook_manager::impl::run_hooks_of_type(*data, hook_manager::Type::PRE, hook);
@@ -212,6 +216,8 @@ void call_function_hook(UObject* obj, FFrame* stack, void* result, UFunction* fu
212216
static_assert(std::is_same_v<decltype(call_function_hook), call_function_func>,
213217
"call_function signature is incorrect");
214218

219+
} // namespace
220+
215221
void BL3Hook::hook_call_function(void) {
216222
detour(CALL_FUNCTION_SIG, call_function_hook, &call_function_ptr, "CallFunction");
217223
}

src/unrealsdk/logging.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ void builtin_logger(const LogMessage* msg) {
293293
#pragma region Public Interface Implementations
294294
#ifndef UNREALSDK_IMPORTING
295295
namespace impl {
296+
namespace {
296297

297298
void enqueue_log_msg(uint64_t unix_time_ms,
298299
Level level,
@@ -326,11 +327,11 @@ void add_callback(log_callback callback) {
326327
void remove_callback(log_callback callback) {
327328
const std::lock_guard<std::mutex> lock(callback_mutex);
328329

329-
all_log_callbacks.erase(
330-
std::remove(all_log_callbacks.begin(), all_log_callbacks.end(), callback),
331-
all_log_callbacks.end());
330+
auto [begin, end] = std::ranges::remove(all_log_callbacks, callback);
331+
all_log_callbacks.erase(begin, end);
332332
}
333333

334+
} // namespace
334335
} // namespace impl
335336

336337
void init(const std::filesystem::path& file, bool unreal_console) {

src/unrealsdk/pch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <mutex>
4545
#include <optional>
4646
#include <queue>
47+
#include <ranges>
4748
#include <sstream>
4849
#include <stdexcept>
4950
#include <string>

src/unrealsdk/unreal/cast.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
#include "unrealsdk/unreal/classes/properties/ubyteproperty.h"
1515
#include "unrealsdk/unreal/classes/properties/uclassproperty.h"
1616
#include "unrealsdk/unreal/classes/properties/ucomponentproperty.h"
17+
#include "unrealsdk/unreal/classes/properties/udelegateproperty.h"
1718
#include "unrealsdk/unreal/classes/properties/uenumproperty.h"
1819
#include "unrealsdk/unreal/classes/properties/uinterfaceproperty.h"
20+
#include "unrealsdk/unreal/classes/properties/umulticastdelegateproperty.h"
1921
#include "unrealsdk/unreal/classes/properties/uobjectproperty.h"
2022
#include "unrealsdk/unreal/classes/properties/ustrproperty.h"
2123
#include "unrealsdk/unreal/classes/properties/ustructproperty.h"
@@ -48,6 +50,7 @@ using all_unreal_classes = std::tuple< //
4850
UClassProperty,
4951
UComponentProperty,
5052
UConst,
53+
UDelegateProperty,
5154
UDoubleProperty,
5255
UEnum,
5356
UEnumProperty,
@@ -62,6 +65,7 @@ using all_unreal_classes = std::tuple< //
6265
UInterfaceProperty,
6366
UIntProperty,
6467
ULazyObjectProperty,
68+
UMulticastDelegateProperty,
6569
UNameProperty,
6670
UObject,
6771
UObjectProperty,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "unrealsdk/pch.h"
2+
#include "unrealsdk/unreal/classes/properties/udelegateproperty.h"
3+
#include "unrealsdk/unreal/prop_traits.h"
4+
#include "unrealsdk/unreal/structs/fscriptdelegate.h"
5+
#include "unrealsdk/unreal/wrappers/unreal_pointer.h"
6+
7+
namespace unrealsdk::unreal {
8+
9+
UFunction* UDelegateProperty::get_signature(void) const {
10+
return this->read_field(&UDelegateProperty::Signature);
11+
}
12+
13+
PropTraits<UDelegateProperty>::Value PropTraits<UDelegateProperty>::get(
14+
const UDelegateProperty* /*prop*/,
15+
uintptr_t addr,
16+
const UnrealPointer<void>& /*parent*/) {
17+
return reinterpret_cast<FScriptDelegate*>(addr)->as_function();
18+
}
19+
20+
void PropTraits<UDelegateProperty>::set(const UDelegateProperty* prop,
21+
uintptr_t addr,
22+
const Value& value) {
23+
FScriptDelegate::validate_signature(value, prop->get_signature());
24+
reinterpret_cast<FScriptDelegate*>(addr)->bind(value);
25+
}
26+
27+
} // namespace unrealsdk::unreal

0 commit comments

Comments
 (0)