PkmnLib_rs/src/ffi/static_data/species.rs

103 lines
3.2 KiB
Rust
Raw Normal View History

use crate::ffi::ffi_handle::FFIHandle;
2023-01-03 12:56:20 +00:00
use crate::ffi::{
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};
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,
name: NonOwnedPtrString,
2022-09-18 16:02:13 +00:00
gender_rate: f32,
growth_rate: NonOwnedPtrString,
2022-09-18 16:02:13 +00:00
capture_rate: u8,
default_form: FFIHandle<Arc<dyn Form>>,
2022-09-18 16:02:13 +00:00
flags: *const *const c_char,
flags_length: usize,
) -> FFIResult<FFIHandle<Arc<dyn Species>>> {
let name: StringKey = match CStr::from_ptr(name).to_str() {
Ok(name) => name.into(),
Err(_) => return FFIResult::err(PkmnError::InvalidCString.into()),
};
let growth_rate: StringKey = match CStr::from_ptr(growth_rate).to_str() {
Ok(growth_rate) => growth_rate.into(),
Err(_) => return FFIResult::err(PkmnError::InvalidCString.into()),
};
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 {
let flag = match CStr::from_ptr(*flag).to_str() {
Ok(flag) => flag,
Err(_) => return FFIResult::err(PkmnError::InvalidCString.into()),
};
flags_set.insert(flag.into());
2022-09-18 16:02:13 +00:00
}
let default_form = if default_form.is_none() {
return FFIResult::err(PkmnError::NullReference.into());
} else {
default_form.from_ffi_handle()
};
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-07-29 10:57:52 +00:00
120,
default_form.clone(),
2022-09-18 16:02:13 +00:00
flags_set,
2023-07-29 10:57:52 +00:00
Vec::new(),
));
FFIResult::ok(FFIHandle::get_handle(a.into()))
2022-09-18 16:02:13 +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(
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()
};
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]
unsafe extern "C" fn species_get_form(
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 {
FFIHandle::get_handle(form.into())
2022-09-18 16:02:13 +00:00
} else {
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]
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);
species.from_ffi_handle().get_random_gender(&mut rand)
2023-04-02 12:17:30 +00:00
}