2023-06-24 12:44:23 +00:00
|
|
|
use crate::ffi::ffi_handle::{FFIHandle, FromFFIHandle};
|
|
|
|
use crate::ffi::{ffi_handle_arc_dyn_getter, FFIResult, OwnedPtrString};
|
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};
|
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-06-24 12:44:23 +00:00
|
|
|
) -> FFIResult<FFIHandle<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(),
|
2023-06-24 12:44:23 +00:00
|
|
|
Err(_) => return FFIResult::err(anyhow!("Unable to convert name to string")),
|
2023-04-16 17:57:21 +00:00
|
|
|
};
|
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,
|
2023-06-24 12:44:23 +00:00
|
|
|
Err(_) => return FFIResult::err(anyhow!("Unable to convert flag to string")),
|
2023-04-16 17:57:21 +00:00
|
|
|
};
|
|
|
|
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-06-24 12:44:23 +00:00
|
|
|
FFIResult::ok(FFIHandle::get_handle(item.into()))
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The name of the item.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
unsafe extern "C" fn item_name(ptr: FFIHandle<Arc<dyn Item>>) -> FFIResult<OwnedPtrString> {
|
|
|
|
let item = ptr.from_ffi_handle();
|
|
|
|
let name = item.name();
|
2023-04-16 17:57:21 +00:00
|
|
|
match CString::new(name.str()) {
|
2023-06-25 14:28:51 +00:00
|
|
|
Ok(name) => FFIResult::ok(OwnedPtrString(name.into_raw())),
|
2023-06-24 12:44:23 +00:00
|
|
|
Err(_) => FFIResult::err(anyhow!("Unable to convert name `{}` to CString", name.str())),
|
2023-04-16 17:57:21 +00:00
|
|
|
}
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
ffi_handle_arc_dyn_getter!(Item, category, ItemCategory);
|
|
|
|
ffi_handle_arc_dyn_getter!(Item, battle_category, BattleItemCategory);
|
|
|
|
ffi_handle_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]
|
2023-06-24 12:44:23 +00:00
|
|
|
unsafe extern "C" fn item_has_flag(ptr: FFIHandle<Arc<dyn Item>>, flag: *const c_char) -> u8 {
|
2022-09-18 16:02:13 +00:00
|
|
|
let flag = CStr::from_ptr(flag).into();
|
2023-06-24 12:44:23 +00:00
|
|
|
u8::from(ptr.from_ffi_handle().has_flag(&flag))
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|