54 lines
1.7 KiB
Rust
54 lines
1.7 KiB
Rust
use crate::ffi::{BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
|
|
use crate::static_data::{Nature, NatureLibrary, NatureLibraryImpl};
|
|
use std::ffi::{c_char, CStr, CString};
|
|
use std::ptr::drop_in_place;
|
|
use std::sync::Arc;
|
|
|
|
/// Creates a new nature library with a given capacity.
|
|
#[no_mangle]
|
|
extern "C" fn nature_library_new(capacity: usize) -> IdentifiablePointer<Box<dyn NatureLibrary>> {
|
|
let b: Box<dyn NatureLibrary> = Box::new(NatureLibraryImpl::new(capacity));
|
|
b.into()
|
|
}
|
|
|
|
/// Drop a nature library.
|
|
#[no_mangle]
|
|
unsafe extern "C" fn nature_library_drop(ptr: OwnedPtr<Box<dyn NatureLibrary>>) {
|
|
drop_in_place(ptr);
|
|
}
|
|
|
|
/// Adds a new nature with name to the library.
|
|
#[no_mangle]
|
|
unsafe extern "C" fn nature_library_load_nature(
|
|
mut ptr: ExternPointer<Box<dyn NatureLibrary>>,
|
|
name: BorrowedPtr<c_char>,
|
|
nature: OwnedPtr<Arc<dyn Nature>>,
|
|
) {
|
|
ptr.as_mut()
|
|
.load_nature(CStr::from_ptr(name).into(), nature.as_ref().unwrap().clone())
|
|
}
|
|
|
|
/// Gets a nature by name.
|
|
#[no_mangle]
|
|
unsafe extern "C" fn nature_library_get_nature(
|
|
ptr: ExternPointer<Box<dyn NatureLibrary>>,
|
|
name: BorrowedPtr<c_char>,
|
|
) -> IdentifiablePointer<Arc<dyn Nature>> {
|
|
if let Some(nature) = ptr.as_ref().get_nature(&CStr::from_ptr(name).into()) {
|
|
nature.clone().into()
|
|
} else {
|
|
IdentifiablePointer::none()
|
|
}
|
|
}
|
|
|
|
/// Finds a nature name by nature.
|
|
#[no_mangle]
|
|
unsafe extern "C" fn nature_library_get_nature_name(
|
|
ptr: ExternPointer<Box<dyn NatureLibrary>>,
|
|
nature: BorrowedPtr<Arc<dyn Nature>>,
|
|
) -> OwnedPtr<c_char> {
|
|
CString::new(ptr.as_ref().get_nature_name(nature.as_ref().unwrap()).str())
|
|
.unwrap()
|
|
.into_raw()
|
|
}
|