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,
_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);
}
}

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

View File

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