PkmnLib_rs/src/ffi/static_data/libraries/growth_rate_library.rs

54 lines
1.8 KiB
Rust

use crate::defines::LevelInt;
use crate::ffi::{BorrowedPtr, ExternPointer, IdentifiablePointer, NativeResult, OwnedPtr};
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<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<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<Box<dyn GrowthRateLibrary>>,
growth_rate: BorrowedPtr<c_char>,
experience: u32,
) -> 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.
#[no_mangle]
unsafe extern "C" fn growth_rate_library_calculate_experience(
ptr: ExternPointer<Box<dyn GrowthRateLibrary>>,
growth_rate: BorrowedPtr<c_char>,
level: LevelInt,
) -> NativeResult<u32> {
ptr.as_ref()
.calculate_experience(&CStr::from_ptr(growth_rate).into(), level)
.into()
}
/// 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<Box<dyn GrowthRateLibrary>>,
name: BorrowedPtr<c_char>,
growth_rate: OwnedPtr<Box<dyn GrowthRate>>,
) {
ptr.as_mut()
.add_growth_rate(&CStr::from_ptr(name).into(), *Box::from_raw(growth_rate));
}