use std::marker::PhantomData;
use uika_ffi::{UClassHandle, UObjectHandle};
use crate::api::api;
use crate::error::{check_ffi, UikaError, UikaResult};
use crate::pinned::Pinned;
use crate::traits::{UeClass, UeHandle, ValidHandle};
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct UObjectRef<T: UeClass> {
handle: UObjectHandle,
_marker: PhantomData<*const T>, }
unsafe impl<T: UeClass> Send for UObjectRef<T> {}
impl<T: UeClass> UObjectRef<T> {
#[inline]
pub unsafe fn from_raw(handle: UObjectHandle) -> Self {
UObjectRef {
handle,
_marker: PhantomData,
}
}
#[inline]
pub fn raw(&self) -> UObjectHandle {
self.handle
}
#[inline]
pub fn is_valid(&self) -> bool {
unsafe { ((*api().core).is_valid)(self.handle) }
}
#[inline]
pub fn checked(&self) -> UikaResult<Checked<T>> {
if self.is_valid() {
Ok(Checked {
handle: self.handle,
_marker: PhantomData,
})
} else {
Err(UikaError::ObjectDestroyed)
}
}
pub fn cast<U: UeClass>(self) -> UikaResult<UObjectRef<U>> {
let h = self.checked()?.raw();
let target = U::static_class();
if unsafe { ((*api().core).is_a)(h, target) } {
Ok(UObjectRef {
handle: self.handle,
_marker: PhantomData,
})
} else {
Err(UikaError::InvalidCast)
}
}
pub fn pin(self) -> UikaResult<Pinned<T>> {
Pinned::new(self)
}
pub fn get_name(&self) -> UikaResult<String> {
let h = self.checked()?.raw();
let mut buf = [0u8; 256];
let mut out_len: u32 = 0;
let code = unsafe {
((*api().core).get_name)(h, buf.as_mut_ptr(), buf.len() as u32, &mut out_len)
};
check_ffi(code)?;
std::str::from_utf8(&buf[..out_len as usize])
.map(|s| s.to_owned())
.map_err(|_| UikaError::Internal("name is not valid UTF-8".into()))
}
pub fn get_class(&self) -> UikaResult<UClassHandle> {
let h = self.checked()?.raw();
Ok(unsafe { ((*api().core).get_class)(h) })
}
pub fn get_outer(&self) -> UikaResult<UObjectHandle> {
let h = self.checked()?.raw();
Ok(unsafe { ((*api().core).get_outer)(h) })
}
}
impl<T: UeClass> UeHandle for UObjectRef<T> {
#[inline]
fn checked_handle(&self) -> UikaResult<UObjectHandle> {
self.checked().map(|c| c.raw())
}
#[inline]
fn raw_handle(&self) -> UObjectHandle {
self.raw()
}
}
impl<T: UeClass> std::fmt::Debug for UObjectRef<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("UObjectRef")
.field("handle", &self.handle)
.field("valid", &self.is_valid())
.finish()
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Checked<T: UeClass> {
handle: UObjectHandle,
_marker: PhantomData<*const T>,
}
unsafe impl<T: UeClass> Send for Checked<T> {}
impl<T: UeClass> Checked<T> {
#[inline]
pub(crate) fn new_unchecked(handle: UObjectHandle) -> Self {
Checked {
handle,
_marker: PhantomData,
}
}
#[inline]
pub fn raw(&self) -> UObjectHandle {
self.handle
}
#[inline]
pub fn as_ref(&self) -> UObjectRef<T> {
unsafe { UObjectRef::from_raw(self.handle) }
}
}
impl<T: UeClass> ValidHandle for Checked<T> {
#[inline]
fn handle(&self) -> UObjectHandle {
self.handle
}
}
impl<T: UeClass> std::fmt::Debug for Checked<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Checked")
.field("handle", &self.handle)
.finish()
}
}