PkmnLib_rs/src/ffi/static_data/mod.rs

118 lines
4.4 KiB
Rust
Raw Normal View History

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;
use crate::{PkmnError, StringKey};
use anyhow::anyhow;
2022-09-18 16:02:13 +00:00
use std::ffi::{c_char, CStr, CString};
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
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]
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]
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]
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]
unsafe extern "C" fn effect_parameter_new_string(value: *const c_char) -> FFIResult<FFIHandle<Arc<EffectParameter>>> {
let sk: StringKey = match CStr::from_ptr(value).to_str() {
Ok(sk) => sk.into(),
Err(_) => return FFIResult::err(PkmnError::InvalidCString.into()),
};
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]
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]
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))
} else {
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]
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)
} else {
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]
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)
} else {
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]
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() {
match CString::new(b.str().to_string()) {
Ok(cstr) => FFIResult::ok(OwnedPtrString(cstr.into_raw())),
Err(_) => FFIResult::err(PkmnError::InvalidCString.into()),
}
} else {
FFIResult::err(anyhow!("Unexpected effect parameter. Expected string, was: {}", p))
2022-09-18 16:02:13 +00:00
}
}