Reworks static library initialiser
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-12-24 12:48:00 +01:00
parent 02e3d7ecc7
commit efdaf730e6
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 107 additions and 46 deletions

View File

@ -9,8 +9,24 @@ use std::ptr::drop_in_place;
#[no_mangle]
unsafe extern "C" fn static_data_new(
settings: OwnedPtr<Box<dyn LibrarySettings>>,
species: OwnedPtr<Box<dyn SpeciesLibrary>>,
moves: OwnedPtr<Box<dyn MoveLibrary>>,
items: OwnedPtr<Box<dyn ItemLibrary>>,
growth_rates: OwnedPtr<Box<dyn GrowthRateLibrary>>,
types: OwnedPtr<Box<dyn TypeLibrary>>,
natures: OwnedPtr<Box<dyn NatureLibrary>>,
abilities: OwnedPtr<Box<dyn AbilityLibrary>>,
) -> IdentifiablePointer<Box<dyn StaticData>> {
let b: Box<dyn StaticData> = Box::new(StaticDataImpl::new(*Box::from_raw(settings)));
let b: Box<dyn StaticData> = 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()
}

View File

@ -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<dyn LibrarySettings>) -> Self {
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>,
) -> 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,
}
}
}

View File

@ -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<dyn DynamicLibrary> {
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<dyn DynamicLibrary> {
))
}
pub fn load_types(path: &String, type_library: &mut Box<dyn TypeLibrary>) {
pub fn load_types(path: &String) -> Box<dyn TypeLibrary> {
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<dyn TypeLibrary>) {
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<dyn NatureLibrary>) {
pub fn load_natures(path: &String) -> Box<dyn NatureLibrary> {
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<dyn NatureLibrary>)
);
}
}
nature_library
}
pub fn load_items(path: &String, lib: &mut Box<dyn ItemLibrary>) {
pub fn load_items(path: &String) -> Box<dyn ItemLibrary> {
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<dyn ItemLibrary>) {
}
}
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<dyn GrowthRateLibrary>) {
pub fn load_growth_rates(path: &String) -> Box<dyn GrowthRateLibrary> {
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<dyn Growth
growth_rate_library.add_growth_rate(&name, Box::new(LookupGrowthRate::new(experience_required)));
}
growth_rate_library
}
pub fn load_abilities(path: &String, ability_library: &mut Box<dyn AbilityLibrary>) {
pub fn load_abilities(path: &String) -> Box<dyn AbilityLibrary> {
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<dyn AbilityLibrar
ability_library.add(&name, Arc::new(AbilityImpl::new(&name, &effect, parameters)));
}
ability_library
}
pub fn load_moves(path: &String, lib: &mut dyn StaticData) {
pub fn load_moves(path: &String, types: &Box<dyn TypeLibrary>) -> Box<dyn MoveLibrary> {
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<dyn TypeLibrary>,
moves: &Box<dyn MoveLibrary>,
) -> Box<dyn SpeciesLibrary> {
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<dyn Form> {
fn parse_form(
name: StringKey,
value: &Value,
types: &Box<dyn TypeLibrary>,
moves: &Box<dyn MoveLibrary>,
) -> Arc<dyn Form> {
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<dyn MoveLibrary>) -> Box<dyn LearnableMoves> {
fn parse_moves(value: &Value, move_library: &Box<dyn MoveLibrary>) -> Box<dyn LearnableMoves> {
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<dyn TypeLibrary> = 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
);