Style and Clippy fixes.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-10-14 16:53:30 +02:00
parent 9efe1b4e22
commit 691bf7c12e
56 changed files with 354 additions and 249 deletions

View File

@@ -1,6 +1,7 @@
use crate::dynamic_data::{BattleStatCalculator, Gen7BattleStatCalculator};
use crate::ffi::IdentifiablePointer;
/// Creates a new Gen 7 battle stat calculator
#[no_mangle]
extern "C" fn gen_7_battle_stat_calculator_new() -> IdentifiablePointer<Box<dyn BattleStatCalculator>> {
let v: Box<dyn BattleStatCalculator> = Box::new(Gen7BattleStatCalculator::new());

View File

@@ -1,6 +1,8 @@
use crate::dynamic_data::{DamageLibrary, Gen7DamageLibrary};
use crate::ffi::IdentifiablePointer;
/// Creates a new generation 7 damage library. `has_randomness` defines whether a random damage
/// modifier (0.85x - 1.00x) is applied to the calculated damage.
#[no_mangle]
extern "C" fn gen_7_damage_library_new(randomness: u8) -> IdentifiablePointer<Box<dyn DamageLibrary>> {
let v: Box<dyn DamageLibrary> = Box::new(Gen7DamageLibrary::new(randomness == 1));

View File

@@ -3,6 +3,7 @@ use crate::ffi::{IdentifiablePointer, OwnedPtr};
use crate::static_data::StaticData;
use std::sync::Arc;
/// Instantiates a new DynamicLibrary with given parameters.
#[no_mangle]
extern "C" fn dynamic_library_new(
static_data: OwnedPtr<StaticData>,

View File

@@ -1,6 +1,7 @@
use crate::dynamic_data::{Gen7MiscLibrary, MiscLibrary};
use crate::ffi::IdentifiablePointer;
/// Instantiates a new MiscLibrary.
#[no_mangle]
extern "C" fn gen_7_misc_library_new() -> IdentifiablePointer<Box<dyn MiscLibrary>> {
let v: Box<dyn MiscLibrary> = Box::new(Gen7MiscLibrary::new());

View File

@@ -1,5 +1,10 @@
/// The foreign function interfaces for the battle stat calculator
mod battle_stat_calculator;
/// The foreign function interfaces for the damage library
mod damage_library;
/// The foreign function interfaces for the dynamic library
mod dynamic_library;
/// The foreign function interfaces for the misc library
mod misc_library;
/// The foreign function interfaces for the script resolver
mod script_resolver;

View File

@@ -1,6 +1,7 @@
use crate::dynamic_data::{EmptyScriptResolver, ScriptResolver};
use crate::ffi::IdentifiablePointer;
/// Instantiates a basic empty script resolver, that always returns None.
#[no_mangle]
extern "C" fn empty_script_resolver_new() -> IdentifiablePointer<Box<dyn ScriptResolver>> {
let v: Box<dyn ScriptResolver> = Box::new(EmptyScriptResolver {
@@ -11,12 +12,14 @@ extern "C" fn empty_script_resolver_new() -> IdentifiablePointer<Box<dyn ScriptR
IdentifiablePointer::new(ptr, id)
}
/// Foreign function interfaces for the Webassembly script resolver.
#[cfg(feature = "wasm")]
mod web_assembly_script_resolver {
use crate::dynamic_data::ScriptResolver;
use crate::ffi::{ExternPointer, IdentifiablePointer};
use crate::script_implementations::wasm::script_resolver::WebAssemblyScriptResolver;
/// Instantiates a new WebAssemblyScriptResolver.
#[no_mangle]
extern "C" fn webassembly_script_resolver_new() -> IdentifiablePointer<Box<dyn ScriptResolver>> {
let v: Box<dyn ScriptResolver> = WebAssemblyScriptResolver::new();
@@ -25,17 +28,19 @@ mod web_assembly_script_resolver {
IdentifiablePointer::new(ptr, id)
}
/// Load a compiled WASM module.
#[no_mangle]
extern "C" fn webassembly_script_resolver_load_wasm_from_bytes(
ptr: ExternPointer<Box<WebAssemblyScriptResolver>>,
mut ptr: ExternPointer<Box<WebAssemblyScriptResolver>>,
arr: *const u8,
len: usize,
) {
unsafe { ptr.as_mut().load_wasm_from_bytes(std::slice::from_raw_parts(arr, len)) }
}
/// Tells the script resolver we're done loading wasm modules, and to finalize the resolver.
#[no_mangle]
extern "C" fn webassembly_script_resolver_finalize(ptr: ExternPointer<Box<WebAssemblyScriptResolver>>) {
extern "C" fn webassembly_script_resolver_finalize(mut ptr: ExternPointer<Box<WebAssemblyScriptResolver>>) {
ptr.as_mut().finalize();
}
}

View File

@@ -1,2 +1,4 @@
/// The foreign function interfaces for the dynamic data libraries
mod libraries;
mod models;
/// The foreign function interfaces for the dynamic data models
mod models;

View File

@@ -1 +1,2 @@
/// The foreign function interface for a Pokemon.
mod pokemon;

View File

@@ -8,6 +8,7 @@ use std::ffi::{c_char, CStr, CString};
use std::ptr::drop_in_place;
use std::sync::Arc;
/// Instantiates a new Pokemon.
#[no_mangle]
extern "C" fn pokemon_new(
library: ExternPointer<Arc<DynamicLibrary>>,
@@ -39,34 +40,40 @@ extern "C" fn pokemon_new(
.into()
}
/// Drops an Arc reference held by the FFI.
#[no_mangle]
unsafe extern "C" fn pokemon_drop(ptr: OwnedPtr<Arc<Pokemon>>) {
drop_in_place(ptr);
}
/// The library data of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_library(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<DynamicLibrary>> {
ptr.as_ref().library().clone().into()
}
/// The species of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_species(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Species>> {
ptr.as_ref().species().clone().into()
ptr.as_ref().species().into()
}
/// The form of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_form(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Form>> {
ptr.as_ref().form().clone().into()
ptr.as_ref().form().into()
}
/// The species that should be displayed to the user. This handles stuff like the Illusion ability.
#[no_mangle]
extern "C" fn pokemon_display_species(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Species>> {
ptr.as_ref().display_species().clone().into()
ptr.as_ref().display_species().into()
}
/// The form that should be displayed to the user. This handles stuff like the Illusion ability.
#[no_mangle]
extern "C" fn pokemon_display_form(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Form>> {
ptr.as_ref().display_form().clone().into()
ptr.as_ref().display_form().into()
}
ffi_arc_getter!(Pokemon, level, LevelInt);
@@ -75,6 +82,7 @@ ffi_arc_getter!(Pokemon, unique_identifier, u32);
ffi_arc_getter!(Pokemon, gender, Gender);
ffi_arc_getter!(Pokemon, coloring, u8);
/// Gets the held item of a Pokemon
#[no_mangle]
extern "C" fn pokemon_held_item(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Item>> {
if let Some(v) = ptr.as_ref().held_item().read().as_ref() {
@@ -84,44 +92,40 @@ extern "C" fn pokemon_held_item(ptr: ExternPointer<Arc<Pokemon>>) -> Identifiabl
}
}
/// Checks whether the Pokemon is holding a specific item.
#[no_mangle]
extern "C" fn pokemon_has_held_item(ptr: ExternPointer<Arc<Pokemon>>, name: *const c_char) -> u8 {
let name = unsafe { CStr::from_ptr(name) }.into();
if ptr.as_ref().has_held_item(&name) {
1
} else {
0
}
u8::from(ptr.as_ref().has_held_item(&name))
}
/// Changes the held item of the Pokemon. Returns the previously held item.
#[no_mangle]
extern "C" fn pokemon_set_held_item(
ptr: ExternPointer<Arc<Pokemon>>,
item: ExternPointer<Arc<Item>>,
) -> IdentifiablePointer<Arc<Item>> {
if let Some(v) = ptr.as_ref().set_held_item(item.as_ref()) {
v.clone().into()
v.into()
} else {
IdentifiablePointer::none()
}
}
/// Removes the held item from the Pokemon. Returns the previously held item.
#[no_mangle]
extern "C" fn pokemon_remove_held_item(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Item>> {
if let Some(v) = ptr.as_ref().remove_held_item() {
v.clone().into()
v.into()
} else {
IdentifiablePointer::none()
}
}
/// Makes the Pokemon uses its held item.
#[no_mangle]
extern "C" fn pokemon_consume_held_item(ptr: ExternPointer<Arc<Pokemon>>) -> u8 {
if ptr.as_ref().consume_held_item() {
1
} else {
0
}
u8::from(ptr.as_ref().consume_held_item())
}
ffi_arc_getter!(Pokemon, current_health, u32);
@@ -129,6 +133,7 @@ ffi_arc_getter!(Pokemon, max_health, u32);
ffi_arc_getter!(Pokemon, weight, f32);
ffi_arc_getter!(Pokemon, height, f32);
/// An optional nickname of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_nickname(ptr: ExternPointer<Arc<Pokemon>>) -> *mut c_char {
let name = ptr.as_ref().nickname();
@@ -139,15 +144,13 @@ extern "C" fn pokemon_nickname(ptr: ExternPointer<Arc<Pokemon>>) -> *mut c_char
}
}
/// Whether the actual ability on the form is a hidden ability.
#[no_mangle]
extern "C" fn pokemon_real_ability_is_hidden(ptr: ExternPointer<Arc<Pokemon>>) -> u8 {
if ptr.as_ref().real_ability().hidden {
1
} else {
0
}
u8::from(ptr.as_ref().real_ability().hidden)
}
/// The index of the actual ability on the form.
#[no_mangle]
extern "C" fn pokemon_real_ability_index(ptr: ExternPointer<Arc<Pokemon>>) -> u8 {
ptr.as_ref().real_ability().index
@@ -155,6 +158,8 @@ extern "C" fn pokemon_real_ability_index(ptr: ExternPointer<Arc<Pokemon>>) -> u8
ffi_vec_value_getters!(Pokemon, types, TypeIdentifier);
/// Gets a learned move of the Pokemon. Index should generally be below [`MAX_MOVES`], you will get
/// a null pointer otherwise.
#[no_mangle]
extern "C" fn pokemon_learned_move_get(
ptr: ExternPointer<Arc<Pokemon>>,
@@ -167,21 +172,25 @@ extern "C" fn pokemon_learned_move_get(
}
}
/// The stats of the Pokemon when disregarding any stat boosts.
#[no_mangle]
extern "C" fn pokemon_flat_stats(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<StatisticSet<u32>> {
(ptr.as_ref().flat_stats() as *const StatisticSet<u32>).into()
}
/// The stats of the Pokemon including the stat boosts.
#[no_mangle]
extern "C" fn pokemon_boosted_stats(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<StatisticSet<u32>> {
(ptr.as_ref().boosted_stats() as *const StatisticSet<u32>).into()
}
/// Get the stat boosts for a specific stat.
#[no_mangle]
extern "C" fn pokemon_get_stat_boost(ptr: ExternPointer<Arc<Pokemon>>, statistic: Statistic) -> i8 {
ptr.as_ref().stat_boost(statistic)
}
/// Change a boosted stat by a certain amount.
#[no_mangle]
extern "C" fn pokemon_change_stat_boost(
ptr: ExternPointer<Arc<Pokemon>>,
@@ -189,35 +198,37 @@ extern "C" fn pokemon_change_stat_boost(
diff_amount: i8,
self_inflicted: u8,
) -> u8 {
if ptr.as_ref().change_stat_boost(stat, diff_amount, self_inflicted == 1) {
1
} else {
0
}
u8::from(ptr.as_ref().change_stat_boost(stat, diff_amount, self_inflicted == 1))
}
/// Gets a [individual value](https://bulbapedia.bulbagarden.net/wiki/Individual_values) of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_get_individual_value(ptr: ExternPointer<Arc<Pokemon>>, stat: Statistic) -> u8 {
ptr.as_ref().individual_values().get_stat(stat)
}
/// Modifies a [individual value](https://bulbapedia.bulbagarden.net/wiki/Individual_values) of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_set_individual_value(ptr: ExternPointer<Arc<Pokemon>>, stat: Statistic, value: u8) {
ptr.as_ref().individual_values().set_stat(stat, value);
ptr.as_ref().recalculate_flat_stats();
}
/// Gets a [effort value](https://bulbapedia.bulbagarden.net/wiki/Effort_values) of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_get_effort_value(ptr: ExternPointer<Arc<Pokemon>>, stat: Statistic) -> u8 {
ptr.as_ref().effort_values().get_stat(stat)
}
/// Modifies a [effort value](https://bulbapedia.bulbagarden.net/wiki/Effort_values) of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_set_effort_value(ptr: ExternPointer<Arc<Pokemon>>, stat: Statistic, value: u8) {
ptr.as_ref().effort_values().set_stat(stat, value);
ptr.as_ref().recalculate_flat_stats();
}
/// Gets the data for the battle the Pokemon is currently in. If the Pokemon is not in a battle, this
/// returns null.
#[no_mangle]
extern "C" fn pokemon_get_battle(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Battle> {
if let Some(v) = ptr.as_ref().get_battle() {
@@ -227,54 +238,59 @@ extern "C" fn pokemon_get_battle(ptr: ExternPointer<Arc<Pokemon>>) -> Identifiab
}
}
/// Get the index of the side of the battle the Pokemon is in. If the Pokemon
/// is not on the battlefield, this always returns 0.
#[no_mangle]
extern "C" fn pokemon_get_battle_side_index(ptr: ExternPointer<Arc<Pokemon>>) -> u8 {
ptr.as_ref().get_battle_side_index().unwrap_or_default()
}
/// Get the index of the slot on the side of the battle the Pokemon is in. If the Pokemon
// is not on the battlefield, this always returns 0.
#[no_mangle]
extern "C" fn pokemon_get_battle_index(ptr: ExternPointer<Arc<Pokemon>>) -> u8 {
ptr.as_ref().get_battle_index().unwrap_or_default()
}
/// Returns whether something overrides the ability.
#[no_mangle]
extern "C" fn pokemon_is_ability_overriden(ptr: ExternPointer<Arc<Pokemon>>) -> u8 {
if ptr.as_ref().is_ability_overriden() {
1
} else {
0
}
u8::from(ptr.as_ref().is_ability_overriden())
}
/// Returns the currently active ability.
#[no_mangle]
extern "C" fn pokemon_active_ability(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Ability> {
(ptr.as_ref().active_ability() as *const Ability).into()
}
/// Whether or not the Pokemon is allowed to gain experience.
#[no_mangle]
extern "C" fn pokemon_allowed_experience_gain(ptr: ExternPointer<Arc<Pokemon>>) -> u8 {
if ptr.as_ref().allowed_experience_gain() {
1
} else {
0
}
u8::from(ptr.as_ref().allowed_experience_gain())
}
/// The [nature](https://bulbapedia.bulbagarden.net/wiki/Nature) of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_nature(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Nature>> {
ptr.as_ref().nature().clone().into()
}
/// Calculates the flat stats on the Pokemon. This should be called when for example the base
/// stats, level, nature, IV, or EV changes. This has a side effect of recalculating the boosted
/// stats, as those depend on the flat stats.
#[no_mangle]
extern "C" fn pokemon_recalculate_flat_stats(ptr: ExternPointer<Arc<Pokemon>>) {
ptr.as_ref().recalculate_flat_stats()
}
/// Calculates the boosted stats on the Pokemon. This should be called when a stat boost changes.
#[no_mangle]
extern "C" fn pokemon_recalculate_boosted_stats(ptr: ExternPointer<Arc<Pokemon>>) {
ptr.as_ref().recalculate_boosted_stats()
}
/// Change the species of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_change_species(
ptr: ExternPointer<Arc<Pokemon>>,
@@ -285,48 +301,44 @@ extern "C" fn pokemon_change_species(
.change_species(species.as_ref().clone(), form.as_ref().clone())
}
/// Change the form of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_change_form(ptr: ExternPointer<Arc<Pokemon>>, form: ExternPointer<Arc<Form>>) {
ptr.as_ref().change_form(form.as_ref())
}
/// Whether or not the Pokemon is useable in a battle.
#[no_mangle]
extern "C" fn pokemon_is_usable(ptr: ExternPointer<Arc<Pokemon>>) -> u8 {
if ptr.as_ref().is_usable() {
1
} else {
0
}
u8::from(ptr.as_ref().is_usable())
}
/// Returns whether the Pokemon is fainted.
#[no_mangle]
extern "C" fn pokemon_is_fainted(ptr: ExternPointer<Arc<Pokemon>>) -> u8 {
if ptr.as_ref().is_fainted() {
1
} else {
0
}
u8::from(ptr.as_ref().is_fainted())
}
/// Whether or not the Pokemon is on the battlefield.
#[no_mangle]
extern "C" fn pokemon_is_on_battlefield(ptr: ExternPointer<Arc<Pokemon>>) -> u8 {
if ptr.as_ref().is_on_battlefield() {
1
} else {
0
}
u8::from(ptr.as_ref().is_on_battlefield())
}
/// Damages the Pokemon by a certain amount of damage, from a damage source.
#[no_mangle]
extern "C" fn pokemon_damage(ptr: ExternPointer<Arc<Pokemon>>, damage: u32, source: DamageSource) {
ptr.as_ref().damage(damage, source)
}
/// Heals the Pokemon by a specific amount. Unless allow_revive is set to true, this will not
/// heal if the Pokemon has 0 health. If the amount healed is 0, this will return false.
#[no_mangle]
extern "C" fn pokemon_heal(ptr: ExternPointer<Arc<Pokemon>>, amount: u32, allow_revive: u8) -> bool {
ptr.as_ref().heal(amount, allow_revive == 1)
}
/// Learn a move.
#[no_mangle]
extern "C" fn pokemon_learn_move(
ptr: ExternPointer<Arc<Pokemon>>,
@@ -336,6 +348,7 @@ extern "C" fn pokemon_learn_move(
unsafe { ptr.as_ref().learn_move(&CStr::from_ptr(move_name).into(), learn_method) }
}
/// Removes the current non-volatile status from the Pokemon.
#[no_mangle]
extern "C" fn pokemon_clear_status(ptr: ExternPointer<Arc<Pokemon>>) {
ptr.as_ref().clear_status()