PkmnLib_rs/src/ffi/static_data/item.rs

51 lines
1.6 KiB
Rust
Raw Normal View History

use crate::ffi::{ffi_arc_getter, ExternPointer, IdentifiablePointer, OwnedPtr};
2022-09-18 16:02:13 +00:00
use crate::static_data::{BattleItemCategory, Item, ItemCategory};
use crate::StringKey;
use hashbrown::HashSet;
use std::ffi::{c_char, CStr, CString};
use std::ptr::drop_in_place;
use std::sync::Arc;
2022-09-18 16:02:13 +00:00
#[no_mangle]
unsafe extern "C" fn item_new(
name: *const c_char,
category: ItemCategory,
battle_category: BattleItemCategory,
price: i32,
flags: *const *const c_char,
flags_length: usize,
) -> IdentifiablePointer<Arc<Item>> {
2022-09-18 16:02:13 +00:00
let flags = std::slice::from_raw_parts(flags, flags_length);
let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into();
let mut flags_set: HashSet<StringKey> = HashSet::with_capacity(flags_length);
for flag in flags {
flags_set.insert(CStr::from_ptr(*flag).to_str().unwrap().into());
}
Arc::new(Item::new(&name, category, battle_category, price, flags_set)).into()
2022-09-18 16:02:13 +00:00
}
#[no_mangle]
unsafe extern "C" fn item_drop(ptr: OwnedPtr<Arc<Item>>) {
2022-09-18 16:02:13 +00:00
drop_in_place(ptr)
}
#[no_mangle]
unsafe extern "C" fn item_name(ptr: ExternPointer<Arc<Item>>) -> OwnedPtr<c_char> {
2022-09-18 16:02:13 +00:00
let name = ptr.as_ref().name();
CString::new(name.str()).unwrap().into_raw()
}
ffi_arc_getter!(Item, category, ItemCategory);
ffi_arc_getter!(Item, battle_category, BattleItemCategory);
ffi_arc_getter!(Item, price, i32);
2022-09-18 16:02:13 +00:00
#[no_mangle]
unsafe extern "C" fn item_has_flag(ptr: ExternPointer<Arc<Item>>, flag: *const c_char) -> u8 {
2022-09-18 16:02:13 +00:00
let flag = CStr::from_ptr(flag).into();
if ptr.as_ref().has_flag(&flag) {
1
} else {
0
}
}