PkmnLib_rs/src/script_implementations/wasm/export_registry/dynamic_data/turn_choice.rs

149 lines
5.1 KiB
Rust
Executable File

use crate::dynamic_data::{LearnedMove, Pokemon, TurnChoice};
use crate::script_implementations::wasm::export_registry::wasm_result::{
get_value, get_value_call_getter, get_value_void, wasm_err,
};
use crate::script_implementations::wasm::export_registry::{
register, wasm_ok, WasmResult, WasmVoidResult, WasmVoidResultExtension,
};
use crate::script_implementations::wasm::extern_ref::ExternRef;
use crate::script_implementations::wasm::script::WebAssemblyScript;
use crate::script_implementations::wasm::script_resolver::WebAssemblyEnv;
use anyhow_ext::Context;
use wasmer::FunctionEnvMut;
register! {
fn turn_choice_get_user(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<ExternRef<Pokemon>> {
let user = get_value_call_getter!(turn_choice.user(), &env);
wasm_ok(ExternRef::func_new(&env, user))
}
fn turn_choice_get_kind(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<u8> {
let turn_choice = get_value!(turn_choice, env);
wasm_ok(match turn_choice {
TurnChoice::Move(_) => 0,
TurnChoice::Item(_) => 1,
TurnChoice::Switch(_) => 2,
TurnChoice::Flee(_) => 3,
TurnChoice::Pass(_) => 4,
})
}
fn turn_choice_get_speed(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<u32> {
wasm_ok(get_value_call_getter!(turn_choice.speed(), &env))
}
fn turn_choice_has_failed(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<u8> {
wasm_ok(if get_value_call_getter!(turn_choice.has_failed(), &env) { 1 } else { 0 })
}
fn turn_choice_fail(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmVoidResult {
let turn_choice = get_value_void!(turn_choice, env);
turn_choice.fail();
WasmVoidResult::ok()
}
fn turn_choice_move_priority(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<i8> {
return match turn_choice.value_func(&env) {
Ok(v) => {
match v {
TurnChoice::Move(m) => wasm_ok(m.priority()),
_ => wasm_err::<i8>(anyhow_ext::anyhow!("Invalid turn choice"), &env)
}
},
Err(e) => wasm_err::<i8>(e, &env),
}
}
fn turn_choice_move_used_move(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<ExternRef<LearnedMove>> {
return match turn_choice.value_func(&env) {
Ok(v) => {
match v {
TurnChoice::Move(m) => wasm_ok(ExternRef::<LearnedMove>::func_new(&env, m.used_move())),
_ => wasm_err::<ExternRef<LearnedMove>>(anyhow_ext::anyhow!("Invalid turn choice"), &env)
}
},
Err(e) => wasm_err::<ExternRef<LearnedMove>>(e, &env),
}
}
fn turn_choice_move_target_side(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<u8> {
return match turn_choice.value_func(&env) {
Ok(v) => {
match v {
TurnChoice::Move(m) => wasm_ok(m.target_side()),
_ => wasm_err::<u8>(anyhow_ext::anyhow!("Invalid turn choice"), &env)
}
},
Err(e) => wasm_err::<u8>(e, &env),
}
}
fn turn_choice_move_target_index(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<u8> {
return match turn_choice.value_func(&env) {
Ok(v) => {
match v {
TurnChoice::Move(m) => wasm_ok(m.target_index()),
_ => wasm_err::<u8>(anyhow_ext::anyhow!("Invalid turn choice"), &env)
}
},
Err(e) => wasm_err::<u8>(e, &env),
}
}
fn turn_choice_move_script(
env: FunctionEnvMut<WebAssemblyEnv>,
turn_choice: ExternRef<TurnChoice>,
) -> WasmResult<u32> {
return match turn_choice.value_func(&env) {
Ok(v) => {
match v {
TurnChoice::Move(d) => {
if let Some(script) = d.script().get() {
let read_lock = script.read();
if let Some(script) = read_lock.as_ref() {
if let Some(v) = script.as_any().downcast_ref::<WebAssemblyScript>(){
return wasm_ok(v.get_wasm_pointer())
}
}
}
return wasm_ok(0);
},
_ => wasm_err::<u32>(anyhow_ext::anyhow!("Invalid turn choice"), &env)
}
},
Err(e) => wasm_err::<u32>(e, &env),
}
}
}