Gen7ScriptsRs/pkmn_lib_interface/src/app_interface/static_data/ability.rs

96 lines
3.1 KiB
Rust
Executable File

use crate::StringKey;
use alloc::rc::Rc;
#[cfg_attr(test, mockall::automock)]
pub trait AbilityTrait {
fn name(&self) -> StringKey;
fn effect(&self) -> StringKey;
}
pub type Ability = Rc<dyn AbilityTrait>;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
#[repr(C)]
pub struct AbilityIndex {
pub hidden: bool,
pub index: u8,
}
#[cfg(not(feature = "mock_data"))]
mod implementation {
use super::*;
use crate::app_interface::{EffectParameter, StringKey};
use crate::handling::cached_value::CachedValue;
use crate::handling::extern_ref::{ExternRef, ExternalReferenceType};
use crate::handling::ffi_array::FFIArray;
use crate::handling::{Cacheable, WasmResult};
use crate::{cached_value, cached_value_getters};
use alloc::rc::Rc;
use alloc::vec::Vec;
struct AbilityInner {
reference: ExternRef<AbilityImpl>,
name: CachedValue<StringKey>,
effect: CachedValue<StringKey>,
parameters: CachedValue<Vec<Rc<EffectParameter>>>,
}
#[derive(Clone)]
pub struct AbilityImpl {
inner: Rc<AbilityInner>,
}
impl AbilityImpl {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(AbilityInner {
reference,
name: cached_value!({
ability_get_name(reference).unwrap().get_value().unwrap()
}),
effect: cached_value!({
ability_get_effect(reference).unwrap().get_value().unwrap()
}),
parameters: cached_value!({
let pars: FFIArray<ExternRef<EffectParameter>> =
FFIArray::from_u64(ability_get_parameters(reference).unwrap());
let pars = Vec::from_raw_parts(pars.ptr(), pars.len(), pars.len());
let pars = pars
.into_iter()
.map(|r| Rc::new(EffectParameter::new(r).unwrap()))
.collect::<Vec<_>>();
pars
}),
}),
})
}
}
impl AbilityTrait for AbilityImpl {
cached_value_getters! {
fn name(&self) -> StringKey;
fn effect(&self) -> StringKey;
}
}
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for AbilityImpl {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
crate::handling::cacheable::cacheable!(AbilityImpl);
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn ability_get_name(r: ExternRef<AbilityImpl>) -> WasmResult<ExternRef<StringKey>>;
fn ability_get_effect(r: ExternRef<AbilityImpl>) -> WasmResult<ExternRef<StringKey>>;
fn ability_get_parameters(r: ExternRef<AbilityImpl>) -> WasmResult<u64>;
}
}
#[cfg(not(feature = "mock_data"))]
pub use implementation::*;