From efdaf730e6a744af08c8be6bd63c289686fe723d Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 24 Dec 2022 12:48:00 +0100 Subject: [PATCH] Reworks static library initialiser --- src/ffi/static_data/libraries/static_data.rs | 18 +++- src/static_data/libraries/static_data.rs | 33 +++--- tests/common/library_loader.rs | 102 +++++++++++++------ 3 files changed, 107 insertions(+), 46 deletions(-) diff --git a/src/ffi/static_data/libraries/static_data.rs b/src/ffi/static_data/libraries/static_data.rs index 8bb377d..370bb68 100644 --- a/src/ffi/static_data/libraries/static_data.rs +++ b/src/ffi/static_data/libraries/static_data.rs @@ -9,8 +9,24 @@ use std::ptr::drop_in_place; #[no_mangle] unsafe extern "C" fn static_data_new( settings: OwnedPtr>, + species: OwnedPtr>, + moves: OwnedPtr>, + items: OwnedPtr>, + growth_rates: OwnedPtr>, + types: OwnedPtr>, + natures: OwnedPtr>, + abilities: OwnedPtr>, ) -> IdentifiablePointer> { - let b: Box = Box::new(StaticDataImpl::new(*Box::from_raw(settings))); + let b: Box = Box::new(StaticDataImpl::new( + *Box::from_raw(settings), + *Box::from_raw(species), + *Box::from_raw(moves), + *Box::from_raw(items), + *Box::from_raw(growth_rates), + *Box::from_raw(types), + *Box::from_raw(natures), + *Box::from_raw(abilities), + )); b.into() } diff --git a/src/static_data/libraries/static_data.rs b/src/static_data/libraries/static_data.rs index 1d7886d..79e3808 100755 --- a/src/static_data/libraries/static_data.rs +++ b/src/static_data/libraries/static_data.rs @@ -1,11 +1,11 @@ +use crate::static_data::AbilityLibrary; +use crate::static_data::GrowthRateLibrary; +use crate::static_data::ItemLibrary; +use crate::static_data::LibrarySettings; use crate::static_data::MoveLibrary; use crate::static_data::NatureLibrary; use crate::static_data::SpeciesLibrary; use crate::static_data::TypeLibrary; -use crate::static_data::{AbilityLibrary, AbilityLibraryImpl, ItemLibraryImpl, MoveLibraryImpl, SpeciesLibraryImpl}; -use crate::static_data::{GrowthRateLibrary, GrowthRateLibraryImpl}; -use crate::static_data::{ItemLibrary, NatureLibraryImpl}; -use crate::static_data::{LibrarySettings, TypeLibraryImpl}; use crate::{ValueIdentifiable, ValueIdentifier}; use std::fmt::Debug; @@ -69,17 +69,26 @@ pub struct StaticDataImpl { impl StaticDataImpl { /// Instantiates a new data collection. - pub fn new(settings: Box) -> Self { + pub fn new( + settings: Box, + species: Box, + moves: Box, + items: Box, + growth_rates: Box, + types: Box, + natures: Box, + abilities: Box, + ) -> Self { Self { identifier: Default::default(), settings, - species: Box::new(SpeciesLibraryImpl::new(0)), - moves: Box::new(MoveLibraryImpl::new(0)), - items: Box::new(ItemLibraryImpl::new(0)), - growth_rates: Box::new(GrowthRateLibraryImpl::new(0)), - types: Box::new(TypeLibraryImpl::new(0)), - natures: Box::new(NatureLibraryImpl::new(0)), - abilities: Box::new(AbilityLibraryImpl::new(0)), + species, + moves, + items, + growth_rates, + types, + natures, + abilities, } } } diff --git a/tests/common/library_loader.rs b/tests/common/library_loader.rs index 6c341e1..1e250fb 100755 --- a/tests/common/library_loader.rs +++ b/tests/common/library_loader.rs @@ -16,10 +16,11 @@ use pkmn_lib::dynamic_data::Gen7MiscLibrary; use pkmn_lib::dynamic_data::{DynamicLibrary, DynamicLibraryImpl}; use pkmn_lib::script_implementations::wasm::script_resolver::WebAssemblyScriptResolver; use pkmn_lib::static_data::{ - AbilityImpl, AbilityLibrary, BattleItemCategory, EffectParameter, Form, FormImpl, GrowthRateLibrary, ItemImpl, - ItemLibrary, LearnableMoves, LearnableMovesImpl, LibrarySettingsImpl, LookupGrowthRate, MoveDataImpl, MoveLibrary, - NatureImpl, NatureLibrary, SecondaryEffect, SecondaryEffectImpl, SpeciesImpl, StaticData, StaticDataImpl, - StaticStatisticSet, Statistic, TypeLibrary, TypeLibraryImpl, + AbilityImpl, AbilityLibrary, AbilityLibraryImpl, BattleItemCategory, DataLibrary, EffectParameter, Form, FormImpl, + GrowthRateLibrary, GrowthRateLibraryImpl, ItemImpl, ItemLibrary, ItemLibraryImpl, LearnableMoves, + LearnableMovesImpl, LibrarySettingsImpl, LookupGrowthRate, MoveDataImpl, MoveLibrary, MoveLibraryImpl, NatureImpl, + NatureLibrary, NatureLibraryImpl, SecondaryEffect, SecondaryEffectImpl, SpeciesImpl, SpeciesLibrary, + SpeciesLibraryImpl, StaticData, StaticDataImpl, StaticStatisticSet, Statistic, TypeLibrary, TypeLibraryImpl, }; use pkmn_lib::StringKey; @@ -27,14 +28,25 @@ pub fn load_library() -> Arc { let mut path = get_project_root().unwrap(); path.push("tests/data/"); let path = path.to_str().unwrap().to_string(); - let mut data = StaticDataImpl::new(Box::new(LibrarySettingsImpl::new(100))); - load_types(&path, data.types_mut()); - load_natures(&path, data.natures_mut()); - load_items(&path, data.items_mut()); - load_growth_rates(&path, data.growth_rates_mut()); - load_abilities(&path, data.abilities_mut()); - load_moves(&path, &mut data); - load_species(&path, &mut data); + + let types = load_types(&path); + let natures = load_natures(&path); + let items = load_items(&path); + let growth_rates = load_growth_rates(&path); + let abilities = load_abilities(&path); + let moves = load_moves(&path, &types); + let species = load_species(&path, &types, &moves); + + let mut data = StaticDataImpl::new( + Box::new(LibrarySettingsImpl::new(100)), + species, + moves, + items, + growth_rates, + types, + natures, + abilities, + ); let mut resolver = WebAssemblyScriptResolver::new(); load_wasm(&path, resolver.as_mut()); @@ -47,11 +59,14 @@ pub fn load_library() -> Arc { )) } -pub fn load_types(path: &String, type_library: &mut Box) { +pub fn load_types(path: &String) -> Box { let mut reader = csv::ReaderBuilder::new() .delimiter(b'|') .from_path(path.to_string() + "Types.csv") .unwrap(); + + let mut type_library = Box::new(TypeLibraryImpl::new(20)); + let headers = reader.headers().unwrap(); for header in headers.iter().skip(1) { type_library.register_type(&header.into()); @@ -67,14 +82,16 @@ pub fn load_types(path: &String, type_library: &mut Box) { type_library.set_effectiveness(offensive_type_id, ((i + 1) as u8).into(), effectiveness); } } + type_library } -pub fn load_natures(path: &String, nature_library: &mut Box) { +pub fn load_natures(path: &String) -> Box { let mut reader = csv::ReaderBuilder::new() .delimiter(b'|') .from_path(path.to_string() + "Natures.csv") .unwrap(); + let mut nature_library = Box::new(NatureLibraryImpl::new(24)); for record in reader.records() { let record = record.unwrap(); let nature_name = record.get(0).unwrap().into(); @@ -91,15 +108,17 @@ pub fn load_natures(path: &String, nature_library: &mut Box) ); } } + nature_library } -pub fn load_items(path: &String, lib: &mut Box) { +pub fn load_items(path: &String) -> Box { let mut file = File::open(path.to_string() + "Items.json").unwrap(); let mut data = String::new(); file.read_to_string(&mut data).unwrap(); let json: Value = serde_json::from_str(&data).unwrap(); let json_array = json.as_array().unwrap(); + let mut item_library = Box::new(ItemLibraryImpl::new(400)); for v in json_array { let name = v.get("name").unwrap().as_str().unwrap().into(); let category = serde_json::from_value(v.get("itemType").unwrap().clone()).unwrap(); @@ -116,20 +135,22 @@ pub fn load_items(path: &String, lib: &mut Box) { } } - lib.add( + item_library.add( &name, Arc::new(ItemImpl::new(&name, category, battle_category, price as i32, flags)), ); } + item_library } -pub fn load_growth_rates(path: &String, growth_rate_library: &mut Box) { +pub fn load_growth_rates(path: &String) -> Box { let mut file = File::open(path.to_string() + "GrowthRates.json").unwrap(); let mut data = String::new(); file.read_to_string(&mut data).unwrap(); let json: Value = serde_json::from_str(&data).unwrap(); let o = json.as_object().unwrap(); + let mut growth_rate_library = Box::new(GrowthRateLibraryImpl::new(10)); for (key, value) in o { let name = StringKey::new(key); let experience_required_json = value.as_array().unwrap(); @@ -140,15 +161,17 @@ pub fn load_growth_rates(path: &String, growth_rate_library: &mut Box) { +pub fn load_abilities(path: &String) -> Box { let mut file = File::open(path.to_string() + "Abilities.json").unwrap(); let mut data = String::new(); file.read_to_string(&mut data).unwrap(); let json: Value = serde_json::from_str(&data).unwrap(); let o = json.as_object().unwrap(); + let mut ability_library = Box::new(AbilityLibraryImpl::new(400)); for (key, value) in o { let name = StringKey::new(key); let mut effect = StringKey::empty(); @@ -164,19 +187,21 @@ pub fn load_abilities(path: &String, ability_library: &mut Box) -> Box { let mut file = File::open(path.to_string() + "Moves.json").unwrap(); let mut data = String::new(); file.read_to_string(&mut data).unwrap(); let json: Value = serde_json::from_str(&data).unwrap(); let data = json.as_object().unwrap().get("data").unwrap().as_array().unwrap(); + let mut move_library = Box::new(MoveLibraryImpl::new(600)); for move_data in data { let move_data = move_data.as_object().unwrap(); let move_name = move_data.get("name").unwrap().as_str().unwrap().into(); let move_type = move_data.get("type").unwrap().as_str().unwrap().into(); - let move_type_id = lib.types().get_type_id(&move_type).unwrap(); + let move_type_id = types.get_type_id(&move_type).unwrap(); let move_category = serde_json::from_value(move_data.get("category").unwrap().clone()).unwrap(); let base_power = move_data.get("power").unwrap().as_i64().unwrap() as u8; let accuracy = move_data.get("accuracy").unwrap().as_i64().unwrap() as u8; @@ -213,7 +238,7 @@ pub fn load_moves(path: &String, lib: &mut dyn StaticData) { } } - lib.moves_mut().add( + move_library.add( &move_name, Arc::new(MoveDataImpl::new( &move_name.clone(), @@ -229,15 +254,21 @@ pub fn load_moves(path: &String, lib: &mut dyn StaticData) { )), ); } + move_library } -pub fn load_species(path: &String, library: &mut dyn StaticData) { +pub fn load_species( + path: &String, + types: &Box, + moves: &Box, +) -> Box { let mut file = File::open(path.to_string() + "Pokemon.json").unwrap(); let mut data = String::new(); file.read_to_string(&mut data).unwrap(); let json: Value = serde_json::from_str(&data).unwrap(); let o = json.as_object().unwrap(); + let mut species_library = Box::new(SpeciesLibraryImpl::new(800)); for (key, value) in o.iter() { if key.starts_with('$') { continue; @@ -261,7 +292,7 @@ pub fn load_species(path: &String, library: &mut dyn StaticData) { let forms = value.get("formes").unwrap().as_object().unwrap(); let default_form_value = forms.get("default").unwrap(); - let default_form = parse_form("default".into(), default_form_value, library); + let default_form = parse_form("default".into(), default_form_value, types, moves); let species = SpeciesImpl::new( id as u16, @@ -272,8 +303,9 @@ pub fn load_species(path: &String, library: &mut dyn StaticData) { default_form, Default::default(), ); - library.species_mut().add(&name, Arc::new(species)); + species_library.add(&name, Arc::new(species)); } + species_library } fn load_wasm(path: &String, library: &mut WebAssemblyScriptResolver) { @@ -285,7 +317,12 @@ fn load_wasm(path: &String, library: &mut WebAssemblyScriptResolver) { library.finalize(); } -fn parse_form(name: StringKey, value: &Value, library: &mut dyn StaticData) -> Arc { +fn parse_form( + name: StringKey, + value: &Value, + types: &Box, + moves: &Box, +) -> Arc { let mut abilities = Vec::new(); for a in value.get("abilities").unwrap().as_array().unwrap() { abilities.push(a.as_str().unwrap().into()); @@ -305,10 +342,10 @@ fn parse_form(name: StringKey, value: &Value, library: &mut dyn StaticData) -> A .as_array() .unwrap() .iter() - .map(|a| library.types().get_type_id(&a.as_str().unwrap().into()).unwrap()) + .map(|a| types.get_type_id(&a.as_str().unwrap().into()).unwrap()) .collect(); - let moves = parse_moves(value.get("moves").unwrap(), library.moves_mut()); + let moves = parse_moves(value.get("moves").unwrap(), moves); Arc::new(FormImpl::new( &name, @@ -368,7 +405,7 @@ where ) } -fn parse_moves(value: &Value, move_library: &mut Box) -> Box { +fn parse_moves(value: &Value, move_library: &Box) -> Box { let mut moves = LearnableMovesImpl::default(); let level_moves = value.get("levelMoves").unwrap().as_array().unwrap(); @@ -410,13 +447,12 @@ fn parse_effect_parameter(value: &Value) -> EffectParameter { fn test_type_library_loaded() { let mut path = get_project_root().unwrap(); path.push("tests/data/"); - let mut lib: Box = Box::new(TypeLibraryImpl::new(18)); - load_types(&path.to_str().unwrap().to_string(), &mut lib); + let types = load_types(&path.to_str().unwrap().to_string()); assert_eq!( - lib.get_effectiveness( - lib.get_type_id(&"fire".into()).unwrap(), - &[lib.get_type_id(&"grass".into()).unwrap()], + types.get_effectiveness( + types.get_type_id(&"fire".into()).unwrap(), + &[types.get_type_id(&"grass".into()).unwrap()], ), 2.0 );