2022-10-08 11:15:04 +00:00
|
|
|
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;
|
2022-10-08 11:15:04 +00:00
|
|
|
use std::sync::Arc;
|
2022-09-18 16:02:13 +00:00
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Instantiates an item.
|
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,
|
2022-10-08 11:15:04 +00:00
|
|
|
) -> 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());
|
|
|
|
}
|
2022-10-08 11:15:04 +00:00
|
|
|
Arc::new(Item::new(&name, category, battle_category, price, flags_set)).into()
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Drops a reference counted item.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2022-10-08 11:15:04 +00:00
|
|
|
unsafe extern "C" fn item_drop(ptr: OwnedPtr<Arc<Item>>) {
|
2022-09-18 16:02:13 +00:00
|
|
|
drop_in_place(ptr)
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The name of the item.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2022-10-08 11:15:04 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-10-08 11:15:04 +00:00
|
|
|
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
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Checks whether the item has a specific flag.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2022-10-08 11:15:04 +00:00
|
|
|
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();
|
2022-10-14 14:53:30 +00:00
|
|
|
u8::from(ptr.as_ref().has_flag(&flag))
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|