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

89 lines
2.6 KiB
Rust
Executable File

use crate::app_interface::{DataLibrary, Species};
use alloc::rc::Rc;
pub trait SpeciesLibraryTrait: DataLibrary<Species> {}
pub type SpeciesLibrary = Rc<dyn SpeciesLibraryTrait>;
#[cfg(not(feature = "mock_data"))]
mod implementation {
use crate::app_interface::species_library::SpeciesLibraryTrait;
use crate::app_interface::{DataLibrary, Species, SpeciesImpl, StringKey};
use crate::handling::extern_ref::{ExternRef, ExternalReferenceType};
use alloc::rc::Rc;
use spin::RwLock;
struct SpeciesLibraryInner {
ptr: ExternRef<SpeciesLibraryImpl>,
cache: RwLock<hashbrown::HashMap<u32, Species>>,
}
#[derive(Clone)]
pub struct SpeciesLibraryImpl {
inner: Rc<SpeciesLibraryInner>,
}
impl SpeciesLibraryImpl {
pub fn new(ptr: ExternRef<SpeciesLibraryImpl>) -> Self {
Self {
inner: Rc::new(SpeciesLibraryInner {
ptr,
cache: Default::default(),
}),
}
}
}
impl SpeciesLibraryTrait for SpeciesLibraryImpl {}
impl DataLibrary<Species> for SpeciesLibraryImpl {
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>) -> u32 {
unsafe { species_library_get_species(ptr, name).get_internal_index() }
}
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> u32 {
unsafe { species_library_get_species_by_hash(ptr, hash).get_internal_index() }
}
fn _from_external_ref_index(index: u32) -> Option<Species> {
let v = ExternRef::<SpeciesImpl>::from(index).get_value();
if let Some(v) = v {
Some(Rc::new(v))
} else {
None
}
}
}
crate::handling::cacheable::cacheable!(SpeciesLibraryImpl);
impl ExternalReferenceType for SpeciesLibraryImpl {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
extern "wasm" {
fn species_library_get_species(
ptr: ExternRef<SpeciesLibraryImpl>,
name: ExternRef<StringKey>,
) -> ExternRef<SpeciesImpl>;
fn species_library_get_species_by_hash(
ptr: ExternRef<SpeciesLibraryImpl>,
hash: u32,
) -> ExternRef<SpeciesImpl>;
}
}
#[cfg(not(feature = "mock_data"))]
pub use implementation::*;