2023-06-24 12:44:23 +00:00
|
|
|
use crate::ffi::ffi_handle::{FFIHandle, FFIObject, FromFFIHandle};
|
|
|
|
use crate::ffi::{FFIResult, OwnedPtrString};
|
2022-09-18 16:02:13 +00:00
|
|
|
use crate::static_data::EffectParameter;
|
2023-04-16 17:57:21 +00:00
|
|
|
use crate::{PkmnError, StringKey};
|
|
|
|
use anyhow::anyhow;
|
2022-09-18 16:02:13 +00:00
|
|
|
use std::ffi::{c_char, CStr, CString};
|
2023-06-24 12:44:23 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::sync::Arc;
|
2022-09-18 16:02:13 +00:00
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The Foreign Function Interface for abilities
|
2022-09-18 16:02:13 +00:00
|
|
|
mod ability;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The Foreign Function Interface for forms
|
2022-09-18 16:02:13 +00:00
|
|
|
mod form;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The Foreign Function Interface for growth rates
|
2022-09-18 16:02:13 +00:00
|
|
|
mod growth_rate;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The Foreign Function Interface for items
|
2022-09-18 16:02:13 +00:00
|
|
|
mod item;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The Foreign Function Interface for learnable moves
|
2022-09-18 16:02:13 +00:00
|
|
|
mod learnable_moves;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The Foreign Function Interface for libraries
|
2022-10-08 11:15:04 +00:00
|
|
|
mod libraries;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The Foreign Function Interface for moves
|
2022-09-18 16:02:13 +00:00
|
|
|
mod move_data;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The Foreign Function Interface for natures
|
2022-09-18 16:02:13 +00:00
|
|
|
mod nature;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The Foreign Function Interface for species
|
2022-09-18 16:02:13 +00:00
|
|
|
mod species;
|
2022-10-14 14:53:30 +00:00
|
|
|
/// The Foreign Function Interface for sets of statistics
|
2022-09-18 16:02:13 +00:00
|
|
|
mod statistic_set;
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Instantiates an effect parameter with a boolean.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn effect_parameter_new_bool(value: u8) -> FFIHandle<Arc<EffectParameter>> {
|
|
|
|
FFIHandle::get_handle(FFIObject::EffectParameter(Arc::new(EffectParameter::from(value == 1))))
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Instantiates an effect parameter with an integer.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn effect_parameter_new_int(value: i64) -> FFIHandle<Arc<EffectParameter>> {
|
|
|
|
FFIHandle::get_handle(FFIObject::EffectParameter(Arc::new(EffectParameter::from(value))))
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Instantiates an effect parameter with a float.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn effect_parameter_new_float(value: f32) -> FFIHandle<Arc<EffectParameter>> {
|
|
|
|
FFIHandle::get_handle(FFIObject::EffectParameter(Arc::new(EffectParameter::from(value))))
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Instantiates an effect parameter with a string.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
unsafe extern "C" fn effect_parameter_new_string(value: *const c_char) -> FFIResult<FFIHandle<Arc<EffectParameter>>> {
|
2023-04-16 17:57:21 +00:00
|
|
|
let sk: StringKey = match CStr::from_ptr(value).to_str() {
|
|
|
|
Ok(sk) => sk.into(),
|
2023-06-24 12:44:23 +00:00
|
|
|
Err(_) => return FFIResult::err(PkmnError::InvalidCString.into()),
|
2023-04-16 17:57:21 +00:00
|
|
|
};
|
2023-06-24 12:44:23 +00:00
|
|
|
FFIResult::ok(FFIHandle::get_handle(FFIObject::EffectParameter(Arc::new(
|
|
|
|
EffectParameter::from(sk),
|
|
|
|
))))
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Get the type of an effect parameter.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn effect_parameter_get_type(ptr: FFIHandle<Arc<EffectParameter>>) -> u8 {
|
|
|
|
match ptr.from_ffi_handle().deref() {
|
|
|
|
EffectParameter::Bool(_) => 0,
|
|
|
|
EffectParameter::Int(_) => 1,
|
|
|
|
EffectParameter::Float(_) => 2,
|
|
|
|
EffectParameter::String(_) => 3,
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Get the boolean contained in the effect parameter, panics if the effect parameter is not a bool.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn effect_parameter_get_as_bool(ptr: FFIHandle<Arc<EffectParameter>>) -> FFIResult<u8> {
|
|
|
|
let p = ptr.from_ffi_handle();
|
|
|
|
if let EffectParameter::Bool(b) = p.deref() {
|
|
|
|
FFIResult::ok(u8::from(*b))
|
2023-04-16 17:57:21 +00:00
|
|
|
} else {
|
2023-06-24 12:44:23 +00:00
|
|
|
FFIResult::err(anyhow!("Unexpected effect parameter. Expected bool, was: {}", p))
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Get the int contained in the effect parameter, panics if the effect parameter is not a int.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn effect_parameter_get_as_int(ptr: FFIHandle<Arc<EffectParameter>>) -> FFIResult<i64> {
|
|
|
|
let p = ptr.from_ffi_handle();
|
|
|
|
if let EffectParameter::Int(b) = p.deref() {
|
|
|
|
FFIResult::ok(*b)
|
2023-04-16 17:57:21 +00:00
|
|
|
} else {
|
2023-06-24 12:44:23 +00:00
|
|
|
FFIResult::err(anyhow!("Unexpected effect parameter. Expected int, was: {}", p))
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Get the float contained in the effect parameter, panics if the effect parameter is not a float.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn effect_parameter_get_as_float(ptr: FFIHandle<Arc<EffectParameter>>) -> FFIResult<f32> {
|
|
|
|
let p = ptr.from_ffi_handle();
|
|
|
|
if let EffectParameter::Float(b) = p.deref() {
|
|
|
|
FFIResult::ok(*b)
|
2023-04-16 17:57:21 +00:00
|
|
|
} else {
|
2023-06-24 12:44:23 +00:00
|
|
|
FFIResult::err(anyhow!("Unexpected effect parameter. Expected float, was: {}", p))
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Get the string contained in the effect parameter, panics if the effect parameter is not a string.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn effect_parameter_get_as_string(ptr: FFIHandle<Arc<EffectParameter>>) -> FFIResult<OwnedPtrString> {
|
|
|
|
let p = ptr.from_ffi_handle();
|
|
|
|
if let EffectParameter::String(b) = p.deref() {
|
2023-04-16 17:57:21 +00:00
|
|
|
match CString::new(b.str().to_string()) {
|
2023-06-25 14:28:51 +00:00
|
|
|
Ok(cstr) => FFIResult::ok(OwnedPtrString(cstr.into_raw())),
|
2023-06-24 12:44:23 +00:00
|
|
|
Err(_) => FFIResult::err(PkmnError::InvalidCString.into()),
|
2023-04-16 17:57:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-06-24 12:44:23 +00:00
|
|
|
FFIResult::err(anyhow!("Unexpected effect parameter. Expected string, was: {}", p))
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
}
|