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]
}
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 stats = user.boosted_stats();
let original_speed = stats.speed();

View File

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

View File

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

View File

@ -41,7 +41,7 @@ impl Script for Struggle {
*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;
if damage == 0 {
damage = 1

View File

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

View File

@ -49,7 +49,7 @@ impl BattleSide {
pub fn has_volatile(&self, script_name: &str) -> bool {
unsafe {
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) {
unsafe {
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 {
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 {
Some(s.as_any().downcast_ref().unwrap())
} else {

View File

@ -207,7 +207,7 @@ impl Pokemon {
pub fn add_volatile_by_name(&self, script_name: &str) -> &dyn Script {
unsafe {
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()
.unwrap()
}
@ -217,7 +217,7 @@ impl Pokemon {
pub fn remove_volatile(&self, script: &dyn Script) {
unsafe {
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 {
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 {
Some(s.as_any().downcast_ref().unwrap())
} else {

View File

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

View File

@ -2,35 +2,28 @@ use alloc::alloc::alloc;
use core::alloc::Layout;
#[cfg(not(feature = "mock_data"))]
use core::panic::PanicInfo;
use cstr_core::c_char;
#[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"))]
extern "wasm" {
fn _print(s: *const u8, len: usize);
fn _error(
message: *const u8,
message_len: usize,
file: *const u8,
file_len: usize,
line: u32,
position: u32,
);
fn _print(s: *const u8);
fn _error(message: *const u8, file: *const u8, line: u32, position: u32);
}
#[cfg(not(feature = "mock_data"))]
pub fn print_raw(s: &[c_char]) {
pub fn print_raw(s: CString) {
unsafe {
_print(s.as_ptr(), s.len());
_print(s.as_ptr());
}
}
#[cfg(feature = "mock_data")]
pub fn print_raw(s: &[u8]) {
pub fn print_raw(s: CString) {
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(test))]
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 position = 0;
let mut file = "";
let mut file = CString::default();
if let Some(s) = panic_info.location() {
line = s.line();
position = s.column();
file = s.file();
file = CString::new(s.file()).unwrap();
}
unsafe {
_error(
msg.as_ptr(),
msg.len(),
file.as_ptr(),
file.len(),
line,
position,
);
_error(msg.as_ptr(), file.as_ptr(), line, position);
}
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 {
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);
}