Moves a bunch of libraries to traits
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-12-24 12:00:50 +01:00
parent bce636b97e
commit 47df85e8d3
47 changed files with 730 additions and 358 deletions

View File

@@ -1,4 +1,6 @@
use crate::dynamic_data::{BattleStatCalculator, DamageLibrary, DynamicLibrary, MiscLibrary, ScriptResolver};
use crate::dynamic_data::{
BattleStatCalculator, DamageLibrary, DynamicLibrary, DynamicLibraryImpl, MiscLibrary, ScriptResolver,
};
use crate::ffi::{IdentifiablePointer, OwnedPtr};
use crate::static_data::StaticData;
use std::sync::Arc;
@@ -6,20 +8,20 @@ use std::sync::Arc;
/// Instantiates a new DynamicLibrary with given parameters.
#[no_mangle]
extern "C" fn dynamic_library_new(
static_data: OwnedPtr<StaticData>,
static_data: OwnedPtr<Box<dyn StaticData>>,
stat_calculator: OwnedPtr<Box<dyn BattleStatCalculator>>,
damage_library: OwnedPtr<Box<dyn DamageLibrary>>,
misc_library: OwnedPtr<Box<dyn MiscLibrary>>,
script_resolver: OwnedPtr<Box<dyn ScriptResolver>>,
) -> IdentifiablePointer<Arc<DynamicLibrary>> {
) -> IdentifiablePointer<Arc<dyn DynamicLibrary>> {
unsafe {
Arc::new(DynamicLibrary::new(
let a: Arc<dyn DynamicLibrary> = Arc::new(DynamicLibraryImpl::new(
*Box::from_raw(static_data),
*Box::from_raw(stat_calculator),
*Box::from_raw(damage_library),
*Box::from_raw(misc_library),
*Box::from_raw(script_resolver),
))
.into()
));
a.into()
}
}

View File

