Rework of FFI, adding a value identifier, so we can keep knowledge of data even when data moves.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
use crate::ffi::{ffi_getter, BorrowedPtr, ExternPointer, OwnedPtr};
|
||||
use crate::ffi::{ffi_arc_getter, ffi_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
|
||||
use crate::static_data::{EffectParameter, MoveCategory, MoveData, MoveTarget, SecondaryEffect, TypeIdentifier};
|
||||
use crate::StringKey;
|
||||
use hashbrown::HashSet;
|
||||
use std::ffi::{c_char, CStr, CString};
|
||||
use std::ptr::drop_in_place;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[no_mangle]
|
||||
unsafe extern "C" fn move_data_new(
|
||||
@@ -18,7 +19,7 @@ unsafe extern "C" fn move_data_new(
|
||||
secondary_effect: *mut SecondaryEffect,
|
||||
flags: *const *const c_char,
|
||||
flags_length: usize,
|
||||
) -> OwnedPtr<MoveData> {
|
||||
) -> IdentifiablePointer<Arc<MoveData>> {
|
||||
let flags = std::slice::from_raw_parts(flags, flags_length);
|
||||
let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into();
|
||||
let mut flags_set: HashSet<StringKey> = HashSet::with_capacity(flags_length);
|
||||
@@ -30,7 +31,7 @@ unsafe extern "C" fn move_data_new(
|
||||
} else {
|
||||
Some(*Box::from_raw(secondary_effect))
|
||||
};
|
||||
Box::into_raw(Box::new(MoveData::new(
|
||||
Arc::new(MoveData::new(
|
||||
&name,
|
||||
move_type,
|
||||
category,
|
||||
@@ -41,40 +42,43 @@ unsafe extern "C" fn move_data_new(
|
||||
priority,
|
||||
secondary_effect,
|
||||
flags_set,
|
||||
)))
|
||||
))
|
||||
.into()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
unsafe extern "C" fn move_data_drop(ptr: OwnedPtr<MoveData>) {
|
||||
unsafe extern "C" fn move_data_drop(ptr: OwnedPtr<Arc<MoveData>>) {
|
||||
drop_in_place(ptr)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
unsafe extern "C" fn move_data_name(ptr: ExternPointer<MoveData>) -> OwnedPtr<c_char> {
|
||||
unsafe extern "C" fn move_data_name(ptr: ExternPointer<Arc<MoveData>>) -> OwnedPtr<c_char> {
|
||||
let name = ptr.as_ref().name();
|
||||
CString::new(name.str()).unwrap().into_raw()
|
||||
}
|
||||
|
||||
ffi_getter!(MoveData, move_type, TypeIdentifier);
|
||||
ffi_getter!(MoveData, category, MoveCategory);
|
||||
ffi_getter!(MoveData, base_power, u8);
|
||||
ffi_getter!(MoveData, accuracy, u8);
|
||||
ffi_getter!(MoveData, base_usages, u8);
|
||||
ffi_getter!(MoveData, target, MoveTarget);
|
||||
ffi_getter!(MoveData, priority, i8);
|
||||
ffi_arc_getter!(MoveData, move_type, TypeIdentifier);
|
||||
ffi_arc_getter!(MoveData, category, MoveCategory);
|
||||
ffi_arc_getter!(MoveData, base_power, u8);
|
||||
ffi_arc_getter!(MoveData, accuracy, u8);
|
||||
ffi_arc_getter!(MoveData, base_usages, u8);
|
||||
ffi_arc_getter!(MoveData, target, MoveTarget);
|
||||
ffi_arc_getter!(MoveData, priority, i8);
|
||||
|
||||
#[no_mangle]
|
||||
unsafe extern "C" fn move_data_secondary_effect(ptr: ExternPointer<MoveData>) -> BorrowedPtr<SecondaryEffect> {
|
||||
unsafe extern "C" fn move_data_secondary_effect(
|
||||
ptr: ExternPointer<Arc<MoveData>>,
|
||||
) -> IdentifiablePointer<SecondaryEffect> {
|
||||
let effect = ptr.as_ref().secondary_effect();
|
||||
if let Some(v) = effect {
|
||||
v as *const SecondaryEffect
|
||||
(v as *const SecondaryEffect).into()
|
||||
} else {
|
||||
std::ptr::null()
|
||||
IdentifiablePointer::none()
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
unsafe extern "C" fn move_data_has_flag(ptr: ExternPointer<MoveData>, flag: *const c_char) -> u8 {
|
||||
unsafe extern "C" fn move_data_has_flag(ptr: ExternPointer<Arc<MoveData>>, flag: *const c_char) -> u8 {
|
||||
let flag = CStr::from_ptr(flag).into();
|
||||
if ptr.as_ref().has_flag(&flag) {
|
||||
1
|
||||
@@ -89,18 +93,19 @@ unsafe extern "C" fn secondary_effect_new(
|
||||
effect_name: BorrowedPtr<c_char>,
|
||||
parameters: *mut OwnedPtr<EffectParameter>,
|
||||
parameters_length: usize,
|
||||
) -> OwnedPtr<SecondaryEffect> {
|
||||
) -> IdentifiablePointer<SecondaryEffect> {
|
||||
let parameter_slice = std::slice::from_raw_parts(parameters, parameters_length);
|
||||
let mut parameters = Vec::with_capacity(parameters_length);
|
||||
for parameter in parameter_slice {
|
||||
parameters.push(*Box::from_raw(*parameter))
|
||||
}
|
||||
|
||||
Box::into_raw(Box::new(SecondaryEffect::new(
|
||||
Box::new(SecondaryEffect::new(
|
||||
chance,
|
||||
CStr::from_ptr(effect_name).into(),
|
||||
parameters,
|
||||
)))
|
||||
))
|
||||
.into()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -124,10 +129,10 @@ unsafe extern "C" fn secondary_effect_parameter_length(ptr: ExternPointer<Second
|
||||
unsafe extern "C" fn secondary_effect_parameter_get(
|
||||
ptr: ExternPointer<SecondaryEffect>,
|
||||
index: usize,
|
||||
) -> BorrowedPtr<EffectParameter> {
|
||||
) -> IdentifiablePointer<EffectParameter> {
|
||||
if let Some(v) = ptr.as_ref().parameters().get(index) {
|
||||
v as *const EffectParameter
|
||||
(v as *const EffectParameter).into()
|
||||
} else {
|
||||
std::ptr::null()
|
||||
IdentifiablePointer::none()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user