Support for new error handling.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-04-15 14:34:42 +02:00
parent 3058739ea0
commit feffb5f030
36 changed files with 466 additions and 274 deletions

View File

@@ -2,7 +2,7 @@ use crate::dynamic_data::{
Battle, BattleParty, BattleRandom, BattleResult, BattleSide, DynamicLibrary, Pokemon, TurnChoice,
};
use crate::ffi::dynamic_data::models::native_event_hook::NativeEventHook;
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::ffi::{ExternPointer, IdentifiablePointer, NativeResult, OwnedPtr};
use std::ffi::{c_char, CStr, CString};
use std::sync::Arc;
@@ -191,20 +191,20 @@ extern "C" fn battle_try_set_choice(ptr: ExternPointer<Arc<Battle>>, choice: Own
/// Sets the current weather for the battle. If nullptr is passed, this clears the weather.
#[no_mangle]
extern "C" fn battle_set_weather(ptr: ExternPointer<Arc<Battle>>, weather: *const c_char) {
extern "C" fn battle_set_weather(ptr: ExternPointer<Arc<Battle>>, weather: *const c_char) -> NativeResult<()> {
if weather.is_null() {
ptr.as_ref().set_weather(None)
ptr.as_ref().set_weather(None).into()
} else {
unsafe { ptr.as_ref().set_weather(Some(CStr::from_ptr(weather).into())) }
unsafe { ptr.as_ref().set_weather(Some(CStr::from_ptr(weather).into())).into() }
}
}
/// Gets the current weather of the battle. If no weather is present, this returns nullptr.
#[no_mangle]
extern "C" fn battle_weather_name(ptr: ExternPointer<Arc<Battle>>) -> *mut c_char {
if let Some(w) = ptr.as_ref().weather_name() {
CString::new(w.str()).unwrap().into_raw()
} else {
std::ptr::null_mut()
extern "C" fn battle_weather_name(ptr: ExternPointer<Arc<Battle>>) -> NativeResult<*mut c_char> {
match ptr.as_ref().weather_name() {
Ok(Some(w)) => CString::new(w.str()).unwrap().into_raw().into(),
Ok(None) => std::ptr::null_mut::<c_char>().into(),
Err(e) => NativeResult::err(e),
}
}

View File

@@ -1,6 +1,6 @@
use crate::defines::LevelInt;
use crate::dynamic_data::{Battle, DamageSource, DynamicLibrary, LearnedMove, MoveLearnMethod, Pokemon};
use crate::ffi::{ffi_arc_getter, ffi_vec_value_getters, ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::ffi::{ffi_arc_getter, ffi_vec_value_getters, ExternPointer, IdentifiablePointer, NativeResult, OwnedPtr};
use crate::static_data::{
Ability, AbilityIndex, Form, Gender, Item, Nature, Species, Statistic, StatisticSet, TypeIdentifier,
};
@@ -21,9 +21,9 @@ extern "C" fn pokemon_new(
gender: Gender,
coloring: u8,
nature: *const c_char,
) -> IdentifiablePointer<Arc<Pokemon>> {
) -> NativeResult<IdentifiablePointer<Arc<Pokemon>>> {
let nature = unsafe { CStr::from_ptr(nature) }.into();
Arc::new(Pokemon::new(
let pokemon = Pokemon::new(
library.as_ref().clone(),
species.as_ref().clone(),
form.as_ref(),
@@ -36,8 +36,11 @@ extern "C" fn pokemon_new(
gender,
coloring,
&nature,
))
.into()
);
match pokemon {
Ok(pokemon) => NativeResult::ok(Arc::new(pokemon).into()),
Err(err) => NativeResult::err(err),
}
}
/// Drops an Arc reference held by the FFI.
@@ -327,8 +330,8 @@ extern "C" fn pokemon_is_on_battlefield(ptr: ExternPointer<Arc<Pokemon>>) -> u8
/// 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)
extern "C" fn pokemon_damage(ptr: ExternPointer<Arc<Pokemon>>, damage: u32, source: DamageSource) -> NativeResult<()> {
ptr.as_ref().damage(damage, source).into()
}
/// Heals the Pokemon by a specific amount. Unless allow_revive is set to true, this will not