2023-04-16 17:57:21 +00:00
|
|
|
use crate::ffi::{ffi_arc_dyn_getter, ExternPointer, IdentifiablePointer, NativeResult, OwnedPtr};
|
2022-11-27 16:29:29 +00:00
|
|
|
use crate::static_data::{BattleItemCategory, Item, ItemCategory, ItemImpl};
|
2022-09-18 16:02:13 +00:00
|
|
|
use crate::StringKey;
|
2023-04-16 17:57:21 +00:00
|
|
|
use anyhow::anyhow;
|
2022-09-18 16:02:13 +00:00
|
|
|
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,
|
2023-04-16 17:57:21 +00:00
|
|
|
) -> NativeResult<IdentifiablePointer<Arc<dyn Item>>> {
|
2022-09-18 16:02:13 +00:00
|
|
|
let flags = std::slice::from_raw_parts(flags, flags_length);
|
2023-04-16 17:57:21 +00:00
|
|
|
let name: StringKey = match CStr::from_ptr(name).to_str() {
|
|
|
|
Ok(name) => name.into(),
|
|
|
|
Err(_) => return NativeResult::err(anyhow!("Unable to convert name to string")),
|
|
|
|
};
|
2022-09-18 16:02:13 +00:00
|
|
|
let mut flags_set: HashSet<StringKey> = HashSet::with_capacity(flags_length);
|
|
|
|
for flag in flags {
|
2023-04-16 17:57:21 +00:00
|
|
|
let flag = match CStr::from_ptr(*flag).to_str() {
|
|
|
|
Ok(flag) => flag,
|
|
|
|
Err(_) => return NativeResult::err(anyhow!("Unable to convert flag to string")),
|
|
|
|
};
|
|
|
|
flags_set.insert(flag.into());
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
2022-11-27 16:29:29 +00:00
|
|
|
|
|
|
|
let item: Arc<dyn Item> = Arc::new(ItemImpl::new(&name, category, battle_category, price, flags_set));
|
2023-04-16 17:57:21 +00:00
|
|
|
NativeResult::ok(item.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-11-27 16:29:29 +00:00
|
|
|
unsafe extern "C" fn item_drop(ptr: OwnedPtr<Arc<dyn 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]
|
2023-04-16 17:57:21 +00:00
|
|
|
unsafe extern "C" fn item_name(ptr: ExternPointer<Arc<dyn Item>>) -> NativeResult<OwnedPtr<c_char>> {
|
2022-09-18 16:02:13 +00:00
|
|
|
let name = ptr.as_ref().name();
|
2023-04-16 17:57:21 +00:00
|
|
|
match CString::new(name.str()) {
|
|
|
|
Ok(name) => NativeResult::ok(name.into_raw()),
|
|
|
|
Err(_) => NativeResult::err(anyhow!("Unable to convert name `{}` to CString", name.str())),
|
|
|
|
}
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 12:56:20 +00:00
|
|
|
ffi_arc_dyn_getter!(Item, category, ItemCategory);
|
|
|
|
ffi_arc_dyn_getter!(Item, battle_category, BattleItemCategory);
|
|
|
|
ffi_arc_dyn_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-11-27 16:29:29 +00:00
|
|
|
unsafe extern "C" fn item_has_flag(ptr: ExternPointer<Arc<dyn 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
|
|
|
}
|