More FFI work
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-04-02 14:17:30 +02:00
parent eec85bb9ed
commit d7b3c0cd8d
4 changed files with 101 additions and 19 deletions

View File

@@ -1,6 +1,8 @@
use crate::dynamic_data::{BattleStatCalculator, Gen7BattleStatCalculator};
use crate::ffi::{IdentifiablePointer, OwnedPtr};
use crate::dynamic_data::{BattleStatCalculator, Gen7BattleStatCalculator, Pokemon};
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{Statistic, StatisticSet};
use std::ptr::drop_in_place;
use std::sync::Arc;
/// Creates a new Gen 7 battle stat calculator
#[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>>) {
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::{
BattleStatCalculator, DamageLibrary, DynamicLibrary, DynamicLibraryImpl, MiscLibrary, ScriptResolver,
};
use crate::ffi::{IdentifiablePointer, OwnedPtr};
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::StaticData;
use std::ptr::drop_in_place;
use std::sync::Arc;
@@ -29,6 +29,40 @@ extern "C" fn dynamic_library_new(
/// Drops a dynamic library.
#[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) };
}
/// 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::{
ffi_arc_dyn_getter, ffi_arc_stringkey_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr,
};
use crate::static_data::{Form, Species, SpeciesImpl};
use crate::StringKey;
use crate::static_data::{Form, Gender, Species, SpeciesImpl};
use crate::{Random, StringKey};
use hashbrown::HashSet;
use std::ffi::{c_char, CStr};
use std::ptr::drop_in_place;
@@ -76,3 +76,10 @@ unsafe extern "C" fn species_get_form(
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)
}