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

87 lines
2.4 KiB
Rust
Executable File

use crate::app_interface::{DataLibrary, Item};
use alloc::rc::Rc;
pub trait ItemLibraryTrait: DataLibrary<Item> {}
pub type ItemLibrary = Rc<dyn ItemLibraryTrait>;
#[cfg(not(feature = "mock_data"))]
mod implementation {
use super::*;
use crate::app_interface::{ItemImpl, StringKey};
use crate::handling::extern_ref::{ExternRef, ExternalReferenceType};
use spin::RwLock;
struct ItemLibraryInner {
ptr: ExternRef<ItemLibraryImpl>,
cache: RwLock<hashbrown::HashMap<u32, Item>>,
}
#[derive(Clone)]
pub struct ItemLibraryImpl {
inner: Rc<ItemLibraryInner>,
}
impl ItemLibraryImpl {
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
Self {
inner: Rc::new(ItemLibraryInner {
ptr,
cache: Default::default(),
}),
}
}
}
impl ItemLibraryTrait for ItemLibraryImpl {}
impl DataLibrary<Item> for ItemLibraryImpl {
fn get_cache(&self) -> &spin::rwlock::RwLock<hashbrown::HashMap<u32, Item>> {
&self.inner.cache
}
fn get_self_ref(&self) -> ExternRef<Self> {
self.inner.ptr
}
fn _get_ref_by_name(ptr: ExternRef<Self>, name: ExternRef<StringKey>) -> u32 {
unsafe { item_library_get_item(ptr, name).get_internal_index() }
}
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> u32 {
unsafe { item_library_get_item_by_hash(ptr, hash).get_internal_index() }
}
fn _from_external_ref_index(index: u32) -> Option<Item> {
let v = ExternRef::<ItemImpl>::from(index).get_value();
if let Some(v) = v {
Some(Rc::new(v))
} else {
None
}
}
}
crate::handling::cacheable::cacheable!(ItemLibraryImpl);
impl ExternalReferenceType for ItemLibraryImpl {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
extern "wasm" {
fn item_library_get_item(
ptr: ExternRef<ItemLibraryImpl>,
name: ExternRef<StringKey>,
) -> ExternRef<ItemImpl>;
fn item_library_get_item_by_hash(
ptr: ExternRef<ItemLibraryImpl>,
hash: u32,
) -> ExternRef<ItemImpl>;
}
}
#[cfg(not(feature = "mock_data"))]
pub use implementation::*;