2022-10-01 13:40:15 +00:00
|
|
|
use crate::defines::LevelInt;
|
2023-06-24 12:44:23 +00:00
|
|
|
use crate::ffi::ffi_handle::{FFIHandle, FromFFIHandle};
|
|
|
|
use crate::ffi::{FFIResult, NonOwnedPtrString};
|
2022-12-24 11:00:50 +00:00
|
|
|
use crate::static_data::{GrowthRate, GrowthRateLibrary, GrowthRateLibraryImpl};
|
2023-06-24 12:44:23 +00:00
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::sync::Arc;
|
2022-10-01 13:40:15 +00:00
|
|
|
|
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]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn growth_rate_library_new(capacity: usize) -> FFIHandle<Arc<dyn GrowthRateLibrary>> {
|
|
|
|
let b: Arc<dyn GrowthRateLibrary> = Arc::new(GrowthRateLibraryImpl::new(capacity));
|
|
|
|
FFIHandle::get_handle(b.into())
|
2022-10-01 13:40:15 +00:00
|
|
|
}
|
|
|
|
|
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(
|
2023-06-24 12:44:23 +00:00
|
|
|
ptr: FFIHandle<Arc<dyn GrowthRateLibrary>>,
|
|
|
|
growth_rate: NonOwnedPtrString,
|
2022-10-01 13:40:15 +00:00
|
|
|
experience: u32,
|
2023-06-24 12:44:23 +00:00
|
|
|
) -> FFIResult<LevelInt> {
|
|
|
|
ptr.from_ffi_handle()
|
2022-10-01 13:40:15 +00:00
|
|
|
.calculate_level(&CStr::from_ptr(growth_rate).into(), experience)
|
2023-04-19 16:44:11 +00:00
|
|
|
.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(
|
2023-06-24 12:44:23 +00:00
|
|
|
ptr: FFIHandle<Arc<dyn GrowthRateLibrary>>,
|
|
|
|
growth_rate: NonOwnedPtrString,
|
2022-10-01 13:40:15 +00:00
|
|
|
level: LevelInt,
|
2023-06-24 12:44:23 +00:00
|
|
|
) -> FFIResult<u32> {
|
|
|
|
ptr.from_ffi_handle()
|
2022-10-01 13:40:15 +00:00
|
|
|
.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(
|
2023-06-24 12:44:23 +00:00
|
|
|
ptr: FFIHandle<Arc<dyn GrowthRateLibrary>>,
|
|
|
|
name: NonOwnedPtrString,
|
|
|
|
growth_rate: FFIHandle<Arc<dyn GrowthRate>>,
|
2022-10-01 13:40:15 +00:00
|
|
|
) {
|
2023-06-24 12:44:23 +00:00
|
|
|
ptr.from_ffi_handle()
|
|
|
|
.add_growth_rate(&CStr::from_ptr(name).into(), growth_rate.from_ffi_handle());
|
2022-10-01 13:40:15 +00:00
|
|
|
}
|