Rework of FFI, adding a value identifier, so we can keep knowledge of data even when data moves.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-10-08 13:15:04 +02:00
parent 84ddf0307d
commit 41b40ef98e
38 changed files with 582 additions and 230 deletions

View File

@@ -4,11 +4,13 @@ use indexmap::IndexMap;
use crate::static_data::Ability;
use crate::static_data::DataLibrary;
use crate::StringKey;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
/// A storage for all abilities that can be used in this data library.
#[derive(Debug)]
pub struct AbilityLibrary {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The underlying map for the library.
map: IndexMap<StringKey, Arc<Ability>>,
}
@@ -17,6 +19,7 @@ impl AbilityLibrary {
/// Instantiates a new ability library.
pub fn new(capacity: usize) -> AbilityLibrary {
AbilityLibrary {
identifier: Default::default(),
map: IndexMap::with_capacity(capacity),
}
}
@@ -31,18 +34,25 @@ impl DataLibrary<'_, Ability> for AbilityLibrary {
}
}
impl ValueIdentifiable for AbilityLibrary {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
pub mod tests {
use crate::static_data::Ability;
use crate::static_data::AbilityLibrary;
use crate::static_data::DataLibrary;
use crate::StringKey;
use std::sync::Arc;
pub fn build() -> AbilityLibrary {
let mut lib = AbilityLibrary::new(1);
lib.add(
&StringKey::new("test_ability".into()),
Ability::new(&"test_ability".into(), &"test_ability".into(), Vec::new()),
Arc::new(Ability::new(&"test_ability".into(), &"test_ability".into(), Vec::new())),
);
// Drops borrow as mut

View File

@@ -14,8 +14,8 @@ pub trait DataLibrary<'a, T: 'a> {
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<T>>;
/// Adds a new value to the library.
fn add(&mut self, key: &StringKey, value: T) {
self.get_modify().insert(key.clone(), Arc::new(value));
fn add(&mut self, key: &StringKey, value: Arc<T>) {
self.get_modify().insert(key.clone(), value);
}
/// Removes a value from the library.

View File

@@ -5,10 +5,12 @@ use hashbrown::HashMap;
use crate::defines::LevelInt;
use crate::static_data::GrowthRate;
use crate::StringKey;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
/// A library to store all growth rates.
pub struct GrowthRateLibrary {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The underlying data structure.
growth_rates: HashMap<StringKey, Box<dyn GrowthRate>>,
}
@@ -17,6 +19,7 @@ impl GrowthRateLibrary {
/// Instantiates a new growth rate library with a capacity.
pub fn new(capacity: usize) -> GrowthRateLibrary {
GrowthRateLibrary {
identifier: Default::default(),
growth_rates: HashMap::with_capacity(capacity),
}
}
@@ -36,6 +39,12 @@ impl GrowthRateLibrary {
}
}
impl ValueIdentifiable for GrowthRateLibrary {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
impl Debug for GrowthRateLibrary {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("GrowthRateLibrary").finish()

View File

@@ -4,12 +4,14 @@ use indexmap::IndexMap;
use crate::static_data::DataLibrary;
use crate::static_data::Item;
use crate::StringKey;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
/// A library to store all items.
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct ItemLibrary {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The underlying data structure.
map: IndexMap<StringKey, Arc<Item>>,
}
@@ -18,6 +20,7 @@ impl ItemLibrary {
/// Instantiates a new Item Library.
pub fn new(capacity: usize) -> ItemLibrary {
ItemLibrary {
identifier: Default::default(),
map: IndexMap::with_capacity(capacity),
}
}
@@ -33,9 +36,16 @@ impl DataLibrary<'_, Item> for ItemLibrary {
}
}
impl ValueIdentifiable for ItemLibrary {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
pub mod tests {
use hashbrown::HashSet;
use std::sync::Arc;
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::item_library::ItemLibrary;
@@ -57,7 +67,7 @@ pub mod tests {
let m = build_item();
// Borrow as mut so we can insert
let w = &mut lib;
w.add(&"foo".into(), m);
w.add(&"foo".into(), Arc::new(m));
// Drops borrow as mut
lib

View File

@@ -1,9 +1,12 @@
use crate::defines::LevelInt;
use crate::{ValueIdentifiable, ValueIdentifier};
/// This library holds several misc settings for the library.
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct LibrarySettings {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The highest level a Pokemon can be.
maximum_level: LevelInt,
}
@@ -11,7 +14,10 @@ pub struct LibrarySettings {
impl LibrarySettings {
/// Creates a new settings library.
pub fn new(maximum_level: LevelInt) -> Self {
Self { maximum_level }
Self {
identifier: Default::default(),
maximum_level,
}
}
/// The highest level a Pokemon can be.
@@ -19,3 +25,9 @@ impl LibrarySettings {
self.maximum_level
}
}
impl ValueIdentifiable for LibrarySettings {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}

View File

@@ -4,12 +4,14 @@ use indexmap::IndexMap;
use crate::static_data::DataLibrary;
use crate::static_data::MoveData;
use crate::StringKey;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
/// A library to store all data for moves.
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct MoveLibrary {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The underlying map.
map: IndexMap<StringKey, Arc<MoveData>>,
}
@@ -18,6 +20,7 @@ impl MoveLibrary {
/// Instantiates a new Move Library.
pub fn new(capacity: usize) -> MoveLibrary {
MoveLibrary {
identifier: Default::default(),
map: IndexMap::with_capacity(capacity),
}
}
@@ -32,9 +35,16 @@ impl DataLibrary<'_, MoveData> for MoveLibrary {
}
}
impl ValueIdentifiable for MoveLibrary {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
pub mod tests {
use hashbrown::HashSet;
use std::sync::Arc;
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::move_library::MoveLibrary;
@@ -61,7 +71,7 @@ pub mod tests {
let m = build_move();
// Borrow as mut so we can insert
let w = &mut lib;
w.add(&StringKey::new("foo".into()), m);
w.add(&StringKey::new("foo".into()), Arc::new(m));
// Drops borrow as mut
lib

View File

@@ -1,11 +1,13 @@
use crate::static_data::Nature;
use crate::StringKey;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
use hashbrown::HashMap;
use std::sync::Arc;
/// A library of all natures that can be used, stored by their names.
#[derive(Debug)]
pub struct NatureLibrary {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The underlying data structure.
map: HashMap<StringKey, Arc<Nature>>,
}
@@ -14,13 +16,14 @@ impl NatureLibrary {
/// Creates a new nature library with a given capacity.
pub fn new(capacity: usize) -> Self {
NatureLibrary {
identifier: Default::default(),
map: HashMap::with_capacity(capacity),
}
}
/// Adds a new nature with name to the library.
pub fn load_nature(&mut self, name: StringKey, nature: Nature) {
self.map.insert(name, Arc::new(nature));
pub fn load_nature(&mut self, name: StringKey, nature: Arc<Nature>) {
self.map.insert(name, nature);
}
/// Gets a nature by name.
@@ -29,11 +32,11 @@ impl NatureLibrary {
}
/// Finds a nature name by nature.
pub fn get_nature_name(&self, nature: &Arc<Nature>) -> StringKey {
pub fn get_nature_name(&self, nature: &Nature) -> StringKey {
for kv in &self.map {
// As natures can't be copied, and should always be the same reference as the value
// in the map, we just compare by reference.
if Arc::ptr_eq(kv.1, nature) {
if std::ptr::eq(Arc::as_ptr(kv.1), nature) {
return kv.0.clone();
}
}
@@ -41,6 +44,12 @@ impl NatureLibrary {
}
}
impl ValueIdentifiable for NatureLibrary {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
pub mod tests {
use crate::static_data::statistics::Statistic;

View File

@@ -4,12 +4,14 @@ use indexmap::IndexMap;
use crate::static_data::DataLibrary;
use crate::static_data::Species;
use crate::StringKey;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
/// A library to store all data for Pokemon species.
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct SpeciesLibrary {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The underlying map.
map: IndexMap<StringKey, Arc<Species>>,
}
@@ -18,6 +20,7 @@ impl SpeciesLibrary {
/// Instantiates a new Species Library.
pub fn new(capacity: usize) -> SpeciesLibrary {
SpeciesLibrary {
identifier: Default::default(),
map: IndexMap::with_capacity(capacity),
}
}
@@ -32,9 +35,16 @@ impl<'a> DataLibrary<'a, Species> for SpeciesLibrary {
}
}
impl ValueIdentifiable for SpeciesLibrary {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
pub mod tests {
use hashbrown::HashSet;
use std::sync::Arc;
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::species_library::SpeciesLibrary;
@@ -50,7 +60,7 @@ pub mod tests {
0.5,
&"test_growthrate".into(),
0,
Form::new(
Arc::new(Form::new(
&"default".into(),
0.0,
0.0,
@@ -61,7 +71,7 @@ pub mod tests {
Vec::new(),
LearnableMoves::new(),
HashSet::new(),
),
)),
HashSet::new(),
)
}
@@ -71,7 +81,7 @@ pub mod tests {
let species = build_species();
// Borrow as mut so we can insert
let w = &mut lib;
w.add(&"foo".into(), species);
w.add(&"foo".into(), Arc::new(species));
// Drops borrow as mut
lib

View File

@@ -6,11 +6,14 @@ use crate::static_data::MoveLibrary;
use crate::static_data::NatureLibrary;
use crate::static_data::SpeciesLibrary;
use crate::static_data::TypeLibrary;
use crate::{ValueIdentifiable, ValueIdentifier};
/// The storage for all different libraries.
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct StaticData {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// Several misc settings for the library.
settings: LibrarySettings,
/// All data for Pokemon species.
@@ -33,6 +36,7 @@ impl StaticData {
/// Instantiates a new data collection.
pub fn new(settings: LibrarySettings) -> Self {
Self {
identifier: Default::default(),
settings,
species: SpeciesLibrary::new(0),
moves: MoveLibrary::new(0),
@@ -106,6 +110,12 @@ impl StaticData {
}
}
impl ValueIdentifiable for StaticData {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
pub mod test {
use crate::static_data::libraries::library_settings::LibrarySettings;
@@ -116,6 +126,7 @@ pub mod test {
pub fn build() -> StaticData {
StaticData {
identifier: Default::default(),
settings: LibrarySettings::new(100),
species: species_library::tests::build(),
moves: move_library::tests::build(),

View File

@@ -1,7 +1,7 @@
use atomig::Atom;
use hashbrown::HashMap;
use crate::StringKey;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
/// A unique key that can be used to store a reference to a type. Opaque reference to a byte
/// internally.
@@ -28,6 +28,8 @@ impl From<TypeIdentifier> for u8 {
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct TypeLibrary {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// A list of types
types: HashMap<StringKey, TypeIdentifier>,
/// The effectiveness of the different types against each other.
@@ -38,6 +40,7 @@ impl TypeLibrary {
/// Instantiates a new type library with a specific capacity.
pub fn new(capacity: usize) -> TypeLibrary {
TypeLibrary {
identifier: Default::default(),
types: HashMap::with_capacity(capacity),
effectiveness: vec![],
}
@@ -93,6 +96,12 @@ impl TypeLibrary {
}
}
impl ValueIdentifiable for TypeLibrary {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
pub mod tests {
use assert_approx_eq::assert_approx_eq;