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

77 lines
1.9 KiB
Rust
Executable File

use crate::app_interface::{DataLibrary, Item};
use crate::{ExternRef, ExternalReferenceType, StringKey};
use alloc::rc::Rc;
use spin::rwlock::RwLock;
struct ItemLibraryInner {
ptr: ExternRef<ItemLibrary>,
cache: RwLock<hashbrown::HashMap<u32, Item>>,
}
#[derive(Clone)]
pub struct ItemLibrary {
inner: Rc<ItemLibraryInner>,
}
impl ItemLibrary {
#[cfg(not(feature = "mock_data"))]
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<hashbrown::HashMap<u32, Item>> {
&self.inner.cache
}
fn get_self_ref(&self) -> ExternRef<Self> {
self.inner.ptr
}
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_name(ptr: ExternRef<Self>, name: ExternRef<StringKey>) -> ExternRef<Item> {
unsafe { move_library_get_move(ptr, name) }
}
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<Item> {
unsafe { move_library_get_move_by_hash(ptr, hash) }
}
}
crate::handling::cacheable::cacheable!(ItemLibrary);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for ItemLibrary {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
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>;
}
#[cfg(feature = "mock_data")]
impl ItemLibrary {
pub fn mock() -> Self {
Self {
inner: Rc::new(ItemLibraryInner {
ptr: ExternRef::mock(),
cache: Default::default(),
}),
}
}
}