Complete refactor of the FFI to use handles instead of pointers.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-06-24 14:44:23 +02:00
parent 4c222cb753
commit 78bb91093b
76 changed files with 1510 additions and 1952 deletions

View File

@@ -107,7 +107,7 @@ pub fn load_types(path: &String) -> Arc<dyn TypeLibrary> {
.from_path(path.to_string() + "Types.csv")
.unwrap();
let mut type_library = TypeLibraryImpl::new(20);
let type_library = TypeLibraryImpl::new(20);
let headers = reader.headers().unwrap();
for header in headers.iter().skip(1) {
@@ -135,7 +135,7 @@ pub fn load_natures(path: &String) -> Arc<dyn NatureLibrary> {
.from_path(path.to_string() + "Natures.csv")
.unwrap();
let mut nature_library = NatureLibraryImpl::new(24);
let nature_library = NatureLibraryImpl::new(24);
for record in reader.records() {
let record = record.unwrap();
let nature_name = record.get(0).unwrap().into();
@@ -162,7 +162,7 @@ pub fn load_items(path: &String) -> Arc<dyn ItemLibrary> {
let json: Value = serde_json::from_str(&data).unwrap();
let json_array = json.as_array().unwrap();
let mut item_library = ItemLibraryImpl::new(400);
let item_library = 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();
@@ -194,7 +194,7 @@ pub fn load_growth_rates(path: &String) -> Arc<dyn GrowthRateLibrary> {
let json: Value = serde_json::from_str(&data).unwrap();
let o = json.as_object().unwrap();
let mut growth_rate_library = GrowthRateLibraryImpl::new(10);
let growth_rate_library = GrowthRateLibraryImpl::new(10);
for (key, value) in o {
let name = StringKey::new(key);
let experience_required_json = value.as_array().unwrap();
@@ -203,7 +203,7 @@ pub fn load_growth_rates(path: &String) -> Arc<dyn GrowthRateLibrary> {
experience_required.push(v.as_i64().unwrap() as u32);
}
growth_rate_library.add_growth_rate(&name, Box::new(LookupGrowthRate::new(experience_required)));
growth_rate_library.add_growth_rate(&name, Arc::new(LookupGrowthRate::new(experience_required)));
}
Arc::new(growth_rate_library)
}
@@ -215,7 +215,7 @@ pub fn load_abilities(path: &String) -> Arc<dyn AbilityLibrary> {
let json: Value = serde_json::from_str(&data).unwrap();
let o = json.as_object().unwrap();
let mut ability_library = AbilityLibraryImpl::new(400);
let ability_library = AbilityLibraryImpl::new(400);
for (key, value) in o {
let name = StringKey::new(key);
let mut effect = StringKey::empty();
@@ -240,7 +240,7 @@ pub fn load_moves(path: &String, types: &Arc<dyn TypeLibrary>) -> Arc<dyn MoveLi
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 = MoveLibraryImpl::new(600);
let move_library = 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();
@@ -252,7 +252,7 @@ pub fn load_moves(path: &String, types: &Arc<dyn TypeLibrary>) -> Arc<dyn MoveLi
let pp = move_data.get("pp").unwrap().as_i64().unwrap() as u8;
let target = serde_json::from_value(move_data.get("target").unwrap().clone()).unwrap();
let priority = move_data.get("priority").unwrap().as_i64().unwrap() as i8;
let secondary_effect: Option<Box<dyn SecondaryEffect>> = if let Some(v) = move_data.get("effect") {
let secondary_effect: Option<Arc<dyn SecondaryEffect>> = if let Some(v) = move_data.get("effect") {
let mut chance = -1.0;
if let Some(chance_value) = v.get("chance") {
chance = chance_value.as_f64().unwrap() as f32;
@@ -265,7 +265,7 @@ pub fn load_moves(path: &String, types: &Arc<dyn TypeLibrary>) -> Arc<dyn MoveLi
}
}
Some(Box::new(SecondaryEffectImpl::new(
Some(Arc::new(SecondaryEffectImpl::new(
chance,
v.get("name").unwrap().as_str().unwrap().into(),
parameters,
@@ -312,7 +312,7 @@ pub fn load_species(
let json: Value = serde_json::from_str(&data).unwrap();
let o = json.as_object().unwrap();
let mut species_library = SpeciesLibraryImpl::new(800);
let species_library = SpeciesLibraryImpl::new(800);
for (key, value) in o.iter() {
if key.starts_with('$') {
continue;
@@ -353,13 +353,13 @@ pub fn load_species(
}
#[cfg(not(feature = "wasm"))]
fn load_script_resolver(path: &String) -> Box<dyn ScriptResolver> {
Box::new(EmptyScriptResolver::default())
fn load_script_resolver(path: &String) -> Arc<dyn ScriptResolver> {
Arc::new(EmptyScriptResolver::default())
}
#[cfg(feature = "wasm")]
fn load_script_resolver(path: &String) -> Box<dyn ScriptResolver> {
let mut resolver = pkmn_lib::script_implementations::wasm::script_resolver::WebAssemblyScriptResolver::new();
fn load_script_resolver(path: &String) -> Arc<dyn ScriptResolver> {
let resolver = pkmn_lib::script_implementations::wasm::script_resolver::WebAssemblyScriptResolver::new();
let file = File::open(path.to_string() + "gen7_scripts.wasm").unwrap();
let mut reader = BufReader::new(file);
let mut buffer = Vec::new();
@@ -413,12 +413,12 @@ fn parse_form(
))
}
fn parse_statistics<T>(value: &Value) -> StaticStatisticSet<T>
fn parse_statistics<T>(value: &Value) -> Arc<StaticStatisticSet<T>>
where
T: PrimInt + TryFrom<u64>,
<T as TryFrom<u64>>::Error: Debug,
{
StaticStatisticSet::new(
Arc::new(StaticStatisticSet::new(
<T as TryFrom<u64>>::try_from(value.get("hp").unwrap_or(&Value::Number(0.into())).as_u64().unwrap()).unwrap(),
<T as TryFrom<u64>>::try_from(
value
@@ -454,11 +454,11 @@ where
.unwrap(),
<T as TryFrom<u64>>::try_from(value.get("speed").unwrap_or(&Value::Number(0.into())).as_u64().unwrap())
.unwrap(),
)
))
}
fn parse_moves(value: &Value, move_library: &Arc<dyn MoveLibrary>) -> Box<dyn LearnableMoves> {
let mut moves = LearnableMovesImpl::new(100);
fn parse_moves(value: &Value, move_library: &Arc<dyn MoveLibrary>) -> Arc<dyn LearnableMoves> {
let moves = LearnableMovesImpl::new(100);
let level_moves = value.get("levelMoves").unwrap().as_array().unwrap();
for level_move in level_moves {
@@ -468,7 +468,7 @@ fn parse_moves(value: &Value, move_library: &Arc<dyn MoveLibrary>) -> Box<dyn Le
moves.add_level_move(level, &name).unwrap();
}
Box::new(moves)
Arc::new(moves)
}
fn parse_effect_parameter(value: &Value) -> Arc<EffectParameter> {

View File

@@ -1,4 +1,5 @@
use serde::Deserialize;
use std::sync::Arc;
use pkmn_lib::dynamic_data::Battle;
use pkmn_lib::dynamic_data::{MoveChoice, PassChoice, TurnChoice};
@@ -56,12 +57,12 @@ impl TestStep {
assert!(used_move.is_some());
assert!(battle
.try_set_choice(TurnChoice::Move(MoveChoice::new(
.try_set_choice(Arc::new(TurnChoice::Move(MoveChoice::new(
pokemon,
used_move.unwrap(),
target[0],
target[1],
)))
))))
.unwrap());
}
TestStep::SetPassChoice { for_pokemon } => {
@@ -69,7 +70,9 @@ impl TestStep {
.as_ref()
.unwrap()
.clone();
assert!(battle.try_set_choice(TurnChoice::Pass(PassChoice::new(p))).unwrap());
assert!(battle
.try_set_choice(Arc::new(TurnChoice::Pass(PassChoice::new(p))))
.unwrap());
}
TestStep::Assert { value, expected } => {
let v = value.get(battle);

View File

@@ -1,7 +1,8 @@
#![feature(custom_test_frameworks)]
#![feature(lazy_cell)]
#![allow(clippy::borrowed_box)]
use std::sync::Arc;
use std::sync::{Arc, LazyLock};
use pkmn_lib::dynamic_data::{
Battle, BattleParty, DamageSource, DynamicLibrary, ExecutingMove, MoveChoice, PokemonBuilder, PokemonParty,
@@ -13,8 +14,10 @@ use crate::common::library_loader;
pub mod common;
pub mod datatests;
static LIBRARY: LazyLock<Arc<dyn DynamicLibrary>> = LazyLock::new(|| library_loader::load_library().library);
fn get_library() -> Arc<dyn DynamicLibrary> {
library_loader::load_library().library
LIBRARY.clone()
}
#[test]