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

78 lines
2.0 KiB
Rust
Executable File

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