Initial work on mocking so we can unit test.

This commit is contained in:
Deukhoofd 2022-08-17 18:05:38 +02:00
parent 45b16f415f
commit 98130706fb
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
39 changed files with 580 additions and 34 deletions

View File

@ -1,4 +1,5 @@
[workspace] [workspace]
edition = "2021"
members = [ members = [
"pkmn_lib_interface", "pkmn_lib_interface",
@ -9,4 +10,5 @@ members = [
# Tell `rustc` to optimize for small code size. # Tell `rustc` to optimize for small code size.
opt-level = "s" opt-level = "s"
lto = false lto = false
debug-assertions = true debug-assertions = true

View File

@ -2,10 +2,13 @@
name = "gen7_scripts" name = "gen7_scripts"
version = "0.1.0" version = "0.1.0"
authors = ["Deukhoofd <Deukhoofd@gmail.com>"] authors = ["Deukhoofd <Deukhoofd@gmail.com>"]
edition = "2018" edition = "2021"
[lib] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
pkmn_lib_interface = { path = "../pkmn_lib_interface" } pkmn_lib_interface = { path = "../pkmn_lib_interface" }
[dev-dependencies]
pkmn_lib_interface = { path = "../pkmn_lib_interface", features = ["mock_data"] }

View File

@ -1,7 +1,7 @@
#![feature(inline_const)] #![feature(inline_const)]
#![feature(inline_const_pat)] #![feature(inline_const_pat)]
#![feature(wasm_abi)] #![feature(wasm_abi)]
#![no_std] #![cfg_attr(not(test), no_std)]
#![allow(incomplete_features)] #![allow(incomplete_features)]
extern crate alloc; extern crate alloc;
@ -15,6 +15,7 @@ pub mod registered_scripts;
pub mod test_script; pub mod test_script;
#[no_mangle] #[no_mangle]
#[cfg(not(test))]
extern "wasm" fn _init() { extern "wasm" fn _init() {
set_load_script_fn(Box::new(registered_scripts::get_script)); set_load_script_fn(Box::new(registered_scripts::get_script));
} }

View File

@ -1,6 +1,6 @@
use crate::test_script::TestScript; use crate::test_script::TestScript;
use alloc::boxed::Box; use alloc::boxed::Box;
use pkmn_lib_interface::app_interface::{get_hash, StringKey}; use pkmn_lib_interface::app_interface::{get_hash_const, StringKey};
use pkmn_lib_interface::handling::{Script, ScriptCategory}; use pkmn_lib_interface::handling::{Script, ScriptCategory};
macro_rules! resolve_match { macro_rules! resolve_match {
@ -12,7 +12,7 @@ macro_rules! resolve_match {
) => ( ) => (
match $mid { match $mid {
$( $(
const { get_hash($key) } => { const { get_hash_const($key) } => {
return Some(Box::new($script {})) return Some(Box::new($script {}))
} }
)* )*

View File

@ -1,6 +1,6 @@
use pkmn_lib_interface::app_interface::list::ImmutableList; use pkmn_lib_interface::app_interface::list::ImmutableList;
use pkmn_lib_interface::app_interface::{ use pkmn_lib_interface::app_interface::{
get_hash, DamageSource, DataLibrary, DynamicLibrary, EffectParameter, TurnChoice, get_hash_const, DamageSource, DataLibrary, DynamicLibrary, EffectParameter, TurnChoice,
}; };
use pkmn_lib_interface::dbg; use pkmn_lib_interface::dbg;
use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities};
@ -31,14 +31,13 @@ impl Script for TestScript {
) { ) {
let l = library.data_library(); let l = library.data_library();
let ml = l.move_library(); let ml = l.move_library();
let m = ml.get_by_hash(const { get_hash(b"tackle") }).unwrap(); let m = ml.get_by_hash(const { get_hash_const(b"tackle") }).unwrap();
dbg!("found move!"); dbg!("found move!");
dbg!("{:?} has {} base power", m.name().str(), m.base_power()); dbg!("{:?} has {} base power", m.name().str(), m.base_power());
dbg!( dbg!(
"Found a parameter with value: {}", "Found a parameter with value: {}",
parameters.unwrap().get(0).unwrap() parameters.unwrap().get(0).unwrap()
); );
if m.has_flag(b"foo") {}
} }
fn on_before_turn(&self, choice: TurnChoice) { fn on_before_turn(&self, choice: TurnChoice) {
@ -46,7 +45,7 @@ impl Script for TestScript {
"On before turn for user: {}", "On before turn for user: {}",
choice.user().species().name() choice.user().species().name()
); );
choice.user().damage(50, DamageSource::Misc); // choice.user().damage(50, DamageSource::Misc);
if let TurnChoice::Move(d) = choice { if let TurnChoice::Move(d) = choice {
dbg!( dbg!(
"On before turn for move choice: {}", "On before turn for move choice: {}",
@ -55,3 +54,49 @@ impl Script for TestScript {
} }
} }
} }
#[cfg(test)]
mod test {
use super::*;
use pkmn_lib_interface::app_interface::move_library::MoveLibrary;
use pkmn_lib_interface::app_interface::species_library::SpeciesLibrary;
use pkmn_lib_interface::app_interface::type_library::TypeLibrary;
use pkmn_lib_interface::app_interface::{
Item, ItemLibrary, LibrarySettings, MoveCategory, MoveData, MoveTarget, StaticData,
};
use pkmn_lib_interface::handling::Script;
#[test]
fn test_foo() {
let item = Item::mock();
assert_eq!(item.name().str().to_str().unwrap(), "test");
let script = TestScript::new();
assert_eq!(script.get_name(), "TestScript");
let lib = DynamicLibrary::new(StaticData::mock(
MoveLibrary::mock(),
ItemLibrary::mock(),
SpeciesLibrary::mock(),
TypeLibrary::mock(),
LibrarySettings::mock(100),
));
lib.data_library().move_library().insert(
const { get_hash_const(b"tackle") },
MoveData::mock(
"tackle",
0,
MoveCategory::Physical,
60,
100,
10,
MoveTarget::Adjacent,
0,
),
);
script.on_initialize(
&lib,
Some(ImmutableList::mock((&[EffectParameter::Int(100)]).to_vec())),
);
}
}

View File

@ -2,7 +2,10 @@
name = "pkmn_lib_interface" name = "pkmn_lib_interface"
version = "0.1.0" version = "0.1.0"
authors = ["Deukhoofd <Deukhoofd@gmail.com>"] authors = ["Deukhoofd <Deukhoofd@gmail.com>"]
edition = "2018" edition = "2021"
[features]
mock_data = []
[dependencies] [dependencies]
wee_alloc = "0.4.5" wee_alloc = "0.4.5"

View File

@ -21,6 +21,7 @@ pub struct Battle {
} }
impl Battle { impl Battle {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<Battle>) -> Self { pub fn new(reference: ExternRef<Battle>) -> Self {
Self::from_ref(reference, &|reference| Self { Self::from_ref(reference, &|reference| Self {
inner: Rc::new(BattleInner { inner: Rc::new(BattleInner {
@ -39,6 +40,7 @@ impl Battle {
pub fn sides(&self) -> ImmutableList<BattleSide>; pub fn sides(&self) -> ImmutableList<BattleSide>;
} }
#[cfg(not(feature = "mock_data"))]
pub fn get_pokemon(&self, side: u8, index: u8) -> Option<Pokemon> { pub fn get_pokemon(&self, side: u8, index: u8) -> Option<Pokemon> {
unsafe { battle_get_pokemon(self.inner.reference, side, index).get_value() } unsafe { battle_get_pokemon(self.inner.reference, side, index).get_value() }
} }
@ -57,12 +59,14 @@ wasm_value_getters! {
crate::handling::cacheable::cacheable!(Battle); crate::handling::cacheable::cacheable!(Battle);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Battle { impl ExternalReferenceType for Battle {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn battle_get_library(r: ExternRef<Battle>) -> ExternRef<DynamicLibrary>; fn battle_get_library(r: ExternRef<Battle>) -> ExternRef<DynamicLibrary>;
fn battle_get_parties(r: ExternRef<Battle>) -> VecExternRef<BattleParty>; fn battle_get_parties(r: ExternRef<Battle>) -> VecExternRef<BattleParty>;

View File

@ -15,6 +15,7 @@ pub struct BattleParty {
} }
impl BattleParty { impl BattleParty {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<BattleParty>) -> Self { pub fn new(reference: ExternRef<BattleParty>) -> Self {
Self::from_ref(reference, &|reference| Self { Self::from_ref(reference, &|reference| Self {
inner: Rc::new(BattlePartyInner { inner: Rc::new(BattlePartyInner {
@ -31,12 +32,15 @@ impl BattleParty {
crate::handling::cacheable::cacheable!(BattleParty); crate::handling::cacheable::cacheable!(BattleParty);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for BattleParty { impl ExternalReferenceType for BattleParty {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn battle_party_get_party(r: ExternRef<BattleParty>) -> ExternRef<Party>; fn battle_party_get_party(r: ExternRef<BattleParty>) -> ExternRef<Party>;
} }

View File

@ -6,12 +6,15 @@ pub struct BattleRandom {
} }
impl BattleRandom { impl BattleRandom {
#[cfg(not(feature = "mock_data"))]
pub fn get(&self) -> i32 { pub fn get(&self) -> i32 {
unsafe { battle_random_get(self.reference) } unsafe { battle_random_get(self.reference) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn get_max(&self, max: i32) -> i32 { pub fn get_max(&self, max: i32) -> i32 {
unsafe { battle_random_get_max(self.reference, max) } unsafe { battle_random_get_max(self.reference, max) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn get_between(&self, min: i32, max: i32) -> i32 { pub fn get_between(&self, min: i32, max: i32) -> i32 {
unsafe { battle_random_get_between(self.reference, min, max) } unsafe { battle_random_get_between(self.reference, min, max) }
} }
@ -24,6 +27,7 @@ impl ExternalReferenceType for BattleRandom {
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn battle_random_get(r: ExternRef<BattleRandom>) -> i32; fn battle_random_get(r: ExternRef<BattleRandom>) -> i32;
fn battle_random_get_max(r: ExternRef<BattleRandom>, max: i32) -> i32; fn battle_random_get_max(r: ExternRef<BattleRandom>, max: i32) -> i32;

View File

@ -19,6 +19,7 @@ pub struct BattleSide {
} }
impl BattleSide { impl BattleSide {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<Self>) -> Self { pub fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self { Self::from_ref(reference, &|reference| Self {
inner: Rc::new(BattleSideInner { inner: Rc::new(BattleSideInner {
@ -36,6 +37,7 @@ impl BattleSide {
pub fn battle(&self) -> Battle; pub fn battle(&self) -> Battle;
} }
#[cfg(not(feature = "mock_data"))]
pub fn get_pokemon(&self, index: usize) -> Option<Pokemon> { pub fn get_pokemon(&self, index: usize) -> Option<Pokemon> {
unsafe { battleside_get_pokemon(self.inner.reference, index).get_value() } unsafe { battleside_get_pokemon(self.inner.reference, index).get_value() }
} }
@ -49,12 +51,14 @@ wasm_value_getters! {
crate::handling::cacheable::cacheable!(BattleSide); crate::handling::cacheable::cacheable!(BattleSide);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for BattleSide { impl ExternalReferenceType for BattleSide {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn battleside_get_side_index(r: ExternRef<BattleSide>) -> u8; fn battleside_get_side_index(r: ExternRef<BattleSide>) -> u8;
fn battleside_get_pokemon_per_side(r: ExternRef<BattleSide>) -> u8; fn battleside_get_pokemon_per_side(r: ExternRef<BattleSide>) -> u8;

View File

@ -17,6 +17,7 @@ pub struct DynamicLibrary {
crate::handling::cacheable::cacheable!(DynamicLibrary); crate::handling::cacheable::cacheable!(DynamicLibrary);
impl DynamicLibrary { impl DynamicLibrary {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<Self>) -> Self { pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
Self::from_ref(ptr, &|ptr| Self { Self::from_ref(ptr, &|ptr| Self {
inner: Rc::new(DynamicLibraryInner { inner: Rc::new(DynamicLibraryInner {
@ -28,17 +29,29 @@ impl DynamicLibrary {
}) })
} }
#[cfg(feature = "mock_data")]
pub fn new(data: StaticData) -> Self {
Self {
inner: Rc::new(DynamicLibraryInner {
ptr: ExternRef::mock(),
static_data: data.into(),
}),
}
}
pub fn data_library(&self) -> StaticData { pub fn data_library(&self) -> StaticData {
self.inner.static_data.value() self.inner.static_data.value()
} }
} }
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for DynamicLibrary { impl ExternalReferenceType for DynamicLibrary {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
DynamicLibrary::new(reference) DynamicLibrary::new(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn dynamic_library_get_static_data(ptr: ExternRef<DynamicLibrary>) -> ExternRef<StaticData>; fn dynamic_library_get_static_data(ptr: ExternRef<DynamicLibrary>) -> ExternRef<StaticData>;
} }

View File

@ -30,6 +30,7 @@ pub struct LearnedMove {
crate::handling::cacheable::cacheable!(LearnedMove); crate::handling::cacheable::cacheable!(LearnedMove);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for LearnedMove { impl ExternalReferenceType for LearnedMove {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
@ -37,6 +38,7 @@ impl ExternalReferenceType for LearnedMove {
} }
impl LearnedMove { impl LearnedMove {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<Self>) -> Self { pub fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self { Self::from_ref(reference, &|reference| Self {
inner: Rc::new(LearnedMoveInner { inner: Rc::new(LearnedMoveInner {
@ -54,12 +56,14 @@ impl LearnedMove {
pub fn learn_method(&self) -> MoveLearnMethod; pub fn learn_method(&self) -> MoveLearnMethod;
} }
#[cfg(not(feature = "mock_data"))]
pub fn restore_all_uses(&self) { pub fn restore_all_uses(&self) {
unsafe { unsafe {
learned_move_restore_all_uses(self.inner.reference); learned_move_restore_all_uses(self.inner.reference);
} }
} }
#[cfg(not(feature = "mock_data"))]
pub fn restore_uses(&self, uses: u8) { pub fn restore_uses(&self, uses: u8) {
unsafe { unsafe {
learned_move_restore_uses(self.inner.reference, uses); learned_move_restore_uses(self.inner.reference, uses);
@ -73,6 +77,7 @@ wasm_value_getters! {
pub fn remaining_pp(&self) -> u8; pub fn remaining_pp(&self) -> u8;
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn learned_move_get_move_data(r: ExternRef<LearnedMove>) -> ExternRef<MoveData>; fn learned_move_get_move_data(r: ExternRef<LearnedMove>) -> ExternRef<MoveData>;
fn learned_move_get_learn_method(r: ExternRef<LearnedMove>) -> MoveLearnMethod; fn learned_move_get_learn_method(r: ExternRef<LearnedMove>) -> MoveLearnMethod;

View File

@ -11,6 +11,7 @@ impl Party {
Self { reference } Self { reference }
} }
#[cfg(not(feature = "mock_data"))]
pub fn get_pokemon(&self, index: usize) -> Option<Pokemon> { pub fn get_pokemon(&self, index: usize) -> Option<Pokemon> {
unsafe { party_get_pokemon(self.reference, index).get_value() } unsafe { party_get_pokemon(self.reference, index).get_value() }
} }
@ -22,6 +23,7 @@ impl ExternalReferenceType for Party {
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn party_get_pokemon(r: ExternRef<Party>, index: usize) -> ExternRef<Pokemon>; fn party_get_pokemon(r: ExternRef<Party>, index: usize) -> ExternRef<Pokemon>;
} }

View File

@ -34,6 +34,7 @@ pub struct Pokemon {
} }
impl Pokemon { impl Pokemon {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<Self>) -> Self { pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self { Self::from_ref(reference, &|reference| Self {
inner: Rc::new(PokemonInner { inner: Rc::new(PokemonInner {
@ -69,33 +70,41 @@ impl Pokemon {
pub fn effort_values(&self) -> ClampedStatisticSet<u8>; pub fn effort_values(&self) -> ClampedStatisticSet<u8>;
} }
#[cfg(not(feature = "mock_data"))]
pub fn has_held_item(&self, name: &str) -> bool { pub fn has_held_item(&self, name: &str) -> bool {
let cstr = CString::new(name).unwrap(); let cstr = CString::new(name).unwrap();
unsafe { pokemon_has_held_item(self.inner.reference, cstr.as_ptr()) } unsafe { pokemon_has_held_item(self.inner.reference, cstr.as_ptr()) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn set_held_item(&self, item: &Item) -> Option<Item> { pub fn set_held_item(&self, item: &Item) -> Option<Item> {
unsafe { pokemon_set_held_item(self.inner.reference, item.reference()).get_value() } unsafe { pokemon_set_held_item(self.inner.reference, item.reference()).get_value() }
} }
#[cfg(not(feature = "mock_data"))]
pub fn remove_held_item(&self) -> Option<Item> { pub fn remove_held_item(&self) -> Option<Item> {
unsafe { pokemon_remove_held_item(self.inner.reference).get_value() } unsafe { pokemon_remove_held_item(self.inner.reference).get_value() }
} }
#[cfg(not(feature = "mock_data"))]
pub fn consume_held_item(&self) -> bool { pub fn consume_held_item(&self) -> bool {
unsafe { pokemon_consume_held_item(self.inner.reference) } unsafe { pokemon_consume_held_item(self.inner.reference) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn max_health(&self) -> u32 { pub fn max_health(&self) -> u32 {
self.boosted_stats().hp() self.boosted_stats().hp()
} }
#[cfg(not(feature = "mock_data"))]
pub fn get_type(&self, index: usize) -> u8 { pub fn get_type(&self, index: usize) -> u8 {
unsafe { pokemon_get_type(self.inner.reference, index) } unsafe { pokemon_get_type(self.inner.reference, index) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn has_type(&self, type_identifier: u8) -> bool { pub fn has_type(&self, type_identifier: u8) -> bool {
unsafe { pokemon_has_type(self.inner.reference, type_identifier) } unsafe { pokemon_has_type(self.inner.reference, type_identifier) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn has_type_by_name(&self, type_name: &str) -> bool { pub fn has_type_by_name(&self, type_name: &str) -> bool {
let type_identifier = self let type_identifier = self
.library() .library()
@ -107,9 +116,12 @@ impl Pokemon {
} }
false false
} }
#[cfg(not(feature = "mock_data"))]
pub fn get_learned_move(&self, index: usize) -> Option<LearnedMove> { pub fn get_learned_move(&self, index: usize) -> Option<LearnedMove> {
unsafe { pokemon_get_learned_move(self.inner.reference, index).get_value() } unsafe { pokemon_get_learned_move(self.inner.reference, index).get_value() }
} }
#[cfg(not(feature = "mock_data"))]
pub fn change_stat_boost( pub fn change_stat_boost(
&self, &self,
stat: Statistic, stat: Statistic,
@ -121,16 +133,19 @@ impl Pokemon {
} }
} }
#[cfg(not(feature = "mock_data"))]
pub fn ability_script(&self) -> Option<&Box<dyn Script>> { pub fn ability_script(&self) -> Option<&Box<dyn Script>> {
unsafe { pokemon_get_ability_script(self.inner.reference).as_ref() } unsafe { pokemon_get_ability_script(self.inner.reference).as_ref() }
} }
#[cfg(not(feature = "mock_data"))]
pub fn change_species(&self, species: Species, form: Form) { pub fn change_species(&self, species: Species, form: Form) {
unsafe { unsafe {
pokemon_change_species(self.inner.reference, species.reference(), form.reference()); pokemon_change_species(self.inner.reference, species.reference(), form.reference());
} }
} }
#[cfg(not(feature = "mock_data"))]
pub fn change_form(&self, form: Form) { pub fn change_form(&self, form: Form) {
unsafe { unsafe {
pokemon_change_form(self.inner.reference, form.reference()); pokemon_change_form(self.inner.reference, form.reference());
@ -141,15 +156,18 @@ impl Pokemon {
self.current_health() == 0 self.current_health() == 0
} }
#[cfg(not(feature = "mock_data"))]
pub fn damage(&self, damage: u32, source: DamageSource) { pub fn damage(&self, damage: u32, source: DamageSource) {
unsafe { pokemon_damage(self.inner.reference, damage, source) } unsafe { pokemon_damage(self.inner.reference, damage, source) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn heal(&self, amount: u32, allow_revive: bool) -> bool { pub fn heal(&self, amount: u32, allow_revive: bool) -> bool {
unsafe { pokemon_heal(self.inner.reference, amount, allow_revive) } unsafe { pokemon_heal(self.inner.reference, amount, allow_revive) }
} }
} }
#[cfg(not(feature = "mock_data"))]
wasm_reference_getters! { wasm_reference_getters! {
Pokemon, Pokemon,
pub fn species(&self) -> Species; pub fn species(&self) -> Species;
@ -158,6 +176,7 @@ wasm_reference_getters! {
pub fn nature(&self) -> Nature; pub fn nature(&self) -> Nature;
} }
#[cfg(not(feature = "mock_data"))]
wasm_optional_reference_getters! { wasm_optional_reference_getters! {
Pokemon, Pokemon,
pub fn display_species(&self) -> Option<Species>; pub fn display_species(&self) -> Option<Species>;
@ -166,6 +185,7 @@ wasm_optional_reference_getters! {
pub fn battle(&self) -> Option<Battle>; pub fn battle(&self) -> Option<Battle>;
} }
#[cfg(not(feature = "mock_data"))]
wasm_value_getters! { wasm_value_getters! {
Pokemon, Pokemon,
pub fn level(&self) -> LevelInt; pub fn level(&self) -> LevelInt;
@ -198,12 +218,14 @@ pub enum DamageSource {
crate::handling::cacheable::cacheable!(Pokemon); crate::handling::cacheable::cacheable!(Pokemon);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Pokemon { impl ExternalReferenceType for Pokemon {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn pokemon_get_library(r: ExternRef<Pokemon>) -> ExternRef<DynamicLibrary>; fn pokemon_get_library(r: ExternRef<Pokemon>) -> ExternRef<DynamicLibrary>;
fn pokemon_get_flat_stats(r: ExternRef<Pokemon>) -> ExternRef<StatisticSet<u32>>; fn pokemon_get_flat_stats(r: ExternRef<Pokemon>) -> ExternRef<StatisticSet<u32>>;
@ -234,3 +256,13 @@ extern "wasm" {
fn pokemon_damage(r: ExternRef<Pokemon>, damage: u32, source: DamageSource); fn pokemon_damage(r: ExternRef<Pokemon>, damage: u32, source: DamageSource);
fn pokemon_heal(r: ExternRef<Pokemon>, amount: u32, allow_revive: bool) -> bool; fn pokemon_heal(r: ExternRef<Pokemon>, amount: u32, allow_revive: bool) -> bool;
} }
#[cfg(feature = "mock_data")]
impl Pokemon {
pub fn current_health(&self) -> u32 {
unimplemented!()
}
pub fn species(&self) -> Species {
unimplemented!()
}
}

View File

@ -28,25 +28,32 @@ where
} }
} }
#[cfg(not(feature = "mock_data"))]
pub fn hp(&self) -> T { pub fn hp(&self) -> T {
self.get_stat(Statistic::HP) self.get_stat(Statistic::HP)
} }
#[cfg(not(feature = "mock_data"))]
pub fn attack(&self) -> T { pub fn attack(&self) -> T {
self.get_stat(Statistic::Attack) self.get_stat(Statistic::Attack)
} }
#[cfg(not(feature = "mock_data"))]
pub fn defense(&self) -> T { pub fn defense(&self) -> T {
self.get_stat(Statistic::Defense) self.get_stat(Statistic::Defense)
} }
#[cfg(not(feature = "mock_data"))]
pub fn special_attack(&self) -> T { pub fn special_attack(&self) -> T {
self.get_stat(Statistic::SpecialAttack) self.get_stat(Statistic::SpecialAttack)
} }
#[cfg(not(feature = "mock_data"))]
pub fn special_defense(&self) -> T { pub fn special_defense(&self) -> T {
self.get_stat(Statistic::SpecialDefense) self.get_stat(Statistic::SpecialDefense)
} }
#[cfg(not(feature = "mock_data"))]
pub fn speed(&self) -> T { pub fn speed(&self) -> T {
self.get_stat(Statistic::Speed) self.get_stat(Statistic::Speed)
} }
#[cfg(not(feature = "mock_data"))]
pub fn get_stat(&self, stat: Statistic) -> T { pub fn get_stat(&self, stat: Statistic) -> T {
unsafe { unsafe {
statistic_set_get(self.reference.cast(), stat) statistic_set_get(self.reference.cast(), stat)
@ -55,15 +62,18 @@ where
} }
} }
#[cfg(not(feature = "mock_data"))]
pub fn set_stat(&self, stat: Statistic, value: T) { pub fn set_stat(&self, stat: Statistic, value: T) {
unsafe { statistic_set_set(self.reference.cast(), stat, value.try_into().unwrap()) } unsafe { statistic_set_set(self.reference.cast(), stat, value.try_into().unwrap()) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn increase_stat(&self, stat: Statistic, value: T) { pub fn increase_stat(&self, stat: Statistic, value: T) {
unsafe { unsafe {
statistic_set_increase_stat(self.reference.cast(), stat, value.try_into().unwrap()) statistic_set_increase_stat(self.reference.cast(), stat, value.try_into().unwrap())
} }
} }
#[cfg(not(feature = "mock_data"))]
pub fn decrease_stat(&self, stat: Statistic, value: T) { pub fn decrease_stat(&self, stat: Statistic, value: T) {
unsafe { unsafe {
statistic_set_decrease_stat(self.reference.cast(), stat, value.try_into().unwrap()) statistic_set_decrease_stat(self.reference.cast(), stat, value.try_into().unwrap())
@ -106,25 +116,33 @@ where
_p: Default::default(), _p: Default::default(),
} }
} }
#[cfg(not(feature = "mock_data"))]
pub fn hp(&self) -> T { pub fn hp(&self) -> T {
self.get_stat(Statistic::HP) self.get_stat(Statistic::HP)
} }
#[cfg(not(feature = "mock_data"))]
pub fn attack(&self) -> T { pub fn attack(&self) -> T {
self.get_stat(Statistic::Attack) self.get_stat(Statistic::Attack)
} }
#[cfg(not(feature = "mock_data"))]
pub fn defense(&self) -> T { pub fn defense(&self) -> T {
self.get_stat(Statistic::Defense) self.get_stat(Statistic::Defense)
} }
#[cfg(not(feature = "mock_data"))]
pub fn special_attack(&self) -> T { pub fn special_attack(&self) -> T {
self.get_stat(Statistic::SpecialAttack) self.get_stat(Statistic::SpecialAttack)
} }
#[cfg(not(feature = "mock_data"))]
pub fn special_defense(&self) -> T { pub fn special_defense(&self) -> T {
self.get_stat(Statistic::SpecialDefense) self.get_stat(Statistic::SpecialDefense)
} }
#[cfg(not(feature = "mock_data"))]
pub fn speed(&self) -> T { pub fn speed(&self) -> T {
self.get_stat(Statistic::Speed) self.get_stat(Statistic::Speed)
} }
#[cfg(not(feature = "mock_data"))]
pub fn get_stat(&self, stat: Statistic) -> T { pub fn get_stat(&self, stat: Statistic) -> T {
unsafe { unsafe {
clamped_statistic_set_get(self.reference.cast(), stat) clamped_statistic_set_get(self.reference.cast(), stat)
@ -133,10 +151,12 @@ where
} }
} }
#[cfg(not(feature = "mock_data"))]
pub fn set_stat(&self, stat: Statistic, value: T) { pub fn set_stat(&self, stat: Statistic, value: T) {
unsafe { clamped_statistic_set_set(self.reference.cast(), stat, value.try_into().unwrap()) } unsafe { clamped_statistic_set_set(self.reference.cast(), stat, value.try_into().unwrap()) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn increase_stat(&self, stat: Statistic, value: T) -> bool { pub fn increase_stat(&self, stat: Statistic, value: T) -> bool {
unsafe { unsafe {
clamped_statistic_set_increase_stat( clamped_statistic_set_increase_stat(
@ -146,6 +166,7 @@ where
) )
} }
} }
#[cfg(not(feature = "mock_data"))]
pub fn decrease_stat(&self, stat: Statistic, value: T) -> bool { pub fn decrease_stat(&self, stat: Statistic, value: T) -> bool {
unsafe { unsafe {
clamped_statistic_set_decrease_stat( clamped_statistic_set_decrease_stat(
@ -169,6 +190,7 @@ where
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn statistic_set_get(r: ExternRef<StatisticSet<i64>>, stat: Statistic) -> i64; fn statistic_set_get(r: ExternRef<StatisticSet<i64>>, stat: Statistic) -> i64;
fn statistic_set_set(r: ExternRef<StatisticSet<i64>>, stat: Statistic, value: i64); fn statistic_set_set(r: ExternRef<StatisticSet<i64>>, stat: Statistic, value: i64);

View File

@ -1,7 +1,10 @@
use crate::app_interface::{LearnedMove, Pokemon}; use crate::app_interface::{LearnedMove, Pokemon};
use crate::handling::cached_value::CachedValue; use crate::handling::cached_value::CachedValue;
use crate::handling::temporary::Temporary; use crate::handling::temporary::Temporary;
use crate::{cached_value, wasm_value_getters, ExternRef, ExternalReferenceType, Script}; use crate::ExternRef;
#[cfg(not(feature = "mock_data"))]
use crate::{cached_value, wasm_value_getters, ExternalReferenceType, Script};
#[cfg(not(feature = "mock_data"))]
use alloc::boxed::Box; use alloc::boxed::Box;
use alloc::rc::Rc; use alloc::rc::Rc;
@ -47,14 +50,17 @@ impl TurnChoice {
self.base().user.value() self.base().user.value()
} }
#[cfg(not(feature = "mock_data"))]
pub fn speed(&self) -> u32 { pub fn speed(&self) -> u32 {
unsafe { turn_choice_get_speed(self.base().reference) } unsafe { turn_choice_get_speed(self.base().reference) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn has_failed(&self) -> bool { pub fn has_failed(&self) -> bool {
unsafe { turn_choice_has_failed(self.base().reference) } unsafe { turn_choice_has_failed(self.base().reference) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn fail(&self) { pub fn fail(&self) {
unsafe { turn_choice_fail(self.base().reference) } unsafe { turn_choice_fail(self.base().reference) }
} }
@ -70,15 +76,18 @@ impl MoveTurnChoiceData {
pub fn target_index(&self) -> u8 { pub fn target_index(&self) -> u8 {
self.temp.value().inner.target_index.value() self.temp.value().inner.target_index.value()
} }
#[cfg(not(feature = "mock_data"))]
pub fn priority(&self) -> i8 { pub fn priority(&self) -> i8 {
unsafe { turn_choice_move_priority(self.temp.value().inner.base.reference.cast()) } unsafe { turn_choice_move_priority(self.temp.value().inner.base.reference.cast()) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn move_script(&self) -> Option<&Box<dyn Script>> { pub fn move_script(&self) -> Option<&Box<dyn Script>> {
unsafe { turn_choice_move_script(self.temp.value().inner.base.reference.cast()).as_ref() } unsafe { turn_choice_move_script(self.temp.value().inner.base.reference.cast()).as_ref() }
} }
} }
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for TurnChoice { impl ExternalReferenceType for TurnChoice {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
let kind = unsafe { turn_choice_get_kind(reference) }; let kind = unsafe { turn_choice_get_kind(reference) };
@ -91,6 +100,7 @@ impl ExternalReferenceType for TurnChoice {
} }
} }
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for MoveTurnChoiceDataTemporary { impl ExternalReferenceType for MoveTurnChoiceDataTemporary {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self { Self {
@ -113,6 +123,7 @@ impl ExternalReferenceType for MoveTurnChoiceDataTemporary {
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn turn_choice_get_kind(r: ExternRef<TurnChoice>) -> u8; fn turn_choice_get_kind(r: ExternRef<TurnChoice>) -> u8;
fn turn_choice_get_user(r: ExternRef<TurnChoice>) -> ExternRef<Pokemon>; fn turn_choice_get_user(r: ExternRef<TurnChoice>) -> ExternRef<Pokemon>;
@ -128,6 +139,7 @@ extern "wasm" {
} }
#[no_mangle] #[no_mangle]
#[cfg(not(feature = "mock_data"))]
unsafe extern "wasm" fn turn_choice_mark_deleted(r: ExternRef<TurnChoice>, kind: u8) { unsafe extern "wasm" fn turn_choice_mark_deleted(r: ExternRef<TurnChoice>, kind: u8) {
match kind { match kind {
0 => Temporary::<MoveTurnChoiceDataTemporary>::mark_as_deleted(r.cast()), 0 => Temporary::<MoveTurnChoiceDataTemporary>::mark_as_deleted(r.cast()),

View File

@ -1,9 +1,11 @@
use crate::{ExternalReferenceType, VecExternRef}; use crate::{ExternalReferenceType, VecExternRef};
use alloc::boxed::Box; use alloc::boxed::Box;
use alloc::collections::BTreeMap; use alloc::collections::BTreeMap;
use alloc::rc::Rc;
use alloc::vec::Vec; use alloc::vec::Vec;
use core::marker::PhantomData; use core::marker::PhantomData;
#[cfg(not(feature = "mock_data"))]
struct ImmutableListInner<T: Clone> { struct ImmutableListInner<T: Clone> {
extern_ref: VecExternRef<T>, extern_ref: VecExternRef<T>,
resource_type: PhantomData<T>, resource_type: PhantomData<T>,
@ -11,6 +13,7 @@ struct ImmutableListInner<T: Clone> {
} }
#[derive(Clone)] #[derive(Clone)]
#[cfg(not(feature = "mock_data"))]
pub struct ImmutableList<T> pub struct ImmutableList<T>
where where
T: Clone, T: Clone,
@ -19,6 +22,7 @@ where
inner: *const ImmutableListInner<T>, inner: *const ImmutableListInner<T>,
} }
#[cfg(not(feature = "mock_data"))]
impl<T> ImmutableList<T> impl<T> ImmutableList<T>
where where
T: Clone, T: Clone,
@ -71,4 +75,31 @@ where
} }
} }
#[cfg(feature = "mock_data")]
pub struct ImmutableListInner<T> {
values: Vec<T>,
}
#[cfg(feature = "mock_data")]
#[derive(Clone)]
pub struct ImmutableList<T> {
inner: Rc<ImmutableListInner<T>>,
}
#[cfg(feature = "mock_data")]
impl<T> ImmutableList<T>
where
T: Clone,
{
pub fn mock(values: Vec<T>) -> Self {
Self {
inner: Rc::new(ImmutableListInner { values }),
}
}
pub fn get(&self, index: u32) -> Option<T> {
self.inner.values.get(index as usize).cloned()
}
}
static mut CACHE: BTreeMap<u32, *const u8> = BTreeMap::new(); static mut CACHE: BTreeMap<u32, *const u8> = BTreeMap::new();

View File

@ -5,5 +5,5 @@ pub mod string_key;
pub use dynamic_data::*; pub use dynamic_data::*;
pub use static_data::*; pub use static_data::*;
pub use string_key::get_hash; pub use string_key::get_hash_const;
pub use string_key::StringKey; pub use string_key::StringKey;

View File

@ -19,6 +19,7 @@ pub struct Ability {
} }
impl Ability { impl Ability {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<Self>) -> Self { pub fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self { Self::from_ref(reference, &|reference| Self {
inner: Rc::new(AbilityInner { inner: Rc::new(AbilityInner {
@ -38,6 +39,7 @@ impl Ability {
} }
} }
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Ability { impl ExternalReferenceType for Ability {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
@ -53,6 +55,7 @@ pub struct AbilityIndex {
pub index: u8, pub index: u8,
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn ability_get_name(r: ExternRef<Ability>) -> ExternRef<StringKey>; fn ability_get_name(r: ExternRef<Ability>) -> ExternRef<StringKey>;
fn ability_get_effect(r: ExternRef<Ability>) -> ExternRef<StringKey>; fn ability_get_effect(r: ExternRef<Ability>) -> ExternRef<StringKey>;

View File

@ -15,6 +15,7 @@ pub struct ItemLibrary {
} }
impl ItemLibrary { impl ItemLibrary {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<Self>) -> Self { pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
Self { Self {
inner: Rc::new(ItemLibraryInner { inner: Rc::new(ItemLibraryInner {
@ -34,10 +35,12 @@ impl DataLibrary<Item> for ItemLibrary {
self.inner.ptr self.inner.ptr
} }
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_name(ptr: ExternRef<Self>, name: ExternRef<StringKey>) -> ExternRef<Item> { fn _get_ref_by_name(ptr: ExternRef<Self>, name: ExternRef<StringKey>) -> ExternRef<Item> {
unsafe { move_library_get_move(ptr, name) } unsafe { move_library_get_move(ptr, name) }
} }
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<Item> { fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<Item> {
unsafe { move_library_get_move_by_hash(ptr, hash) } unsafe { move_library_get_move_by_hash(ptr, hash) }
} }
@ -45,12 +48,14 @@ impl DataLibrary<Item> for ItemLibrary {
crate::handling::cacheable::cacheable!(ItemLibrary); crate::handling::cacheable::cacheable!(ItemLibrary);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for ItemLibrary { impl ExternalReferenceType for ItemLibrary {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn move_library_get_move( fn move_library_get_move(
ptr: ExternRef<ItemLibrary>, ptr: ExternRef<ItemLibrary>,
@ -58,3 +63,15 @@ extern "wasm" {
) -> ExternRef<Item>; ) -> ExternRef<Item>;
fn move_library_get_move_by_hash(ptr: ExternRef<ItemLibrary>, hash: u32) -> ExternRef<Item>; fn move_library_get_move_by_hash(ptr: ExternRef<ItemLibrary>, hash: u32) -> ExternRef<Item>;
} }
#[cfg(feature = "mock_data")]
impl ItemLibrary {
pub fn mock() -> Self {
Self {
inner: Rc::new(ItemLibraryInner {
ptr: ExternRef::mock(),
cache: Default::default(),
}),
}
}
}

View File

@ -23,6 +23,7 @@ struct StaticDataInner {
item_library: CachedValue<ItemLibrary>, item_library: CachedValue<ItemLibrary>,
species_library: CachedValue<SpeciesLibrary>, species_library: CachedValue<SpeciesLibrary>,
type_library: CachedValue<TypeLibrary>, type_library: CachedValue<TypeLibrary>,
settings: CachedValue<LibrarySettings>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -31,6 +32,7 @@ pub struct StaticData {
} }
impl StaticData { impl StaticData {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<StaticData>) -> Self { pub(crate) fn new(reference: ExternRef<StaticData>) -> Self {
Self::from_ref(reference, &|reference| Self { Self::from_ref(reference, &|reference| Self {
inner: Rc::new(StaticDataInner { inner: Rc::new(StaticDataInner {
@ -53,6 +55,26 @@ impl StaticData {
}) })
} }
#[cfg(feature = "mock_data")]
pub fn mock(
moves: MoveLibrary,
items: ItemLibrary,
species: SpeciesLibrary,
types: TypeLibrary,
settings: LibrarySettings,
) -> Self {
Self {
inner: Rc::new(StaticDataInner {
reference: ExternRef::mock(),
move_library: moves.into(),
item_library: items.into(),
species_library: species.into(),
type_library: types.into(),
settings: settings.into(),
}),
}
}
cached_value_getters! { cached_value_getters! {
pub fn move_library(&self) -> MoveLibrary; pub fn move_library(&self) -> MoveLibrary;
pub fn item_library(&self) -> ItemLibrary; pub fn item_library(&self) -> ItemLibrary;
@ -63,9 +85,10 @@ impl StaticData {
crate::handling::cacheable::cacheable!(StaticData); crate::handling::cacheable::cacheable!(StaticData);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for StaticData { impl ExternalReferenceType for StaticData {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
StaticData::new(reference) StaticData::mock(reference)
} }
} }
@ -79,6 +102,7 @@ pub struct LibrarySettings {
} }
impl LibrarySettings { impl LibrarySettings {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<LibrarySettings>) -> Self { pub(crate) fn new(ptr: ExternRef<LibrarySettings>) -> Self {
Self { Self {
inner: Rc::new(LibrarySettingsInner { inner: Rc::new(LibrarySettingsInner {
@ -87,11 +111,21 @@ impl LibrarySettings {
} }
} }
#[cfg(feature = "mock_data")]
pub fn mock(maximum_level: LevelInt) -> Self {
Self {
inner: Rc::new(LibrarySettingsInner {
maximum_level: maximum_level.into(),
}),
}
}
cached_value_getters! { cached_value_getters! {
pub fn maximum_level(&self) -> LevelInt; pub fn maximum_level(&self) -> LevelInt;
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn static_data_get_move_library(ptr: ExternRef<StaticData>) -> ExternRef<MoveLibrary>; fn static_data_get_move_library(ptr: ExternRef<StaticData>) -> ExternRef<MoveLibrary>;
fn static_data_get_item_library(ptr: ExternRef<StaticData>) -> ExternRef<ItemLibrary>; fn static_data_get_item_library(ptr: ExternRef<StaticData>) -> ExternRef<ItemLibrary>;
@ -101,6 +135,7 @@ extern "wasm" {
fn library_settings_get_maximum_level(ptr: ExternRef<LibrarySettings>) -> LevelInt; fn library_settings_get_maximum_level(ptr: ExternRef<LibrarySettings>) -> LevelInt;
} }
#[cfg(not(feature = "mock_data"))]
pub trait DataLibrary<T>: Cacheable pub trait DataLibrary<T>: Cacheable
where where
T: ExternalReferenceType, T: ExternalReferenceType,
@ -147,3 +182,32 @@ where
v v
} }
} }
#[cfg(feature = "mock_data")]
pub trait DataLibrary<T>: Cacheable
where
T: Clone,
{
fn get_cache(&self) -> &RwLock<BTreeMap<u32, T>>;
fn get_self_ref(&self) -> ExternRef<Self>
where
Self: Sized;
fn get(&self, name: &StringKey) -> Option<T>
where
Self: Sized,
{
self.get_cache().read().get(&name.hash()).cloned()
}
fn get_by_hash(&self, hash: u32) -> Option<T>
where
Self: Sized,
{
self.get_cache().read().get(&hash).cloned()
}
fn insert(&self, hash: u32, item: T) {
self.get_cache().write().insert(hash, item);
}
}

View File

@ -16,6 +16,7 @@ pub struct MoveLibrary {
} }
impl MoveLibrary { impl MoveLibrary {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<Self>) -> Self { pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
Self { Self {
inner: Rc::new(MoveLibraryInner { inner: Rc::new(MoveLibraryInner {
@ -24,6 +25,16 @@ impl MoveLibrary {
}), }),
} }
} }
#[cfg(feature = "mock_data")]
pub fn mock() -> Self {
Self {
inner: Rc::new(MoveLibraryInner {
ptr: ExternRef::mock(),
cache: Default::default(),
}),
}
}
} }
impl DataLibrary<MoveData> for MoveLibrary { impl DataLibrary<MoveData> for MoveLibrary {
@ -35,10 +46,12 @@ impl DataLibrary<MoveData> for MoveLibrary {
self.inner.ptr self.inner.ptr
} }
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_name(ptr: ExternRef<Self>, name: ExternRef<StringKey>) -> ExternRef<MoveData> { fn _get_ref_by_name(ptr: ExternRef<Self>, name: ExternRef<StringKey>) -> ExternRef<MoveData> {
unsafe { move_library_get_move(ptr, name) } unsafe { move_library_get_move(ptr, name) }
} }
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<MoveData> { fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<MoveData> {
unsafe { move_library_get_move_by_hash(ptr, hash) } unsafe { move_library_get_move_by_hash(ptr, hash) }
} }
@ -46,12 +59,14 @@ impl DataLibrary<MoveData> for MoveLibrary {
crate::handling::cacheable::cacheable!(MoveLibrary); crate::handling::cacheable::cacheable!(MoveLibrary);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for MoveLibrary { impl ExternalReferenceType for MoveLibrary {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn move_library_get_move( fn move_library_get_move(
ptr: ExternRef<MoveLibrary>, ptr: ExternRef<MoveLibrary>,

View File

@ -15,7 +15,8 @@ pub struct SpeciesLibrary {
} }
impl SpeciesLibrary { impl SpeciesLibrary {
pub(crate) fn new(ptr: ExternRef<SpeciesLibrary>) -> Self { #[cfg(not(feature = "mock_data"))]
pub fn new(ptr: ExternRef<SpeciesLibrary>) -> Self {
Self { Self {
inner: Rc::new(SpeciesLibraryInner { inner: Rc::new(SpeciesLibraryInner {
ptr, ptr,
@ -23,6 +24,16 @@ impl SpeciesLibrary {
}), }),
} }
} }
#[cfg(feature = "mock_data")]
pub fn mock() -> Self {
Self {
inner: Rc::new(SpeciesLibraryInner {
ptr: ExternRef::mock(),
cache: Default::default(),
}),
}
}
} }
impl DataLibrary<Species> for SpeciesLibrary { impl DataLibrary<Species> for SpeciesLibrary {
@ -34,10 +45,12 @@ impl DataLibrary<Species> for SpeciesLibrary {
self.inner.ptr self.inner.ptr
} }
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_name(ptr: ExternRef<Self>, name: ExternRef<StringKey>) -> ExternRef<Species> { fn _get_ref_by_name(ptr: ExternRef<Self>, name: ExternRef<StringKey>) -> ExternRef<Species> {
unsafe { species_library_get_species(ptr, name) } unsafe { species_library_get_species(ptr, name) }
} }
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<Species> { fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<Species> {
unsafe { species_library_get_species_by_hash(ptr, hash) } unsafe { species_library_get_species_by_hash(ptr, hash) }
} }
@ -45,12 +58,14 @@ impl DataLibrary<Species> for SpeciesLibrary {
crate::handling::cacheable::cacheable!(SpeciesLibrary); crate::handling::cacheable::cacheable!(SpeciesLibrary);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for SpeciesLibrary { impl ExternalReferenceType for SpeciesLibrary {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn species_library_get_species( fn species_library_get_species(
ptr: ExternRef<SpeciesLibrary>, ptr: ExternRef<SpeciesLibrary>,

View File

@ -17,6 +17,7 @@ pub struct TypeLibrary {
} }
impl TypeLibrary { impl TypeLibrary {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<Self>) -> Self { pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self { Self {
inner: Rc::new(TypeLibraryInner { inner: Rc::new(TypeLibraryInner {
@ -26,7 +27,18 @@ impl TypeLibrary {
}), }),
} }
} }
#[cfg(feature = "mock_data")]
pub fn mock() -> Self {
Self {
inner: Rc::new(TypeLibraryInner {
reference: ExternRef::mock(),
name_to_type_cache: Default::default(),
effectiveness_cache: Default::default(),
}),
}
}
#[cfg(not(feature = "mock_data"))]
pub fn get_type_from_name(&self, name: &str) -> Option<u8> { pub fn get_type_from_name(&self, name: &str) -> Option<u8> {
if let Some(cached) = self.inner.name_to_type_cache.read().get(name) { if let Some(cached) = self.inner.name_to_type_cache.read().get(name) {
return Some(*cached); return Some(*cached);
@ -43,6 +55,7 @@ impl TypeLibrary {
Some(v) Some(v)
} }
#[cfg(not(feature = "mock_data"))]
pub fn get_single_effectiveness(&self, attacking_type: u8, defending_type: u8) -> f32 { pub fn get_single_effectiveness(&self, attacking_type: u8, defending_type: u8) -> f32 {
if let Some(cached) = self if let Some(cached) = self
.inner .inner
@ -66,6 +79,7 @@ impl TypeLibrary {
effectiveness effectiveness
} }
#[cfg(not(feature = "mock_data"))]
pub fn get_effectiveness(&self, attacking_type: u8, defending_types: &[u8]) -> f32 { pub fn get_effectiveness(&self, attacking_type: u8, defending_types: &[u8]) -> f32 {
let mut f = 1.0; let mut f = 1.0;
for defending_type in defending_types { for defending_type in defending_types {
@ -77,12 +91,14 @@ impl TypeLibrary {
crate::handling::cacheable::cacheable!(TypeLibrary); crate::handling::cacheable::cacheable!(TypeLibrary);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for TypeLibrary { impl ExternalReferenceType for TypeLibrary {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn type_library_get_single_effectiveness( fn type_library_get_single_effectiveness(
r: ExternRef<TypeLibrary>, r: ExternRef<TypeLibrary>,

View File

@ -21,7 +21,8 @@ pub enum EffectParameter {
} }
impl EffectParameter { impl EffectParameter {
pub(crate) fn create(ptr: ExternRef<Self>) -> Self { #[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
unsafe { unsafe {
match effect_parameter_get_type(ptr) { match effect_parameter_get_type(ptr) {
EffectParameterType::None => Self::None, EffectParameterType::None => Self::None,
@ -36,12 +37,13 @@ impl EffectParameter {
} }
} }
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for EffectParameter { impl ExternalReferenceType for EffectParameter {
fn from_extern_value(reference: ExternRef<Self>) -> Self fn from_extern_value(reference: ExternRef<Self>) -> Self
where where
Self: Sized, Self: Sized,
{ {
EffectParameter::create(reference) EffectParameter::new(reference)
} }
} }
@ -59,6 +61,7 @@ impl Display for EffectParameter {
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn effect_parameter_get_type(ptr: ExternRef<EffectParameter>) -> EffectParameterType; fn effect_parameter_get_type(ptr: ExternRef<EffectParameter>) -> EffectParameterType;
fn effect_parameter_as_bool(ptr: ExternRef<EffectParameter>) -> bool; fn effect_parameter_as_bool(ptr: ExternRef<EffectParameter>) -> bool;

View File

@ -56,6 +56,7 @@ pub struct Item {
} }
impl Item { impl Item {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<Self>) -> Self { pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self { Self::from_ref(reference, &|reference| Self {
inner: Rc::new(ItemInner { inner: Rc::new(ItemInner {
@ -68,6 +69,7 @@ impl Item {
}) })
} }
#[cfg(not(feature = "mock_data"))]
pub(crate) fn reference(&self) -> ExternRef<Self> { pub(crate) fn reference(&self) -> ExternRef<Self> {
self.inner.reference self.inner.reference
} }
@ -83,6 +85,7 @@ impl Item {
pub fn price(&self) -> i32; pub fn price(&self) -> i32;
} }
#[cfg(not(feature = "mock_data"))]
pub fn has_flag(&self, flag: &StringKey) -> bool { pub fn has_flag(&self, flag: &StringKey) -> bool {
unsafe { item_has_flag(self.inner.reference, flag.ptr()) } unsafe { item_has_flag(self.inner.reference, flag.ptr()) }
} }
@ -90,12 +93,14 @@ impl Item {
crate::handling::cacheable::cacheable!(Item); crate::handling::cacheable::cacheable!(Item);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Item { impl ExternalReferenceType for Item {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Item::new(reference) Item::mock(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn item_get_name(ptr: ExternRef<Item>) -> ExternRef<StringKey>; fn item_get_name(ptr: ExternRef<Item>) -> ExternRef<StringKey>;
fn item_get_category(ptr: ExternRef<Item>) -> ItemCategory; fn item_get_category(ptr: ExternRef<Item>) -> ItemCategory;
@ -103,3 +108,30 @@ extern "wasm" {
fn item_get_price(ptr: ExternRef<Item>) -> i32; fn item_get_price(ptr: ExternRef<Item>) -> i32;
fn item_has_flag(ptr: ExternRef<Item>, flag: ExternRef<StringKey>) -> bool; fn item_has_flag(ptr: ExternRef<Item>, flag: ExternRef<StringKey>) -> bool;
} }
#[cfg(feature = "mock_data")]
mod test {
use super::Item;
use super::ItemInner;
use crate::app_interface::{BattleItemCategory, ItemCategory};
use crate::{cached_value, ExternRef, StringKey};
use alloc::rc::Rc;
impl Item {
pub fn mock() -> Self {
Self {
inner: Rc::new(ItemInner {
reference: ExternRef::mock(),
name: StringKey::new("test").into(),
category: ItemCategory::MiscItem.into(),
battle_category: BattleItemCategory::None.into(),
price: 0.into(),
}),
}
}
pub fn has_flag(&self) -> bool {
unimplemented!()
}
}
}

View File

@ -1,4 +1,4 @@
use crate::app_interface::{get_hash, StringKey}; use crate::app_interface::{get_hash_const, StringKey};
use crate::handling::cached_value::CachedValue; use crate::handling::cached_value::CachedValue;
use crate::handling::Cacheable; use crate::handling::Cacheable;
use crate::{cached_value, cached_value_getters, ExternRef, ExternalReferenceType}; use crate::{cached_value, cached_value_getters, ExternRef, ExternalReferenceType};
@ -50,6 +50,7 @@ pub struct MoveData {
} }
impl MoveData { impl MoveData {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<Self>) -> Self { pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
MoveData::from_ref(ptr, &|ptr| Self { MoveData::from_ref(ptr, &|ptr| Self {
inner: Rc::new(MoveDataInner { inner: Rc::new(MoveDataInner {
@ -65,6 +66,31 @@ impl MoveData {
}), }),
}) })
} }
#[cfg(feature = "mock_data")]
pub fn mock(
name: &str,
move_type: u8,
category: MoveCategory,
base_power: u8,
accuracy: u8,
base_usages: u8,
target: MoveTarget,
priority: i8,
) -> Self {
Self {
inner: Rc::new(MoveDataInner {
ptr: ExternRef::mock(),
name: StringKey::new(name).into(),
move_type: move_type.into(),
category: category.into(),
base_power: base_power.into(),
accuracy: accuracy.into(),
base_usages: base_usages.into(),
target: target.into(),
priority: priority.into(),
}),
}
}
cached_value_getters! { cached_value_getters! {
pub fn name(&self) -> StringKey; pub fn name(&self) -> StringKey;
@ -76,12 +102,15 @@ impl MoveData {
pub fn target(&self) -> MoveTarget; pub fn target(&self) -> MoveTarget;
pub fn priority(&self) -> i8; pub fn priority(&self) -> i8;
} }
#[cfg(not(feature = "mock_data"))]
pub fn has_flag<const N: usize>(&self, flag: &[u8; N]) -> bool { pub fn has_flag<const N: usize>(&self, flag: &[u8; N]) -> bool {
let hash = get_hash(flag); let hash = get_hash_const(flag);
unsafe { move_data_has_flag_by_hash(self.inner.ptr, hash) } unsafe { move_data_has_flag_by_hash(self.inner.ptr, hash) }
} }
} }
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for MoveData { impl ExternalReferenceType for MoveData {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
MoveData::new(reference) MoveData::new(reference)
@ -90,6 +119,7 @@ impl ExternalReferenceType for MoveData {
crate::handling::cacheable::cacheable!(MoveData); crate::handling::cacheable::cacheable!(MoveData);
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn move_data_get_name(ptr: ExternRef<MoveData>) -> ExternRef<StringKey>; fn move_data_get_name(ptr: ExternRef<MoveData>) -> ExternRef<StringKey>;
fn move_data_get_type(ptr: ExternRef<MoveData>) -> u8; fn move_data_get_type(ptr: ExternRef<MoveData>) -> u8;

View File

@ -23,6 +23,7 @@ pub struct Nature {
crate::handling::cacheable::cacheable!(Nature); crate::handling::cacheable::cacheable!(Nature);
impl Nature { impl Nature {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<Self>) -> Self { pub fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| unsafe { Self::from_ref(reference, &|reference| unsafe {
Self { Self {
@ -38,12 +39,14 @@ impl Nature {
} }
} }
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Nature { impl ExternalReferenceType for Nature {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn nature_get_increase_stat(r: ExternRef<Nature>) -> Statistic; fn nature_get_increase_stat(r: ExternRef<Nature>) -> Statistic;
fn nature_get_decrease_stat(r: ExternRef<Nature>) -> Statistic; fn nature_get_decrease_stat(r: ExternRef<Nature>) -> Statistic;

View File

@ -1,4 +1,4 @@
use crate::app_interface::get_hash; use crate::app_interface::get_hash_const;
use crate::handling::cached_value::CachedValue; use crate::handling::cached_value::CachedValue;
use crate::handling::Cacheable; use crate::handling::Cacheable;
use crate::{ use crate::{
@ -49,6 +49,7 @@ pub struct ImmutableStatisticSet {
} }
impl ImmutableStatisticSet { impl ImmutableStatisticSet {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<Self>) -> Self { pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self { Self::from_ref(reference, &|reference| Self {
inner: Rc::new(ImmutableStatisticSetInner { inner: Rc::new(ImmutableStatisticSetInner {
@ -106,6 +107,7 @@ pub struct Form {
} }
impl Form { impl Form {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<Self>) -> Self { pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self { Self::from_ref(reference, &|reference| Self {
inner: Rc::new(FormInner { inner: Rc::new(FormInner {
@ -144,8 +146,9 @@ impl Form {
self.inner.types.value_ref() self.inner.types.value_ref()
} }
#[cfg(not(feature = "mock_data"))]
pub fn has_flag<const N: usize>(&self, flag: &[u8; N]) -> bool { pub fn has_flag<const N: usize>(&self, flag: &[u8; N]) -> bool {
let hash = get_hash(flag); let hash = get_hash_const(flag);
unsafe { form_has_flag_by_hash(self.inner.reference, hash) } unsafe { form_has_flag_by_hash(self.inner.reference, hash) }
} }
} }
@ -166,6 +169,7 @@ pub struct Species {
} }
impl Species { impl Species {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<Species>) -> Self { pub(crate) fn new(reference: ExternRef<Species>) -> Self {
Self { Self {
inner: Rc::new(SpeciesInner { inner: Rc::new(SpeciesInner {
@ -200,8 +204,9 @@ impl Species {
pub fn capture_rate(&self) -> u8; pub fn capture_rate(&self) -> u8;
} }
#[cfg(not(feature = "mock_data"))]
pub fn get_form<const N: usize>(&self, form_name: &[u8; N]) -> Option<Form> { pub fn get_form<const N: usize>(&self, form_name: &[u8; N]) -> Option<Form> {
let hash = get_hash(form_name); let hash = get_hash_const(form_name);
unsafe { unsafe {
if let Some(v) = self.inner.forms.read().get(&hash) { if let Some(v) = self.inner.forms.read().get(&hash) {
v.clone() v.clone()
@ -214,24 +219,28 @@ impl Species {
} }
} }
#[cfg(not(feature = "mock_data"))]
pub fn has_flag<const N: usize>(&self, flag: &[u8; N]) -> bool { pub fn has_flag<const N: usize>(&self, flag: &[u8; N]) -> bool {
let hash = get_hash(flag); let hash = get_hash_const(flag);
unsafe { species_has_flag_by_hash(self.inner.reference, hash) } unsafe { species_has_flag_by_hash(self.inner.reference, hash) }
} }
} }
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for ImmutableStatisticSet { impl ExternalReferenceType for ImmutableStatisticSet {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Form { impl ExternalReferenceType for Form {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
} }
} }
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Species { impl ExternalReferenceType for Species {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference) Self::new(reference)
@ -242,6 +251,7 @@ crate::handling::cacheable::cacheable!(ImmutableStatisticSet);
crate::handling::cacheable::cacheable!(Form); crate::handling::cacheable::cacheable!(Form);
crate::handling::cacheable::cacheable!(Species); crate::handling::cacheable::cacheable!(Species);
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn static_statistics_set_get_hp(r: ExternRef<ImmutableStatisticSet>) -> u16; fn static_statistics_set_get_hp(r: ExternRef<ImmutableStatisticSet>) -> u16;
fn static_statistics_set_get_attack(r: ExternRef<ImmutableStatisticSet>) -> u16; fn static_statistics_set_get_attack(r: ExternRef<ImmutableStatisticSet>) -> u16;

View File

@ -17,6 +17,7 @@ pub struct StringKey {
} }
impl StringKey { impl StringKey {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<Self>) -> Self { pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
StringKey::from_ref(ptr, &|ptr| -> StringKey { StringKey::from_ref(ptr, &|ptr| -> StringKey {
StringKey { StringKey {
@ -29,10 +30,12 @@ impl StringKey {
}) })
} }
#[cfg(not(feature = "mock_data"))]
pub(super) fn ptr(&self) -> ExternRef<Self> { pub(super) fn ptr(&self) -> ExternRef<Self> {
self.data.ptr self.data.ptr
} }
#[cfg(not(feature = "mock_data"))]
pub fn str(&self) -> &CString { pub fn str(&self) -> &CString {
if self.data.str.borrow().is_none() { if self.data.str.borrow().is_none() {
unsafe { unsafe {
@ -43,6 +46,13 @@ impl StringKey {
} }
unsafe { (*self.data.str.as_ptr()).as_ref().unwrap() } unsafe { (*self.data.str.as_ptr()).as_ref().unwrap() }
} }
#[cfg(feature = "mock_data")]
pub fn str(&self) -> &CString {
unsafe { (*self.data.str.as_ptr()).as_ref().unwrap() }
}
#[cfg(not(feature = "mock_data"))]
pub fn hash(&self) -> u32 { pub fn hash(&self) -> u32 {
if self.data.hash.borrow().is_none() { if self.data.hash.borrow().is_none() {
unsafe { unsafe {
@ -53,10 +63,16 @@ impl StringKey {
} }
self.data.hash.borrow().unwrap() self.data.hash.borrow().unwrap()
} }
#[cfg(feature = "mock_data")]
pub fn hash(&self) -> u32 {
self.data.hash.borrow().unwrap()
}
} }
crate::handling::cacheable::cacheable!(StringKey); crate::handling::cacheable::cacheable!(StringKey);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for StringKey { impl ExternalReferenceType for StringKey {
fn from_extern_value(reference: ExternRef<Self>) -> Self { fn from_extern_value(reference: ExternRef<Self>) -> Self {
StringKey::new(reference) StringKey::new(reference)
@ -70,6 +86,7 @@ impl Display for StringKey {
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn string_key_get_str(ptr: ExternRef<StringKey>) -> *mut c_char; fn string_key_get_str(ptr: ExternRef<StringKey>) -> *mut c_char;
fn string_key_get_hash(ptr: ExternRef<StringKey>) -> u32; fn string_key_get_hash(ptr: ExternRef<StringKey>) -> u32;
@ -117,7 +134,7 @@ const fn to_lower(c: u8) -> u8 {
c c
} }
pub const fn get_hash<const N: usize>(s: &[u8; N]) -> u32 { pub const fn get_hash_const<const N: usize>(s: &[u8; N]) -> u32 {
let mut crc: u32 = 0xffffffff; let mut crc: u32 = 0xffffffff;
let mut i: usize = 0; let mut i: usize = 0;
@ -127,3 +144,37 @@ pub const fn get_hash<const N: usize>(s: &[u8; N]) -> u32 {
} }
crc ^ 0xffffffff crc ^ 0xffffffff
} }
pub const fn get_hash(s: &[u8]) -> u32 {
let mut crc: u32 = 0xffffffff;
let mut i: usize = 0;
while i < s.len() {
crc = (crc >> 8) ^ CRC_TABLE[((crc ^ (to_lower(s[i]) as u32)) & 0xff) as usize];
i += 1;
}
crc ^ 0xffffffff
}
#[cfg(feature = "mock_data")]
mod test {
use super::get_hash;
use super::StringKeyInner;
use crate::{ExternRef, StringKey};
use alloc::rc::Rc;
use core::cell::RefCell;
use cstr_core::CString;
impl StringKey {
pub fn new(s: &str) -> Self {
let hash = get_hash(s.as_bytes());
Self {
data: Rc::new(StringKeyInner {
ptr: ExternRef::mock(),
str: RefCell::new(Some(CString::new(s).unwrap())),
hash: RefCell::new(Some(hash)),
}),
}
}
}
}

View File

@ -1,11 +1,15 @@
#[cfg(not(feature = "mock_data"))]
use crate::ExternRef; use crate::ExternRef;
#[cfg(not(feature = "mock_data"))]
use alloc::collections::BTreeMap; use alloc::collections::BTreeMap;
pub trait Cacheable { pub trait Cacheable {
#[cfg(not(feature = "mock_data"))]
fn get_cache<'a>() -> &'a mut BTreeMap<ExternRef<Self>, Self> fn get_cache<'a>() -> &'a mut BTreeMap<ExternRef<Self>, Self>
where where
Self: Sized; Self: Sized;
#[cfg(not(feature = "mock_data"))]
fn from_ref(ptr: ExternRef<Self>, ctor: &dyn Fn(ExternRef<Self>) -> Self) -> Self fn from_ref(ptr: ExternRef<Self>, ctor: &dyn Fn(ExternRef<Self>) -> Self) -> Self
where where
Self: Sized, Self: Sized,
@ -23,9 +27,11 @@ pub trait Cacheable {
macro_rules! cacheable { macro_rules! cacheable {
($type: ty) => { ($type: ty) => {
paste::paste!{ paste::paste!{
#[cfg(not(feature = "mock_data"))]
static mut [<$type:upper _CACHE>]: alloc::collections::BTreeMap<ExternRef<$type>, $type> = static mut [<$type:upper _CACHE>]: alloc::collections::BTreeMap<ExternRef<$type>, $type> =
alloc::collections::BTreeMap::new(); alloc::collections::BTreeMap::new();
impl crate::handling::Cacheable for $type { impl crate::handling::Cacheable for $type {
#[cfg(not(feature = "mock_data"))]
fn get_cache<'a>() -> &'a mut alloc::collections::BTreeMap<ExternRef<$type>, $type> { fn get_cache<'a>() -> &'a mut alloc::collections::BTreeMap<ExternRef<$type>, $type> {
unsafe { &mut [<$type:upper _CACHE>] } unsafe { &mut [<$type:upper _CACHE>] }
} }

View File

@ -1,10 +1,12 @@
use alloc::boxed::Box; use alloc::boxed::Box;
#[cfg(not(feature = "mock_data"))]
pub struct CachedValue<T> { pub struct CachedValue<T> {
init_fn: Box<dyn Fn() -> T>, init_fn: Box<dyn Fn() -> T>,
value: Option<T>, value: Option<T>,
} }
#[cfg(not(feature = "mock_data"))]
impl<T> CachedValue<T> { impl<T> CachedValue<T> {
pub fn new(init_fn: Box<dyn Fn() -> T>) -> Self { pub fn new(init_fn: Box<dyn Fn() -> T>) -> Self {
Self { Self {
@ -39,6 +41,37 @@ impl<T> CachedValue<T> {
} }
} }
#[cfg(feature = "mock_data")]
pub struct CachedValue<T: Clone> {
value: T,
}
#[cfg(feature = "mock_data")]
impl<T: Clone> CachedValue<T> {
pub fn new(value: T) -> Self {
Self { value }
}
#[inline]
pub fn value(&self) -> T
where
T: Clone,
{
self.value.clone()
}
#[inline]
pub fn value_ref(&self) -> &T {
&self.value
}
}
#[cfg(feature = "mock_data")]
impl<T: Clone> From<T> for CachedValue<T> {
fn from(value: T) -> Self {
Self::new(value)
}
}
#[macro_export] #[macro_export]
macro_rules! cached_value { macro_rules! cached_value {
($init: block) => { ($init: block) => {

View File

@ -35,6 +35,14 @@ impl<T> ExternRef<T> {
resource_type: Default::default(), resource_type: Default::default(),
} }
} }
#[cfg(feature = "mock_data")]
pub(crate) fn mock() -> Self {
Self {
p: 0,
resource_type: Default::default(),
}
}
} }
impl<T> Clone for ExternRef<T> { impl<T> Clone for ExternRef<T> {
@ -89,6 +97,7 @@ impl<T> VecExternRef<T> {
v.1 v.1
} }
#[cfg(not(feature = "mock_data"))]
pub(crate) fn at(&self, index: u32) -> ExternRef<T> { pub(crate) fn at(&self, index: u32) -> ExternRef<T> {
let p = unsafe { _vec_extern_ref_get_value(self.get_internal_index(), index) }; let p = unsafe { _vec_extern_ref_get_value(self.get_internal_index(), index) };
ExternRef { ExternRef {
@ -97,6 +106,7 @@ impl<T> VecExternRef<T> {
} }
} }
#[cfg(not(feature = "mock_data"))]
pub(crate) fn get_immutable_list(&self) -> ImmutableList<T> pub(crate) fn get_immutable_list(&self) -> ImmutableList<T>
where where
T: Clone, T: Clone,
@ -168,6 +178,7 @@ macro_rules! impl_extern_ctor {
}; };
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn _vec_extern_ref_get_value(extern_ref: u32, index: u32) -> u32; fn _vec_extern_ref_get_value(extern_ref: u32, index: u32) -> u32;
} }

View File

@ -34,6 +34,7 @@ impl<T> FFIArray<T> {
} }
#[no_mangle] #[no_mangle]
#[cfg(not(feature = "mock_data"))]
extern "wasm" fn delete_ffi_array(a: FFIArray<u8>) { extern "wasm" fn delete_ffi_array(a: FFIArray<u8>) {
a.delete() a.delete()
} }

View File

@ -45,7 +45,8 @@ macro_rules! wasm_reference_getters {
} }
extern "wasm" { #[cfg(not(feature = "mock_data"))]
extern "wasm" {
$( $(
paste::paste!{ paste::paste!{
fn [<$base_type:snake _get_ $name>](r: ExternRef<$base_type>) -> ExternRef<$type>; fn [<$base_type:snake _get_ $name>](r: ExternRef<$base_type>) -> ExternRef<$type>;
@ -78,7 +79,8 @@ macro_rules! wasm_optional_reference_getters {
} }
extern "wasm" { #[cfg(not(feature = "mock_data"))]
extern "wasm" {
$( $(
paste::paste!{ paste::paste!{
fn [<$base_type:snake get_ $name>](r: ExternRef<$base_type>) -> ExternRef<$type>; fn [<$base_type:snake get_ $name>](r: ExternRef<$base_type>) -> ExternRef<$type>;
@ -97,13 +99,14 @@ macro_rules! wasm_value_getters {
$v:vis fn $name:ident(&self) -> $type:ty; $v:vis fn $name:ident(&self) -> $type:ty;
)* )*
) => { ) => {
#[cfg(not(feature = "mock_data"))]
impl $base_type { impl $base_type {
$( $(
$(#[$attr])* $(#[$attr])*
$v fn $name(&self) -> $type { $v fn $name(&self) -> $type {
paste::paste!{ paste::paste!{
unsafe{ unsafe{
[<$base_type:snake get_ $name>](self.inner.reference) [<$base_type:snake _get_ $name>](self.inner.reference)
} }
} }
} }
@ -111,10 +114,11 @@ macro_rules! wasm_value_getters {
} }
extern "wasm" { #[cfg(not(feature = "mock_data"))]
extern "wasm" {
$( $(
paste::paste!{ paste::paste!{
fn [<$base_type:snake get_ $name>](r: ExternRef<$base_type>) -> $type; fn [<$base_type:snake _get_ $name>](r: ExternRef<$base_type>) -> $type;
} }
)* )*
} }

View File

@ -34,6 +34,7 @@ pub trait OwnerGetter {
} }
} }
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
pub fn get_script_owner(pointer: *const c_void) -> ExternRef<u8>; pub fn get_script_owner(pointer: *const c_void) -> ExternRef<u8>;
} }

View File

@ -9,12 +9,16 @@ struct TemporaryData<T> {
value: T, value: T,
} }
#[cfg(not(feature = "mock_data"))]
pub struct Temporary<T> { pub struct Temporary<T> {
value: *mut TemporaryData<T>, value: *mut TemporaryData<T>,
} }
#[cfg(not(feature = "mock_data"))]
static mut TEMPORARIES: alloc::collections::BTreeMap<u32, *const u8> = static mut TEMPORARIES: alloc::collections::BTreeMap<u32, *const u8> =
alloc::collections::BTreeMap::new(); alloc::collections::BTreeMap::new();
#[cfg(not(feature = "mock_data"))]
impl<T> Temporary<T> impl<T> Temporary<T>
where where
T: ExternalReferenceType, T: ExternalReferenceType,
@ -75,6 +79,7 @@ where
} }
} }
#[cfg(not(feature = "mock_data"))]
impl<T> Clone for Temporary<T> { impl<T> Clone for Temporary<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
*unsafe { self.value.as_mut() }.unwrap().use_count.get_mut() += 1; *unsafe { self.value.as_mut() }.unwrap().use_count.get_mut() += 1;
@ -82,6 +87,7 @@ impl<T> Clone for Temporary<T> {
} }
} }
#[cfg(not(feature = "mock_data"))]
impl<T> Drop for Temporary<T> { impl<T> Drop for Temporary<T> {
fn drop(&mut self) { fn drop(&mut self) {
let data = unsafe { self.value.as_mut() }.unwrap(); let data = unsafe { self.value.as_mut() }.unwrap();
@ -92,3 +98,21 @@ impl<T> Drop for Temporary<T> {
} }
} }
} }
#[cfg(feature = "mock_data")]
pub struct Temporary<T: Clone> {
value: T,
}
#[cfg(feature = "mock_data")]
impl<T: Clone> Temporary<T> {
#[inline]
pub fn value(&self) -> T {
self.value.clone()
}
#[inline]
pub fn value_ref(&self) -> &T {
&self.value
}
}

View File

@ -12,7 +12,7 @@
#![feature(wasm_abi)] #![feature(wasm_abi)]
#![feature(thread_local)] #![feature(thread_local)]
#![feature(build_hasher_simple_hash_one)] #![feature(build_hasher_simple_hash_one)]
#![no_std] #![cfg_attr(not(feature = "mock_data"), no_std)]
#![allow(incomplete_features)] #![allow(incomplete_features)]
extern crate alloc; extern crate alloc;
@ -26,7 +26,9 @@ use crate::app_interface::list::ImmutableList;
use crate::app_interface::{DynamicLibrary, EffectParameter, StringKey, TurnChoice}; use crate::app_interface::{DynamicLibrary, EffectParameter, StringKey, TurnChoice};
pub(crate) use crate::handling::extern_ref::*; pub(crate) use crate::handling::extern_ref::*;
use crate::handling::ffi_array::FFIArray; use crate::handling::ffi_array::FFIArray;
use crate::handling::{Script, ScriptCapabilities, ScriptCategory}; #[cfg(not(feature = "mock_data"))]
use crate::handling::ScriptCapabilities;
use crate::handling::{Script, ScriptCategory};
use alloc::boxed::Box; use alloc::boxed::Box;
#[macro_use] #[macro_use]
@ -46,6 +48,7 @@ pub fn set_load_script_fn(f: LoadScriptFnType) {
} }
#[no_mangle] #[no_mangle]
#[cfg(not(feature = "mock_data"))]
extern "wasm" fn load_script(category: ScriptCategory, name: ExternRef<StringKey>) -> u32 { extern "wasm" fn load_script(category: ScriptCategory, name: ExternRef<StringKey>) -> u32 {
let name_c = StringKey::new(name); let name_c = StringKey::new(name);
let boxed_script = unsafe { &LOAD_SCRIPT_FN }.as_ref().unwrap()(category, &name_c); let boxed_script = unsafe { &LOAD_SCRIPT_FN }.as_ref().unwrap()(category, &name_c);
@ -57,6 +60,7 @@ extern "wasm" fn load_script(category: ScriptCategory, name: ExternRef<StringKey
} }
#[no_mangle] #[no_mangle]
#[cfg(not(feature = "mock_data"))]
unsafe extern "wasm" fn destroy_script(script: *mut Box<dyn Script>) { 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. // 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. // This lets Rust do the cleanup.
@ -65,6 +69,7 @@ unsafe extern "wasm" fn destroy_script(script: *mut Box<dyn Script>) {
} }
#[no_mangle] #[no_mangle]
#[cfg(not(feature = "mock_data"))]
unsafe extern "wasm" fn get_script_capabilities( unsafe extern "wasm" fn get_script_capabilities(
script: *const Box<dyn Script>, script: *const Box<dyn Script>,
) -> FFIArray<ScriptCapabilities> { ) -> FFIArray<ScriptCapabilities> {
@ -73,6 +78,7 @@ unsafe extern "wasm" fn get_script_capabilities(
} }
#[no_mangle] #[no_mangle]
#[cfg(not(feature = "mock_data"))]
unsafe extern "wasm" fn script_on_initialize( unsafe extern "wasm" fn script_on_initialize(
script: *const Box<dyn Script>, script: *const Box<dyn Script>,
library: ExternRef<DynamicLibrary>, library: ExternRef<DynamicLibrary>,
@ -88,6 +94,7 @@ unsafe extern "wasm" fn script_on_initialize(
} }
#[no_mangle] #[no_mangle]
#[cfg(not(feature = "mock_data"))]
unsafe extern "wasm" fn script_on_before_turn( unsafe extern "wasm" fn script_on_before_turn(
script: *const Box<dyn Script>, script: *const Box<dyn Script>,
choice: ExternRef<TurnChoice>, choice: ExternRef<TurnChoice>,

View File

@ -1,9 +1,11 @@
use alloc::alloc::alloc; use alloc::alloc::alloc;
use core::alloc::Layout; use core::alloc::Layout;
#[cfg(not(test))] #[cfg(not(feature = "mock_data"))]
use core::panic::PanicInfo; use core::panic::PanicInfo;
use cstr_core::c_char; use cstr_core::{c_char, CStr, CString};
#[cfg(not(feature = "mock_data"))]
#[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn _print(s: *const u8, len: usize); fn _print(s: *const u8, len: usize);
fn _error( fn _error(
@ -16,12 +18,20 @@ extern "wasm" {
); );
} }
#[cfg(not(feature = "mock_data"))]
pub fn print_raw(s: &[c_char]) { pub fn print_raw(s: &[c_char]) {
unsafe { unsafe {
_print(s.as_ptr(), s.len()); _print(s.as_ptr(), s.len());
} }
} }
#[cfg(feature = "mock_data")]
pub fn print_raw(s: &[u8]) {
unsafe {
println!("{}", CString::new(s).unwrap().into_string().unwrap());
}
}
#[macro_export] #[macro_export]
macro_rules! println { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } } macro_rules! println { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } }
@ -37,6 +47,7 @@ macro_rules! dbg {
#[panic_handler] #[panic_handler]
#[no_mangle] #[no_mangle]
#[cfg(not(feature = "mock_data"))]
#[cfg(not(test))] #[cfg(not(test))]
pub fn begin_panic_handler(panic_info: &PanicInfo<'_>) -> ! { pub fn begin_panic_handler(panic_info: &PanicInfo<'_>) -> ! {
let msg = panic_info.message().unwrap().as_str().unwrap(); let msg = panic_info.message().unwrap().as_str().unwrap();
@ -64,12 +75,14 @@ pub fn begin_panic_handler(panic_info: &PanicInfo<'_>) -> ! {
#[alloc_error_handler] #[alloc_error_handler]
#[no_mangle] #[no_mangle]
#[cfg(not(feature = "mock_data"))]
#[cfg(not(test))] #[cfg(not(test))]
fn allocation_error_handler(layout: core::alloc::Layout) -> ! { fn allocation_error_handler(layout: core::alloc::Layout) -> ! {
panic!("memory allocation of {} bytes failed", layout.size()) panic!("memory allocation of {} bytes failed", layout.size())
} }
#[no_mangle] #[no_mangle]
#[cfg(not(feature = "mock_data"))]
unsafe extern "wasm" fn allocate_mem(len: u32, align: u32) -> *mut u8 { unsafe extern "wasm" fn allocate_mem(len: u32, align: u32) -> *mut u8 {
alloc(Layout::from_size_align(len as usize, align as usize).unwrap()) alloc(Layout::from_size_align(len as usize, align as usize).unwrap())
} }