More work on WASM handling.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-08-20 12:22:12 +02:00
parent 703fd2c147
commit 2d4253e155
36 changed files with 922 additions and 87 deletions

View File

@@ -0,0 +1,25 @@
use std::intrinsics::transmute;
use crate::dynamic_data::{LearnedMove, MoveLearnMethod, Pokemon, TurnChoice};
use crate::script_implementations::wasm::export_registry::register;
use crate::script_implementations::wasm::extern_ref::ExternRef;
use crate::script_implementations::wasm::script_resolver::WebAssemblyEnv;
use crate::static_data::MoveData;
register! {
fn learned_move_get_learn_method<'a>(
env: &WebAssemblyEnv,
turn_choice: ExternRef<LearnedMove>,
) -> u8 {
unsafe {
transmute(turn_choice.value(env).unwrap().learn_method())
}
}
fn learned_move_get_move_data<'a>(
env: &WebAssemblyEnv,
turn_choice: ExternRef<LearnedMove>,
) -> ExternRef<MoveData> {
ExternRef::new(env.data().as_ref(), turn_choice.value(env).unwrap().move_data())
}
}

View File

@@ -0,0 +1,13 @@
use wasmer::{Exports, Store};
use crate::script_implementations::wasm::script_resolver::WebAssemblyEnv;
mod learned_move;
mod pokemon;
mod turn_choice;
pub(crate) fn register(exports: &mut Exports, store: &Store, env: WebAssemblyEnv) {
turn_choice::register(exports, store, env.clone());
pokemon::register(exports, store, env.clone());
learned_move::register(exports, store, env.clone());
}

View File

@@ -0,0 +1,77 @@
use std::mem::transmute;
use crate::dynamic_data::{DamageSource, DynamicLibrary, Pokemon};
use crate::script_implementations::wasm::export_registry::register;
use crate::script_implementations::wasm::extern_ref::ExternRef;
use crate::script_implementations::wasm::script_resolver::WebAssemblyEnv;
use crate::static_data::StatisticSet;
use crate::static_data::{ClampedStatisticSet, Species};
register! {
fn pokemon_get_library(
env: &WebAssemblyEnv,
pokemon: ExternRef<Pokemon>,
) -> ExternRef<DynamicLibrary> {
let lib = pokemon.value(env).unwrap().library();
ExternRef::new(env.data().as_ref(), lib)
}
fn pokemon_get_boosted_stats(
env: &WebAssemblyEnv,
pokemon: ExternRef<Pokemon>,
) -> ExternRef<StatisticSet<u32>> {
let statistic_set = pokemon.value(env).unwrap().boosted_stats();
ExternRef::new(env.data().as_ref(), statistic_set)
}
fn pokemon_get_flat_stats(
env: &WebAssemblyEnv,
pokemon: ExternRef<Pokemon>,
) -> ExternRef<StatisticSet<u32>> {
let statistic_set = pokemon.value(env).unwrap().flat_stats();
ExternRef::new(env.data().as_ref(), statistic_set)
}
fn pokemon_get_stat_boosts(
env: &WebAssemblyEnv,
pokemon: ExternRef<Pokemon>,
) -> ExternRef<ClampedStatisticSet<i8, -6, 6>> {
let statistic_set = pokemon.value(env).unwrap().stat_boosts();
ExternRef::new(env.data().as_ref(), statistic_set)
}
fn pokemon_get_individual_values(
env: &WebAssemblyEnv,
pokemon: ExternRef<Pokemon>,
) -> ExternRef<ClampedStatisticSet<u8, 0, 31>> {
let statistic_set = pokemon.value(env).unwrap().individual_values();
ExternRef::new(env.data().as_ref(), statistic_set)
}
fn pokemon_get_effort_values(
env: &WebAssemblyEnv,
pokemon: ExternRef<Pokemon>,
) -> ExternRef<ClampedStatisticSet<u8, 0, 252>> {
let statistic_set = pokemon.value(env).unwrap().effort_values();
ExternRef::new(env.data().as_ref(), statistic_set)
}
fn pokemon_get_species(
env: &WebAssemblyEnv,
pokemon: ExternRef<Pokemon>,
) -> ExternRef<Species> {
let species = pokemon.value(env).unwrap().species();
ExternRef::new(env.data().as_ref(), species)
}
fn pokemon_damage(
env: &WebAssemblyEnv,
pokemon: ExternRef<Pokemon>,
damage: u32,
source: u8
) {
unsafe{
pokemon.value(env).unwrap().damage(damage, transmute(source));
}
}
}

View File

@@ -0,0 +1,61 @@
use std::ops::Deref;
use crate::dynamic_data::{LearnedMove, Pokemon, TurnChoice};
use crate::script_implementations::wasm::export_registry::register;
use crate::script_implementations::wasm::extern_ref::ExternRef;
use crate::script_implementations::wasm::script_resolver::WebAssemblyEnv;
register! {
fn turn_choice_get_user<'a>(
env: &WebAssemblyEnv,
turn_choice: ExternRef<TurnChoice<'a, 'a>>,
) -> ExternRef<Pokemon<'a, 'a>> {
let turn_choice = turn_choice.value(env).unwrap();
ExternRef::new(env.data().as_ref(), turn_choice.user().as_ref().deref())
}
fn turn_choice_get_kind(
env: &WebAssemblyEnv,
turn_choice: ExternRef<TurnChoice>,
) -> u8 {
match turn_choice.value(env).unwrap() {
TurnChoice::Move(_) => 0,
TurnChoice::Item(_) => 1,
TurnChoice::Switch(_) => 2,
TurnChoice::Flee(_) => 3,
TurnChoice::Pass(_) => 4,
}
}
fn turn_choice_move_used_move<'a>(
env: &WebAssemblyEnv,
turn_choice: ExternRef<TurnChoice<'a, 'a>>,
) -> ExternRef<LearnedMove<'a>> {
if let TurnChoice::Move(d) = turn_choice.value(env).unwrap() {
return ExternRef::new(env.data().as_ref(), d.used_move().as_ref());
}
panic!("Invalid turn choice");
}
fn turn_choice_move_target_side(
env: &WebAssemblyEnv,
turn_choice: ExternRef<TurnChoice>,
) -> u8 {
if let TurnChoice::Move(d) = turn_choice.value(env).unwrap() {
return d.target_side();
}
panic!("Invalid turn choice");
}
fn turn_choice_move_target_index(
env: &WebAssemblyEnv,
turn_choice: ExternRef<TurnChoice>,
) -> u8 {
if let TurnChoice::Move(d) = turn_choice.value(env).unwrap() {
return d.target_index();
}
panic!("Invalid turn choice");
}
}