Fixes for interop with host

This commit is contained in:
Deukhoofd 2022-09-16 11:01:17 +02:00
parent 3c8bd5f7c5
commit f3f5b2acb0
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
9 changed files with 29 additions and 36 deletions

View File

@ -18,7 +18,7 @@ impl Script for Automize {
&[ScriptCapabilities::OnSecondaryEffect] &[ScriptCapabilities::OnSecondaryEffect]
} }
fn on_secondary_effect(&self, mv: ExecutingMove, target: Pokemon, hit: u8) { fn on_secondary_effect(&self, mv: ExecutingMove, _target: Pokemon, _hit: u8) {
let user = mv.user(); let user = mv.user();
let stats = user.boosted_stats(); let stats = user.boosted_stats();
let original_speed = stats.speed(); let original_speed = stats.speed();

View File

@ -33,7 +33,7 @@ impl Script for ChangeAllTargetStats {
fn on_initialize( fn on_initialize(
&self, &self,
library: &DynamicLibrary, _library: &DynamicLibrary,
parameters: Option<ImmutableList<EffectParameter>>, parameters: Option<ImmutableList<EffectParameter>>,
) { ) {
self.amount.store( self.amount.store(

View File

@ -1,4 +1,3 @@
use crate::script;
use core::any::Any; use core::any::Any;
use core::sync::atomic::{AtomicI8, Ordering}; use core::sync::atomic::{AtomicI8, Ordering};
use pkmn_lib_interface::app_interface::list::ImmutableList; use pkmn_lib_interface::app_interface::list::ImmutableList;

View File

@ -41,7 +41,7 @@ impl Script for Struggle {
*number_of_hits = 1 *number_of_hits = 1
} }
fn on_secondary_effect(&self, mv: ExecutingMove, _target: Pokemon, hit: u8) { fn on_secondary_effect(&self, mv: ExecutingMove, _target: Pokemon, _hit: u8) {
let mut damage = mv.user().max_health() / 4; let mut damage = mv.user().max_health() / 4;
if damage == 0 { if damage == 0 {
damage = 1 damage = 1

View File

@ -58,6 +58,7 @@ pub fn get_script(category: ScriptCategory, name: &StringKey) -> Option<Box<dyn
} }
ScriptCategory::Side => {} ScriptCategory::Side => {}
ScriptCategory::ItemBattleTrigger => {} ScriptCategory::ItemBattleTrigger => {}
ScriptCategory::Weather => {}
} }
None None

View File

@ -49,7 +49,7 @@ impl BattleSide {
pub fn has_volatile(&self, script_name: &str) -> bool { pub fn has_volatile(&self, script_name: &str) -> bool {
unsafe { unsafe {
let script_name = CString::new(script_name).unwrap(); let script_name = CString::new(script_name).unwrap();
battleside_has_volatile(self.inner.reference, script_name.into_raw()) battleside_has_volatile(self.inner.reference, script_name.as_ptr())
} }
} }
@ -66,7 +66,7 @@ impl BattleSide {
pub fn remove_volatile(&self, script: &dyn Script) { pub fn remove_volatile(&self, script: &dyn Script) {
unsafe { unsafe {
let name = CString::new(script.get_name()).unwrap(); let name = CString::new(script.get_name()).unwrap();
battleside_remove_volatile(self.inner.reference, name.into_raw()); battleside_remove_volatile(self.inner.reference, name.as_ptr());
} }
} }
@ -77,7 +77,7 @@ impl BattleSide {
{ {
unsafe { unsafe {
let script_name = CString::new(script_name).unwrap(); let script_name = CString::new(script_name).unwrap();
let s = battleside_get_volatile(self.inner.reference, script_name.into_raw()).val(); let s = battleside_get_volatile(self.inner.reference, script_name.as_ptr()).val();
if let Some(s) = s { if let Some(s) = s {
Some(s.as_any().downcast_ref().unwrap()) Some(s.as_any().downcast_ref().unwrap())
} else { } else {

View File

@ -207,7 +207,7 @@ impl Pokemon {
pub fn add_volatile_by_name(&self, script_name: &str) -> &dyn Script { pub fn add_volatile_by_name(&self, script_name: &str) -> &dyn Script {
unsafe { unsafe {
let ptr = CString::new(script_name).unwrap(); let ptr = CString::new(script_name).unwrap();
pokemon_add_volatile_by_name(self.inner.reference, ptr.into_raw()) pokemon_add_volatile_by_name(self.inner.reference, ptr.as_ptr())
.val() .val()
.unwrap() .unwrap()
} }
@ -217,7 +217,7 @@ impl Pokemon {
pub fn remove_volatile(&self, script: &dyn Script) { pub fn remove_volatile(&self, script: &dyn Script) {
unsafe { unsafe {
let name = CString::new(script.get_name()).unwrap(); let name = CString::new(script.get_name()).unwrap();
pokemon_remove_volatile(self.inner.reference, name.into_raw()); pokemon_remove_volatile(self.inner.reference, name.as_ptr());
} }
} }
@ -228,7 +228,7 @@ impl Pokemon {
{ {
unsafe { unsafe {
let script_name = CString::new(script_name).unwrap(); let script_name = CString::new(script_name).unwrap();
let s = pokemon_get_volatile(self.inner.reference, script_name.into_raw()).val(); let s = pokemon_get_volatile(self.inner.reference, script_name.as_ptr()).val();
if let Some(s) = s { if let Some(s) = s {
Some(s.as_any().downcast_ref().unwrap()) Some(s.as_any().downcast_ref().unwrap())
} else { } else {

View File

@ -18,6 +18,7 @@ pub enum ScriptCategory {
Status, Status,
Pokemon, Pokemon,
Battle, Battle,
Weather,
Side, Side,
ItemBattleTrigger, ItemBattleTrigger,
} }

View File

@ -2,35 +2,28 @@ use alloc::alloc::alloc;
use core::alloc::Layout; use core::alloc::Layout;
#[cfg(not(feature = "mock_data"))] #[cfg(not(feature = "mock_data"))]
use core::panic::PanicInfo; use core::panic::PanicInfo;
use cstr_core::c_char;
#[cfg(feature = "mock_data")] #[cfg(feature = "mock_data")]
use cstr_core::{CStr, CString}; use cstr_core::CStr;
use cstr_core::{c_char, CString};
#[cfg(not(feature = "mock_data"))] #[cfg(not(feature = "mock_data"))]
#[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);
fn _error( fn _error(message: *const u8, file: *const u8, line: u32, position: u32);
message: *const u8,
message_len: usize,
file: *const u8,
file_len: usize,
line: u32,
position: u32,
);
} }
#[cfg(not(feature = "mock_data"))] #[cfg(not(feature = "mock_data"))]
pub fn print_raw(s: &[c_char]) { pub fn print_raw(s: CString) {
unsafe { unsafe {
_print(s.as_ptr(), s.len()); _print(s.as_ptr());
} }
} }
#[cfg(feature = "mock_data")] #[cfg(feature = "mock_data")]
pub fn print_raw(s: &[u8]) { pub fn print_raw(s: CString) {
unsafe { unsafe {
println!("{}", CString::new(s).unwrap().into_string().unwrap()); println!("{}", s.into_string().unwrap());
} }
} }
@ -52,25 +45,18 @@ macro_rules! dbg {
#[cfg(not(feature = "mock_data"))] #[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 = CString::new(panic_info.message().unwrap().as_str().unwrap()).unwrap();
let mut line = 0; let mut line = 0;
let mut position = 0; let mut position = 0;
let mut file = ""; let mut file = CString::default();
if let Some(s) = panic_info.location() { if let Some(s) = panic_info.location() {
line = s.line(); line = s.line();
position = s.column(); position = s.column();
file = s.file(); file = CString::new(s.file()).unwrap();
} }
unsafe { unsafe {
_error( _error(msg.as_ptr(), file.as_ptr(), line, position);
msg.as_ptr(),
msg.len(),
file.as_ptr(),
file.len(),
line,
position,
);
} }
loop {} loop {}
} }
@ -88,3 +74,9 @@ fn allocation_error_handler(layout: core::alloc::Layout) -> ! {
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())
} }
#[no_mangle]
#[cfg(not(feature = "mock_data"))]
unsafe extern "wasm" fn dealloc_cstring(ptr: *mut c_char) {
CString::from_raw(ptr);
}