Gen7ScriptsRs/pkmn_lib_interface/src/app_interface/library/data_libraries/item_library.rs

65 lines
1.6 KiB
Rust

use crate::app_interface::{DataLibrary, Item};
use crate::handling::Cacheable;
use crate::{ExternRef, StringKey};
use alloc::collections::BTreeMap;
use alloc::rc::Rc;
use spin::rwlock::RwLock;
struct ItemLibraryInner {
ptr: ExternRef<ItemLibrary>,
cache: RwLock<BTreeMap<u32, Item>>,
}
#[derive(Clone)]
pub struct ItemLibrary {
inner: Rc<ItemLibraryInner>,
}
impl ItemLibrary {
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
Self {
inner: Rc::new(ItemLibraryInner {
ptr,
cache: Default::default(),
}),
}
}
}
impl DataLibrary<Item> for ItemLibrary {
fn get_cache(&self) -> &spin::rwlock::RwLock<BTreeMap<u32, Item>> {
&self.inner.cache
}
fn get_self_ref(&self) -> ExternRef<Self> {
self.inner.ptr.clone()
}
fn _get_ref_by_name(ptr: ExternRef<Self>, name: ExternRef<StringKey>) -> ExternRef<Item> {
unsafe { move_library_get_move(ptr, name) }
}
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<Item> {
unsafe { move_library_get_move_by_hash(ptr, hash) }
}
}
impl Cacheable for ItemLibrary {
fn get_cache<'a>() -> &'a mut BTreeMap<ExternRef<Self>, Self>
where
Self: Sized,
{
unsafe { &mut CACHE }
}
}
static mut CACHE: BTreeMap<ExternRef<ItemLibrary>, ItemLibrary> = BTreeMap::new();
extern "wasm" {
fn move_library_get_move(
ptr: ExternRef<ItemLibrary>,
name: ExternRef<StringKey>,
) -> ExternRef<Item>;
fn move_library_get_move_by_hash(ptr: ExternRef<ItemLibrary>, hash: u32) -> ExternRef<Item>;
}