75 lines
3.1 KiB
Rust
75 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::{FFIHandle, FromFFIHandle};
|
|
use crate::ffi::{NonOwnedPtrString, OwnedPtrString};
|
|
use crate::static_data::*;
|
|
use std::ffi::CStr;
|
|
use std::sync::Arc;
|
|
|
|
/// Generates foreign function interfaces for a DataLibrary trait implementation.
|
|
macro_rules! library_interface {
|
|
($library_type_name:ident, $library_type:ty, $return_type:ty) => {
|
|
paste::paste! {
|
|
#[no_mangle]
|
|
extern "C" fn [< $library_type_name:snake _new >](capacity: usize) -> FFIHandle<$library_type> {
|
|
let value: $library_type = Arc::new([<$library_type_name Impl>]::new(capacity));
|
|
FFIHandle::get_handle(value.into())
|
|
}
|
|
|
|
#[no_mangle]
|
|
unsafe extern "C" fn [< $library_type_name:snake _add >](ptr: FFIHandle<$library_type>, key: NonOwnedPtrString, value: FFIHandle<Arc<$return_type>>) {
|
|
let lib = ptr.from_ffi_handle();
|
|
lib.add(&CStr::from_ptr(key).into(), value.from_ffi_handle());
|
|
}
|
|
|
|
#[no_mangle]
|
|
unsafe extern "C" fn [< $library_type_name:snake _remove >](ptr: FFIHandle<$library_type>, key: NonOwnedPtrString) {
|
|
let lib = ptr.from_ffi_handle();
|
|
lib.remove(&CStr::from_ptr(key).into());
|
|
}
|
|
|
|
#[no_mangle]
|
|
unsafe extern "C" fn [< $library_type_name:snake _get >](ptr: FFIHandle<$library_type>, key: NonOwnedPtrString) -> FFIHandle<Arc<$return_type>> {
|
|
let lib = ptr.from_ffi_handle();
|
|
let v = lib.get(&CStr::from_ptr(key).into());
|
|
if let Some(value) = v {
|
|
FFIHandle::get_handle(value.clone().into())
|
|
} else {
|
|
FFIHandle::none()
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
unsafe extern "C" fn [< $library_type_name:snake _get_key_by_index >](ptr: FFIHandle<$library_type>, index: usize) -> OwnedPtrString {
|
|
let lib = ptr.from_ffi_handle();
|
|
let v = lib.get_key_by_index(index);
|
|
if let Some(value) = v {
|
|
OwnedPtrString(std::ffi::CString::new(value.str()).unwrap().into_raw())
|
|
} else {
|
|
OwnedPtrString(std::ptr::null_mut())
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
unsafe extern "C" fn [< $library_type_name:snake _len >](ptr: FFIHandle<$library_type>) -> usize {
|
|
let lib = ptr.from_ffi_handle();
|
|
lib.len()
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
library_interface!(SpeciesLibrary, Arc<dyn SpeciesLibrary>, dyn Species);
|
|
library_interface!(MoveLibrary, Arc<dyn MoveLibrary>, dyn MoveData);
|
|
library_interface!(AbilityLibrary, Arc<dyn AbilityLibrary>, dyn Ability);
|
|
library_interface!(ItemLibrary, Arc<dyn ItemLibrary>, dyn Item);
|