2022-10-14 14:53:30 +00:00
|
|
|
/// The foreign function interface for the growth rate library.
|
2022-10-01 13:40:15 +00:00
|
|
|
mod growth_rate_library;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The foreign function interface for the library settings.
|
2022-10-01 13:40:15 +00:00
|
|
|
mod library_settings;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The foreign function interface for the nature library.
|
2022-10-01 13:40:15 +00:00
|
|
|
mod nature_library;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The foreign function interface for the static data.
|
2022-10-01 13:40:15 +00:00
|
|
|
mod static_data;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The foreign function interface for the type library.
|
2022-10-01 13:40:15 +00:00
|
|
|
mod type_library;
|
|
|
|
|
2022-10-08 11:15:04 +00:00
|
|
|
use crate::ffi::{BorrowedPtr, IdentifiablePointer, OwnedPtr};
|
2022-10-01 13:40:15 +00:00
|
|
|
use crate::static_data::*;
|
|
|
|
use std::ffi::{c_char, CStr};
|
|
|
|
use std::ptr::drop_in_place;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Generates foreign function interfaces for a DataLibrary trait implementation.
|
2022-10-01 13:40:15 +00:00
|
|
|
macro_rules! library_interface {
|
|
|
|
($library_type:ty, $return_type:ty) => {
|
|
|
|
paste::paste! {
|
|
|
|
#[no_mangle]
|
2022-10-08 11:15:04 +00:00
|
|
|
extern "C" fn [< $library_type:snake _new >](capacity: usize) -> IdentifiablePointer<$library_type> {
|
|
|
|
Box::new($library_type::new(capacity)).into()
|
2022-10-01 13:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn [< $library_type:snake _drop >](ptr: OwnedPtr<$library_type>) {
|
|
|
|
drop_in_place(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2022-10-08 11:15:04 +00:00
|
|
|
unsafe extern "C" fn [< $library_type:snake _add >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>, value: OwnedPtr<Arc<$return_type>>) {
|
2022-10-01 13:40:15 +00:00
|
|
|
let lib = ptr.as_mut().unwrap();
|
|
|
|
lib.add(&CStr::from_ptr(key).into(), *Box::from_raw(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn [< $library_type:snake _remove >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>) {
|
|
|
|
let lib = ptr.as_mut().unwrap();
|
|
|
|
lib.remove(&CStr::from_ptr(key).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2022-10-08 11:15:04 +00:00
|
|
|
unsafe extern "C" fn [< $library_type:snake _get >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>) -> IdentifiablePointer<Arc<$return_type>> {
|
2022-10-01 13:40:15 +00:00
|
|
|
let lib = ptr.as_mut().unwrap();
|
|
|
|
let v = lib.get(&CStr::from_ptr(key).into());
|
|
|
|
if let Some(value) = v {
|
2022-10-08 11:15:04 +00:00
|
|
|
value.clone().into()
|
2022-10-01 13:40:15 +00:00
|
|
|
} else {
|
2022-10-08 11:15:04 +00:00
|
|
|
IdentifiablePointer::none()
|
2022-10-01 13:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn [< $library_type:snake _get_key_by_index >](ptr: OwnedPtr<$library_type>, index: usize) -> OwnedPtr<c_char> {
|
|
|
|
let lib = ptr.as_mut().unwrap();
|
|
|
|
let v = lib.get_key_by_index(index);
|
|
|
|
if let Some(value) = v {
|
|
|
|
std::ffi::CString::new(value.str()).unwrap().into_raw()
|
|
|
|
} else {
|
|
|
|
std::ptr::null_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn [< $library_type:snake _len >](ptr: OwnedPtr<$library_type>) -> usize {
|
|
|
|
let lib = ptr.as_mut().unwrap();
|
|
|
|
lib.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
library_interface!(SpeciesLibrary, Species);
|
|
|
|
library_interface!(MoveLibrary, MoveData);
|
2022-11-27 16:47:51 +00:00
|
|
|
library_interface!(AbilityLibrary, dyn Ability);
|
2022-11-27 16:29:29 +00:00
|
|
|
library_interface!(ItemLibrary, dyn Item);
|