PkmnLib_rs/src/ffi/static_data/species.rs

86 lines
2.6 KiB
Rust
Raw Normal View History

2023-01-03 12:56:20 +00:00
use crate::ffi::{
ffi_arc_dyn_getter, ffi_arc_stringkey_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr,
};
2023-04-02 12:17:30 +00:00
use crate::static_data::{Form, Gender, Species, SpeciesImpl};
use crate::{Random, StringKey};
2022-09-18 16:02:13 +00:00
use hashbrown::HashSet;
use std::ffi::{c_char, CStr};
2022-09-22 15:33:45 +00:00
use std::ptr::drop_in_place;
2022-09-18 16:02:13 +00:00
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: BorrowedPtr<c_char>,
gender_rate: f32,
growth_rate: BorrowedPtr<c_char>,
capture_rate: u8,
default_form: OwnedPtr<Arc<dyn Form>>,
2022-09-18 16:02:13 +00:00
flags: *const *const c_char,
flags_length: usize,
) -> IdentifiablePointer<Arc<dyn Species>> {
2022-09-18 16:02:13 +00:00
let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into();
let growth_rate: StringKey = CStr::from_ptr(growth_rate).to_str().unwrap().into();
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 {
flags_set.insert(CStr::from_ptr(*flag).to_str().unwrap().into());
}
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,
default_form.as_ref().unwrap().clone(),
2022-09-18 16:02:13 +00:00
flags_set,
));
a.into()
2022-09-18 16:02:13 +00:00
}
2022-10-14 14:53:30 +00:00
/// Drop a reference to the species.
2022-09-22 15:33:45 +00:00
#[no_mangle]
unsafe extern "C" fn species_drop(ptr: OwnedPtr<Arc<dyn Species>>) {
2022-09-22 15:33:45 +00:00
drop_in_place(ptr);
}
2023-01-03 12:56:20 +00:00
ffi_arc_dyn_getter!(Species, id, u16);
ffi_arc_stringkey_getter!(Species, name);
ffi_arc_dyn_getter!(Species, gender_rate, f32);
ffi_arc_stringkey_getter!(Species, growth_rate);
ffi_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(
mut species: ExternPointer<Arc<dyn Species>>,
2022-09-18 16:02:13 +00:00
name: BorrowedPtr<c_char>,
form: OwnedPtr<Arc<dyn Form>>,
2022-09-18 16:02:13 +00:00
) {
let form = form.as_ref().unwrap().clone();
2022-09-18 16:02:13 +00:00
species.as_mut().add_form(CStr::from_ptr(name).into(), form)
}
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: ExternPointer<Arc<dyn Species>>,
name: BorrowedPtr<c_char>,
) -> IdentifiablePointer<Arc<dyn Form>> {
2022-09-18 16:02:13 +00:00
let form = species.as_ref().get_form(&CStr::from_ptr(name).into());
if let Some(form) = form {
form.into()
2022-09-18 16:02:13 +00:00
} else {
IdentifiablePointer::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: ExternPointer<Arc<dyn Species>>, seed: u64) -> Gender {
let mut rand = Random::new(seed as u128);
species.as_ref().get_random_gender(&mut rand)
}