Major amounts of progress
This commit is contained in:
48
src/c_interface/static_data/item.rs
Normal file
48
src/c_interface/static_data/item.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
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().to_string());
|
||||
}
|
||||
|
||||
let handle = ItemHandle::alloc(ItemC {
|
||||
inner: thread_bound::DeferredCleanup::new(Item::new(
|
||||
CStr::from_ptr(name).to_str().unwrap(),
|
||||
std::mem::transmute(category),
|
||||
std::mem::transmute(battle_category),
|
||||
price,
|
||||
flags_map,
|
||||
)),
|
||||
});
|
||||
|
||||
out.init(handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user