PkmnLib_rs/src/ffi/static_data/ability.rs

65 lines
2.2 KiB
Rust
Raw Normal View History

use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
2022-11-27 16:47:51 +00:00
use crate::static_data::{Ability, AbilityImpl, EffectParameter};
2022-09-18 16:02:13 +00:00
use crate::StringKey;
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 ability.
2022-09-18 16:02:13 +00:00
#[no_mangle]
unsafe extern "C" fn ability_new(
name: *const c_char,
effect: *const c_char,
parameters: *const OwnedPtr<EffectParameter>,
parameters_length: usize,
2022-11-27 16:47:51 +00:00
) -> IdentifiablePointer<Arc<dyn Ability>> {
2022-09-18 16:02:13 +00:00
let parameters = std::slice::from_raw_parts(parameters, parameters_length);
let mut parameters_vec: Vec<EffectParameter> = Vec::with_capacity(parameters_length);
for parameter in parameters {
parameters_vec.push(*Box::from_raw(*parameter));
}
let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into();
let effect: StringKey = CStr::from_ptr(effect).to_str().unwrap().into();
2022-11-27 16:47:51 +00:00
let arc: Arc<dyn Ability> = Arc::new(AbilityImpl::new(&name, &effect, parameters_vec));
arc.into()
2022-09-18 16:02:13 +00:00
}
2022-10-14 14:53:30 +00:00
/// Drops a reference counted ability.
2022-09-18 16:02:13 +00:00
#[no_mangle]
2022-11-27 16:47:51 +00:00
unsafe extern "C" fn ability_drop(ptr: OwnedPtr<Arc<dyn Ability>>) {
2022-09-18 16:02:13 +00:00
drop_in_place(ptr)
}
2022-10-14 14:53:30 +00:00
/// The name of the ability.
2022-09-18 16:02:13 +00:00
#[no_mangle]
2022-11-27 16:47:51 +00:00
unsafe extern "C" fn ability_name(ptr: ExternPointer<Arc<dyn Ability>>) -> OwnedPtr<c_char> {
2022-09-18 16:02:13 +00:00
CString::new(ptr.as_ref().name().str()).unwrap().into_raw()
}
2022-10-14 14:53:30 +00:00
/// The name of the script effect of the ability.
2022-09-18 16:02:13 +00:00
#[no_mangle]
2022-11-27 16:47:51 +00:00
unsafe extern "C" fn ability_effect(ptr: ExternPointer<Arc<dyn Ability>>) -> OwnedPtr<c_char> {
2022-09-18 16:02:13 +00:00
CString::new(ptr.as_ref().effect().str()).unwrap().into_raw()
}
2022-10-14 14:53:30 +00:00
/// The length of the parameters for the script effect of the ability.
2022-09-18 16:02:13 +00:00
#[no_mangle]
2022-11-27 16:47:51 +00:00
unsafe extern "C" fn ability_parameter_length(ptr: ExternPointer<Arc<dyn Ability>>) -> usize {
2022-09-18 16:02:13 +00:00
ptr.as_ref().parameters().len()
}
2022-10-14 14:53:30 +00:00
/// Gets a parameter for the script effect of the ability.
2022-09-18 16:02:13 +00:00
#[no_mangle]
unsafe extern "C" fn ability_parameter_get(
2022-11-27 16:47:51 +00:00
ptr: ExternPointer<Arc<dyn Ability>>,
index: usize,
) -> IdentifiablePointer<EffectParameter> {
2022-09-18 16:02:13 +00:00
if let Some(p) = ptr.as_ref().parameters().get(index) {
(p as *const EffectParameter).into()
2022-09-18 16:02:13 +00:00
} else {
IdentifiablePointer::none()
2022-09-18 16:02:13 +00:00
}
}