Skip to main content

rusteal_core/
soft_ref.rs

1use std::fmt;
2use std::marker::PhantomData;
3
4use crate::error::RustealResult;
5use crate::object_ref::UObjectRef;
6use crate::traits::UeClass;
7use crate::world;
8
9pub struct SoftObjectRef<T: UeClass> {
10    path: String,
11    _marker: PhantomData<T>,
12}
13
14impl<T: UeClass> SoftObjectRef<T> {
15    pub fn new(path: impl Into<String>) -> Self {
16        SoftObjectRef {
17            path: path.into(),
18            _marker: PhantomData,
19        }
20    }
21
22    pub fn null() -> Self {
23        Self::new(String::new())
24    }
25
26    pub fn path(&self) -> &str {
27        &self.path
28    }
29
30    pub fn is_null(&self) -> bool {
31        self.path.is_empty()
32    }
33
34    pub fn get(&self) -> UObjectRef<T> {
35        if self.is_null() {
36            return UObjectRef::null();
37        }
38
39        world::find_object_raw(T::static_class(), &self.path)
40            .map(|h| unsafe { UObjectRef::from_raw(h) })
41            .unwrap_or_else(|_| UObjectRef::null())
42    }
43
44    #[track_caller]
45    pub fn load_synchronous(&self) -> RustealResult<UObjectRef<T>> {
46        if self.is_null() {
47            return Ok(UObjectRef::null());
48        }
49
50        let handle = world::load_object_raw(T::static_class(), &self.path)?;
51
52        Ok(unsafe { UObjectRef::from_raw(handle) })
53    }
54}
55
56impl<T: UeClass> Default for SoftObjectRef<T> {
57    fn default() -> Self {
58        Self::null()
59    }
60}
61
62impl<T: UeClass> Clone for SoftObjectRef<T> {
63    fn clone(&self) -> Self {
64        Self::new(self.path.clone())
65    }
66}
67
68impl<T: UeClass> PartialEq for SoftObjectRef<T> {
69    fn eq(&self, other: &Self) -> bool {
70        self.path == other.path
71    }
72}
73
74impl<T: UeClass> Eq for SoftObjectRef<T> {}
75
76impl<T: UeClass> fmt::Debug for SoftObjectRef<T> {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        f.debug_tuple("SoftObjectRef").field(&self.path).finish()
79    }
80}