2022-10-08 11:15:04 +00:00
|
|
|
use crate::ffi::{BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
|
2022-10-01 13:40:15 +00:00
|
|
|
use crate::static_data::{TypeIdentifier, TypeLibrary};
|
|
|
|
use std::ffi::{c_char, CStr, CString};
|
|
|
|
use std::ptr::drop_in_place;
|
|
|
|
|
|
|
|
#[no_mangle]
|
2022-10-08 11:15:04 +00:00
|
|
|
extern "C" fn type_library_new(capacity: usize) -> IdentifiablePointer<TypeLibrary> {
|
|
|
|
Box::new(TypeLibrary::new(capacity)).into()
|
2022-10-01 13:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn type_library_drop(ptr: OwnedPtr<TypeLibrary>) {
|
|
|
|
drop_in_place(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn type_library_get_type_id(
|
|
|
|
ptr: ExternPointer<TypeLibrary>,
|
|
|
|
key: BorrowedPtr<c_char>,
|
|
|
|
found: *mut bool,
|
|
|
|
) -> TypeIdentifier {
|
|
|
|
if let Some(v) = ptr.as_ref().get_type_id(&CStr::from_ptr(key).into()) {
|
|
|
|
*found = true;
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
*found = false;
|
|
|
|
TypeIdentifier::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn type_library_get_type_name(
|
|
|
|
ptr: ExternPointer<TypeLibrary>,
|
|
|
|
type_id: TypeIdentifier,
|
|
|
|
found: *mut bool,
|
|
|
|
) -> *mut c_char {
|
|
|
|
if let Some(v) = ptr.as_ref().get_type_name(type_id) {
|
|
|
|
*found = true;
|
|
|
|
CString::new(v.str()).unwrap().into_raw()
|
|
|
|
} else {
|
|
|
|
*found = false;
|
|
|
|
std::ptr::null_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
extern "C" fn type_library_get_single_effectiveness(
|
|
|
|
ptr: ExternPointer<TypeLibrary>,
|
|
|
|
attacking: TypeIdentifier,
|
|
|
|
defending: TypeIdentifier,
|
|
|
|
) -> f32 {
|
|
|
|
ptr.as_ref().get_single_effectiveness(attacking, defending)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn type_library_get_effectiveness(
|
|
|
|
ptr: ExternPointer<TypeLibrary>,
|
|
|
|
attacking: TypeIdentifier,
|
|
|
|
defending: OwnedPtr<TypeIdentifier>,
|
|
|
|
defending_length: usize,
|
|
|
|
) -> f32 {
|
|
|
|
let v = std::slice::from_raw_parts(defending, defending_length);
|
|
|
|
ptr.as_ref().get_effectiveness(attacking, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn type_library_register_type(
|
|
|
|
ptr: ExternPointer<TypeLibrary>,
|
|
|
|
name: BorrowedPtr<c_char>,
|
|
|
|
) -> TypeIdentifier {
|
|
|
|
ptr.as_mut().register_type(&CStr::from_ptr(name).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn type_library_set_effectiveness(
|
|
|
|
ptr: ExternPointer<TypeLibrary>,
|
|
|
|
attacking: TypeIdentifier,
|
|
|
|
defending: TypeIdentifier,
|
|
|
|
effectiveness: f32,
|
|
|
|
) {
|
|
|
|
ptr.as_mut().set_effectiveness(attacking, defending, effectiveness);
|
|
|
|
}
|