1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// DynamicCall: reflection-based function invocation via ProcessEvent.
//
// This is the "safety net" fallback for functions not covered by codegen's
// direct call path. It uses UE's reflection system to find functions, allocate
// parameter buffers, set/get parameter values, and invoke via ProcessEvent.
use rusteal_ffi::{FPropertyHandle, UFunctionHandle, UObjectHandle};
use crate::error::{check_ffi, RustealError, RustealResult};
use crate::ffi_dispatch::{self, NativePtr, NATIVE_PTR_NULL, native_ptr_is_null};
use crate::object_ref::UObjectRef;
use crate::traits::UeClass;
/// Builder for a reflection-based function call.
///
/// Typical usage:
/// ```ignore
/// let mut call = DynamicCall::new(&obj, "DoSomething")?;
/// call.set::<i32>("Param1", 42)?;
/// let result = call.call()?;
/// let ret: f32 = result.get::<f32>("ReturnValue")?;
/// ```
pub struct DynamicCall {
obj: UObjectHandle,
func: UFunctionHandle,
params: NativePtr,
/// The delegate property a broadcast fires; null for a function call.
delegate: FPropertyHandle,
}
impl DynamicCall {
/// Prepare a reflection call to the named function on `obj`.
pub fn new(obj: &UObjectRef<impl UeClass>, func_name: &str) -> RustealResult<Self> {
let h = obj.checked()?.raw();
let func = unsafe {
ffi_dispatch::reflection_find_function(h, func_name.as_ptr(), func_name.len() as u32)
};
if func.is_null() {
return Err(RustealError::FunctionNotFound(func_name.to_string()));
}
let params = unsafe { ffi_dispatch::reflection_alloc_params(func) };
Ok(DynamicCall {
obj: h,
func,
params,
delegate: FPropertyHandle::null(),
})
}
/// Write a parameter value into the params buffer.
///
/// # Safety contract
/// `T` must match the actual UE property type at the named parameter.
/// Using the wrong type leads to undefined behavior at runtime. This is
/// inherently less safe than the codegen direct-call path.
/// Parameters for broadcasting the multicast delegate `delegate` (a
/// delegate property) on `obj`: set them, then [`DynamicCall::broadcast`].
pub fn for_delegate(obj: &UObjectRef<impl UeClass>, delegate: FPropertyHandle) -> RustealResult<Self> {
let h = obj.checked()?.raw();
if delegate.is_null() {
return Err(RustealError::PropertyNotFound("delegate".to_string()));
}
let func = unsafe { ffi_dispatch::reflection_get_delegate_signature(delegate) };
if func.is_null() {
return Err(RustealError::TypeMismatch);
}
let params = unsafe { ffi_dispatch::reflection_alloc_params(func) };
Ok(DynamicCall { obj: h, func, params, delegate })
}
pub fn set<T: Copy>(&mut self, name: &str, value: T) -> RustealResult<()> {
let (prop, offset) = self.find_param(name)?;
let _ = prop; // used only for lookup
// SAFETY: The offset is provided by UE reflection and the caller
// guarantees T matches the property type.
unsafe {
ffi_dispatch::native_mem_write(self.params, offset as usize, value);
}
Ok(())
}
/// Copy a struct into a parameter with the struct's own copy semantics.
pub fn set_struct<T: crate::traits::UeStruct>(
&mut self,
name: &str,
value: &crate::containers::OwnedStruct<T>,
) -> RustealResult<()> {
let (_, offset) = self.find_param(name)?;
check_ffi(unsafe {
ffi_dispatch::reflection_copy_struct(
T::static_struct(),
self.params.add(offset as usize),
value.as_bytes().as_ptr(),
)
})
}
/// Invoke the function via ProcessEvent. Consumes this builder and returns
/// a `DynamicCallResult` for reading output/return values.
/// Fire every function bound to the delegate this call was made
/// [`for`](DynamicCall::for_delegate), as C++'s `Broadcast` does.
pub fn broadcast(self) -> RustealResult<()> {
if self.delegate.is_null() {
return Err(RustealError::InvalidOperation("not a delegate call".into()));
}
check_ffi(unsafe { ffi_dispatch::delegate_broadcast_multicast(self.obj, self.delegate, self.params) })
}
pub fn call(mut self) -> RustealResult<DynamicCallResult> {
let code =
unsafe { ffi_dispatch::reflection_call_function(self.obj, self.func, self.params) };
check_ffi(code)?;
// Transfer params ownership to DynamicCallResult.
let result = DynamicCallResult {
func: self.func,
params: self.params,
};
// Prevent Drop from double-freeing.
self.params = NATIVE_PTR_NULL;
Ok(result)
}
/// Look up a named parameter and return its property handle + offset.
fn find_param(&self, name: &str) -> RustealResult<(FPropertyHandle, u32)> {
let prop = unsafe {
ffi_dispatch::reflection_get_function_param(self.func, name.as_ptr(), name.len() as u32)
};
if prop.is_null() {
return Err(RustealError::PropertyNotFound(name.to_string()));
}
let offset = unsafe { ffi_dispatch::reflection_get_property_offset(prop) };
Ok((prop, offset))
}
}
impl Drop for DynamicCall {
fn drop(&mut self) {
if !native_ptr_is_null(self.params) {
unsafe { ffi_dispatch::reflection_free_params(self.func, self.params) };
}
}
}
/// Holds the params buffer after a successful `DynamicCall::call()`.
/// Use `get()` to read output parameters and return values.
pub struct DynamicCallResult {
func: UFunctionHandle,
params: NativePtr,
}
impl DynamicCallResult {
/// Read an output parameter or return value from the params buffer.
///
/// # Safety contract
/// `T` must match the actual UE property type at the named parameter.
pub fn get<T: Copy>(&self, name: &str) -> RustealResult<T> {
let prop = unsafe {
ffi_dispatch::reflection_get_function_param(self.func, name.as_ptr(), name.len() as u32)
};
if prop.is_null() {
return Err(RustealError::PropertyNotFound(name.to_string()));
}
let offset = unsafe { ffi_dispatch::reflection_get_property_offset(prop) };
// SAFETY: The offset is provided by UE reflection and the caller
// guarantees T matches the property type.
let value = unsafe { ffi_dispatch::native_mem_read(self.params, offset as usize) };
Ok(value)
}
}
impl Drop for DynamicCallResult {
fn drop(&mut self) {
if !native_ptr_is_null(self.params) {
unsafe { ffi_dispatch::reflection_free_params(self.func, self.params) };
}
}
}