use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Mutex, OnceLock};
use uika_ffi::{FPropertyHandle, UObjectHandle, UikaErrorCode};
use crate::error::{check_ffi, UikaResult};
type DelegateCallback = Option<Box<dyn FnMut(*mut u8) + Send>>;
static NEXT_ID: AtomicU64 = AtomicU64::new(1);
static REGISTRY: OnceLock<Mutex<HashMap<u64, DelegateCallback>>> = OnceLock::new();
fn registry() -> &'static Mutex<HashMap<u64, DelegateCallback>> {
REGISTRY.get_or_init(|| Mutex::new(HashMap::new()))
}
pub fn register_callback(f: impl FnMut(*mut u8) + Send + 'static) -> u64 {
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
registry().lock().unwrap().insert(id, Some(Box::new(f)));
id
}
pub fn unregister_callback(id: u64) {
registry().lock().unwrap().remove(&id);
}
pub fn clear_all() {
if let Some(reg) = REGISTRY.get() {
reg.lock().unwrap().clear();
}
NEXT_ID.store(1, Ordering::Relaxed);
}
pub fn invoke(callback_id: u64, params: *mut u8) {
let mut cb = {
let mut reg = registry().lock().unwrap();
reg.get_mut(&callback_id).and_then(|slot| slot.take())
};
if let Some(ref mut f) = cb {
f(params);
}
if let Some(f) = cb {
let mut reg = registry().lock().unwrap();
if let Some(slot) = reg.get_mut(&callback_id) {
if slot.is_none() {
*slot = Some(f);
}
}
}
}
pub struct DelegateBinding {
callback_id: u64,
owner: UObjectHandle,
prop: FPropertyHandle,
is_multicast: bool,
}
impl DelegateBinding {
pub fn new(
callback_id: u64,
owner: UObjectHandle,
prop: FPropertyHandle,
is_multicast: bool,
) -> Self {
Self {
callback_id,
owner,
prop,
is_multicast,
}
}
pub fn callback_id(&self) -> u64 {
self.callback_id
}
pub fn unbind(self) {
}
}
impl Drop for DelegateBinding {
fn drop(&mut self) {
unregister_callback(self.callback_id);
unsafe {
let api = crate::api::api();
if !api.delegate.is_null() {
if self.is_multicast {
let _ = ((*api.delegate).remove_multicast)(
self.owner,
self.prop,
self.callback_id,
);
} else {
let _ = ((*api.delegate).unbind_delegate)(self.owner, self.prop);
}
}
}
}
}
pub fn bind_unicast(
owner: UObjectHandle,
prop: FPropertyHandle,
callback: impl FnMut(*mut u8) + Send + 'static,
) -> UikaResult<DelegateBinding> {
let id = register_callback(callback);
let result = unsafe { ((*crate::api::api().delegate).bind_delegate)(owner, prop, id) };
if result != UikaErrorCode::Ok {
unregister_callback(id);
check_ffi(result)?;
}
Ok(DelegateBinding::new(id, owner, prop, false))
}
pub fn bind_multicast(
owner: UObjectHandle,
prop: FPropertyHandle,
callback: impl FnMut(*mut u8) + Send + 'static,
) -> UikaResult<DelegateBinding> {
let id = register_callback(callback);
let result = unsafe { ((*crate::api::api().delegate).add_multicast)(owner, prop, id) };
if result != UikaErrorCode::Ok {
unregister_callback(id);
check_ffi(result)?;
}
Ok(DelegateBinding::new(id, owner, prop, true))
}