Skip to main content

rusteal_core/
struct_ref.rs

1use std::marker::PhantomData;
2
3use rusteal_ffi::UObjectHandle;
4
5use crate::traits::UeStruct;
6
7pub struct UStructRef<T: UeStruct> {
8    ptr: *mut u8,
9    _marker: PhantomData<T>,
10}
11
12impl<T: UeStruct> UStructRef<T> {
13    #[inline]
14    pub unsafe fn from_raw(ptr: *mut u8) -> Self {
15        UStructRef {
16            ptr,
17            _marker: PhantomData,
18        }
19    }
20
21    #[inline]
22    pub fn as_ptr(&self) -> UObjectHandle {
23        UObjectHandle(self.ptr as *mut std::ffi::c_void)
24    }
25
26    pub fn to_owned(&self) -> crate::containers::OwnedStruct<T> {
27        crate::containers::OwnedStruct::copy_from(self)
28    }
29}
30
31#[inline(always)]
32pub unsafe fn struct_ref_from_param<T: UeStruct>(ptr: *mut u8, offset: usize) -> UStructRef<T> {
33    unsafe { UStructRef::from_raw(ptr.add(offset)) }
34}
35
36impl<T: UeStruct> std::fmt::Debug for UStructRef<T> {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        f.debug_struct("UStructRef")
39            .field("ptr", &self.ptr)
40            .finish()
41    }
42}
43
44pub struct OutRef<T: Copy> {
45    ptr: *mut T,
46}
47
48impl<T: Copy> OutRef<T> {
49    #[inline]
50    pub fn get(&self) -> T {
51        unsafe { self.ptr.read_unaligned() }
52    }
53
54    #[inline]
55    pub fn set(&self, value: T) {
56        unsafe { self.ptr.write_unaligned(value) }
57    }
58}
59
60#[inline(always)]
61pub unsafe fn out_ref_from_param<T: Copy>(ptr: *mut u8, offset: usize) -> OutRef<T> {
62    OutRef {
63        ptr: unsafe { ptr.add(offset) } as *mut T,
64    }
65}
66
67impl<T: Copy> std::fmt::Debug for OutRef<T> {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        f.debug_struct("OutRef").field("ptr", &self.ptr).finish()
70    }
71}