Gen7ScriptsRs/pkmn_lib_interface/src/lib.rs

97 lines
2.7 KiB
Rust

#![feature(core_panic)]
#![feature(alloc_error_handler)]
#![feature(fn_traits)]
#![feature(const_for)]
#![feature(const_mut_refs)]
#![feature(inline_const)]
#![feature(inline_const_pat)]
#![feature(repr128)]
#![feature(downcast_unchecked)]
#![feature(panic_info_message)]
#![feature(const_btree_new)]
#![feature(wasm_abi)]
#![feature(thread_local)]
#![feature(build_hasher_simple_hash_one)]
#![no_std]
#![allow(incomplete_features)]
extern crate alloc;
extern crate core;
extern crate wee_alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
use crate::app_interface::list::ImmutableList;
use crate::app_interface::{BaseTurnChoice, BattleLibrary, EffectParameter, StringKey};
pub(crate) use crate::handling::extern_ref::*;
use crate::handling::ffi_array::FFIArray;
use crate::handling::{Script, ScriptCapabilities, ScriptCategory};
use alloc::boxed::Box;
#[macro_use]
#[allow(dead_code)]
pub mod app_interface;
pub mod handling;
pub mod utils;
pub type LoadScriptFnType = Box<dyn Fn(ScriptCategory, &StringKey) -> Option<Box<dyn Script>>>;
static mut LOAD_SCRIPT_FN: Option<LoadScriptFnType> = None;
pub fn set_load_script_fn(f: LoadScriptFnType) {
unsafe {
LOAD_SCRIPT_FN = Some(f);
}
}
#[no_mangle]
extern "wasm" fn load_script(category: ScriptCategory, name: ExternRef<StringKey>) -> u32 {
let name_c = StringKey::new(name);
let boxed_script = unsafe { &LOAD_SCRIPT_FN }.as_ref().unwrap()(category, &name_c);
if boxed_script.is_none() {
return 0;
}
let b = Box::new(boxed_script.unwrap());
Box::into_raw(b) as u32
}
#[no_mangle]
unsafe extern "wasm" fn destroy_script(script: *mut u8) {
// By turning it from a raw pointer back into a Box with from_raw, we give ownership back to rust.
// This lets Rust do the cleanup.
let boxed_script = Box::from_raw(script as *mut Box<dyn Script>);
boxed_script.destroy();
}
#[no_mangle]
unsafe extern "wasm" fn get_script_capabilities(
script: *const Box<dyn Script>,
) -> FFIArray<ScriptCapabilities> {
let c = script.as_ref().unwrap().get_capabilities();
FFIArray::new(c)
}
#[no_mangle]
unsafe extern "wasm" fn script_on_initialize(
script: *const Box<dyn Script>,
library: ExternRef<BattleLibrary>,
parameters: VecExternRef<EffectParameter>,
) {
let lib = BattleLibrary::new(library);
let parameters = ImmutableList::from_ref(parameters);
script
.as_ref()
.unwrap()
.as_ref()
.on_initialize(&lib, Some(parameters));
}
#[no_mangle]
unsafe extern "wasm" fn script_on_before_turn(
script: *const Box<dyn Script>,
choice: ExternRef<BaseTurnChoice>,
) {
script.as_ref().unwrap().as_ref().on_before_turn(choice)
}