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

77 lines
2.0 KiB
Rust
Executable File

use crate::app_interface::data_libraries::DataLibrary;
use crate::app_interface::{MoveData, StringKey};
use crate::{ExternRef, ExternalReferenceType};
use alloc::rc::Rc;
use spin::RwLock;
struct MoveLibraryInner {
ptr: ExternRef<MoveLibrary>,
cache: RwLock<hashbrown::HashMap<u32, MoveData>>,
}
#[derive(Clone)]
pub struct MoveLibrary {
inner: Rc<MoveLibraryInner>,
}
impl MoveLibrary {
#[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 DataLibrary<MoveData> for MoveLibrary {
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>) -> ExternRef<MoveData> {
unsafe { move_library_get_move(ptr, name) }
}
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<MoveData> {
unsafe { move_library_get_move_by_hash(ptr, hash) }
}
}
crate::handling::cacheable::cacheable!(MoveLibrary);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for MoveLibrary {
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<MoveLibrary>,
name: ExternRef<StringKey>,
) -> ExternRef<MoveData>;
fn move_library_get_move_by_hash(ptr: ExternRef<MoveLibrary>, hash: u32)
-> ExternRef<MoveData>;
}