@@ -9,7 +9,7 @@ use std::sync::Arc;
/// Initializes a new battle.
#[no_mangle]
extern "C" fn battle_new(
library: ExternPointer<Arc<DynamicLibrary>>,
library: ExternPointer<Arc<dyn DynamicLibrary>>,
parties: *const OwnedPtr<BattleParty>,
parties_length: usize,
can_flee: u8,
@@ -46,7 +46,7 @@ extern "C" fn battle_new(
/// The library the battle uses for handling.
#[no_mangle]
extern "C" fn battle_library(ptr: ExternPointer<Arc<Battle>>) -> IdentifiablePointer<Arc<DynamicLibrary>> {
extern "C" fn battle_library(ptr: ExternPointer<Arc<Battle>>) -> IdentifiablePointer<Arc<dyn DynamicLibrary>> {
ptr.as_ref().library().clone().into()
}

View File

@@ -10,7 +10,7 @@ extern "C" fn learned_move_new(
move_data: ExternPointer<Arc<dyn MoveData>>,
learn_method: MoveLearnMethod,
) -> IdentifiablePointer<Arc<LearnedMove>> {
Arc::new(LearnedMove::new(move_data.as_ref(), learn_method)).into()
Arc::new(LearnedMove::new(move_data.as_ref().clone(), learn_method)).into()
}
/// Drops a learned move.

View File

@@ -11,7 +11,7 @@ use std::sync::Arc;
/// Instantiates a new Pokemon.
#[no_mangle]
extern "C" fn pokemon_new(
library: ExternPointer<Arc<DynamicLibrary>>,
library: ExternPointer<Arc<dyn DynamicLibrary>>,
species: ExternPointer<Arc<dyn Species>>,
form: ExternPointer<Arc<dyn Form>>,
hidden_ability: bool,
@@ -48,7 +48,7 @@ unsafe extern "C" fn pokemon_drop(ptr: OwnedPtr<Arc<Pokemon>>) {
/// The library data of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_library(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<DynamicLibrary>> {
extern "C" fn pokemon_library(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn DynamicLibrary>> {
ptr.as_ref().library().clone().into()
}

View File

@@ -163,6 +163,16 @@ impl<T: ValueIdentifiable + ?Sized> From<Box<T>> for IdentifiablePointer<Box<T>>
}
}
impl<T: ValueIdentifiable + ?Sized> From<&Box<T>> for IdentifiablePointer<Box<T>> {
fn from(v: &Box<T>) -> Self {
let id = unsafe { transmute(v.value_identifier()) };
Self {
ptr: v as *const Box<T>,
id,
}
}
}
impl<T: ValueIdentifiable + ?Sized> From<Option<Box<T>>> for IdentifiablePointer<Box<T>> {
fn from(v: Option<Box<T>>) -> Self {
if let Some(v) = v {

View File

@@ -1,25 +1,26 @@
use crate::defines::LevelInt;
use crate::ffi::{BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{GrowthRate, GrowthRateLibrary};
use crate::static_data::{GrowthRate, GrowthRateLibrary, GrowthRateLibraryImpl};
use std::ffi::{c_char, CStr};
use std::ptr::drop_in_place;
/// Instantiates a new growth rate library with a capacity
#[no_mangle]
extern "C" fn growth_rate_library_new(capacity: usize) -> IdentifiablePointer<GrowthRateLibrary> {
Box::new(GrowthRateLibrary::new(capacity)).into()
extern "C" fn growth_rate_library_new(capacity: usize) -> IdentifiablePointer<Box<dyn GrowthRateLibrary>> {
let b: Box<dyn GrowthRateLibrary> = Box::new(GrowthRateLibraryImpl::new(capacity));
b.into()
}
/// Drops the growthrate library.
#[no_mangle]
unsafe extern "C" fn growth_rate_library_drop(ptr: OwnedPtr<GrowthRateLibrary>) {
unsafe extern "C" fn growth_rate_library_drop(ptr: OwnedPtr<Box<dyn GrowthRateLibrary>>) {
drop_in_place(ptr)
}
/// Calculates the level for a given growth key name and a certain experience.
#[no_mangle]
unsafe extern "C" fn growth_rate_library_calculate_level(
ptr: ExternPointer<GrowthRateLibrary>,
ptr: ExternPointer<Box<dyn GrowthRateLibrary>>,
growth_rate: BorrowedPtr<c_char>,
experience: u32,
) -> LevelInt {
@@ -30,7 +31,7 @@ unsafe extern "C" fn growth_rate_library_calculate_level(
/// Calculates the experience for a given growth key name and a certain level.
#[no_mangle]
unsafe extern "C" fn growth_rate_library_calculate_experience(
ptr: ExternPointer<GrowthRateLibrary>,
ptr: ExternPointer<Box<dyn GrowthRateLibrary>>,
growth_rate: BorrowedPtr<c_char>,
level: LevelInt,
) -> u32 {
@@ -41,7 +42,7 @@ unsafe extern "C" fn growth_rate_library_calculate_experience(
/// Adds a new growth rate with a name and value.
#[no_mangle]
unsafe extern "C" fn growth_rate_library_add_growth_rate(
mut ptr: ExternPointer<GrowthRateLibrary>,
mut ptr: ExternPointer<Box<dyn GrowthRateLibrary>>,
name: BorrowedPtr<c_char>,
growth_rate: OwnedPtr<Box<dyn GrowthRate>>,
) {

View File

@@ -1,22 +1,23 @@
use crate::defines::LevelInt;
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::LibrarySettings;
use crate::static_data::{LibrarySettings, LibrarySettingsImpl};
use std::ptr::drop_in_place;
/// Creates a new settings library.
#[no_mangle]
extern "C" fn library_settings_new(max_level: LevelInt) -> IdentifiablePointer<LibrarySettings> {
Box::new(LibrarySettings::new(max_level)).into()
extern "C" fn library_settings_new(max_level: LevelInt) -> IdentifiablePointer<Box<dyn LibrarySettings>> {
let b: Box<dyn LibrarySettings> = Box::new(LibrarySettingsImpl::new(max_level));
b.into()
}
/// Drop a library settings object.
#[no_mangle]
unsafe extern "C" fn library_settings_drop(ptr: OwnedPtr<LibrarySettings>) {
unsafe extern "C" fn library_settings_drop(ptr: OwnedPtr<Box<dyn LibrarySettings>>) {
drop_in_place(ptr)
}
/// The highest level a Pokemon can be.
#[no_mangle]
extern "C" fn library_settings_maximum_level(ptr: ExternPointer<LibrarySettings>) -> LevelInt {
extern "C" fn library_settings_maximum_level(ptr: ExternPointer<Box<dyn LibrarySettings>>) -> LevelInt {
ptr.as_ref().maximum_level()
}

View File

@@ -17,32 +17,33 @@ use std::sync::Arc;
/// Generates foreign function interfaces for a DataLibrary trait implementation.
macro_rules! library_interface {
($library_type:ty, $return_type:ty) => {
($library_type_name:ident, $library_type:ty, $return_type:ty) => {
paste::paste! {
#[no_mangle]
extern "C" fn [< $library_type:snake _new >](capacity: usize) -> IdentifiablePointer<$library_type> {
Box::new($library_type::new(capacity)).into()
extern "C" fn [< $library_type_name:snake _new >](capacity: usize) -> IdentifiablePointer<$library_type> {
let value: $library_type = Box::new([<$library_type_name Impl>]::new(capacity));
value.into()
}
#[no_mangle]
unsafe extern "C" fn [< $library_type:snake _drop >](ptr: OwnedPtr<$library_type>) {
unsafe extern "C" fn [< $library_type_name:snake _drop >](ptr: OwnedPtr<$library_type>) {
drop_in_place(ptr);
}
#[no_mangle]
unsafe extern "C" fn [< $library_type:snake _add >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>, value: OwnedPtr<Arc<$return_type>>) {
unsafe extern "C" fn [< $library_type_name:snake _add >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>, value: OwnedPtr<Arc<$return_type>>) {
let lib = ptr.as_mut().unwrap();
lib.add(&CStr::from_ptr(key).into(), *Box::from_raw(value));
}
#[no_mangle]
unsafe extern "C" fn [< $library_type:snake _remove >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>) {
unsafe extern "C" fn [< $library_type_name:snake _remove >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>) {
let lib = ptr.as_mut().unwrap();
lib.remove(&CStr::from_ptr(key).into());
}
#[no_mangle]
unsafe extern "C" fn [< $library_type:snake _get >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>) -> IdentifiablePointer<Arc<$return_type>> {
unsafe extern "C" fn [< $library_type_name:snake _get >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>) -> IdentifiablePointer<Arc<$return_type>> {
let lib = ptr.as_mut().unwrap();
let v = lib.get(&CStr::from_ptr(key).into());
if let Some(value) = v {
@@ -53,7 +54,7 @@ macro_rules! library_interface {
}
#[no_mangle]
unsafe extern "C" fn [< $library_type:snake _get_key_by_index >](ptr: OwnedPtr<$library_type>, index: usize) -> OwnedPtr<c_char> {
unsafe extern "C" fn [< $library_type_name:snake _get_key_by_index >](ptr: OwnedPtr<$library_type>, index: usize) -> OwnedPtr<c_char> {
let lib = ptr.as_mut().unwrap();
let v = lib.get_key_by_index(index);
if let Some(value) = v {
@@ -64,7 +65,7 @@ macro_rules! library_interface {
}
#[no_mangle]
unsafe extern "C" fn [< $library_type:snake _len >](ptr: OwnedPtr<$library_type>) -> usize {
unsafe extern "C" fn [< $library_type_name:snake _len >](ptr: OwnedPtr<$library_type>) -> usize {
let lib = ptr.as_mut().unwrap();
lib.len()
}
@@ -72,7 +73,7 @@ macro_rules! library_interface {
};
}
library_interface!(SpeciesLibrary, dyn Species);
library_interface!(MoveLibrary, dyn MoveData);
library_interface!(AbilityLibrary, dyn Ability);
library_interface!(ItemLibrary, dyn Item);
library_interface!(SpeciesLibrary, Box<dyn SpeciesLibrary>, dyn Species);
library_interface!(MoveLibrary, Box<dyn MoveLibrary>, dyn MoveData);
library_interface!(AbilityLibrary, Box<dyn AbilityLibrary>, dyn Ability);
library_interface!(ItemLibrary, Box<dyn ItemLibrary>, dyn Item);

View File

@@ -1,25 +1,26 @@
use crate::ffi::{BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{Nature, NatureLibrary};
use crate::static_data::{Nature, NatureLibrary, NatureLibraryImpl};
use std::ffi::{c_char, CStr, CString};
use std::ptr::drop_in_place;
use std::sync::Arc;
/// Creates a new nature library with a given capacity.
#[no_mangle]
extern "C" fn nature_library_new(capacity: usize) -> IdentifiablePointer<NatureLibrary> {
Box::new(NatureLibrary::new(capacity)).into()
extern "C" fn nature_library_new(capacity: usize) -> IdentifiablePointer<Box<dyn NatureLibrary>> {
let b: Box<dyn NatureLibrary> = Box::new(NatureLibraryImpl::new(capacity));
b.into()
}
/// Drop a nature library.
#[no_mangle]
unsafe extern "C" fn nature_library_drop(ptr: OwnedPtr<NatureLibrary>) {
unsafe extern "C" fn nature_library_drop(ptr: OwnedPtr<Box<dyn NatureLibrary>>) {
drop_in_place(ptr);
}
/// Adds a new nature with name to the library.
#[no_mangle]
unsafe extern "C" fn nature_library_load_nature(
mut ptr: ExternPointer<NatureLibrary>,
mut ptr: ExternPointer<Box<dyn NatureLibrary>>,
name: BorrowedPtr<c_char>,
nature: OwnedPtr<Arc<dyn Nature>>,
) {
@@ -30,7 +31,7 @@ unsafe extern "C" fn nature_library_load_nature(
/// Gets a nature by name.
#[no_mangle]
unsafe extern "C" fn nature_library_get_nature(
ptr: ExternPointer<NatureLibrary>,
ptr: ExternPointer<Box<dyn NatureLibrary>>,
name: BorrowedPtr<c_char>,
) -> IdentifiablePointer<Arc<dyn Nature>> {
if let Some(nature) = ptr.as_ref().get_nature(&CStr::from_ptr(name).into()) {
@@ -43,7 +44,7 @@ unsafe extern "C" fn nature_library_get_nature(
/// Finds a nature name by nature.
#[no_mangle]
unsafe extern "C" fn nature_library_get_nature_name(
ptr: ExternPointer<NatureLibrary>,
ptr: ExternPointer<Box<dyn NatureLibrary>>,
nature: BorrowedPtr<Arc<dyn Nature>>,
) -> OwnedPtr<c_char> {
CString::new(ptr.as_ref().get_nature_name(nature.as_ref().unwrap()).str())

View File

@@ -1,68 +1,85 @@
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{
AbilityLibrary, GrowthRateLibrary, ItemLibrary, LibrarySettings, MoveLibrary, NatureLibrary, SpeciesLibrary,
StaticData, TypeLibrary,
StaticData, StaticDataImpl, TypeLibrary,
};
use std::ptr::drop_in_place;
/// Instantiates a new data collection.
#[no_mangle]
unsafe extern "C" fn static_data_new(settings: OwnedPtr<LibrarySettings>) -> IdentifiablePointer<StaticData> {
Box::new(StaticData::new(*Box::from_raw(settings))).into()
unsafe extern "C" fn static_data_new(
settings: OwnedPtr<Box<dyn LibrarySettings>>,
) -> IdentifiablePointer<Box<dyn StaticData>> {
let b: Box<dyn StaticData> = Box::new(StaticDataImpl::new(*Box::from_raw(settings)));
b.into()
}
/// Drop a static data.
#[no_mangle]
unsafe extern "C" fn static_data_drop(ptr: OwnedPtr<StaticData>) {
unsafe extern "C" fn static_data_drop(ptr: OwnedPtr<Box<dyn StaticData>>) {
drop_in_place(ptr)
}
/// Several misc settings for the library.
#[no_mangle]
unsafe extern "C" fn static_data_settings(mut data: ExternPointer<StaticData>) -> IdentifiablePointer<LibrarySettings> {
(data.as_mut().settings() as *const LibrarySettings).into()
unsafe extern "C" fn static_data_settings(
mut data: ExternPointer<Box<dyn StaticData>>,
) -> IdentifiablePointer<Box<dyn LibrarySettings>> {
data.as_mut().settings().into()
}
/// All data for Pokemon species.
#[no_mangle]
unsafe extern "C" fn static_data_species(mut data: ExternPointer<StaticData>) -> IdentifiablePointer<SpeciesLibrary> {
(data.as_mut().species_mut() as *const SpeciesLibrary).into()
unsafe extern "C" fn static_data_species(
mut data: ExternPointer<Box<dyn StaticData>>,
) -> IdentifiablePointer<Box<dyn SpeciesLibrary>> {
data.as_mut().species().into()
}
/// All data for the moves.
#[no_mangle]
unsafe extern "C" fn static_data_moves(mut data: ExternPointer<StaticData>) -> IdentifiablePointer<MoveLibrary> {
(data.as_mut().moves_mut() as *const MoveLibrary).into()
unsafe extern "C" fn static_data_moves(
mut data: ExternPointer<Box<dyn StaticData>>,
) -> IdentifiablePointer<Box<dyn MoveLibrary>> {
data.as_mut().moves().into()
}
/// All data for the items.
#[no_mangle]
unsafe extern "C" fn static_data_items(mut data: ExternPointer<StaticData>) -> IdentifiablePointer<ItemLibrary> {
(data.as_mut().items_mut() as *const ItemLibrary).into()
unsafe extern "C" fn static_data_items(
mut data: ExternPointer<Box<dyn StaticData>>,
) -> IdentifiablePointer<Box<dyn ItemLibrary>> {
(data.as_mut().items()).into()
}
/// All data for growth rates.
#[no_mangle]
unsafe extern "C" fn static_data_growth_rates(
mut data: ExternPointer<StaticData>,
) -> IdentifiablePointer<GrowthRateLibrary> {
(data.as_mut().growth_rates_mut() as *const GrowthRateLibrary).into()
mut data: ExternPointer<Box<dyn StaticData>>,
) -> IdentifiablePointer<Box<dyn GrowthRateLibrary>> {
data.as_mut().growth_rates().into()
}
/// All data related to types and type effectiveness.
#[no_mangle]
unsafe extern "C" fn static_data_types(mut data: ExternPointer<StaticData>) -> IdentifiablePointer<TypeLibrary> {
(data.as_mut().types_mut() as *const TypeLibrary).into()
unsafe extern "C" fn static_data_types(
mut data: ExternPointer<Box<dyn StaticData>>,
) -> IdentifiablePointer<Box<dyn TypeLibrary>> {
data.as_mut().types().into()
}
/// All data related to natures.
#[no_mangle]
unsafe extern "C" fn static_data_natures(mut data: ExternPointer<StaticData>) -> IdentifiablePointer<NatureLibrary> {
(data.as_mut().natures_mut() as *const NatureLibrary).into()
unsafe extern "C" fn static_data_natures(
data: ExternPointer<Box<dyn StaticData>>,
) -> IdentifiablePointer<Box<dyn NatureLibrary>> {
data.as_ref().natures().into()
}
/// All data related to abilities.
#[no_mangle]
unsafe extern "C" fn static_data_abilities(mut data: ExternPointer<StaticData>) -> IdentifiablePointer<AbilityLibrary> {
(data.as_mut().abilities_mut() as *const AbilityLibrary).into()
unsafe extern "C" fn static_data_abilities(
mut data: ExternPointer<Box<dyn StaticData>>,
) -> IdentifiablePointer<Box<dyn AbilityLibrary>> {
(data.as_mut().abilities()).into()
}

View File

@@ -1,24 +1,25 @@
use crate::ffi::{BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{TypeIdentifier, TypeLibrary};
use crate::static_data::{TypeIdentifier, TypeLibrary, TypeLibraryImpl};
use std::ffi::{c_char, CStr, CString};
use std::ptr::drop_in_place;
/// Instantiates a new type library with a specific capacity.
#[no_mangle]
extern "C" fn type_library_new(capacity: usize) -> IdentifiablePointer<TypeLibrary> {
Box::new(TypeLibrary::new(capacity)).into()
extern "C" fn type_library_new(capacity: usize) -> IdentifiablePointer<Box<dyn TypeLibrary>> {
let b: Box<dyn TypeLibrary> = Box::new(TypeLibraryImpl::new(capacity));
b.into()
}
/// Drops a type library.
#[no_mangle]
unsafe extern "C" fn type_library_drop(ptr: OwnedPtr<TypeLibrary>) {
unsafe extern "C" fn type_library_drop(ptr: OwnedPtr<Box<dyn TypeLibrary>>) {
drop_in_place(ptr);
}
/// Gets the type identifier for a type with a name.
#[no_mangle]
unsafe extern "C" fn type_library_get_type_id(
ptr: ExternPointer<TypeLibrary>,
ptr: ExternPointer<Box<dyn TypeLibrary>>,
key: BorrowedPtr<c_char>,
found: *mut bool,
) -> TypeIdentifier {
@@ -34,7 +35,7 @@ unsafe extern "C" fn type_library_get_type_id(
/// Gets the type name from the type identifier.
#[no_mangle]
unsafe extern "C" fn type_library_get_type_name(
ptr: ExternPointer<TypeLibrary>,
ptr: ExternPointer<Box<dyn TypeLibrary>>,
type_id: TypeIdentifier,
found: *mut bool,
) -> *mut c_char {
@@ -50,7 +51,7 @@ unsafe extern "C" fn type_library_get_type_name(
/// Gets the effectiveness for a single attacking type against a single defending type.
#[no_mangle]
extern "C" fn type_library_get_single_effectiveness(
ptr: ExternPointer<TypeLibrary>,
ptr: ExternPointer<Box<dyn TypeLibrary>>,
attacking: TypeIdentifier,
defending: TypeIdentifier,
) -> f32 {
@@ -62,7 +63,7 @@ extern "C" fn type_library_get_single_effectiveness(
/// and multiplying the results with each other.
#[no_mangle]
unsafe extern "C" fn type_library_get_effectiveness(
ptr: ExternPointer<TypeLibrary>,
ptr: ExternPointer<Box<dyn TypeLibrary>>,
attacking: TypeIdentifier,
defending: OwnedPtr<TypeIdentifier>,
defending_length: usize,
@@ -74,7 +75,7 @@ unsafe extern "C" fn type_library_get_effectiveness(
/// Registers a new type in the library.
#[no_mangle]
unsafe extern "C" fn type_library_register_type(
mut ptr: ExternPointer<TypeLibrary>,
mut ptr: ExternPointer<Box<dyn TypeLibrary>>,
name: BorrowedPtr<c_char>,
) -> TypeIdentifier {
ptr.as_mut().register_type(&CStr::from_ptr(name).into())
@@ -83,7 +84,7 @@ unsafe extern "C" fn type_library_register_type(
/// Sets the effectiveness for an attacking type against a defending type.
#[no_mangle]
unsafe extern "C" fn type_library_set_effectiveness(
mut ptr: ExternPointer<TypeLibrary>,
mut ptr: ExternPointer<Box<dyn TypeLibrary>>,
attacking: TypeIdentifier,
defending: TypeIdentifier,
effectiveness: f32,