PkmnLib_rs/src/ffi/static_data/form.rs

117 lines
4.0 KiB
Rust
Raw Normal View History

use crate::ffi::{
2023-01-03 12:56:20 +00:00
ffi_arc_dyn_getter, ffi_vec_stringkey_getters, ffi_vec_value_getters, BorrowedPtr, ExternPointer,
IdentifiablePointer, NativeResult, OwnedPtr,
};
use crate::static_data::{Form, FormImpl, LearnableMoves, StaticStatisticSet, TypeIdentifier};
2022-09-18 16:02:13 +00:00
use crate::StringKey;
use anyhow::anyhow;
2022-09-18 16:02:13 +00:00
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,
2022-11-28 19:20:46 +00:00
moves: OwnedPtr<Box<dyn LearnableMoves>>,
2022-09-18 16:02:13 +00:00
flags: *const *const c_char,
flags_length: usize,
) -> NativeResult<IdentifiablePointer<Arc<dyn Form>>> {
let name: StringKey = match CStr::from_ptr(name).to_str() {
Ok(name) => name.into(),
Err(_) => return NativeResult::err(anyhow!("Unable to convert name to string")),
};
2022-09-18 16:02:13 +00:00
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 {
let flag = match CStr::from_ptr(*flag).to_str() {
Ok(flag) => flag,
Err(_) => return NativeResult::err(anyhow!("Unable to convert flag to string")),
};
flags_set.insert(flag.into());
2022-09-18 16:02:13 +00:00
}
let a: Arc<dyn Form> = Arc::new(FormImpl::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,
));
NativeResult::ok(a.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<dyn 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<dyn Form>>) -> NativeResult<OwnedPtr<c_char>> {
2022-09-18 16:02:13 +00:00
let name = ptr.as_ref().name();
match CString::new(name.str()) {
Ok(name) => NativeResult::ok(name.into_raw()),
Err(_) => NativeResult::err(anyhow!("Unable to convert name `{}` to CString", name.str())),
}
2022-09-18 16:02:13 +00:00
}
2023-01-03 12:56:20 +00:00
ffi_arc_dyn_getter!(Form, height, f32);
ffi_arc_dyn_getter!(Form, weight, f32);
ffi_arc_dyn_getter!(Form, base_experience, u32);
2022-09-18 16:02:13 +00:00
2023-01-03 12:56:20 +00:00
ffi_vec_value_getters!(Form, dyn Form, types, TypeIdentifier);
2022-09-18 16:02:13 +00:00
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<dyn Form>>,
) -> IdentifiablePointer<Arc<StaticStatisticSet<u16>>> {
ptr.as_ref().base_stats().clone().into()
}
2022-09-18 16:02:13 +00:00
2023-01-03 12:56:20 +00:00
ffi_vec_stringkey_getters!(Form, abilities);
ffi_vec_stringkey_getters!(Form, hidden_abilities);
2022-09-18 16:02:13 +00:00
2022-11-28 19:20:46 +00:00
/// The moves a Pokemon with this form can learn.
#[no_mangle]
extern "C" fn form_moves(ptr: ExternPointer<Arc<dyn Form>>) -> BorrowedPtr<Box<dyn LearnableMoves>> {
2022-11-28 19:20:46 +00:00
ptr.as_ref().moves() as *const Box<dyn 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<dyn 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
}