79 lines
3.1 KiB
Rust
79 lines
3.1 KiB
Rust
/// The foreign function interface for the growth rate library.
|
|
mod growth_rate_library;
|
|
/// The foreign function interface for the library settings.
|
|
mod library_settings;
|
|
/// The foreign function interface for the nature library.
|
|
mod nature_library;
|
|
/// The foreign function interface for the static data.
|
|
mod static_data;
|
|
/// The foreign function interface for the type library.
|
|
mod type_library;
|
|
|
|
use crate::ffi::{BorrowedPtr, IdentifiablePointer, OwnedPtr};
|
|
use crate::static_data::*;
|
|
use std::ffi::{c_char, CStr};
|
|
use std::ptr::drop_in_place;
|
|
use std::sync::Arc;
|
|
|
|
/// Generates foreign function interfaces for a DataLibrary trait implementation.
|
|
macro_rules! library_interface {
|
|
($library_type:ty, $return_type:ty) => {
|
|
paste::paste! {
|
|
#[no_mangle]
|
|
extern "C" fn [< $library_type:snake _new >](capacity: usize) -> IdentifiablePointer<$library_type> {
|
|
Box::new($library_type::new(capacity)).into()
|
|
}
|
|
|
|
#[no_mangle]
|
|
unsafe extern "C" fn [< $library_type:snake _drop >](ptr: OwnedPtr<$library_type>) {
|
|
drop_in_place(ptr);
|
|
}
|
|
|
|
#[no_mangle]
|
|
unsafe extern "C" fn [< $library_type:snake _add >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>, value: OwnedPtr<Arc<$return_type>>) {
|
|
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]
|
|
unsafe extern "C" fn [< $library_type:snake _get >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>) -> IdentifiablePointer<Arc<$return_type>> {
|
|
let lib = ptr.as_mut().unwrap();
|
|
let v = lib.get(&CStr::from_ptr(key).into());
|
|
if let Some(value) = v {
|
|
value.clone().into()
|
|
} else {
|
|
IdentifiablePointer::none()
|
|
}
|
|
}
|
|
|
|
#[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, dyn Species);
|
|
library_interface!(MoveLibrary, dyn MoveData);
|
|
library_interface!(AbilityLibrary, dyn Ability);
|
|
library_interface!(ItemLibrary, dyn Item);
|