diff --git a/gen_7_scripts/src/moves/acrobatics.rs b/gen_7_scripts/src/moves/acrobatics.rs index 74e01e5..0cd9fec 100755 --- a/gen_7_scripts/src/moves/acrobatics.rs +++ b/gen_7_scripts/src/moves/acrobatics.rs @@ -1,5 +1,4 @@ use crate::script; -use alloc::boxed::Box; use core::any::Any; use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; diff --git a/gen_7_scripts/src/moves/after_you.rs b/gen_7_scripts/src/moves/after_you.rs index 09ac776..7846183 100755 --- a/gen_7_scripts/src/moves/after_you.rs +++ b/gen_7_scripts/src/moves/after_you.rs @@ -1,4 +1,3 @@ -use alloc::boxed::Box; use core::any::Any; use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; diff --git a/gen_7_scripts/src/moves/assurance.rs b/gen_7_scripts/src/moves/assurance.rs index 505a12d..49100f0 100755 --- a/gen_7_scripts/src/moves/assurance.rs +++ b/gen_7_scripts/src/moves/assurance.rs @@ -3,7 +3,7 @@ use alloc::boxed::Box; use core::any::Any; use core::sync::atomic::{AtomicBool, Ordering}; use pkmn_lib_interface::app_interface::{ - BattleSide, DamageSource, DataLibrary, ExecutingMove, Pokemon, TurnChoice, WithVolatile, + BattleSide, DamageSource, ExecutingMove, Pokemon, TurnChoice, WithVolatile, }; use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; diff --git a/gen_7_scripts/src/moves/aurora_veil.rs b/gen_7_scripts/src/moves/aurora_veil.rs index 1e92247..2af9187 100644 --- a/gen_7_scripts/src/moves/aurora_veil.rs +++ b/gen_7_scripts/src/moves/aurora_veil.rs @@ -5,9 +5,7 @@ use crate::weather::hail::Hail; use alloc::boxed::Box; use core::any::Any; use core::sync::atomic::{AtomicU32, Ordering}; -use pkmn_lib_interface::app_interface::{ - BattleSide, ExecutingMove, MoveCategory, Pokemon, WithVolatile, -}; +use pkmn_lib_interface::app_interface::{ExecutingMove, MoveCategory, Pokemon, WithVolatile}; use pkmn_lib_interface::handling::ScriptCapabilities::OnEndTurn; use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; diff --git a/gen_7_scripts/src/moves/change_all_target_stats.rs b/gen_7_scripts/src/moves/change_all_target_stats.rs index b543008..9d53159 100644 --- a/gen_7_scripts/src/moves/change_all_target_stats.rs +++ b/gen_7_scripts/src/moves/change_all_target_stats.rs @@ -2,7 +2,7 @@ use crate::script; use alloc::rc::Rc; use core::any::Any; use core::sync::atomic::{AtomicI8, Ordering}; -use pkmn_lib_interface::app_interface::list::{ImmutableList, ImmutableListTrait}; +use pkmn_lib_interface::app_interface::list::ImmutableList; use pkmn_lib_interface::app_interface::{ DynamicLibrary, EffectParameter, ExecutingMove, Pokemon, Statistic, }; @@ -34,7 +34,7 @@ impl Script for ChangeAllTargetStats { fn on_initialize( &self, - _library: &DynamicLibrary, + _library: DynamicLibrary, parameters: Option>>, ) { self.amount.store( diff --git a/gen_7_scripts/src/moves/change_target_stats.rs b/gen_7_scripts/src/moves/change_target_stats.rs index d6281ff..0154e82 100644 --- a/gen_7_scripts/src/moves/change_target_stats.rs +++ b/gen_7_scripts/src/moves/change_target_stats.rs @@ -42,7 +42,7 @@ macro_rules! change_stat_effect { fn on_initialize( &self, - _library: &DynamicLibrary, + _library: DynamicLibrary, parameters: Option>>, ) { self.amount.store( diff --git a/gen_7_scripts/src/moves/drain.rs b/gen_7_scripts/src/moves/drain.rs index 2847fef..c6a31ea 100644 --- a/gen_7_scripts/src/moves/drain.rs +++ b/gen_7_scripts/src/moves/drain.rs @@ -3,7 +3,7 @@ use alloc::rc::Rc; use atomic_float::AtomicF32; use core::any::Any; use core::sync::atomic::Ordering; -use pkmn_lib_interface::app_interface::list::{ImmutableList, ImmutableListTrait}; +use pkmn_lib_interface::app_interface::list::ImmutableList; use pkmn_lib_interface::app_interface::{DynamicLibrary, EffectParameter, ExecutingMove, Pokemon}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; @@ -29,7 +29,7 @@ impl Script for Drain { fn on_initialize( &self, - _library: &DynamicLibrary, + _library: DynamicLibrary, parameters: Option>>, ) { self.heal_modifier.store( diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/battle.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/battle.rs index 817450f..84e4152 100755 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/battle.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/battle.rs @@ -2,8 +2,7 @@ use alloc::rc::Rc; use crate::app_interface::list::ImmutableList; use crate::app_interface::{ - BattleParty, BattlePartyImpl, BattleRandom, BattleSide, ChoiceQueue, DynamicLibrary, Pokemon, - StringKey, + BattleParty, BattleRandom, BattleSide, ChoiceQueue, DynamicLibrary, Pokemon, StringKey, }; #[cfg_attr(feature = "mock_data", mockall::automock)] @@ -33,6 +32,7 @@ pub type MockBattle = MockBattleTrait; #[cfg(not(feature = "mock_data"))] mod implementation { use super::*; + use crate::app_interface::dynamic_data::dynamic_library::DynamicLibraryImpl; use crate::app_interface::list::{ BattlePartyImmutableList, BattleSideImmutableList, ImmutableListWasm, }; @@ -44,8 +44,8 @@ mod implementation { use crate::handling::cached_value::CachedValue; use crate::handling::Cacheable; use crate::{ - cached_value, cached_value_getters, wasm_value_getters, wasm_value_getters_extern, - wasm_value_getters_funcs, ExternRef, ExternalReferenceType, StringKey, VecExternRef, + cached_value, cached_value_getters, wasm_value_getters_extern, wasm_value_getters_funcs, + ExternRef, ExternalReferenceType, StringKey, VecExternRef, }; struct BattleInner { @@ -68,7 +68,9 @@ mod implementation { Self::from_ref(reference, &|reference| Self { inner: Rc::new(BattleInner { reference, - library: cached_value!({ battle_get_library(reference).get_value().unwrap() }), + library: cached_value!({ + Rc::new(battle_get_library(reference).get_value().unwrap()) + }), parties: cached_value!({ let reference = battle_get_parties(reference); Rc::new(BattlePartyImmutableList::from_ref(reference)) @@ -168,7 +170,7 @@ mod implementation { #[cfg(not(feature = "mock_data"))] extern "wasm" { - fn battle_get_library(r: ExternRef) -> ExternRef; + fn battle_get_library(r: ExternRef) -> ExternRef; fn battle_get_parties(r: ExternRef) -> VecExternRef; fn battle_get_sides(r: ExternRef) -> VecExternRef; fn battle_get_random(r: ExternRef) -> ExternRef; diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/battle_party.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/battle_party.rs index addfe30..619e621 100755 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/battle_party.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/battle_party.rs @@ -1,7 +1,4 @@ -use crate::app_interface::{Party, PartyImpl}; -use crate::handling::cached_value::CachedValue; -use crate::handling::Cacheable; -use crate::{cached_value, cached_value_getters, ExternRef, ExternalReferenceType}; +use crate::app_interface::Party; use alloc::rc::Rc; pub trait BattlePartyTrait { @@ -10,47 +7,58 @@ pub trait BattlePartyTrait { pub type BattleParty = Rc; -struct BattlePartyInner { - reference: ExternRef, - party: CachedValue>, -} - -#[derive(Clone)] -pub struct BattlePartyImpl { - inner: Rc, -} - #[cfg(not(feature = "mock_data"))] -impl BattlePartyImpl { - pub fn new(reference: ExternRef) -> Self { - Self::from_ref(reference, &|reference| Self { - inner: Rc::new(BattlePartyInner { - reference, - party: cached_value!({ - Rc::new(battle_party_get_party(reference).get_value().unwrap()) +mod implementation { + use crate::app_interface::{BattlePartyTrait, Party, PartyImpl}; + use crate::handling::cached_value::CachedValue; + use crate::handling::extern_ref::{ExternRef, ExternalReferenceType}; + use crate::handling::Cacheable; + use crate::{cached_value, cached_value_getters}; + use alloc::rc::Rc; + + struct BattlePartyInner { + reference: ExternRef, + party: CachedValue>, + } + + #[derive(Clone)] + pub struct BattlePartyImpl { + inner: Rc, + } + + impl BattlePartyImpl { + pub fn new(reference: ExternRef) -> Self { + Self::from_ref(reference, &|reference| Self { + inner: Rc::new(BattlePartyInner { + reference, + party: cached_value!({ + Rc::new(battle_party_get_party(reference).get_value().unwrap()) + }), }), - }), - }) + }) + } + } + + impl BattlePartyTrait for BattlePartyImpl { + cached_value_getters! { + fn party(&self) -> Party; + } + } + + crate::handling::cacheable::cacheable!(BattlePartyImpl); + + #[cfg(not(feature = "mock_data"))] + impl ExternalReferenceType for BattlePartyImpl { + fn from_extern_value(reference: ExternRef) -> Self { + Self::new(reference) + } + } + + #[cfg(not(feature = "mock_data"))] + extern "wasm" { + fn battle_party_get_party(r: ExternRef) -> ExternRef; } } #[cfg(not(feature = "mock_data"))] -impl BattlePartyTrait for BattlePartyImpl { - cached_value_getters! { - fn party(&self) -> Party; - } -} - -crate::handling::cacheable::cacheable!(BattlePartyImpl); - -#[cfg(not(feature = "mock_data"))] -impl ExternalReferenceType for BattlePartyImpl { - fn from_extern_value(reference: ExternRef) -> Self { - Self::new(reference) - } -} - -#[cfg(not(feature = "mock_data"))] -extern "wasm" { - fn battle_party_get_party(r: ExternRef) -> ExternRef; -} +pub use implementation::*; diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/dynamic_library.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/dynamic_library.rs index 1f1fbb5..0c4e809 100755 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/dynamic_library.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/dynamic_library.rs @@ -1,57 +1,66 @@ use crate::app_interface::StaticData; -use crate::handling::cached_value::CachedValue; -use crate::handling::Cacheable; -use crate::{cached_value, ExternRef, ExternalReferenceType}; use alloc::rc::Rc; -struct DynamicLibraryInner { - ptr: ExternRef, - static_data: CachedValue, +pub trait DynamicLibraryTrait { + fn data_library(&self) -> StaticData; } -#[derive(Clone)] -pub struct DynamicLibrary { - inner: Rc, -} +pub type DynamicLibrary = Rc; -crate::handling::cacheable::cacheable!(DynamicLibrary); +#[cfg(not(feature = "mock_data"))] +mod implementation { + use super::*; + use crate::app_interface::dynamic_data::dynamic_library::DynamicLibraryTrait; + use crate::app_interface::StaticDataImpl; + use crate::cached_value; + use crate::handling::cached_value::CachedValue; + use crate::handling::extern_ref::{ExternRef, ExternalReferenceType}; + use crate::handling::Cacheable; + use alloc::rc::Rc; -impl DynamicLibrary { - #[cfg(not(feature = "mock_data"))] - pub(crate) fn new(ptr: ExternRef) -> Self { - Self::from_ref(ptr, &|ptr| Self { - inner: Rc::new(DynamicLibraryInner { - ptr, - static_data: cached_value!({ - dynamic_library_get_static_data(ptr).get_value().unwrap() - }), - }), - }) + struct DynamicLibraryInner { + ptr: ExternRef, + static_data: CachedValue, } - #[cfg(feature = "mock_data")] - pub fn new(data: StaticData) -> Self { - Self { - inner: Rc::new(DynamicLibraryInner { - ptr: ExternRef::mock(), - static_data: data.into(), - }), + #[derive(Clone)] + pub struct DynamicLibraryImpl { + inner: Rc, + } + + crate::handling::cacheable::cacheable!(DynamicLibraryImpl); + + impl DynamicLibraryImpl { + pub(crate) fn new(ptr: ExternRef) -> Self { + Self::from_ref(ptr, &|ptr| Self { + inner: Rc::new(DynamicLibraryInner { + ptr, + static_data: cached_value!({ + Rc::new(dynamic_library_get_static_data(ptr).get_value().unwrap()) + }), + }), + }) } } - pub fn data_library(&self) -> StaticData { - self.inner.static_data.value() + impl DynamicLibraryTrait for DynamicLibraryImpl { + fn data_library(&self) -> StaticData { + self.inner.static_data.value() + } + } + + impl ExternalReferenceType for DynamicLibraryImpl { + fn from_extern_value(reference: ExternRef) -> Self { + DynamicLibraryImpl::new(reference) + } + } + + extern "wasm" { + fn dynamic_library_get_static_data( + ptr: ExternRef, + ) -> ExternRef; } } #[cfg(not(feature = "mock_data"))] -impl ExternalReferenceType for DynamicLibrary { - fn from_extern_value(reference: ExternRef) -> Self { - DynamicLibrary::new(reference) - } -} - -#[cfg(not(feature = "mock_data"))] -extern "wasm" { - fn dynamic_library_get_static_data(ptr: ExternRef) -> ExternRef; -} +pub use implementation::*; diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/executing_move.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/executing_move.rs index 905a99f..7ffbd2d 100755 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/executing_move.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/executing_move.rs @@ -1,9 +1,5 @@ -#[cfg(not(feature = "mock_data"))] -use crate::app_interface::PokemonImpl; use crate::app_interface::{LearnedMove, MoveData, Pokemon}; -use crate::handling::cached_value::CachedValue; -use crate::handling::temporary::Temporary; -use crate::{cached_value, ExternRef, ExternalReferenceType, Script}; +use crate::handling::Script; use alloc::rc::Rc; #[cfg_attr(feature = "mock_data", mockall::automock)] @@ -47,6 +43,11 @@ pub use implementation::*; #[cfg(not(feature = "mock_data"))] mod implementation { use super::*; + use crate::app_interface::{LearnedMoveImpl, PokemonImpl}; + use crate::cached_value; + use crate::handling::cached_value::CachedValue; + use crate::handling::extern_ref::{ExternRef, ExternalReferenceType}; + use crate::handling::temporary::Temporary; #[derive(Clone)] pub struct ExecutingMoveImpl { @@ -57,7 +58,7 @@ mod implementation { reference: ExternRef, number_of_hits: CachedValue, user: CachedValue, - chosen_move: CachedValue, + chosen_move: CachedValue>, use_move: CachedValue, } @@ -76,9 +77,11 @@ mod implementation { Rc::new(executing_move_get_user(reference).get_value().unwrap()) }), chosen_move: cached_value!({ - executing_move_get_chosen_move(reference) - .get_value() - .unwrap() + Rc::new( + executing_move_get_chosen_move(reference) + .get_value() + .unwrap(), + ) }), use_move: cached_value!({ executing_move_get_use_move(reference).get_value().unwrap() @@ -196,7 +199,7 @@ mod implementation { fn executing_move_get_user(r: ExternRef) -> ExternRef; fn executing_move_get_chosen_move( r: ExternRef, - ) -> ExternRef; + ) -> ExternRef; fn executing_move_get_use_move(r: ExternRef) -> ExternRef; #[allow(improper_ctypes)] fn executing_move_get_script(r: ExternRef) -> *const dyn Script; diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/learned_move.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/learned_move.rs index 5d32b49..7a24ebc 100755 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/learned_move.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/learned_move.rs @@ -1,18 +1,6 @@ use crate::app_interface::MoveData; -use crate::handling::cacheable::Cacheable; -use crate::handling::cached_value::CachedValue; -use crate::{ - cached_value, cached_value_getters, wasm_value_getters, ExternRef, ExternalReferenceType, -}; - use alloc::rc::Rc; -struct LearnedMoveInner { - reference: ExternRef, - move_data: CachedValue, - learn_method: CachedValue, -} - #[derive(Copy, Clone, Debug, Default)] #[repr(u8)] pub enum MoveLearnMethod { @@ -23,64 +11,89 @@ pub enum MoveLearnMethod { Level = 1, } -#[derive(Clone)] -pub struct LearnedMove { - inner: Rc, +pub trait LearnedMoveTrait { + fn move_data(&self) -> MoveData; + fn learn_method(&self) -> MoveLearnMethod; + fn restore_all_uses(&self); + fn restore_uses(&self, uses: u8); } -crate::handling::cacheable::cacheable!(LearnedMove); +pub type LearnedMove = Rc; #[cfg(not(feature = "mock_data"))] -impl ExternalReferenceType for LearnedMove { - fn from_extern_value(reference: ExternRef) -> Self { - Self::new(reference) - } -} +mod implementation { + use super::*; + use crate::handling::cached_value::CachedValue; + use crate::handling::extern_ref::{ExternRef, ExternalReferenceType}; + use crate::handling::Cacheable; + use crate::{cached_value, cached_value_getters, wasm_value_getters}; + use alloc::rc::Rc; -impl LearnedMove { - #[cfg(not(feature = "mock_data"))] - pub fn new(reference: ExternRef) -> Self { - Self::from_ref(reference, &|reference| Self { - inner: Rc::new(LearnedMoveInner { - reference, - move_data: cached_value!({ - learned_move_get_move_data(reference).get_value().unwrap() + #[derive(Clone)] + pub struct LearnedMoveImpl { + inner: Rc, + } + + struct LearnedMoveInner { + reference: ExternRef, + move_data: CachedValue, + learn_method: CachedValue, + } + + crate::handling::cacheable::cacheable!(LearnedMoveImpl); + + impl ExternalReferenceType for LearnedMoveImpl { + fn from_extern_value(reference: ExternRef) -> Self { + Self::new(reference) + } + } + + impl LearnedMoveImpl { + pub fn new(reference: ExternRef) -> Self { + Self::from_ref(reference, &|reference| Self { + inner: Rc::new(LearnedMoveInner { + reference, + move_data: cached_value!({ + learned_move_get_move_data(reference).get_value().unwrap() + }), + learn_method: cached_value!({ learned_move_get_learn_method(reference) }), }), - learn_method: cached_value!({ learned_move_get_learn_method(reference) }), - }), - }) - } - - cached_value_getters! { - pub fn move_data(&self) -> MoveData; - pub fn learn_method(&self) -> MoveLearnMethod; - } - - #[cfg(not(feature = "mock_data"))] - pub fn restore_all_uses(&self) { - unsafe { - learned_move_restore_all_uses(self.inner.reference); + }) } } - #[cfg(not(feature = "mock_data"))] - pub fn restore_uses(&self, uses: u8) { - unsafe { - learned_move_restore_uses(self.inner.reference, uses); + impl LearnedMoveTrait for LearnedMoveImpl { + cached_value_getters! { + fn move_data(&self) -> MoveData; + fn learn_method(&self) -> MoveLearnMethod; + } + + fn restore_all_uses(&self) { + unsafe { + learned_move_restore_all_uses(self.inner.reference); + } + } + + fn restore_uses(&self, uses: u8) { + unsafe { + learned_move_restore_uses(self.inner.reference, uses); + } } } -} -wasm_value_getters! { - LearnedMove, - pub fn max_pp(&self) -> u8; - pub fn remaining_pp(&self) -> u8; + wasm_value_getters! { + LearnedMoveImpl, + pub fn max_pp(&self) -> u8; + pub fn remaining_pp(&self) -> u8; + } + + extern "wasm" { + fn learned_move_get_move_data(r: ExternRef) -> ExternRef; + fn learned_move_get_learn_method(r: ExternRef) -> MoveLearnMethod; + fn learned_move_restore_uses(r: ExternRef, uses: u8); + fn learned_move_restore_all_uses(r: ExternRef); + } } #[cfg(not(feature = "mock_data"))] -extern "wasm" { - fn learned_move_get_move_data(r: ExternRef) -> ExternRef; - fn learned_move_get_learn_method(r: ExternRef) -> MoveLearnMethod; - fn learned_move_restore_uses(r: ExternRef, uses: u8); - fn learned_move_restore_all_uses(r: ExternRef); -} +pub use implementation::*; diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/mod.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/mod.rs index e7209da..dfd91d3 100755 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/mod.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/mod.rs @@ -17,7 +17,7 @@ pub use battle_party::*; pub use battle_random::*; pub use battle_side::*; pub use choice_queue::*; -pub use dynamic_library::DynamicLibrary; +pub use dynamic_library::*; pub use executing_move::*; pub use learned_move::*; pub use party::*; diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/party.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/party.rs index 1d01beb..a6e1507 100755 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/party.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/party.rs @@ -1,7 +1,4 @@ use crate::app_interface::Pokemon; -#[cfg(not(feature = "mock_data"))] -use crate::app_interface::PokemonImpl; -use crate::{ExternRef, ExternalReferenceType}; use alloc::rc::Rc; pub trait PartyTrait { @@ -11,45 +8,51 @@ pub trait PartyTrait { pub type Party = Rc; -#[derive(Clone)] -pub struct PartyImpl { - reference: ExternRef, -} - #[cfg(not(feature = "mock_data"))] -impl PartyImpl { - pub fn new(reference: ExternRef) -> Self { - Self { reference } +mod implementation { + use super::*; + use crate::app_interface::PokemonImpl; + use crate::handling::extern_ref::{ExternRef, ExternalReferenceType}; + + #[derive(Clone)] + pub struct PartyImpl { + reference: ExternRef, } -} -#[cfg(not(feature = "mock_data"))] -impl PartyTrait for PartyImpl { - fn get_pokemon(&self, index: usize) -> Option { - unsafe { - let v = party_get_pokemon(self.reference, index).get_value(); - if let Some(v) = v { - Some(Rc::new(v)) - } else { - None - } + impl PartyImpl { + pub fn new(reference: ExternRef) -> Self { + Self { reference } } } - fn length(&self) -> usize { - unsafe { party_get_length(self.reference) } + impl PartyTrait for PartyImpl { + fn get_pokemon(&self, index: usize) -> Option { + unsafe { + let v = party_get_pokemon(self.reference, index).get_value(); + if let Some(v) = v { + Some(Rc::new(v)) + } else { + None + } + } + } + + fn length(&self) -> usize { + unsafe { party_get_length(self.reference) } + } + } + + impl ExternalReferenceType for PartyImpl { + fn from_extern_value(reference: ExternRef) -> Self { + Self::new(reference) + } + } + + extern "wasm" { + fn party_get_pokemon(r: ExternRef, index: usize) -> ExternRef; + fn party_get_length(r: ExternRef) -> usize; } } #[cfg(not(feature = "mock_data"))] -impl ExternalReferenceType for PartyImpl { - fn from_extern_value(reference: ExternRef) -> Self { - Self::new(reference) - } -} - -#[cfg(not(feature = "mock_data"))] -extern "wasm" { - fn party_get_pokemon(r: ExternRef, index: usize) -> ExternRef; - fn party_get_length(r: ExternRef) -> usize; -} +pub use implementation::*; diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/pokemon.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/pokemon.rs index 160a025..4587bfc 100755 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/pokemon.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/pokemon.rs @@ -86,7 +86,10 @@ mod implementation { use super::*; use cstr_core::CString; - use crate::app_interface::{AbilityImpl, BattleImpl, ItemImpl, StatisticSetImpl}; + use crate::app_interface::dynamic_data::dynamic_library::DynamicLibraryImpl; + use crate::app_interface::{ + AbilityImpl, BattleImpl, ItemImpl, LearnedMoveImpl, StatisticSetImpl, + }; use crate::handling::cached_value::CachedValue; use crate::handling::Cacheable; use crate::implementation::ScriptPtr; @@ -94,7 +97,7 @@ mod implementation { cached_value, cached_value_getters, wasm_optional_reference_getters_extern, wasm_optional_reference_getters_funcs, wasm_reference_getters_extern, wasm_reference_getters_funcs, wasm_value_getters_extern, wasm_value_getters_funcs, - ExternRef, ExternalReferenceType, Script, + ExternRef, ExternalReferenceType, }; struct PokemonInner { @@ -208,7 +211,14 @@ mod implementation { false } fn get_learned_move(&self, index: usize) -> Option { - unsafe { pokemon_get_learned_move(self.inner.reference, index).get_value() } + unsafe { + let v = pokemon_get_learned_move(self.inner.reference, index).get_value(); + if let Some(v) = v { + Some(Rc::new(v)) + } else { + None + } + } } fn change_stat_boost( &self, @@ -350,7 +360,9 @@ mod implementation { Self::from_ref(reference, &|reference| Self { inner: Rc::new(PokemonInner { reference, - library: cached_value!({ pokemon_get_library(reference).get_value().unwrap() }), + library: cached_value!({ + Rc::new(pokemon_get_library(reference).get_value().unwrap()) + }), flat_stats: cached_value!({ Rc::new(pokemon_get_flat_stats(reference).get_value().unwrap()) }), @@ -428,7 +440,7 @@ mod implementation { } extern "wasm" { - fn pokemon_get_library(r: ExternRef) -> ExternRef; + fn pokemon_get_library(r: ExternRef) -> ExternRef; fn pokemon_get_flat_stats(r: ExternRef) -> ExternRef>; fn pokemon_get_stat_boosts(r: ExternRef) -> ExternRef>; @@ -452,7 +464,7 @@ mod implementation { fn pokemon_get_learned_move( r: ExternRef, index: usize, - ) -> ExternRef; + ) -> ExternRef; fn pokemon_change_stat_boost( r: ExternRef, tat: Statistic, diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/turn_choices.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/turn_choices.rs index 5cfbd60..ea54ec1 100755 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/turn_choices.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/turn_choices.rs @@ -1,70 +1,8 @@ -#[cfg(not(feature = "mock_data"))] -use crate::app_interface::PokemonImpl; use crate::app_interface::{LearnedMove, Pokemon}; -use crate::handling::cached_value::CachedValue; -use crate::handling::temporary::Temporary; -use crate::ExternRef; -use crate::{cached_value, ExternalReferenceType, Script}; +use crate::handling::Script; use alloc::boxed::Box; use alloc::rc::Rc; -pub trait BaseTurnChoiceDataTrait { - fn reference(&self) -> u32; - fn user(&self) -> Pokemon; - fn speed(&self) -> u32; - fn has_failed(&self) -> bool; - fn fail(&self); -} - -#[cfg(not(feature = "mock_data"))] -struct BaseTurnChoiceDataImpl { - reference: ExternRef, - user: CachedValue>, -} - -#[cfg(not(feature = "mock_data"))] -impl BaseTurnChoiceDataTrait for BaseTurnChoiceDataImpl { - fn reference(&self) -> u32 { - self.reference.get_internal_index() - } - - fn user(&self) -> Pokemon { - self.user.value() - } - fn speed(&self) -> u32 { - unsafe { turn_choice_get_speed(self.reference) } - } - fn has_failed(&self) -> bool { - unsafe { turn_choice_has_failed(self.reference) } - } - fn fail(&self) { - unsafe { turn_choice_fail(self.reference) } - } -} - -pub type BaseTurnChoiceData = Rc; - -struct MoveTurnChoiceDataInner { - base: BaseTurnChoiceData, - used_move: CachedValue, - target_side: CachedValue, - target_index: CachedValue, -} - -pub trait MoveTurnChoiceDataTrait { - fn base(&self) -> BaseTurnChoiceData; - fn used_move(&self) -> LearnedMove; - fn target_side(&self) -> u8; - fn target_index(&self) -> u8; - fn priority(&self) -> i8; - fn move_script(&self) -> Option<&Box>; -} - -#[derive(Clone)] -pub struct MoveTurnChoiceDataImpl { - inner: Temporary, -} - pub enum TurnChoice { Move(Box), Item(), @@ -98,78 +36,149 @@ impl TurnChoice { } } -#[cfg(not(feature = "mock_data"))] -impl MoveTurnChoiceDataTrait for MoveTurnChoiceDataImpl { - fn base(&self) -> BaseTurnChoiceData { - self.inner.value().base.clone() - } +pub trait BaseTurnChoiceDataTrait { + fn reference(&self) -> u32; + fn user(&self) -> Pokemon; + fn speed(&self) -> u32; + fn has_failed(&self) -> bool; + fn fail(&self); +} - fn used_move(&self) -> LearnedMove { - self.inner.value().used_move.value() - } - fn target_side(&self) -> u8 { - self.inner.value().target_side.value() - } - fn target_index(&self) -> u8 { - self.inner.value().target_index.value() - } - fn priority(&self) -> i8 { - unsafe { turn_choice_move_priority(self.base().reference().into()) } - } - fn move_script(&self) -> Option<&Box> { - unsafe { turn_choice_move_script(self.base().reference().into()).as_ref() } - } +pub type BaseTurnChoiceData = Rc; + +pub trait MoveTurnChoiceDataTrait { + fn base(&self) -> BaseTurnChoiceData; + fn used_move(&self) -> LearnedMove; + fn target_side(&self) -> u8; + fn target_index(&self) -> u8; + fn priority(&self) -> i8; + fn move_script(&self) -> Option<&Box>; } #[cfg(not(feature = "mock_data"))] -impl ExternalReferenceType for TurnChoice { - fn from_extern_value(reference: ExternRef) -> Self { - let kind = unsafe { turn_choice_get_kind(reference) }; - match kind { - 0 => TurnChoice::Move(Box::new(MoveTurnChoiceDataImpl { - inner: Temporary::new( - reference.get_internal_index(), - MoveTurnChoiceDataInner::from_reference(reference.cast()), - ), - })), - _ => panic!("Unknown turn choice type"), +mod implementation { + use super::*; + use crate::app_interface::{LearnedMoveImpl, PokemonImpl}; + use crate::cached_value; + use crate::handling::cached_value::CachedValue; + use crate::handling::extern_ref::{ExternRef, ExternalReferenceType}; + use crate::handling::temporary::Temporary; + + struct BaseTurnChoiceDataImpl { + reference: ExternRef, + user: CachedValue>, + } + + impl BaseTurnChoiceDataTrait for BaseTurnChoiceDataImpl { + fn reference(&self) -> u32 { + self.reference.get_internal_index() + } + + fn user(&self) -> Pokemon { + self.user.value() + } + fn speed(&self) -> u32 { + unsafe { turn_choice_get_speed(self.reference) } + } + fn has_failed(&self) -> bool { + unsafe { turn_choice_has_failed(self.reference) } + } + fn fail(&self) { + unsafe { turn_choice_fail(self.reference) } } } -} -#[cfg(not(feature = "mock_data"))] -impl MoveTurnChoiceDataInner { - fn from_reference(reference: ExternRef) -> Self { - Self { - base: Rc::new(BaseTurnChoiceDataImpl { - reference: reference.cast(), - user: cached_value!({ - Rc::new(turn_choice_get_user(reference.cast()).get_value().unwrap()) + struct MoveTurnChoiceDataInner { + base: BaseTurnChoiceData, + used_move: CachedValue, + target_side: CachedValue, + target_index: CachedValue, + } + + #[derive(Clone)] + pub struct MoveTurnChoiceDataImpl { + inner: Temporary, + } + + #[cfg(not(feature = "mock_data"))] + impl MoveTurnChoiceDataTrait for MoveTurnChoiceDataImpl { + fn base(&self) -> BaseTurnChoiceData { + self.inner.value().base.clone() + } + + fn used_move(&self) -> LearnedMove { + self.inner.value().used_move.value() + } + fn target_side(&self) -> u8 { + self.inner.value().target_side.value() + } + fn target_index(&self) -> u8 { + self.inner.value().target_index.value() + } + fn priority(&self) -> i8 { + unsafe { turn_choice_move_priority(self.base().reference().into()) } + } + fn move_script(&self) -> Option<&Box> { + unsafe { turn_choice_move_script(self.base().reference().into()).as_ref() } + } + } + + #[cfg(not(feature = "mock_data"))] + impl ExternalReferenceType for TurnChoice { + fn from_extern_value(reference: ExternRef) -> Self { + let kind = unsafe { turn_choice_get_kind(reference) }; + match kind { + 0 => TurnChoice::Move(Box::new(MoveTurnChoiceDataImpl { + inner: Temporary::new( + reference.get_internal_index(), + MoveTurnChoiceDataInner::from_reference(reference.cast()), + ), + })), + _ => panic!("Unknown turn choice type"), + } + } + } + + #[cfg(not(feature = "mock_data"))] + impl MoveTurnChoiceDataInner { + fn from_reference(reference: ExternRef) -> Self { + Self { + base: Rc::new(BaseTurnChoiceDataImpl { + reference: reference.cast(), + user: cached_value!({ + Rc::new(turn_choice_get_user(reference.cast()).get_value().unwrap()) + }), }), - }), - used_move: cached_value!({ - turn_choice_move_used_move(reference.cast()) - .get_value() - .unwrap() - }), - target_side: cached_value!({ turn_choice_move_target_side(reference.cast()) }), - target_index: cached_value!({ turn_choice_move_target_index(reference.cast()) }), + used_move: cached_value!({ + Rc::new( + turn_choice_move_used_move(reference.cast()) + .get_value() + .unwrap(), + ) + }), + target_side: cached_value!({ turn_choice_move_target_side(reference.cast()) }), + target_index: cached_value!({ turn_choice_move_target_index(reference.cast()) }), + } } } + + extern "wasm" { + fn turn_choice_get_kind(r: ExternRef) -> u8; + fn turn_choice_get_user(r: ExternRef) -> ExternRef; + fn turn_choice_get_speed(r: ExternRef) -> u32; + fn turn_choice_has_failed(r: ExternRef) -> bool; + fn turn_choice_fail(r: ExternRef); + + fn turn_choice_move_used_move( + r: ExternRef, + ) -> ExternRef; + fn turn_choice_move_target_side(r: ExternRef) -> u8; + fn turn_choice_move_target_index(r: ExternRef) -> u8; + fn turn_choice_move_priority(r: ExternRef) -> i8; + #[allow(improper_ctypes)] + fn turn_choice_move_script(r: ExternRef) -> *const Box; + } } #[cfg(not(feature = "mock_data"))] -extern "wasm" { - fn turn_choice_get_kind(r: ExternRef) -> u8; - fn turn_choice_get_user(r: ExternRef) -> ExternRef; - fn turn_choice_get_speed(r: ExternRef) -> u32; - fn turn_choice_has_failed(r: ExternRef) -> bool; - fn turn_choice_fail(r: ExternRef); - - fn turn_choice_move_used_move(r: ExternRef) -> ExternRef; - fn turn_choice_move_target_side(r: ExternRef) -> u8; - fn turn_choice_move_target_index(r: ExternRef) -> u8; - fn turn_choice_move_priority(r: ExternRef) -> i8; - #[allow(improper_ctypes)] - fn turn_choice_move_script(r: ExternRef) -> *const Box; -} +pub use implementation::*; diff --git a/pkmn_lib_interface/src/app_interface/list.rs b/pkmn_lib_interface/src/app_interface/list.rs index 48c1848..d91a5df 100755 --- a/pkmn_lib_interface/src/app_interface/list.rs +++ b/pkmn_lib_interface/src/app_interface/list.rs @@ -1,4 +1,3 @@ -use crate::{ExternalReferenceType, VecExternRef}; use alloc::rc::Rc; pub trait ImmutableListTrait { diff --git a/pkmn_lib_interface/src/app_interface/static_data/data_libraries/item_library.rs b/pkmn_lib_interface/src/app_interface/static_data/data_libraries/item_library.rs index a9d2efc..4925aae 100755 --- a/pkmn_lib_interface/src/app_interface/static_data/data_libraries/item_library.rs +++ b/pkmn_lib_interface/src/app_interface/static_data/data_libraries/item_library.rs @@ -1,4 +1,4 @@ -use crate::app_interface::{DataLibrary, Item, ItemImpl}; +use crate::app_interface::{DataLibrary, ItemImpl}; use crate::{ExternRef, ExternalReferenceType, StringKey}; use alloc::rc::Rc; use spin::rwlock::RwLock; diff --git a/pkmn_lib_interface/src/app_interface/static_data/data_libraries/mod.rs b/pkmn_lib_interface/src/app_interface/static_data/data_libraries/mod.rs index 52414fe..cae27d4 100755 --- a/pkmn_lib_interface/src/app_interface/static_data/data_libraries/mod.rs +++ b/pkmn_lib_interface/src/app_interface/static_data/data_libraries/mod.rs @@ -1,232 +1,249 @@ -use crate::{cached_value, cached_value_getters, ExternRef, ExternalReferenceType, StringKey}; +use crate::app_interface::item_library::ItemLibrary; +use crate::app_interface::move_library::MoveLibrary; +use crate::app_interface::species_library::SpeciesLibrary; +use crate::app_interface::type_library::TypeLibrary; +use crate::app_interface::LevelInt; use alloc::rc::Rc; -use move_library::MoveLibrary; -use spin::rwlock::RwLock; pub mod item_library; pub mod move_library; pub mod species_library; pub mod type_library; -use crate::app_interface::species_library::SpeciesLibrary; -use crate::app_interface::type_library::TypeLibrary; -use crate::app_interface::{get_hash, LevelInt}; -use crate::handling::cached_value::CachedValue; -use crate::handling::Cacheable; -pub use item_library::*; -pub use move_library::*; - -struct StaticDataInner { - reference: ExternRef, - move_library: CachedValue, - item_library: CachedValue, - species_library: CachedValue, - type_library: CachedValue, - settings: CachedValue, +pub trait StaticDataTrait { + fn move_library(&self) -> MoveLibrary; + fn item_library(&self) -> ItemLibrary; + fn species_library(&self) -> SpeciesLibrary; + fn type_library(&self) -> TypeLibrary; } -#[derive(Clone)] -pub struct StaticData { - inner: Rc, +pub type StaticData = Rc; + +pub trait LibrarySettingsTrait { + fn maximum_level(&self) -> LevelInt; } -impl StaticData { - #[cfg(not(feature = "mock_data"))] - pub(crate) fn new(reference: ExternRef) -> Self { - Self::from_ref(reference, &|reference| Self { - inner: Rc::new(StaticDataInner { - reference, - move_library: cached_value!({ - static_data_get_move_library(reference).get_value().unwrap() - }), - item_library: cached_value!({ - static_data_get_item_library(reference).get_value().unwrap() - }), - species_library: cached_value!({ - static_data_get_species_library(reference) - .get_value() - .unwrap() - }), - type_library: cached_value!({ - static_data_get_type_library(reference).get_value().unwrap() - }), - settings: cached_value!({ - static_data_get_library_settings(reference) - .get_value() - .unwrap() - }), - }), - }) +pub type LibrarySettings = Rc; + +#[cfg(not(feature = "mock_data"))] +mod implementation { + use super::*; + use crate::app_interface::{get_hash, StringKey}; + use crate::handling::cached_value::CachedValue; + use crate::handling::extern_ref::{ExternRef, ExternalReferenceType}; + use crate::handling::Cacheable; + use crate::{cached_value, cached_value_getters}; + use spin::RwLock; + + struct StaticDataInner { + reference: ExternRef, + move_library: CachedValue, + item_library: CachedValue, + species_library: CachedValue, + type_library: CachedValue, + settings: CachedValue, } + #[derive(Clone)] + pub struct StaticDataImpl { + inner: Rc, + } + + impl StaticDataImpl { + #[cfg(not(feature = "mock_data"))] + pub(crate) fn new(reference: ExternRef) -> Self { + Self::from_ref(reference, &|reference| Self { + inner: Rc::new(StaticDataInner { + reference, + move_library: cached_value!({ + static_data_get_move_library(reference).get_value().unwrap() + }), + item_library: cached_value!({ + static_data_get_item_library(reference).get_value().unwrap() + }), + species_library: cached_value!({ + static_data_get_species_library(reference) + .get_value() + .unwrap() + }), + type_library: cached_value!({ + static_data_get_type_library(reference).get_value().unwrap() + }), + settings: cached_value!({ + Rc::new( + static_data_get_library_settings(reference) + .get_value() + .unwrap(), + ) + }), + }), + }) + } + } + + impl StaticDataTrait for StaticDataImpl { + cached_value_getters! { + fn move_library(&self) -> MoveLibrary; + fn item_library(&self) -> ItemLibrary; + fn species_library(&self) -> SpeciesLibrary; + fn type_library(&self) -> TypeLibrary; + } + } + + crate::handling::cacheable::cacheable!(StaticDataImpl); + + #[cfg(not(feature = "mock_data"))] + impl ExternalReferenceType for StaticDataImpl { + fn from_extern_value(reference: ExternRef) -> Self { + StaticDataImpl::new(reference) + } + } + + #[derive(Clone)] + pub struct LibrarySettingsImpl { + inner: Rc, + } + + struct LibrarySettingsInner { + maximum_level: CachedValue, + } + + impl LibrarySettingsImpl { + #[cfg(not(feature = "mock_data"))] + pub(crate) fn new(ptr: ExternRef) -> Self { + Self { + inner: Rc::new(LibrarySettingsInner { + maximum_level: cached_value!({ library_settings_get_maximum_level(ptr) }), + }), + } + } + } + + impl LibrarySettingsTrait for LibrarySettingsImpl { + cached_value_getters! { + fn maximum_level(&self) -> LevelInt; + } + } + + #[cfg(not(feature = "mock_data"))] + impl ExternalReferenceType for LibrarySettingsImpl { + fn from_extern_value(reference: ExternRef) -> Self { + LibrarySettingsImpl::new(reference) + } + } + + #[cfg(not(feature = "mock_data"))] + extern "wasm" { + fn static_data_get_move_library(ptr: ExternRef) -> ExternRef; + fn static_data_get_item_library(ptr: ExternRef) -> ExternRef; + fn static_data_get_species_library( + ptr: ExternRef, + ) -> ExternRef; + fn static_data_get_type_library(ptr: ExternRef) -> ExternRef; + fn static_data_get_library_settings( + ptr: ExternRef, + ) -> ExternRef; + + fn library_settings_get_maximum_level(ptr: ExternRef) -> LevelInt; + } + + pub trait DataLibrary: Cacheable + where + T: ExternalReferenceType, + T: Clone, + { + fn get_cache(&self) -> &RwLock>; + fn get_self_ref(&self) -> ExternRef + where + Self: Sized; + fn _get_ref_by_name(ptr: ExternRef, name: ExternRef) -> ExternRef + where + Self: Sized; + fn _get_ref_by_hash(ptr: ExternRef, hash: u32) -> ExternRef + where + Self: Sized; + + fn get(&self, name: &StringKey) -> Option + where + Self: Sized, + { + if let Some(v) = self.get_cache().read().get(&name.hash()) { + return Some(v.clone()); + } + + let v = Self::_get_ref_by_name(self.get_self_ref(), name.ptr()).get_value(); + if let Some(v) = &v { + self.get_cache().write().insert(name.hash(), v.clone()); + } + v + } + + fn get_by_str(&self, name: &str) -> Option + where + Self: Sized, + { + self.get_by_hash(get_hash(name)) + } + + fn get_by_hash(&self, hash: u32) -> Option + where + Self: Sized, + { + if let Some(v) = self.get_cache().read().get(&hash) { + return Some(v.clone()); + } + + let v = Self::_get_ref_by_hash(self.get_self_ref(), hash).get_value(); + if let Some(v) = &v { + self.get_cache().write().insert(hash, v.clone()); + } + v + } + } +} + +#[cfg(not(feature = "mock_data"))] +pub use implementation::*; + +#[cfg(feature = "mock_data")] +mod mocked { + use super::*; + use crate::app_interface::StringKey; + use crate::handling::cached_value::CachedValue; + use crate::handling::extern_ref::{ExternRef, ExternalReferenceType}; + use crate::handling::Cacheable; + use crate::{cached_value, cached_value_getters}; + use spin::RwLock; + #[cfg(feature = "mock_data")] - pub fn mock( - moves: MoveLibrary, - items: ItemLibrary, - species: SpeciesLibrary, - types: TypeLibrary, - settings: LibrarySettings, - ) -> Self { - Self { - inner: Rc::new(StaticDataInner { - reference: ExternRef::mock(), - move_library: moves.into(), - item_library: items.into(), - species_library: species.into(), - type_library: types.into(), - settings: settings.into(), - }), - } - } - - cached_value_getters! { - pub fn move_library(&self) -> MoveLibrary; - pub fn item_library(&self) -> ItemLibrary; - pub fn species_library(&self) -> SpeciesLibrary; - pub fn type_library(&self) -> TypeLibrary; - } -} - -crate::handling::cacheable::cacheable!(StaticData); - -#[cfg(not(feature = "mock_data"))] -impl ExternalReferenceType for StaticData { - fn from_extern_value(reference: ExternRef) -> Self { - StaticData::new(reference) - } -} - -struct LibrarySettingsInner { - maximum_level: CachedValue, -} - -#[derive(Clone)] -pub struct LibrarySettings { - inner: Rc, -} - -impl LibrarySettings { - #[cfg(not(feature = "mock_data"))] - pub(crate) fn new(ptr: ExternRef) -> Self { - Self { - inner: Rc::new(LibrarySettingsInner { - maximum_level: cached_value!({ library_settings_get_maximum_level(ptr) }), - }), - } - } - - #[cfg(feature = "mock_data")] - pub fn mock(maximum_level: LevelInt) -> Self { - Self { - inner: Rc::new(LibrarySettingsInner { - maximum_level: maximum_level.into(), - }), - } - } - - cached_value_getters! { - pub fn maximum_level(&self) -> LevelInt; - } -} - -#[cfg(not(feature = "mock_data"))] -impl ExternalReferenceType for LibrarySettings { - fn from_extern_value(reference: ExternRef) -> Self { - LibrarySettings::new(reference) - } -} - -#[cfg(not(feature = "mock_data"))] -extern "wasm" { - fn static_data_get_move_library(ptr: ExternRef) -> ExternRef; - fn static_data_get_item_library(ptr: ExternRef) -> ExternRef; - fn static_data_get_species_library(ptr: ExternRef) -> ExternRef; - fn static_data_get_type_library(ptr: ExternRef) -> ExternRef; - fn static_data_get_library_settings(ptr: ExternRef) -> ExternRef; - - fn library_settings_get_maximum_level(ptr: ExternRef) -> LevelInt; -} - -#[cfg(not(feature = "mock_data"))] -pub trait DataLibrary: Cacheable -where - T: ExternalReferenceType, - T: Clone, -{ - fn get_cache(&self) -> &RwLock>; - fn get_self_ref(&self) -> ExternRef + pub trait DataLibrary: Cacheable where - Self: Sized; - fn _get_ref_by_name(ptr: ExternRef, name: ExternRef) -> ExternRef - where - Self: Sized; - fn _get_ref_by_hash(ptr: ExternRef, hash: u32) -> ExternRef - where - Self: Sized; - - fn get(&self, name: &StringKey) -> Option - where - Self: Sized, + T: Clone, { - if let Some(v) = self.get_cache().read().get(&name.hash()) { - return Some(v.clone()); + fn get_cache(&self) -> &RwLock>; + fn get_self_ref(&self) -> ExternRef + where + Self: Sized; + + fn get(&self, name: &StringKey) -> Option + where + Self: Sized, + { + self.get_cache().read().get(&name.hash()).cloned() } - let v = Self::_get_ref_by_name(self.get_self_ref(), name.ptr()).get_value(); - if let Some(v) = &v { - self.get_cache().write().insert(name.hash(), v.clone()); - } - v - } - - fn get_by_str(&self, name: &str) -> Option - where - Self: Sized, - { - self.get_by_hash(get_hash(name)) - } - - fn get_by_hash(&self, hash: u32) -> Option - where - Self: Sized, - { - if let Some(v) = self.get_cache().read().get(&hash) { - return Some(v.clone()); + fn get_by_hash(&self, hash: u32) -> Option + where + Self: Sized, + { + self.get_cache().read().get(&hash).cloned() } - let v = Self::_get_ref_by_hash(self.get_self_ref(), hash).get_value(); - if let Some(v) = &v { - self.get_cache().write().insert(hash, v.clone()); + fn insert(&self, hash: u32, item: T) { + self.get_cache().write().insert(hash, item); } - v } } #[cfg(feature = "mock_data")] -pub trait DataLibrary: Cacheable -where - T: Clone, -{ - fn get_cache(&self) -> &RwLock>; - fn get_self_ref(&self) -> ExternRef - where - Self: Sized; - - fn get(&self, name: &StringKey) -> Option - where - Self: Sized, - { - self.get_cache().read().get(&name.hash()).cloned() - } - - fn get_by_hash(&self, hash: u32) -> Option - where - Self: Sized, - { - self.get_cache().read().get(&hash).cloned() - } - - fn insert(&self, hash: u32, item: T) { - self.get_cache().write().insert(hash, item); - } -} +pub use mocked::*; diff --git a/pkmn_lib_interface/src/handling/script.rs b/pkmn_lib_interface/src/handling/script.rs index baebd09..0845091 100755 --- a/pkmn_lib_interface/src/handling/script.rs +++ b/pkmn_lib_interface/src/handling/script.rs @@ -5,7 +5,6 @@ use crate::app_interface::{ }; use crate::handling::ScriptCapabilities; use crate::{ExternRef, ExternalReferenceType, StringKey}; -use alloc::boxed::Box; use alloc::rc::Rc; use core::any::Any; use core::fmt::Debug; @@ -26,7 +25,7 @@ pub trait Script { /// This function is ran when this script starts being in effect. fn on_initialize( &self, - _library: &DynamicLibrary, + _library: DynamicLibrary, _parameters: Option>>, ) { } diff --git a/pkmn_lib_interface/src/handling/temporary.rs b/pkmn_lib_interface/src/handling/temporary.rs index f9662fc..cc36f6f 100755 --- a/pkmn_lib_interface/src/handling/temporary.rs +++ b/pkmn_lib_interface/src/handling/temporary.rs @@ -41,14 +41,6 @@ impl Temporary { } self.value.clone() } - - #[inline] - pub fn value_ref(&self) -> &T { - if self.is_deleted.load(Ordering::SeqCst) { - panic!("Accessed value after it had been deleted"); - } - self.value.as_ref() - } } #[cfg(not(feature = "mock_data"))] diff --git a/pkmn_lib_interface/src/lib.rs b/pkmn_lib_interface/src/lib.rs index 19191ef..afb7316 100755 --- a/pkmn_lib_interface/src/lib.rs +++ b/pkmn_lib_interface/src/lib.rs @@ -24,7 +24,6 @@ static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc {}; pub(crate) use crate::app_interface::StringKey; pub(crate) use crate::handling::extern_ref::*; -pub(crate) use crate::handling::Script; #[macro_use] #[allow(dead_code)] @@ -36,7 +35,7 @@ pub mod utils; mod implementation { use crate::app_interface::list::{EffectParameterImmutableList, ImmutableListWasm}; use crate::app_interface::{ - BattleImpl, DamageSource, DynamicLibrary, EffectParameter, ExecutingMoveImpl, ItemImpl, + BattleImpl, DamageSource, DynamicLibraryImpl, EffectParameter, ExecutingMoveImpl, ItemImpl, PokemonImpl, Statistic, StringKey, TurnChoice, TypeIdentifier, }; use crate::handling::extern_ref::ExternRef; @@ -187,11 +186,11 @@ mod implementation { fn script_on_initialize( script: ScriptPtr, - library: ExternRef, + library: ExternRef, parameters: VecExternRef, ) { let parameters = Rc::new(EffectParameterImmutableList::from_ref(parameters)); - script.val().unwrap().on_initialize(&library.not_null(), Some(parameters)); + script.val().unwrap().on_initialize(library.not_null_rc(), Some(parameters)); } fn script_on_before_turn( diff --git a/pkmn_lib_interface/src/utils.rs b/pkmn_lib_interface/src/utils.rs index eddafb5..18e2fa8 100755 --- a/pkmn_lib_interface/src/utils.rs +++ b/pkmn_lib_interface/src/utils.rs @@ -1,40 +1,9 @@ -use alloc::alloc::alloc; -use alloc::string::String; -use core::alloc::Layout; -#[cfg(not(feature = "mock_data"))] -use core::panic::PanicInfo; -#[cfg(feature = "mock_data")] -use cstr_core::CStr; -use cstr_core::{c_char, CString}; - -#[cfg(not(feature = "mock_data"))] -#[cfg(not(feature = "mock_data"))] -extern "wasm" { - fn _print(s: *const u8); - fn _error(message: *const u8, file: *const u8, line: u32, position: u32); -} - -#[cfg(not(feature = "mock_data"))] -pub fn print_raw(s: &[u8]) { - unsafe { - let cstring = CString::from_vec_unchecked(s.to_vec()); - _print(cstring.as_ptr()); - } -} - -#[cfg(feature = "mock_data")] -pub fn print_raw(s: &[u8]) { - unsafe { - println!("{}", String::from_utf8_lossy(s)); - } -} - #[macro_export] -macro_rules! println { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } } +macro_rules! println { ($($args:tt)*) => { crate::utils::print_raw(alloc::format!($($args)*).as_bytes()); } } #[macro_export] #[cfg(debug_assertions)] -macro_rules! dbg { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } } +macro_rules! dbg { ($($args:tt)*) => { crate::utils::print_raw(alloc::format!($($args)*).as_bytes()); } } #[macro_export] #[cfg(not(debug_assertions))] @@ -42,43 +11,81 @@ macro_rules! dbg { ($($args:tt)*) => {{}}; } -#[panic_handler] -#[no_mangle] #[cfg(not(feature = "mock_data"))] -#[cfg(not(test))] -pub fn begin_panic_handler(panic_info: &PanicInfo<'_>) -> ! { - let msg = CString::new(panic_info.message().unwrap().as_str().unwrap()).unwrap(); - let mut line = 0; - let mut position = 0; - let mut file = CString::default(); - if let Some(s) = panic_info.location() { - line = s.line(); - position = s.column(); - file = CString::new(s.file()).unwrap(); +mod implementation { + use alloc::alloc::alloc; + use core::alloc::Layout; + use core::panic::PanicInfo; + use cstr_core::{c_char, CString}; + + extern "wasm" { + fn _print(s: *const u8); + fn _error(message: *const u8, file: *const u8, line: u32, position: u32); } - unsafe { - _error(msg.as_ptr(), file.as_ptr(), line, position); + #[cfg(not(feature = "mock_data"))] + pub fn print_raw(s: &[u8]) { + unsafe { + let cstring = CString::from_vec_unchecked(s.to_vec()); + _print(cstring.as_ptr()); + } + } + + #[panic_handler] + #[no_mangle] + #[cfg(not(feature = "mock_data"))] + #[cfg(not(test))] + pub fn begin_panic_handler(panic_info: &PanicInfo<'_>) -> ! { + let msg = CString::new(panic_info.message().unwrap().as_str().unwrap()).unwrap(); + let mut line = 0; + let mut position = 0; + let mut file = CString::default(); + if let Some(s) = panic_info.location() { + line = s.line(); + position = s.column(); + file = CString::new(s.file()).unwrap(); + } + + unsafe { + _error(msg.as_ptr(), file.as_ptr(), line, position); + } + loop {} + } + + #[alloc_error_handler] + #[no_mangle] + #[cfg(not(feature = "mock_data"))] + #[cfg(not(test))] + fn allocation_error_handler(layout: core::alloc::Layout) -> ! { + panic!("memory allocation of {} bytes failed", layout.size()) + } + + #[no_mangle] + #[cfg(not(feature = "mock_data"))] + unsafe extern "wasm" fn allocate_mem(len: u32, align: u32) -> *mut u8 { + alloc(Layout::from_size_align(len as usize, align as usize).unwrap()) + } + + #[no_mangle] + #[cfg(not(feature = "mock_data"))] + unsafe extern "wasm" fn dealloc_cstring(ptr: *mut c_char) { + CString::from_raw(ptr); } - loop {} } -#[alloc_error_handler] -#[no_mangle] #[cfg(not(feature = "mock_data"))] -#[cfg(not(test))] -fn allocation_error_handler(layout: core::alloc::Layout) -> ! { - panic!("memory allocation of {} bytes failed", layout.size()) +pub use implementation::*; + +#[cfg(feature = "mock_data")] +mod mocked { + use super::*; + + pub fn print_raw(s: &[u8]) { + unsafe { + println!("{}", String::from_utf8_lossy(s)); + } + } } -#[no_mangle] -#[cfg(not(feature = "mock_data"))] -unsafe extern "wasm" fn allocate_mem(len: u32, align: u32) -> *mut u8 { - alloc(Layout::from_size_align(len as usize, align as usize).unwrap()) -} - -#[no_mangle] -#[cfg(not(feature = "mock_data"))] -unsafe extern "wasm" fn dealloc_cstring(ptr: *mut c_char) { - CString::from_raw(ptr); -} +#[cfg(feature = "mock_data")] +pub use mocked::*;