2023-06-24 12:44:23 +00:00
|
|
|
use crate::ffi::ffi_handle::FFIHandle;
|
2023-01-03 12:56:20 +00:00
|
|
|
use crate::ffi::{
|
2023-06-24 12:44:23 +00:00
|
|
|
ffi_handle_arc_dyn_getter, ffi_handle_arc_stringkey_getter, FFIResult, FromFFIHandle, NonOwnedPtrString,
|
2023-01-03 12:56:20 +00:00
|
|
|
};
|
2023-04-02 12:17:30 +00:00
|
|
|
use crate::static_data::{Form, Gender, Species, SpeciesImpl};
|
2023-04-16 17:57:21 +00:00
|
|
|
use crate::{PkmnError, Random, StringKey};
|
2022-09-18 16:02:13 +00:00
|
|
|
use hashbrown::HashSet;
|
|
|
|
use std::ffi::{c_char, CStr};
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Creates a new species.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn species_new(
|
|
|
|
id: u16,
|
2023-06-24 12:44:23 +00:00
|
|
|
name: NonOwnedPtrString,
|
2022-09-18 16:02:13 +00:00
|
|
|
gender_rate: f32,
|
2023-06-24 12:44:23 +00:00
|
|
|
growth_rate: NonOwnedPtrString,
|
2022-09-18 16:02:13 +00:00
|
|
|
capture_rate: u8,
|
2023-06-24 12:44:23 +00:00
|
|
|
default_form: FFIHandle<Arc<dyn Form>>,
|
2022-09-18 16:02:13 +00:00
|
|
|
flags: *const *const c_char,
|
|
|
|
flags_length: usize,
|
2023-06-24 12:44:23 +00:00
|
|
|
) -> FFIResult<FFIHandle<Arc<dyn Species>>> {
|
2023-04-16 17:57:21 +00:00
|
|
|
let name: StringKey = match CStr::from_ptr(name).to_str() {
|
|
|
|
Ok(name) => name.into(),
|
2023-06-24 12:44:23 +00:00
|
|
|
Err(_) => return FFIResult::err(PkmnError::InvalidCString.into()),
|
2023-04-16 17:57:21 +00:00
|
|
|
};
|
|
|
|
let growth_rate: StringKey = match CStr::from_ptr(growth_rate).to_str() {
|
|
|
|
Ok(growth_rate) => growth_rate.into(),
|
2023-06-24 12:44:23 +00:00
|
|
|
Err(_) => return FFIResult::err(PkmnError::InvalidCString.into()),
|
2023-04-16 17:57:21 +00:00
|
|
|
};
|
2022-09-18 16:02:13 +00:00
|
|
|
|
|
|
|
let flags = std::slice::from_raw_parts(flags, flags_length);
|
|
|
|
let mut flags_set: HashSet<StringKey> = HashSet::with_capacity(flags_length);
|
|
|
|
for flag in flags {
|
2023-04-16 17:57:21 +00:00
|
|
|
let flag = match CStr::from_ptr(*flag).to_str() {
|
|
|
|
Ok(flag) => flag,
|
2023-06-24 12:44:23 +00:00
|
|
|
Err(_) => return FFIResult::err(PkmnError::InvalidCString.into()),
|
2023-04-16 17:57:21 +00:00
|
|
|
};
|
|
|
|
flags_set.insert(flag.into());
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
2023-06-24 12:44:23 +00:00
|
|
|
let default_form = if default_form.is_none() {
|
|
|
|
return FFIResult::err(PkmnError::NullReference.into());
|
|
|
|
} else {
|
|
|
|
default_form.from_ffi_handle()
|
2023-04-16 17:57:21 +00:00
|
|
|
};
|
|
|
|
|
2022-11-28 20:34:28 +00:00
|
|
|
let a: Arc<dyn Species> = Arc::new(SpeciesImpl::new(
|
2022-09-18 16:02:13 +00:00
|
|
|
id,
|
|
|
|
&name,
|
|
|
|
gender_rate,
|
|
|
|
&growth_rate,
|
|
|
|
capture_rate,
|
2023-04-16 17:57:21 +00:00
|
|
|
default_form.clone(),
|
2022-09-18 16:02:13 +00:00
|
|
|
flags_set,
|
2022-11-28 20:34:28 +00:00
|
|
|
));
|
2023-06-24 12:44:23 +00:00
|
|
|
FFIResult::ok(FFIHandle::get_handle(a.into()))
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
ffi_handle_arc_dyn_getter!(Species, id, u16);
|
|
|
|
ffi_handle_arc_stringkey_getter!(Species, name);
|
|
|
|
ffi_handle_arc_dyn_getter!(Species, gender_rate, f32);
|
|
|
|
ffi_handle_arc_stringkey_getter!(Species, growth_rate);
|
|
|
|
ffi_handle_arc_dyn_getter!(Species, capture_rate, u8);
|
2022-09-18 16:02:13 +00:00
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Adds a new form to the species.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn species_add_form(
|
2023-06-24 12:44:23 +00:00
|
|
|
species: FFIHandle<Arc<dyn Species>>,
|
|
|
|
name: NonOwnedPtrString,
|
|
|
|
form: FFIHandle<Arc<dyn Form>>,
|
|
|
|
) -> FFIResult<()> {
|
|
|
|
let form = if form.is_none() {
|
|
|
|
return FFIResult::err(PkmnError::NullReference.into());
|
|
|
|
} else {
|
|
|
|
form.from_ffi_handle()
|
2023-04-16 17:57:21 +00:00
|
|
|
};
|
2023-06-24 12:44:23 +00:00
|
|
|
species.from_ffi_handle().add_form(CStr::from_ptr(name).into(), form);
|
|
|
|
FFIResult::ok(())
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Gets a form by name.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2022-10-08 11:15:04 +00:00
|
|
|
unsafe extern "C" fn species_get_form(
|
2023-06-24 12:44:23 +00:00
|
|
|
species: FFIHandle<Arc<dyn Species>>,
|
|
|
|
name: NonOwnedPtrString,
|
|
|
|
) -> FFIHandle<Arc<dyn Form>> {
|
|
|
|
let form = species.from_ffi_handle().get_form(&CStr::from_ptr(name).into());
|
2022-09-18 16:02:13 +00:00
|
|
|
if let Some(form) = form {
|
2023-06-24 12:44:23 +00:00
|
|
|
FFIHandle::get_handle(form.into())
|
2022-09-18 16:02:13 +00:00
|
|
|
} else {
|
2023-06-24 12:44:23 +00:00
|
|
|
FFIHandle::none()
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-02 12:17:30 +00:00
|
|
|
|
|
|
|
/// Gets a form by name.
|
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
unsafe extern "C" fn species_get_random_gender(species: FFIHandle<Arc<dyn Species>>, seed: u64) -> Gender {
|
2023-04-02 12:17:30 +00:00
|
|
|
let mut rand = Random::new(seed as u128);
|
2023-06-24 12:44:23 +00:00
|
|
|
species.from_ffi_handle().get_random_gender(&mut rand)
|
2023-04-02 12:17:30 +00:00
|
|
|
}
|