Loads of work to replace panics with results.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-04-15 19:33:29 +02:00
parent 2849cad57b
commit 6f1880c768
25 changed files with 547 additions and 309 deletions

View File

@@ -1,5 +1,5 @@
use crate::dynamic_data::{BattleStatCalculator, Gen7BattleStatCalculator, Pokemon};
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::ffi::{ExternPointer, IdentifiablePointer, NativeResult, OwnedPtr};
use crate::static_data::{Statistic, StatisticSet};
use std::ptr::drop_in_place;
use std::sync::Arc;
@@ -25,8 +25,10 @@ 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());
) -> NativeResult<()> {
ptr.as_ref()
.calculate_flat_stats(pokemon.as_ref(), stats.as_mut())
.into()
}
/// Calculate a single flat stat of a Pokemon, disregarding stat boost
@@ -35,8 +37,8 @@ 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)
) -> NativeResult<u32> {
ptr.as_ref().calculate_flat_stat(pokemon.as_ref(), stat).into()
}
/// Calculate all the boosted stats of a Pokemon, including stat boosts.
@@ -45,8 +47,10 @@ 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());
) -> NativeResult<()> {
ptr.as_ref()
.calculate_boosted_stats(pokemon.as_ref(), stats.as_mut())
.into()
}
/// Calculate a single boosted stat of a Pokemon, including stat boosts.
@@ -55,6 +59,6 @@ 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)
) -> NativeResult<u32> {
ptr.as_ref().calculate_boosted_stat(pokemon.as_ref(), stat).into()
}

View File

