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

54 lines
1.8 KiB
Rust
Raw Normal View History

2022-10-01 13:40:15 +00:00
use crate::defines::LevelInt;
2023-04-15 12:34:42 +00:00
use crate::ffi::{BorrowedPtr, ExternPointer, IdentifiablePointer, NativeResult, OwnedPtr};
2022-12-24 11:00:50 +00:00
use crate::static_data::{GrowthRate, GrowthRateLibrary, GrowthRateLibraryImpl};
2022-10-01 13:40:15 +00:00
use std::ffi::{c_char, CStr};
use std::ptr::drop_in_place;
2022-10-14 14:53:30 +00:00
/// Instantiates a new growth rate library with a capacity
2022-10-01 13:40:15 +00:00
#[no_mangle]
2022-12-24 11:00:50 +00:00
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()
2022-10-01 13:40:15 +00:00
}
2022-10-14 14:53:30 +00:00
/// Drops the growthrate library.
2022-10-01 13:40:15 +00:00
#[no_mangle]
2022-12-24 11:00:50 +00:00
unsafe extern "C" fn growth_rate_library_drop(ptr: OwnedPtr<Box<dyn GrowthRateLibrary>>) {
2022-10-01 13:40:15 +00:00
drop_in_place(ptr)
}
2022-10-14 14:53:30 +00:00
/// Calculates the level for a given growth key name and a certain experience.
2022-10-01 13:40:15 +00:00
#[no_mangle]
unsafe extern "C" fn growth_rate_library_calculate_level(
2022-12-24 11:00:50 +00:00
ptr: ExternPointer<Box<dyn GrowthRateLibrary>>,
2022-10-01 13:40:15 +00:00
growth_rate: BorrowedPtr<c_char>,
experience: u32,
) -> NativeResult<LevelInt> {
2022-10-01 13:40:15 +00:00
ptr.as_ref()
.calculate_level(&CStr::from_ptr(growth_rate).into(), experience)
.into()
2022-10-01 13:40:15 +00:00
}
2022-10-14 14:53:30 +00:00
/// Calculates the experience for a given growth key name and a certain level.
2022-10-01 13:40:15 +00:00
#[no_mangle]
unsafe extern "C" fn growth_rate_library_calculate_experience(
2022-12-24 11:00:50 +00:00
ptr: ExternPointer<Box<dyn GrowthRateLibrary>>,
2022-10-01 13:40:15 +00:00
growth_rate: BorrowedPtr<c_char>,
level: LevelInt,
2023-04-15 12:34:42 +00:00
) -> NativeResult<u32> {
2022-10-01 13:40:15 +00:00
ptr.as_ref()
.calculate_experience(&CStr::from_ptr(growth_rate).into(), level)
2023-04-15 12:34:42 +00:00
.into()
2022-10-01 13:40:15 +00:00
}
2022-10-14 14:53:30 +00:00
/// Adds a new growth rate with a name and value.
2022-10-01 13:40:15 +00:00
#[no_mangle]
unsafe extern "C" fn growth_rate_library_add_growth_rate(
2022-12-24 11:00:50 +00:00
mut ptr: ExternPointer<Box<dyn GrowthRateLibrary>>,
2022-10-01 13:40:15 +00:00
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));
}