More FFI work
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2023-04-02 14:17:30 +02:00
parent eec85bb9ed
commit d7b3c0cd8d
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 101 additions and 19 deletions

View File

@ -1,5 +1,4 @@
use std::fmt::Debug; use std::fmt::Debug;
use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
use crate::dynamic_data::libraries::battle_stat_calculator::BattleStatCalculator; use crate::dynamic_data::libraries::battle_stat_calculator::BattleStatCalculator;
@ -19,12 +18,12 @@ pub trait DynamicLibrary: Debug + ValueIdentifiable {
fn static_data(&self) -> &Box<dyn StaticData>; fn static_data(&self) -> &Box<dyn StaticData>;
/// The stat calculator deals with the calculation of flat and boosted stats, based on the /// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes. /// Pokemons attributes.
fn stat_calculator(&self) -> &dyn BattleStatCalculator; fn stat_calculator(&self) -> &Box<dyn BattleStatCalculator>;
/// The damage calculator deals with the calculation of things relating to damage. /// The damage calculator deals with the calculation of things relating to damage.
fn damage_calculator(&self) -> &dyn DamageLibrary; fn damage_calculator(&self) -> &Box<dyn DamageLibrary>;
/// The Misc Library holds minor functions that do not fall in any of the other libraries and /// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators. /// calculators.
fn misc_library(&self) -> &dyn MiscLibrary; fn misc_library(&self) -> &Box<dyn MiscLibrary>;
/// Loads a standard script with a given unique combination of category and key. If no script /// Loads a standard script with a given unique combination of category and key. If no script
/// can be created with this combination, returns None. /// can be created with this combination, returns None.
@ -88,17 +87,17 @@ impl DynamicLibrary for DynamicLibraryImpl {
} }
/// The stat calculator deals with the calculation of flat and boosted stats, based on the /// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes. /// Pokemons attributes.
fn stat_calculator(&self) -> &dyn BattleStatCalculator { fn stat_calculator(&self) -> &Box<dyn BattleStatCalculator> {
self.stat_calculator.deref() &self.stat_calculator
} }
/// The damage calculator deals with the calculation of things relating to damage. /// The damage calculator deals with the calculation of things relating to damage.
fn damage_calculator(&self) -> &dyn DamageLibrary { fn damage_calculator(&self) -> &Box<dyn DamageLibrary> {
self.damage_calculator.deref() &self.damage_calculator
} }
/// The Misc Library holds minor functions that do not fall in any of the other libraries and /// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators. /// calculators.
fn misc_library(&self) -> &dyn MiscLibrary { fn misc_library(&self) -> &Box<dyn MiscLibrary> {
self.misc_library.deref() &self.misc_library
} }
/// Loads a standard script with a given unique combination of category and key. If no script /// Loads a standard script with a given unique combination of category and key. If no script
@ -138,9 +137,9 @@ pub mod test {
pub DynamicLibrary{} pub DynamicLibrary{}
impl DynamicLibrary for DynamicLibrary { impl DynamicLibrary for DynamicLibrary {
fn static_data(&self) -> &Box<dyn StaticData>; fn static_data(&self) -> &Box<dyn StaticData>;
fn stat_calculator(&self) -> &dyn BattleStatCalculator; fn stat_calculator(&self) -> &Box<dyn BattleStatCalculator>;
fn damage_calculator(&self) -> &dyn DamageLibrary; fn damage_calculator(&self) -> &Box<dyn DamageLibrary>;
fn misc_library(&self) -> &dyn MiscLibrary; fn misc_library(&self) -> &Box<dyn MiscLibrary>;
fn load_script( fn load_script(
&self, &self,
owner: ScriptOwnerData, owner: ScriptOwnerData,

View File

@ -1,6 +1,8 @@
use crate::dynamic_data::{BattleStatCalculator, Gen7BattleStatCalculator}; use crate::dynamic_data::{BattleStatCalculator, Gen7BattleStatCalculator, Pokemon};
use crate::ffi::{IdentifiablePointer, OwnedPtr}; use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{Statistic, StatisticSet};
use std::ptr::drop_in_place; use std::ptr::drop_in_place;
use std::sync::Arc;
/// Creates a new Gen 7 battle stat calculator /// Creates a new Gen 7 battle stat calculator
#[no_mangle] #[no_mangle]
@ -16,3 +18,43 @@ extern "C" fn gen_7_battle_stat_calculator_new() -> IdentifiablePointer<Box<dyn
extern "C" fn battle_stat_calculator_drop(ptr: OwnedPtr<Box<dyn BattleStatCalculator>>) { extern "C" fn battle_stat_calculator_drop(ptr: OwnedPtr<Box<dyn BattleStatCalculator>>) {
unsafe { drop_in_place(ptr) }; unsafe { drop_in_place(ptr) };
} }
/// Calculate all the flat stats of a Pokemon, disregarding stat boosts.
#[no_mangle]
extern "C" fn battle_stat_calculator_calculate_flat_stats(
ptr: ExternPointer<Box<dyn BattleStatCalculator>>,
pokemon: ExternPointer<Arc<Pokemon>>,
mut stats: ExternPointer<Box<StatisticSet<u32>>>,
) {
ptr.as_ref().calculate_flat_stats(pokemon.as_ref(), stats.as_mut());
}
/// Calculate a single flat stat of a Pokemon, disregarding stat boost
#[no_mangle]
extern "C" fn battle_stat_calculator_calculate_flat_stat(
ptr: ExternPointer<Box<dyn BattleStatCalculator>>,
pokemon: ExternPointer<Arc<Pokemon>>,
stat: Statistic,
) -> u32 {
ptr.as_ref().calculate_flat_stat(pokemon.as_ref(), stat)
}
/// Calculate all the boosted stats of a Pokemon, including stat boosts.
#[no_mangle]
extern "C" fn battle_stat_calculator_calculate_boosted_stats(
ptr: ExternPointer<Box<dyn BattleStatCalculator>>,
pokemon: ExternPointer<Arc<Pokemon>>,
mut stats: ExternPointer<Box<StatisticSet<u32>>>,
) {
ptr.as_ref().calculate_boosted_stats(pokemon.as_ref(), stats.as_mut());
}
/// Calculate a single boosted stat of a Pokemon, including stat boosts.
#[no_mangle]
extern "C" fn battle_stat_calculator_calculate_boosted_stat(
ptr: ExternPointer<Box<dyn BattleStatCalculator>>,
pokemon: ExternPointer<Arc<Pokemon>>,
stat: Statistic,
) -> u32 {
ptr.as_ref().calculate_boosted_stat(pokemon.as_ref(), stat)
}

View File

@ -1,7 +1,7 @@
use crate::dynamic_data::{ use crate::dynamic_data::{
BattleStatCalculator, DamageLibrary, DynamicLibrary, DynamicLibraryImpl, MiscLibrary, ScriptResolver, BattleStatCalculator, DamageLibrary, DynamicLibrary, DynamicLibraryImpl, MiscLibrary, ScriptResolver,
}; };
use crate::ffi::{IdentifiablePointer, OwnedPtr}; use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::StaticData; use crate::static_data::StaticData;
use std::ptr::drop_in_place; use std::ptr::drop_in_place;
use std::sync::Arc; use std::sync::Arc;
@ -29,6 +29,40 @@ extern "C" fn dynamic_library_new(
/// Drops a dynamic library. /// Drops a dynamic library.
#[no_mangle] #[no_mangle]
extern "C" fn dynamic_library_drop(ptr: OwnedPtr<Arc<dyn StaticData>>) { extern "C" fn dynamic_library_drop(ptr: OwnedPtr<Arc<dyn DynamicLibrary>>) {
unsafe { drop_in_place(ptr) }; unsafe { drop_in_place(ptr) };
} }
/// The static data is the immutable storage data for this library.
#[no_mangle]
extern "C" fn dynamic_library_get_static_data(
ptr: ExternPointer<Arc<dyn DynamicLibrary>>,
) -> IdentifiablePointer<Box<dyn StaticData>> {
ptr.as_ref().static_data().into()
}
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
#[no_mangle]
extern "C" fn dynamic_library_get_stat_calculator(
ptr: ExternPointer<Arc<dyn DynamicLibrary>>,
) -> IdentifiablePointer<Box<dyn BattleStatCalculator>> {
ptr.as_ref().stat_calculator().into()
}
/// The damage calculator deals with the calculation of things relating to damage.
#[no_mangle]
extern "C" fn dynamic_library_get_damage_calculator(
ptr: ExternPointer<Arc<dyn DynamicLibrary>>,
) -> IdentifiablePointer<Box<dyn DamageLibrary>> {
ptr.as_ref().damage_calculator().into()
}
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
#[no_mangle]
extern "C" fn dynamic_library_get_misc_library(
ptr: ExternPointer<Arc<dyn DynamicLibrary>>,
) -> IdentifiablePointer<Box<dyn MiscLibrary>> {
ptr.as_ref().misc_library().into()
}

View File

@ -1,8 +1,8 @@
use crate::ffi::{ use crate::ffi::{
ffi_arc_dyn_getter, ffi_arc_stringkey_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr, ffi_arc_dyn_getter, ffi_arc_stringkey_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr,
}; };
use crate::static_data::{Form, Species, SpeciesImpl}; use crate::static_data::{Form, Gender, Species, SpeciesImpl};
use crate::StringKey; use crate::{Random, StringKey};
use hashbrown::HashSet; use hashbrown::HashSet;
use std::ffi::{c_char, CStr}; use std::ffi::{c_char, CStr};
use std::ptr::drop_in_place; use std::ptr::drop_in_place;
@ -76,3 +76,10 @@ unsafe extern "C" fn species_get_form(
IdentifiablePointer::none() IdentifiablePointer::none()
} }
} }
/// Gets a form by name.
#[no_mangle]
unsafe extern "C" fn species_get_random_gender(species: ExternPointer<Arc<dyn Species>>, seed: u64) -> Gender {
let mut rand = Random::new(seed as u128);
species.as_ref().get_random_gender(&mut rand)
}