CLeanup, fixes for Assurance

This commit is contained in:
Deukhoofd 2022-09-10 10:07:26 +02:00
parent 365bdc8aec
commit 4275816fd9
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 19 additions and 24 deletions

View File

@ -98,7 +98,7 @@ impl Script for AssuranceData {
_old_health: u32, _old_health: u32,
_new_health: u32, _new_health: u32,
) { ) {
if pokemon.battle_side_index() == self.for_position { if pokemon.battle_index() == self.for_position {
self.has_hit.store(true, Ordering::Relaxed); self.has_hit.store(true, Ordering::Relaxed);
} }
} }

View File

@ -74,9 +74,9 @@ macro_rules! exported_functions {
}; };
} }
static mut script_ptr_cache: Option<hashbrown::HashMap<*const dyn Script, u32>> = None; static mut SCRIPT_PTR_CACHE: Option<hashbrown::HashMap<*const dyn Script, u32>> = None;
static mut script_ptr_reverse_cache: Option<hashbrown::HashMap<u32, Box<dyn Script>>> = None; static mut SCRIPT_PTR_REVERSE_CACHE: Option<hashbrown::HashMap<u32, Box<dyn Script>>> = None;
static mut script_index_counter: AtomicU32 = AtomicU32::new(1); static mut SCRIPT_INDEX_COUNTER: AtomicU32 = AtomicU32::new(1);
#[repr(C)] #[repr(C)]
pub struct ScriptPtr { pub struct ScriptPtr {
@ -86,20 +86,20 @@ pub struct ScriptPtr {
impl ScriptPtr { impl ScriptPtr {
fn get_cache<'a>() -> &'a mut HashMap<*const dyn Script, u32> { fn get_cache<'a>() -> &'a mut HashMap<*const dyn Script, u32> {
unsafe { unsafe {
if let None = script_ptr_cache { if let None = SCRIPT_PTR_CACHE {
script_ptr_cache = Some(hashbrown::HashMap::new()); SCRIPT_PTR_CACHE = Some(hashbrown::HashMap::new());
} }
let cache = script_ptr_cache.as_mut().unwrap(); let cache = SCRIPT_PTR_CACHE.as_mut().unwrap();
cache cache
} }
} }
fn get_reverse_cache<'a>() -> &'a mut HashMap<u32, Box<dyn Script>> { fn get_reverse_cache<'a>() -> &'a mut HashMap<u32, Box<dyn Script>> {
unsafe { unsafe {
if let None = script_ptr_reverse_cache { if let None = SCRIPT_PTR_REVERSE_CACHE {
script_ptr_reverse_cache = Some(hashbrown::HashMap::new()); SCRIPT_PTR_REVERSE_CACHE = Some(hashbrown::HashMap::new());
} }
let cache = script_ptr_reverse_cache.as_mut().unwrap(); let cache = SCRIPT_PTR_REVERSE_CACHE.as_mut().unwrap();
cache cache
} }
} }
@ -115,7 +115,7 @@ impl ScriptPtr {
let cache = Self::get_cache(); let cache = Self::get_cache();
let mut index = cache.get(&(ptr.as_ref() as *const dyn Script)).cloned(); let mut index = cache.get(&(ptr.as_ref() as *const dyn Script)).cloned();
if index.is_none() { if index.is_none() {
index = Some(script_index_counter.fetch_add(1, Ordering::SeqCst)); index = Some(SCRIPT_INDEX_COUNTER.fetch_add(1, Ordering::SeqCst));
let reverse_cache = Self::get_reverse_cache(); let reverse_cache = Self::get_reverse_cache();
reverse_cache.insert(index.unwrap(), ptr); reverse_cache.insert(index.unwrap(), ptr);
@ -133,16 +133,14 @@ impl ScriptPtr {
} }
pub fn val<'a, 'b>(&'a self) -> Option<&'b dyn Script> { pub fn val<'a, 'b>(&'a self) -> Option<&'b dyn Script> {
unsafe { if self.index == 0 {
if self.index == 0 { return None;
return None; }
} let cache = Self::get_reverse_cache();
let cache = Self::get_reverse_cache(); if let Some(c) = cache.get(&self.index) {
if let Some(c) = cache.get(&self.index) { Some(c.as_ref())
Some(c.as_ref()) } else {
} else { None
None
}
} }
} }
} }

View File

@ -37,9 +37,6 @@ pub fn print_raw(s: &[u8]) {
#[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()); } }
#[macro_export]
macro_rules! crate_println { ($($args:tt)*) => { crate::utils::print_raw(alloc::format!($($args)*).as_bytes()); } }
#[macro_export] #[macro_export]
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
macro_rules! dbg { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } } macro_rules! dbg { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } }