PkmnLib_rs/src/c_interface/static_data/item.rs

49 lines
1.4 KiB
Rust

use super::super::handle::Out;
use super::super::pkmn_result::PkmnResult;
use crate::c_interface::handle::{thread_bound, HandleExclusive};
use crate::static_data::items::item::Item;
use std::collections::HashSet;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::slice;
use thread_bound::DeferredCleanup;
#[repr(C)]
pub struct ItemC {
inner: DeferredCleanup<Item>,
}
type ItemHandle<'a> = HandleExclusive<'a, ItemC>;
ffi! {
fn pkmnlib_item_new(
name: *const c_char,
category: u8,
battle_category: u8,
price: i32,
flags: *mut *const c_char,
flags_length: usize,
out: Out<ItemHandle>
) -> PkmnResult {
unsafe {
let v = slice::from_raw_parts(flags, flags_length as usize).to_vec();
let mut flags_map = HashSet::new();
for flag in v {
flags_map.insert(CStr::from_ptr(flag).to_str().unwrap().into());
}
let handle = ItemHandle::alloc(ItemC {
inner: thread_bound::DeferredCleanup::new(Item::new(
&CStr::from_ptr(name).to_str().unwrap().into(),
std::mem::transmute(category),
std::mem::transmute(battle_category),
price,
flags_map,
)),
});
out.init(handle);
}
}
}