Various fixes for FFI
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2023-01-03 13:56:20 +01:00
parent bd804ea280
commit 62a3ffb2bf
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
7 changed files with 51 additions and 34 deletions

View File

@ -156,7 +156,7 @@ extern "C" fn pokemon_real_ability_index(ptr: ExternPointer<Arc<Pokemon>>) -> u8
ptr.as_ref().real_ability().index ptr.as_ref().real_ability().index
} }
ffi_vec_value_getters!(Pokemon, types, TypeIdentifier); ffi_vec_value_getters!(Pokemon, Pokemon, types, TypeIdentifier);
/// Gets a learned move of the Pokemon. Index should generally be below [`MAX_MOVES`], you will get /// Gets a learned move of the Pokemon. Index should generally be below [`MAX_MOVES`], you will get
/// a null pointer otherwise. /// a null pointer otherwise.

View File

@ -22,6 +22,20 @@ macro_rules! ffi_arc_getter {
}; };
} }
/// Generates a basic getter foreign function interface.
macro_rules! ffi_arc_dyn_getter {
(
$type:ty, $func:ident, $returns: ty
) => {
paste::paste! {
#[no_mangle]
extern "C" fn [< $type:snake _ $func >](ptr: ExternPointer<Arc<dyn $type>>) -> $returns {
ptr.as_ref().$func()
}
}
};
}
/// Generates a basic getter foreign function interface where the return type is a [`crate::StringKey`]. /// Generates a basic getter foreign function interface where the return type is a [`crate::StringKey`].
macro_rules! ffi_arc_stringkey_getter { macro_rules! ffi_arc_stringkey_getter {
( (
@ -29,7 +43,7 @@ macro_rules! ffi_arc_stringkey_getter {
) => { ) => {
paste::paste! { paste::paste! {
#[no_mangle] #[no_mangle]
extern "C" fn [< $type:lower _ $func >](ptr: ExternPointer<Arc<$type>>) -> OwnedPtr<c_char> { extern "C" fn [< $type:lower _ $func >](ptr: ExternPointer<Arc<dyn $type>>) -> OwnedPtr<c_char> {
std::ffi::CString::new(ptr.as_ref().$func().str()).unwrap().into_raw() std::ffi::CString::new(ptr.as_ref().$func().str()).unwrap().into_raw()
} }
} }
@ -39,15 +53,15 @@ macro_rules! ffi_arc_stringkey_getter {
/// Generates a foreign function interface for a vec. This generates a length function, and a getter. /// Generates a foreign function interface for a vec. This generates a length function, and a getter.
macro_rules! ffi_vec_value_getters { macro_rules! ffi_vec_value_getters {
( (
$type:ty, $func:ident, $returns: ty $name:ident, $type:ty, $func:ident, $returns: ty
) => { ) => {
paste::paste! { paste::paste! {
#[no_mangle] #[no_mangle]
extern "C" fn [< $type:lower _ $func _length>](ptr: ExternPointer<Arc<$type>>) -> usize { extern "C" fn [< $name:lower _ $func _length>](ptr: ExternPointer<Arc<$type>>) -> usize {
ptr.as_ref().$func().len() ptr.as_ref().$func().len()
} }
#[no_mangle] #[no_mangle]
extern "C" fn [< $type:lower _ $func _get>](ptr: ExternPointer<Arc<$type>>, index: usize) -> $returns { extern "C" fn [< $name:lower _ $func _get>](ptr: ExternPointer<Arc<$type>>, index: usize) -> $returns {
*ptr.as_ref().$func().get(index).unwrap() *ptr.as_ref().$func().get(index).unwrap()
} }
} }
@ -62,11 +76,11 @@ macro_rules! ffi_vec_stringkey_getters {
) => { ) => {
paste::paste! { paste::paste! {
#[no_mangle] #[no_mangle]
extern "C" fn [< $type:lower _ $func _length>](ptr: ExternPointer<Arc<$type>>) -> usize { extern "C" fn [< $type:lower _ $func _length>](ptr: ExternPointer<Arc<dyn $type>>) -> usize {
ptr.as_ref().$func().len() ptr.as_ref().$func().len()
} }
#[no_mangle] #[no_mangle]
extern "C" fn [< $type:lower _ $func _get>](ptr: ExternPointer<Arc<$type>>, index: usize) -> OwnedPtr<c_char> { extern "C" fn [< $type:lower _ $func _get>](ptr: ExternPointer<Arc<dyn $type>>, index: usize) -> OwnedPtr<c_char> {
CString::new(ptr.as_ref().$func().get(index).unwrap().str()).unwrap().into_raw() CString::new(ptr.as_ref().$func().get(index).unwrap().str()).unwrap().into_raw()
} }
} }
@ -74,6 +88,7 @@ macro_rules! ffi_vec_stringkey_getters {
} }
use crate::{ValueIdentifiable, ValueIdentifier}; use crate::{ValueIdentifiable, ValueIdentifier};
pub(self) use ffi_arc_dyn_getter;
pub(self) use ffi_arc_getter; pub(self) use ffi_arc_getter;
pub(self) use ffi_arc_stringkey_getter; pub(self) use ffi_arc_stringkey_getter;
pub(self) use ffi_vec_stringkey_getters; pub(self) use ffi_vec_stringkey_getters;

View File

@ -1,6 +1,6 @@
use crate::ffi::{ use crate::ffi::{
ffi_arc_getter, ffi_vec_stringkey_getters, ffi_vec_value_getters, BorrowedPtr, ExternPointer, IdentifiablePointer, ffi_arc_dyn_getter, ffi_vec_stringkey_getters, ffi_vec_value_getters, BorrowedPtr, ExternPointer,
OwnedPtr, IdentifiablePointer, OwnedPtr,
}; };
use crate::static_data::{Form, FormImpl, LearnableMoves, StaticStatisticSet, TypeIdentifier}; use crate::static_data::{Form, FormImpl, LearnableMoves, StaticStatisticSet, TypeIdentifier};
use crate::StringKey; use crate::StringKey;
@ -74,11 +74,11 @@ unsafe extern "C" fn form_name(ptr: ExternPointer<Arc<dyn Form>>) -> OwnedPtr<c_
CString::new(name.str()).unwrap().into_raw() CString::new(name.str()).unwrap().into_raw()
} }
ffi_arc_getter!(dyn Form, height, f32); ffi_arc_dyn_getter!(Form, height, f32);
ffi_arc_getter!(dyn Form, weight, f32); ffi_arc_dyn_getter!(Form, weight, f32);
ffi_arc_getter!(dyn Form, base_experience, u32); ffi_arc_dyn_getter!(Form, base_experience, u32);
ffi_vec_value_getters!(dyn Form, types, TypeIdentifier); ffi_vec_value_getters!(Form, dyn Form, types, TypeIdentifier);
/// The inherent values of a form of species that are used for the stats of a Pokemon. /// The inherent values of a form of species that are used for the stats of a Pokemon.
#[no_mangle] #[no_mangle]
@ -88,8 +88,8 @@ unsafe extern "C" fn form_base_stats(
(ptr.as_ref().base_stats() as *const StaticStatisticSet<u16>).into() (ptr.as_ref().base_stats() as *const StaticStatisticSet<u16>).into()
} }
ffi_vec_stringkey_getters!(dyn Form, abilities); ffi_vec_stringkey_getters!(Form, abilities);
ffi_vec_stringkey_getters!(dyn Form, hidden_abilities); ffi_vec_stringkey_getters!(Form, hidden_abilities);
/// The moves a Pokemon with this form can learn. /// The moves a Pokemon with this form can learn.
#[no_mangle] #[no_mangle]

View File

@ -1,4 +1,4 @@
use crate::ffi::{ffi_arc_getter, ExternPointer, IdentifiablePointer, OwnedPtr}; use crate::ffi::{ffi_arc_dyn_getter, ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{BattleItemCategory, Item, ItemCategory, ItemImpl}; use crate::static_data::{BattleItemCategory, Item, ItemCategory, ItemImpl};
use crate::StringKey; use crate::StringKey;
use hashbrown::HashSet; use hashbrown::HashSet;
@ -40,9 +40,9 @@ unsafe extern "C" fn item_name(ptr: ExternPointer<Arc<dyn Item>>) -> OwnedPtr<c_
CString::new(name.str()).unwrap().into_raw() CString::new(name.str()).unwrap().into_raw()
} }
ffi_arc_getter!(dyn Item, category, ItemCategory); ffi_arc_dyn_getter!(Item, category, ItemCategory);
ffi_arc_getter!(dyn Item, battle_category, BattleItemCategory); ffi_arc_dyn_getter!(Item, battle_category, BattleItemCategory);
ffi_arc_getter!(dyn Item, price, i32); ffi_arc_dyn_getter!(Item, price, i32);
/// Checks whether the item has a specific flag. /// Checks whether the item has a specific flag.
#[no_mangle] #[no_mangle]

View File

@ -1,4 +1,4 @@
use crate::ffi::{ffi_arc_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr}; use crate::ffi::{ffi_arc_dyn_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{ use crate::static_data::{
EffectParameter, MoveCategory, MoveData, MoveDataImpl, MoveTarget, SecondaryEffect, SecondaryEffectImpl, EffectParameter, MoveCategory, MoveData, MoveDataImpl, MoveTarget, SecondaryEffect, SecondaryEffectImpl,
TypeIdentifier, TypeIdentifier,
@ -63,13 +63,13 @@ unsafe extern "C" fn move_data_name(ptr: ExternPointer<Arc<dyn MoveData>>) -> Ow
CString::new(name.str()).unwrap().into_raw() CString::new(name.str()).unwrap().into_raw()
} }
ffi_arc_getter!(dyn MoveData, move_type, TypeIdentifier); ffi_arc_dyn_getter!(MoveData, move_type, TypeIdentifier);
ffi_arc_getter!(dyn MoveData, category, MoveCategory); ffi_arc_dyn_getter!(MoveData, category, MoveCategory);
ffi_arc_getter!(dyn MoveData, base_power, u8); ffi_arc_dyn_getter!(MoveData, base_power, u8);
ffi_arc_getter!(dyn MoveData, accuracy, u8); ffi_arc_dyn_getter!(MoveData, accuracy, u8);
ffi_arc_getter!(dyn MoveData, base_usages, u8); ffi_arc_dyn_getter!(MoveData, base_usages, u8);
ffi_arc_getter!(dyn MoveData, target, MoveTarget); ffi_arc_dyn_getter!(MoveData, target, MoveTarget);
ffi_arc_getter!(dyn MoveData, priority, i8); ffi_arc_dyn_getter!(MoveData, priority, i8);
/// The optional secondary effect the move has. /// The optional secondary effect the move has.
#[no_mangle] #[no_mangle]

View File

@ -1,4 +1,6 @@
use crate::ffi::{ffi_arc_getter, ffi_arc_stringkey_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr}; use crate::ffi::{
ffi_arc_dyn_getter, ffi_arc_stringkey_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr,
};
use crate::static_data::{Form, Species, SpeciesImpl}; use crate::static_data::{Form, Species, SpeciesImpl};
use crate::StringKey; use crate::StringKey;
use hashbrown::HashSet; use hashbrown::HashSet;
@ -44,11 +46,11 @@ unsafe extern "C" fn species_drop(ptr: OwnedPtr<Arc<dyn Species>>) {
drop_in_place(ptr); drop_in_place(ptr);
} }
ffi_arc_getter!(dyn Species, id, u16); ffi_arc_dyn_getter!(Species, id, u16);
ffi_arc_stringkey_getter!(dyn Species, name); ffi_arc_stringkey_getter!(Species, name);
ffi_arc_getter!(dyn Species, gender_rate, f32); ffi_arc_dyn_getter!(Species, gender_rate, f32);
ffi_arc_stringkey_getter!(dyn Species, growth_rate); ffi_arc_stringkey_getter!(Species, growth_rate);
ffi_arc_getter!(dyn Species, capture_rate, u8); ffi_arc_dyn_getter!(Species, capture_rate, u8);
/// Adds a new form to the species. /// Adds a new form to the species.
#[no_mangle] #[no_mangle]

View File

@ -14,7 +14,7 @@ use pkmn_lib::defines::LevelInt;
use pkmn_lib::dynamic_data::Gen7DamageLibrary; use pkmn_lib::dynamic_data::Gen7DamageLibrary;
use pkmn_lib::dynamic_data::Gen7MiscLibrary; use pkmn_lib::dynamic_data::Gen7MiscLibrary;
use pkmn_lib::dynamic_data::{DynamicLibrary, DynamicLibraryImpl}; use pkmn_lib::dynamic_data::{DynamicLibrary, DynamicLibraryImpl};
use pkmn_lib::dynamic_data::{EmptyScriptResolver, Gen7BattleStatCalculator, ScriptResolver}; use pkmn_lib::dynamic_data::{Gen7BattleStatCalculator, ScriptResolver};
use pkmn_lib::static_data::{ use pkmn_lib::static_data::{
AbilityImpl, AbilityLibrary, AbilityLibraryImpl, BattleItemCategory, DataLibrary, EffectParameter, Form, FormImpl, AbilityImpl, AbilityLibrary, AbilityLibraryImpl, BattleItemCategory, DataLibrary, EffectParameter, Form, FormImpl,
GrowthRateLibrary, GrowthRateLibraryImpl, ItemImpl, ItemLibrary, ItemLibraryImpl, LearnableMoves, GrowthRateLibrary, GrowthRateLibraryImpl, ItemImpl, ItemLibrary, ItemLibraryImpl, LearnableMoves,