Replace most panics in the core library with results
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-04-19 18:44:11 +02:00
parent 00d596d656
commit c0e4702e45
33 changed files with 222 additions and 102 deletions

View File

@@ -1,5 +1,5 @@
use crate::defines::LevelInt;
use crate::ffi::{BorrowedPtr, ExternPointer, OwnedPtr};
use crate::ffi::{BorrowedPtr, ExternPointer, NativeResult, OwnedPtr};
use crate::static_data::{LearnableMoves, LearnableMovesImpl};
use std::ffi::{c_char, CStr};
use std::ptr::drop_in_place;
@@ -22,6 +22,8 @@ unsafe extern "C" fn learnable_moves_add_level_move(
mut ptr: ExternPointer<Box<dyn LearnableMoves>>,
level: LevelInt,
move_name: BorrowedPtr<c_char>,
) {
ptr.as_mut().add_level_move(level, &CStr::from_ptr(move_name).into())
) -> NativeResult<()> {
ptr.as_mut()
.add_level_move(level, &CStr::from_ptr(move_name).into())
.into()
}

View File

@@ -23,9 +23,10 @@ unsafe extern "C" fn growth_rate_library_calculate_level(
ptr: ExternPointer<Box<dyn GrowthRateLibrary>>,
growth_rate: BorrowedPtr<c_char>,
experience: u32,
) -> LevelInt {
) -> NativeResult<LevelInt> {
ptr.as_ref()
.calculate_level(&CStr::from_ptr(growth_rate).into(), experience)
.into()
}
/// Calculates the experience for a given growth key name and a certain level.

View File

@@ -54,9 +54,12 @@ unsafe extern "C" fn nature_library_get_nature(
unsafe extern "C" fn nature_library_get_random_nature(
ptr: ExternPointer<Box<dyn NatureLibrary>>,
seed: u64,
) -> IdentifiablePointer<Arc<dyn Nature>> {
) -> NativeResult<IdentifiablePointer<Arc<dyn Nature>>> {
let mut rand = Random::new(seed as u128);
ptr.as_ref().get_random_nature(&mut rand).into()
match ptr.as_ref().get_random_nature(&mut rand) {
Ok(nature) => NativeResult::ok(nature.into()),
Err(e) => NativeResult::err(e),
}
}
/// Finds a nature name by nature.
@@ -68,9 +71,12 @@ unsafe extern "C" fn nature_library_get_nature_name(
match nature.as_ref() {
Some(nature) => {
let name = ptr.as_ref().get_nature_name(nature);
match CString::new(name.str()) {
Ok(cstr) => NativeResult::ok(cstr.into_raw()),
Err(_) => NativeResult::err(anyhow!("Failed to convert nature name to C string")),
match name {
Ok(name) => match CString::new(name.str()) {
Ok(cstr) => NativeResult::ok(cstr.into_raw()),
Err(_) => NativeResult::err(anyhow!("Failed to convert nature name to C string")),
},
Err(e) => NativeResult::err(e),
}
}
None => NativeResult::err(PkmnError::NullReference.into()),

View File

@@ -58,8 +58,8 @@ extern "C" fn type_library_get_single_effectiveness(
ptr: ExternPointer<Box<dyn TypeLibrary>>,
attacking: TypeIdentifier,
defending: TypeIdentifier,
) -> f32 {
ptr.as_ref().get_single_effectiveness(attacking, defending)
) -> NativeResult<f32> {
ptr.as_ref().get_single_effectiveness(attacking, defending).into()
}
/// Gets the effectiveness for a single attacking type against an amount of defending types.
@@ -71,9 +71,9 @@ unsafe extern "C" fn type_library_get_effectiveness(
attacking: TypeIdentifier,
defending: OwnedPtr<TypeIdentifier>,
defending_length: usize,
) -> f32 {
) -> NativeResult<f32> {
let v = std::slice::from_raw_parts(defending, defending_length);
ptr.as_ref().get_effectiveness(attacking, v)
ptr.as_ref().get_effectiveness(attacking, v).into()
}
/// Registers a new type in the library.
@@ -92,6 +92,8 @@ unsafe extern "C" fn type_library_set_effectiveness(
attacking: TypeIdentifier,
defending: TypeIdentifier,
effectiveness: f32,
) {
ptr.as_mut().set_effectiveness(attacking, defending, effectiveness);
) -> NativeResult<()> {
ptr.as_mut()
.set_effectiveness(attacking, defending, effectiveness)
.into()
}