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> { let b: Box = Box::new(GrowthRateLibraryImpl::new(capacity)); b.into() } /// Drops the growthrate library. #[no_mangle] unsafe extern "C" fn growth_rate_library_drop(ptr: OwnedPtr>) { 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>, growth_rate: BorrowedPtr, experience: u32, ) -> LevelInt { ptr.as_ref() .calculate_level(&CStr::from_ptr(growth_rate).into(), experience) } /// 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>, growth_rate: BorrowedPtr, level: LevelInt, ) -> NativeResult { 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>, name: BorrowedPtr, growth_rate: OwnedPtr>, ) { ptr.as_mut() .add_growth_rate(&CStr::from_ptr(name).into(), *Box::from_raw(growth_rate)); }