108 lines
3.0 KiB
Rust
108 lines
3.0 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)]
|
|
#![cfg_attr(not(feature = "mock_data"), 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::{DynamicLibrary, EffectParameter, StringKey, TurnChoice};
|
|
pub(crate) use crate::handling::extern_ref::*;
|
|
use crate::handling::ffi_array::FFIArray;
|
|
#[cfg(not(feature = "mock_data"))]
|
|
use crate::handling::ScriptCapabilities;
|
|
use crate::handling::{Script, 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]
|
|
#[cfg(not(feature = "mock_data"))]
|
|
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]
|
|
#[cfg(not(feature = "mock_data"))]
|
|
unsafe extern "wasm" fn destroy_script(script: *mut Box<dyn Script>) {
|
|
// 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);
|
|
boxed_script.destroy();
|
|
}
|
|
|
|
#[no_mangle]
|
|
#[cfg(not(feature = "mock_data"))]
|
|
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]
|
|
#[cfg(not(feature = "mock_data"))]
|
|
unsafe extern "wasm" fn script_on_initialize(
|
|
script: *const Box<dyn Script>,
|
|
library: ExternRef<DynamicLibrary>,
|
|
parameters: VecExternRef<EffectParameter>,
|
|
) {
|
|
let lib = DynamicLibrary::new(library);
|
|
let parameters = ImmutableList::from_ref(parameters);
|
|
script
|
|
.as_ref()
|
|
.unwrap()
|
|
.as_ref()
|
|
.on_initialize(&lib, Some(parameters));
|
|
}
|
|
|
|
#[no_mangle]
|
|
#[cfg(not(feature = "mock_data"))]
|
|
unsafe extern "wasm" fn script_on_before_turn(
|
|
script: *const Box<dyn Script>,
|
|
choice: ExternRef<TurnChoice>,
|
|
) {
|
|
script
|
|
.as_ref()
|
|
.unwrap()
|
|
.as_ref()
|
|
.on_before_turn(choice.get_value().unwrap())
|
|
}
|