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

67 lines
1.7 KiB
Rust

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