Major rework of extern ref system for WASM, fixes most possible panics in WASM handling
All checks were successful
continuous-integration/drone Build is passing

This commit is contained in:
2023-06-22 15:43:41 +02:00
parent 6a2353df4c
commit 46195d3042
71 changed files with 2142 additions and 1488 deletions

View File

@@ -67,7 +67,7 @@ pub mod tests {
)
}
pub fn build() -> Box<dyn ItemLibrary> {
pub fn build() -> Arc<dyn ItemLibrary> {
let mut lib = ItemLibraryImpl::new(1);
let m = build_item();
// Borrow as mut so we can insert
@@ -75,6 +75,6 @@ pub mod tests {
w.add(&"foo".into(), Arc::new(m));
// Drops borrow as mut
Box::new(lib)
Arc::new(lib)
}
}

View File

@@ -71,7 +71,7 @@ pub mod tests {
)
}
pub fn build() -> Box<dyn MoveLibrary> {
pub fn build() -> Arc<dyn MoveLibrary> {
let mut lib = MoveLibraryImpl::new(1);
let m = build_move();
// Borrow as mut so we can insert
@@ -79,6 +79,6 @@ pub mod tests {
w.add(&StringKey::new("foo"), Arc::new(m));
// Drops borrow as mut
Box::new(lib)
Arc::new(lib)
}
}

View File

@@ -95,7 +95,7 @@ pub mod tests {
))
}
pub fn build() -> Box<dyn SpeciesLibrary> {
pub fn build() -> Arc<dyn SpeciesLibrary> {
let mut lib = SpeciesLibraryImpl::new(1);
let species = build_species();
// Borrow as mut so we can insert
@@ -103,7 +103,7 @@ pub mod tests {
w.add(&"foo".into(), species);
// Drops borrow as mut
Box::new(lib)
Arc::new(lib)
}
#[test]
@@ -124,6 +124,8 @@ pub mod tests {
fn add_species_to_library_then_remove() {
let mut lib = build();
let lib = Arc::get_mut(&mut lib).unwrap();
lib.remove(&"foo".into());
// Borrow as read so we can read

View File

@@ -8,40 +8,27 @@ use crate::static_data::SpeciesLibrary;
use crate::static_data::TypeLibrary;
use crate::{ValueIdentifiable, ValueIdentifier};
use std::fmt::Debug;
use std::sync::Arc;
/// The storage for all different libraries.
pub trait StaticData: Debug + ValueIdentifiable {
/// Several misc settings for the library.
fn settings(&self) -> &Box<dyn LibrarySettings>;
fn settings(&self) -> &Arc<dyn LibrarySettings>;
/// All data for Pokemon species.
fn species(&self) -> &Box<dyn SpeciesLibrary>;
/// All data for Pokemon species.
fn species_mut(&mut self) -> &mut Box<dyn SpeciesLibrary>;
fn species(&self) -> &Arc<dyn SpeciesLibrary>;
/// All data for the moves.
fn moves(&self) -> &Box<dyn MoveLibrary>;
/// All data for the moves.
fn moves_mut(&mut self) -> &mut Box<dyn MoveLibrary>;
fn moves(&self) -> &Arc<dyn MoveLibrary>;
/// All data for the items.
fn items(&self) -> &Box<dyn ItemLibrary>;
/// All data for the items.
fn items_mut(&mut self) -> &mut Box<dyn ItemLibrary>;
fn items(&self) -> &Arc<dyn ItemLibrary>;
/// All data for growth rates.
fn growth_rates(&self) -> &Box<dyn GrowthRateLibrary>;
/// All data for growth rates.
fn growth_rates_mut(&mut self) -> &mut Box<dyn GrowthRateLibrary>;
fn growth_rates(&self) -> &Arc<dyn GrowthRateLibrary>;
/// All data related to types and type effectiveness.
fn types(&self) -> &Box<dyn TypeLibrary>;
/// All data related to types and type effectiveness.
fn types_mut(&mut self) -> &mut Box<dyn TypeLibrary>;
fn types(&self) -> &Arc<dyn TypeLibrary>;
/// All data related to natures.
fn natures(&self) -> &Box<dyn NatureLibrary>;
/// All data related to natures.
fn natures_mut(&mut self) -> &mut Box<dyn NatureLibrary>;
fn natures(&self) -> &Arc<dyn NatureLibrary>;
/// All data related to abilities.
fn abilities(&self) -> &Box<dyn AbilityLibrary>;
/// All data related to abilities.
fn abilities_mut(&mut self) -> &mut Box<dyn AbilityLibrary>;
fn abilities(&self) -> &Arc<dyn AbilityLibrary>;
}
/// The storage for all different libraries.
@@ -50,34 +37,34 @@ pub struct StaticDataImpl {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// Several misc settings for the library.
settings: Box<dyn LibrarySettings>,
settings: Arc<dyn LibrarySettings>,
/// All data for Pokemon species.
species: Box<dyn SpeciesLibrary>,
species: Arc<dyn SpeciesLibrary>,
/// All data for the moves.
moves: Box<dyn MoveLibrary>,
moves: Arc<dyn MoveLibrary>,
/// All data for the items.
items: Box<dyn ItemLibrary>,
items: Arc<dyn ItemLibrary>,
/// All data for growth rates.
growth_rates: Box<dyn GrowthRateLibrary>,
growth_rates: Arc<dyn GrowthRateLibrary>,
/// All data related to types and type effectiveness.
types: Box<dyn TypeLibrary>,
types: Arc<dyn TypeLibrary>,
/// All data related to natures.
natures: Box<dyn NatureLibrary>,
natures: Arc<dyn NatureLibrary>,
/// All data related to abilities.
abilities: Box<dyn AbilityLibrary>,
abilities: Arc<dyn AbilityLibrary>,
}
impl StaticDataImpl {
/// Instantiates a new data collection.
pub fn new(
settings: Box<dyn LibrarySettings>,
species: Box<dyn SpeciesLibrary>,
moves: Box<dyn MoveLibrary>,
items: Box<dyn ItemLibrary>,
growth_rates: Box<dyn GrowthRateLibrary>,
types: Box<dyn TypeLibrary>,
natures: Box<dyn NatureLibrary>,
abilities: Box<dyn AbilityLibrary>,
settings: Arc<dyn LibrarySettings>,
species: Arc<dyn SpeciesLibrary>,
moves: Arc<dyn MoveLibrary>,
items: Arc<dyn ItemLibrary>,
growth_rates: Arc<dyn GrowthRateLibrary>,
types: Arc<dyn TypeLibrary>,
natures: Arc<dyn NatureLibrary>,
abilities: Arc<dyn AbilityLibrary>,
) -> Self {
Self {
identifier: Default::default(),
@@ -95,66 +82,38 @@ impl StaticDataImpl {
impl StaticData for StaticDataImpl {
/// Several misc settings for the library.
fn settings(&self) -> &Box<dyn LibrarySettings> {
fn settings(&self) -> &Arc<dyn LibrarySettings> {
&self.settings
}
/// All data for Pokemon species.
fn species(&self) -> &Box<dyn SpeciesLibrary> {
fn species(&self) -> &Arc<dyn SpeciesLibrary> {
&self.species
}
/// All data for Pokemon species.
fn species_mut(&mut self) -> &mut Box<dyn SpeciesLibrary> {
&mut self.species
}
/// All data for the moves.
fn moves(&self) -> &Box<dyn MoveLibrary> {
fn moves(&self) -> &Arc<dyn MoveLibrary> {
&self.moves
}
/// All data for the moves.
fn moves_mut(&mut self) -> &mut Box<dyn MoveLibrary> {
&mut self.moves
}
/// All data for the items.
fn items(&self) -> &Box<dyn ItemLibrary> {
fn items(&self) -> &Arc<dyn ItemLibrary> {
&self.items
}
/// All data for the items.
fn items_mut(&mut self) -> &mut Box<dyn ItemLibrary> {
&mut self.items
}
/// All data for growth rates.
fn growth_rates(&self) -> &Box<dyn GrowthRateLibrary> {
fn growth_rates(&self) -> &Arc<dyn GrowthRateLibrary> {
&self.growth_rates
}
/// All data for growth rates.
fn growth_rates_mut(&mut self) -> &mut Box<dyn GrowthRateLibrary> {
&mut self.growth_rates
}
/// All data related to types and type effectiveness.
fn types(&self) -> &Box<dyn TypeLibrary> {
fn types(&self) -> &Arc<dyn TypeLibrary> {
&self.types
}
/// All data related to types and type effectiveness.
fn types_mut(&mut self) -> &mut Box<dyn TypeLibrary> {
&mut self.types
}
/// All data related to natures.
fn natures(&self) -> &Box<dyn NatureLibrary> {
fn natures(&self) -> &Arc<dyn NatureLibrary> {
&self.natures
}
/// All data related to natures.
fn natures_mut(&mut self) -> &mut Box<dyn NatureLibrary> {
&mut self.natures
}
/// All data related to abilities.
fn abilities(&self) -> &Box<dyn AbilityLibrary> {
fn abilities(&self) -> &Arc<dyn AbilityLibrary> {
&self.abilities
}
/// All data related to abilities.
fn abilities_mut(&mut self) -> &mut Box<dyn AbilityLibrary> {
&mut self.abilities
}
}
impl ValueIdentifiable for StaticDataImpl {
@@ -174,22 +133,15 @@ pub mod test {
#[derive(Debug)]
pub StaticData{}
impl StaticData for StaticData {
fn settings(&self) -> &Box<dyn LibrarySettings>;
fn species(&self) -> &Box<dyn SpeciesLibrary>;
fn species_mut(&mut self) -> &mut Box<dyn SpeciesLibrary>;
fn moves(&self) -> &Box<dyn MoveLibrary>;
fn moves_mut(&mut self) -> &mut Box<dyn MoveLibrary>;
fn items(&self) -> &Box<dyn ItemLibrary>;
fn items_mut(&mut self) -> &mut Box<dyn ItemLibrary>;
fn settings(&self) -> &Arc<dyn LibrarySettings>;
fn species(&self) -> &Arc<dyn SpeciesLibrary>;
fn moves(&self) -> &Arc<dyn MoveLibrary>;
fn items(&self) -> &Arc<dyn ItemLibrary>;
fn growth_rates(&self) -> & Box<dyn GrowthRateLibrary>;
fn growth_rates_mut(&mut self) -> &mut Box<dyn GrowthRateLibrary>;
fn types(&self) -> &Box<dyn TypeLibrary>;
fn types_mut(&mut self) -> &mut Box<dyn TypeLibrary>;
fn natures(&self) -> &Box<dyn NatureLibrary>;
fn natures_mut(&mut self) -> &mut Box<dyn NatureLibrary>;
fn abilities(&self) -> &Box<dyn AbilityLibrary>;
fn abilities_mut(&mut self) -> &mut Box<dyn AbilityLibrary>;
fn growth_rates(&self) -> & Arc<dyn GrowthRateLibrary>;
fn types(&self) -> &Arc<dyn TypeLibrary>;
fn natures(&self) -> &Arc<dyn NatureLibrary>;
fn abilities(&self) -> &Arc<dyn AbilityLibrary>;
}
impl ValueIdentifiable for StaticData {
fn value_identifier(&self) -> ValueIdentifier{
@@ -201,14 +153,14 @@ pub mod test {
pub fn build() -> StaticDataImpl {
StaticDataImpl {
identifier: Default::default(),
settings: Box::new(LibrarySettingsImpl::new(100, 100).unwrap()),
settings: Arc::new(LibrarySettingsImpl::new(100, 100).unwrap()),
species: crate::static_data::libraries::species_library::tests::build(),
moves: crate::static_data::libraries::move_library::tests::build(),
items: crate::static_data::libraries::item_library::tests::build(),
growth_rates: Box::new(crate::static_data::libraries::growth_rate_library::tests::build()),
types: Box::new(crate::static_data::libraries::type_library::tests::build()),
natures: Box::new(crate::static_data::libraries::nature_library::tests::build()),
abilities: Box::new(crate::static_data::libraries::ability_library::tests::build()),
growth_rates: Arc::new(crate::static_data::libraries::growth_rate_library::tests::build()),
types: Arc::new(crate::static_data::libraries::type_library::tests::build()),
natures: Arc::new(crate::static_data::libraries::nature_library::tests::build()),
abilities: Arc::new(crate::static_data::libraries::ability_library::tests::build()),
}
}
}

View File

@@ -1,6 +1,7 @@
use crate::static_data::EffectParameter;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
use std::fmt::Debug;
use std::sync::Arc;
/// A secondary effect is an effect on a move that happens after it hits.
pub trait SecondaryEffect: Debug + ValueIdentifiable {
@@ -9,7 +10,7 @@ pub trait SecondaryEffect: Debug + ValueIdentifiable {
/// The name of the effect.
fn effect_name(&self) -> &StringKey;
/// A list of parameters for the effect.
fn parameters(&self) -> &Vec<EffectParameter>;
fn parameters(&self) -> &Vec<Arc<EffectParameter>>;
}
/// A secondary effect is an effect on a move that happens after it hits.
@@ -22,12 +23,12 @@ pub struct SecondaryEffectImpl {
/// The name of the effect.
effect_name: StringKey,
/// A list of parameters for the effect.
parameters: Vec<EffectParameter>,
parameters: Vec<Arc<EffectParameter>>,
}
impl SecondaryEffectImpl {
/// Instantiates a new Secondary Effect.
pub fn new(chance: f32, effect_name: StringKey, parameters: Vec<EffectParameter>) -> Self {
pub fn new(chance: f32, effect_name: StringKey, parameters: Vec<Arc<EffectParameter>>) -> Self {
Self {
identifier: Default::default(),
chance,
@@ -47,7 +48,7 @@ impl SecondaryEffect for SecondaryEffectImpl {
&self.effect_name
}
/// A list of parameters for the effect.
fn parameters(&self) -> &Vec<EffectParameter> {
fn parameters(&self) -> &Vec<Arc<EffectParameter>> {
&self.parameters
}
}
@@ -74,7 +75,7 @@ pub(crate) mod tests {
impl SecondaryEffect for SecondaryEffect {
fn chance(&self) -> f32;
fn effect_name(&self) -> &StringKey;
fn parameters(&self) -> &Vec<EffectParameter>;
fn parameters(&self) -> &Vec<Arc<EffectParameter>>;
}
impl ValueIdentifiable for SecondaryEffect{
fn value_identifier(&self) -> ValueIdentifier{

View File

@@ -1,6 +1,7 @@
use crate::static_data::EffectParameter;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
use std::fmt::Debug;
use std::sync::Arc;
/// An ability is a passive effect in battle that is attached to a Pokemon.
pub trait Ability: Debug + ValueIdentifiable {
@@ -9,7 +10,7 @@ pub trait Ability: Debug + ValueIdentifiable {
/// The name of the script effect of the ability.
fn effect(&self) -> &StringKey;
/// The parameters for the script effect of the ability.
fn parameters(&self) -> &Vec<EffectParameter>;
fn parameters(&self) -> &Vec<Arc<EffectParameter>>;
}
/// An ability is a passive effect in battle that is attached to a Pokemon.
@@ -22,12 +23,12 @@ pub struct AbilityImpl {
/// The name of the script effect of the ability.
effect: StringKey,
/// The parameters for the script effect of the ability.
parameters: Vec<EffectParameter>,
parameters: Vec<Arc<EffectParameter>>,
}
impl AbilityImpl {
/// Instantiates a new ability.
pub fn new(name: &StringKey, effect: &StringKey, parameters: Vec<EffectParameter>) -> Self {
pub fn new(name: &StringKey, effect: &StringKey, parameters: Vec<Arc<EffectParameter>>) -> Self {
Self {
identifier: Default::default(),
name: name.clone(),
@@ -47,7 +48,7 @@ impl Ability for AbilityImpl {
&self.effect
}
/// The parameters for the script effect of the ability.
fn parameters(&self) -> &Vec<EffectParameter> {
fn parameters(&self) -> &Vec<Arc<EffectParameter>> {
&self.parameters
}
}
@@ -81,7 +82,7 @@ pub(crate) mod tests {
impl Ability for Ability {
fn name(&self) -> &StringKey;
fn effect(&self) -> &StringKey;
fn parameters(&self) -> &Vec<EffectParameter>;
fn parameters(&self) -> &Vec<Arc<EffectParameter>>;
}
impl ValueIdentifiable for Ability {
fn value_identifier(&self) -> ValueIdentifier {

View File

@@ -1,6 +1,7 @@
use anyhow_ext::{ensure, Result};
use hashbrown::HashSet;
use std::fmt::Debug;
use std::sync::Arc;
use crate::static_data::Statistic;
use crate::static_data::TypeIdentifier;
@@ -23,7 +24,7 @@ pub trait Form: ValueIdentifiable + Debug {
/// The normal types a Pokemon with this form has.
fn types(&self) -> &Vec<TypeIdentifier>;
/// The inherent values of a form of species that are used for the stats of a Pokemon.
fn base_stats(&self) -> &StaticStatisticSet<u16>;
fn base_stats(&self) -> &Arc<StaticStatisticSet<u16>>;
/// The possible abilities a Pokemon with this form can have.
fn abilities(&self) -> &Vec<StringKey>;
/// The possible hidden abilities a Pokemon with this form can have.
@@ -74,7 +75,7 @@ pub struct FormImpl {
/// The normal types a Pokemon with this form has.
types: Vec<TypeIdentifier>,
/// The inherent values of a form of species that are used for the stats of a Pokemon.
base_stats: StaticStatisticSet<u16>,
base_stats: Arc<StaticStatisticSet<u16>>,
/// The possible abilities a Pokemon with this form can have.
abilities: Vec<StringKey>,
/// The possible hidden abilities a Pokemon with this form can have.
@@ -106,7 +107,7 @@ impl FormImpl {
weight,
base_experience,
types,
base_stats,
base_stats: Arc::new(base_stats),
abilities,
hidden_abilities,
moves,
@@ -137,7 +138,7 @@ impl Form for FormImpl {
&self.types
}
/// The inherent values of a form of species that are used for the stats of a Pokemon.
fn base_stats(&self) -> &StaticStatisticSet<u16> {
fn base_stats(&self) -> &Arc<StaticStatisticSet<u16>> {
&self.base_stats
}
/// The possible abilities a Pokemon with this form can have.
@@ -242,7 +243,7 @@ pub(crate) mod tests {
fn weight(&self) -> f32;
fn base_experience(&self) -> u32;
fn types(&self) -> &Vec<TypeIdentifier>;
fn base_stats(&self) -> &StaticStatisticSet<u16>;
fn base_stats(&self) -> &Arc<StaticStatisticSet<u16>>;
fn abilities(&self) -> &Vec<StringKey>;
fn hidden_abilities(&self) -> &Vec<StringKey>;
fn moves(&self) -> &Box<dyn LearnableMoves>;