use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock, RwLock};
use crate::{lock_or_recover, read_or_recover, write_or_recover};
pub struct ClassRegistration {
pub register: fn(),
pub finalize: fn(),
}
inventory::collect!(ClassRegistration);
pub struct ClassFunctionRegistration {
pub register_functions: fn(),
}
inventory::collect!(ClassFunctionRegistration);
pub fn register_all_from_inventory() {
let mut class_count = 0u32;
for reg in inventory::iter::<ClassRegistration> {
(reg.register)();
class_count += 1;
}
let mut func_reg_count = 0u32;
for freg in inventory::iter::<ClassFunctionRegistration> {
(freg.register_functions)();
func_reg_count += 1;
}
for reg in inventory::iter::<ClassRegistration> {
(reg.finalize)();
}
let total_funcs = read_or_recover(func_registry()).len();
let msg = format!(
"[Rusteal] register_all_from_inventory: {} classes, {} impl blocks, {} function callbacks",
class_count, func_reg_count, total_funcs,
);
let bytes = msg.as_bytes();
unsafe {
crate::ffi_dispatch::logging_log(0, bytes.as_ptr(), bytes.len() as u32);
}
}
use rusteal_ffi::UObjectHandle;
pub struct RustTypeInfo {
pub name: &'static str,
pub construct_fn: fn() -> *mut u8,
pub drop_fn: unsafe fn(*mut u8),
}
use crate::ffi_dispatch::NativePtr;
type ReifyFunctionCallback = Arc<dyn Fn(UObjectHandle, *mut u8, NativePtr) + Send + Sync>;
static TYPE_REGISTRY: OnceLock<Mutex<HashMap<u64, RustTypeInfo>>> = OnceLock::new();
static FUNC_REGISTRY: OnceLock<RwLock<Vec<ReifyFunctionCallback>>> = OnceLock::new();
static INSTANCE_DATA: OnceLock<RwLock<HashMap<u64, InstanceEntry>>> = OnceLock::new();
struct InstanceEntry {
data: *mut u8,
type_id: u64,
}
unsafe impl Send for InstanceEntry {}
unsafe impl Sync for InstanceEntry {}
fn type_registry() -> &'static Mutex<HashMap<u64, RustTypeInfo>> {
TYPE_REGISTRY.get_or_init(|| Mutex::new(HashMap::new()))
}
fn func_registry() -> &'static RwLock<Vec<ReifyFunctionCallback>> {
FUNC_REGISTRY.get_or_init(|| RwLock::new(Vec::new()))
}
fn instance_data() -> &'static RwLock<HashMap<u64, InstanceEntry>> {
INSTANCE_DATA.get_or_init(|| RwLock::new(HashMap::new()))
}
pub fn register_type(type_id: u64, info: RustTypeInfo) {
lock_or_recover(type_registry())
.insert(type_id, info);
}
pub fn register_function<F>(f: F) -> u64
where
F: Fn(UObjectHandle, *mut u8, NativePtr) + Send + Sync + 'static,
{
let mut vec = write_or_recover(func_registry());
let id = vec.len() as u64;
vec.push(Arc::new(f));
id
}
pub fn construct_instance(obj: UObjectHandle, type_id: u64) {
let types = lock_or_recover(type_registry());
let Some(info) = types.get(&type_id) else {
if crate::api::is_api_initialized() {
let msg = format!("[Rusteal] construct_instance: unknown type_id {type_id}");
let bytes = msg.as_bytes();
unsafe {
crate::ffi_dispatch::logging_log(1, bytes.as_ptr(), bytes.len() as u32);
}
}
return;
};
let data = (info.construct_fn)();
drop(types);
let key = obj.to_addr();
write_or_recover(instance_data())
.insert(key, InstanceEntry { data, type_id });
}
pub fn drop_instance(obj: UObjectHandle, _type_id: u64) {
let key = obj.to_addr();
let entry = write_or_recover(instance_data()).remove(&key);
if let Some(entry) = entry {
let types = lock_or_recover(type_registry());
if let Some(info) = types.get(&entry.type_id) {
unsafe {
(info.drop_fn)(entry.data);
}
}
}
}
pub fn invoke_function(callback_id: u64, obj: UObjectHandle, params: NativePtr) {
let key = obj.to_addr();
let rust_data = read_or_recover(instance_data())
.get(&key)
.map(|e| e.data)
.unwrap_or(std::ptr::null_mut());
let func = {
let vec = read_or_recover(func_registry());
vec.get(callback_id as usize).cloned()
};
if let Some(func) = func {
func(obj, rust_data, params);
} else if crate::api::is_api_initialized() {
let vec_len = read_or_recover(func_registry()).len();
let msg = format!(
"[Rusteal] invoke_function: callback_id {} not found (registry size = {})",
callback_id, vec_len,
);
let bytes = msg.as_bytes();
unsafe {
crate::ffi_dispatch::logging_log(1, bytes.as_ptr(), bytes.len() as u32);
}
}
}
pub fn clear_all() {
if let Some(instances) = INSTANCE_DATA.get() {
let mut map = write_or_recover(instances);
let types = lock_or_recover(type_registry());
for (_, entry) in map.drain() {
if let Some(info) = types.get(&entry.type_id) {
unsafe {
(info.drop_fn)(entry.data);
}
}
}
drop(types);
}
if let Some(funcs) = FUNC_REGISTRY.get() {
write_or_recover(funcs).clear();
}
if let Some(types) = TYPE_REGISTRY.get() {
lock_or_recover(types).clear();
}
}
pub fn get_instance_data(obj: UObjectHandle) -> *mut u8 {
let key = obj.to_addr();
read_or_recover(instance_data())
.get(&key)
.map(|e| e.data)
.unwrap_or(std::ptr::null_mut())
}