PkmnLib_rs/src/ffi/static_data/form.rs

100 lines
3.3 KiB
Rust
Raw Normal View History

use crate::ffi::{
ffi_arc_getter, ffi_vec_stringkey_getters, ffi_vec_value_getters, BorrowedPtr, ExternPointer, IdentifiablePointer,
OwnedPtr,
};
2022-09-18 16:02:13 +00:00
use crate::static_data::{Form, LearnableMoves, StaticStatisticSet, TypeIdentifier};
use crate::StringKey;
use hashbrown::HashSet;
use std::ffi::{c_char, CStr, CString};
use std::ptr::drop_in_place;
use std::sync::Arc;
2022-09-18 16:02:13 +00:00
2022-10-14 14:53:30 +00:00
/// Instantiates a new form.
2022-09-18 16:02:13 +00:00
#[no_mangle]
unsafe extern "C" fn form_new(
name: *const c_char,
height: f32,
weight: f32,
base_experience: u32,
types: *const TypeIdentifier,
types_length: usize,
base_stats: OwnedPtr<StaticStatisticSet<u16>>,
abilities: *const BorrowedPtr<c_char>,
abilities_length: usize,
hidden_abilities: *const BorrowedPtr<c_char>,
hidden_abilities_length: usize,
moves: OwnedPtr<LearnableMoves>,
flags: *const *const c_char,
flags_length: usize,
) -> IdentifiablePointer<Arc<Form>> {
2022-09-18 16:02:13 +00:00
let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into();
let abilities = std::slice::from_raw_parts(abilities, abilities_length);
let mut abilities_vec = Vec::with_capacity(abilities_length);
for ability in abilities {
abilities_vec.push(CStr::from_ptr(*ability).into())
}
let hidden_abilities = std::slice::from_raw_parts(hidden_abilities, hidden_abilities_length);
let mut hidden_abilities_vec = Vec::with_capacity(hidden_abilities_length);
for ability in hidden_abilities {
hidden_abilities_vec.push(CStr::from_ptr(*ability).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());
}
Arc::new(Form::new(
2022-09-18 16:02:13 +00:00
&name,
height,
weight,
base_experience,
std::slice::from_raw_parts(types, types_length).to_vec(),
*Box::from_raw(base_stats),
abilities_vec,
hidden_abilities_vec,
*Box::from_raw(moves),
flags_set,
))
.into()
2022-09-18 16:02:13 +00:00
}
2022-10-14 14:53:30 +00:00
/// Drops a reference count for a form.
2022-09-18 16:02:13 +00:00
#[no_mangle]
unsafe extern "C" fn form_drop(ptr: OwnedPtr<Arc<Form>>) {
2022-09-18 16:02:13 +00:00
drop_in_place(ptr)
}
2022-10-14 14:53:30 +00:00
/// The name of the form.
2022-09-18 16:02:13 +00:00
#[no_mangle]
unsafe extern "C" fn form_name(ptr: ExternPointer<Arc<Form>>) -> OwnedPtr<c_char> {
2022-09-18 16:02:13 +00:00
let name = ptr.as_ref().name();
CString::new(name.str()).unwrap().into_raw()
}
ffi_arc_getter!(Form, height, f32);
ffi_arc_getter!(Form, weight, f32);
ffi_arc_getter!(Form, base_experience, u32);
2022-09-18 16:02:13 +00:00
ffi_vec_value_getters!(Form, types, TypeIdentifier);
2022-10-14 14:53:30 +00:00
/// The inherent values of a form of species that are used for the stats of a Pokemon.
#[no_mangle]
unsafe extern "C" fn form_base_stats(ptr: ExternPointer<Arc<Form>>) -> IdentifiablePointer<StaticStatisticSet<u16>> {
(ptr.as_ref().base_stats() as *const StaticStatisticSet<u16>).into()
}
2022-09-18 16:02:13 +00:00
ffi_vec_stringkey_getters!(Form, abilities);
ffi_vec_stringkey_getters!(Form, hidden_abilities);
ffi_arc_getter!(Form, moves, BorrowedPtr<LearnableMoves>);
2022-09-18 16:02:13 +00:00
2022-10-14 14:53:30 +00:00
/// Check if the form has a specific flag set.
2022-09-18 16:02:13 +00:00
#[no_mangle]
unsafe extern "C" fn form_has_flag(ptr: ExternPointer<Arc<Form>>, flag: *const c_char) -> u8 {
2022-09-18 16:02:13 +00:00
let flag = CStr::from_ptr(flag).into();
2022-10-14 14:53:30 +00:00
u8::from(ptr.as_ref().has_flag(&flag))
2022-09-18 16:02:13 +00:00
}