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

102 lines
2.9 KiB
Rust
Executable File

use crate::app_interface::{DataLibrary, MoveData};
use alloc::rc::Rc;
pub trait MoveLibraryTrait: DataLibrary<MoveData> {}
pub type MoveLibrary = Rc<dyn MoveLibraryTrait>;
#[cfg(not(feature = "mock_data"))]
mod implementation {
use super::*;
use crate::app_interface::{MoveDataImpl, StringKey};
use crate::handling::extern_ref::{ExternRef, ExternalReferenceType};
use spin::RwLock;
struct MoveLibraryInner {
ptr: ExternRef<MoveLibraryImpl>,
cache: RwLock<hashbrown::HashMap<u32, MoveData>>,
}
#[derive(Clone)]
pub struct MoveLibraryImpl {
inner: Rc<MoveLibraryInner>,
}
impl MoveLibraryImpl {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
Self {
inner: Rc::new(MoveLibraryInner {
ptr,
cache: Default::default(),
}),
}
}
#[cfg(feature = "mock_data")]
pub fn mock() -> Self {
Self {
inner: Rc::new(MoveLibraryInner {
ptr: ExternRef::mock(),
cache: Default::default(),
}),
}
}
}
impl MoveLibraryTrait for MoveLibraryImpl {}
impl DataLibrary<MoveData> for MoveLibraryImpl {
fn get_cache(&self) -> &spin::rwlock::RwLock<hashbrown::HashMap<u32, MoveData>> {
&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 { move_library_get_move(ptr, name).get_internal_index() }
}
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> u32 {
unsafe { move_library_get_move_by_hash(ptr, hash).get_internal_index() }
}
fn _from_external_ref_index(index: u32) -> Option<MoveData> {
let v = ExternRef::<MoveDataImpl>::from(index).get_value();
if let Some(v) = v {
Some(Rc::new(v))
} else {
None
}
}
}
crate::handling::cacheable::cacheable!(MoveLibraryImpl);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for MoveLibraryImpl {
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<MoveLibraryImpl>,
name: ExternRef<StringKey>,
) -> ExternRef<MoveDataImpl>;
fn move_library_get_move_by_hash(
ptr: ExternRef<MoveLibraryImpl>,
hash: u32,
) -> ExternRef<MoveDataImpl>;
}
}
#[cfg(not(feature = "mock_data"))]
pub use implementation::*;