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

78 lines
2.0 KiB
Rust
Executable File

use crate::app_interface::{DataLibrary, Item, ItemImpl};
use crate::{ExternRef, ExternalReferenceType, StringKey};
use alloc::rc::Rc;
use spin::rwlock::RwLock;
struct ItemLibraryInner {
ptr: ExternRef<ItemLibrary>,
cache: RwLock<hashbrown::HashMap<u32, ItemImpl>>,
}
#[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<ItemImpl> for ItemLibrary {
fn get_cache(&self) -> &spin::rwlock::RwLock<hashbrown::HashMap<u32, ItemImpl>> {
&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<ItemImpl> {
unsafe { item_library_get_item(ptr, name) }
}
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<ItemImpl> {
unsafe { item_library_get_item_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 item_library_get_item(
ptr: ExternRef<ItemLibrary>,
name: ExternRef<StringKey>,
) -> ExternRef<ItemImpl>;
fn item_library_get_item_by_hash(ptr: ExternRef<ItemLibrary>, hash: u32)
-> ExternRef<ItemImpl>;
}
#[cfg(feature = "mock_data")]
impl ItemLibrary {
pub fn mock() -> Self {
Self {
inner: Rc::new(ItemLibraryInner {
ptr: ExternRef::mock(),
cache: Default::default(),
}),
}
}
}