@@ -1,5 +1,5 @@
use crate::dynamic_data::{LearnedMove, MoveLearnMethod};
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::ffi::{ExternPointer, IdentifiablePointer, NativeResult, OwnedPtr};
use crate::static_data::MoveData;
use std::ptr::drop_in_place;
use std::sync::Arc;
@@ -60,6 +60,6 @@ extern "C" fn learned_move_restore_all_uses(learned_move: ExternPointer<Arc<Lear
/// Restore the remaining PP by a certain amount. Will prevent it from going above max PP.
#[no_mangle]
extern "C" fn learned_move_restore_uses(learned_move: ExternPointer<Arc<LearnedMove>>, amount: u8) {
learned_move.as_ref().restore_uses(amount);
extern "C" fn learned_move_restore_uses(learned_move: ExternPointer<Arc<LearnedMove>>, amount: u8) -> NativeResult<()> {
learned_move.as_ref().restore_uses(amount).into()
}

View File

@@ -127,8 +127,11 @@ extern "C" fn pokemon_remove_held_item(ptr: ExternPointer<Arc<Pokemon>>) -> Iden
/// Makes the Pokemon uses its held item.
#[no_mangle]
extern "C" fn pokemon_consume_held_item(ptr: ExternPointer<Arc<Pokemon>>) -> u8 {
u8::from(ptr.as_ref().consume_held_item())
extern "C" fn pokemon_consume_held_item(ptr: ExternPointer<Arc<Pokemon>>) -> NativeResult<u8> {
match ptr.as_ref().consume_held_item() {
Ok(v) => NativeResult::ok(u8::from(v)),
Err(err) => NativeResult::err(err),
}
}
ffi_arc_getter!(Pokemon, current_health, u32);
@@ -200,8 +203,11 @@ extern "C" fn pokemon_change_stat_boost(
stat: Statistic,
diff_amount: i8,
self_inflicted: u8,
) -> u8 {
u8::from(ptr.as_ref().change_stat_boost(stat, diff_amount, self_inflicted == 1))
) -> NativeResult<u8> {
match ptr.as_ref().change_stat_boost(stat, diff_amount, self_inflicted == 1) {
Ok(v) => NativeResult::ok(u8::from(v)),
Err(e) => NativeResult::err(e),
}
}
/// Gets a [individual value](https://bulbapedia.bulbagarden.net/wiki/Individual_values) of the Pokemon.
@@ -212,9 +218,13 @@ extern "C" fn pokemon_get_individual_value(ptr: ExternPointer<Arc<Pokemon>>, sta
/// 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) {
extern "C" fn pokemon_set_individual_value(
ptr: ExternPointer<Arc<Pokemon>>,
stat: Statistic,
value: u8,
) -> NativeResult<()> {
ptr.as_ref().individual_values().set_stat(stat, value);
ptr.as_ref().recalculate_flat_stats();
ptr.as_ref().recalculate_flat_stats().into()
}
/// Gets a [effort value](https://bulbapedia.bulbagarden.net/wiki/Effort_values) of the Pokemon.
@@ -225,9 +235,13 @@ extern "C" fn pokemon_get_effort_value(ptr: ExternPointer<Arc<Pokemon>>, stat: S
/// 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) {
extern "C" fn pokemon_set_effort_value(
ptr: ExternPointer<Arc<Pokemon>>,
stat: Statistic,
value: u8,
) -> NativeResult<()> {
ptr.as_ref().effort_values().set_stat(stat, value);
ptr.as_ref().recalculate_flat_stats();
ptr.as_ref().recalculate_flat_stats().into()
}
/// Gets the data for the battle the Pokemon is currently in. If the Pokemon is not in a battle, this
@@ -263,8 +277,13 @@ extern "C" fn pokemon_is_ability_overriden(ptr: ExternPointer<Arc<Pokemon>>) ->
/// Returns the currently active ability.
#[no_mangle]
extern "C" fn pokemon_active_ability(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn Ability>> {
ptr.as_ref().active_ability().into()
extern "C" fn pokemon_active_ability(
ptr: ExternPointer<Arc<Pokemon>>,
) -> NativeResult<IdentifiablePointer<Arc<dyn Ability>>> {
match ptr.as_ref().active_ability() {
Ok(v) => NativeResult::ok(v.clone().into()),
Err(e) => NativeResult::err(e),
}
}
/// Whether or not the Pokemon is allowed to gain experience.
@@ -283,14 +302,14 @@ extern "C" fn pokemon_nature(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePo
/// 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()
extern "C" fn pokemon_recalculate_flat_stats(ptr: ExternPointer<Arc<Pokemon>>) -> NativeResult<()> {
ptr.as_ref().recalculate_flat_stats().into()
}
/// 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()
extern "C" fn pokemon_recalculate_boosted_stats(ptr: ExternPointer<Arc<Pokemon>>) -> NativeResult<()> {
ptr.as_ref().recalculate_boosted_stats().into()
}
/// Change the species of the Pokemon.
@@ -299,15 +318,19 @@ extern "C" fn pokemon_change_species(
ptr: ExternPointer<Arc<Pokemon>>,
species: ExternPointer<Arc<dyn Species>>,
form: ExternPointer<Arc<dyn Form>>,
) {
) -> NativeResult<()> {
ptr.as_ref()
.change_species(species.as_ref().clone(), form.as_ref().clone())
.into()
}
/// Change the form of the Pokemon.
#[no_mangle]
extern "C" fn pokemon_change_form(ptr: ExternPointer<Arc<Pokemon>>, form: ExternPointer<Arc<dyn Form>>) {
ptr.as_ref().change_form(form.as_ref())
extern "C" fn pokemon_change_form(
ptr: ExternPointer<Arc<Pokemon>>,
form: ExternPointer<Arc<dyn Form>>,
) -> NativeResult<()> {
ptr.as_ref().change_form(form.as_ref()).into()
}
/// Whether or not the Pokemon is useable in a battle.
@@ -347,8 +370,12 @@ extern "C" fn pokemon_learn_move(
ptr: ExternPointer<Arc<Pokemon>>,
move_name: *const c_char,
learn_method: MoveLearnMethod,
) {
unsafe { ptr.as_ref().learn_move(&CStr::from_ptr(move_name).into(), learn_method) }
) -> NativeResult<()> {
unsafe {
ptr.as_ref()
.learn_move(&CStr::from_ptr(move_name).into(), learn_method)
.into()
}
}
/// Removes the current non-volatile status from the Pokemon.