From e0e3a57f6d56f00479c7f9c8a267c58928d68ed9 Mon Sep 17 00:00:00 2001 From: Raytwo Date: Thu, 24 Dec 2020 18:02:31 +0100 Subject: [PATCH 1/3] Byte pattern matching for Smash > 9.0.2 This should make the plugin compatible for future updates --- src/lib.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 510ab52..1cc4db9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,12 @@ lazy_static::lazy_static! { ); } +static mut FIGHTER_SELECTED_OFFSET: usize = 0x66267c; + +static FIGHTER_SELECTED_SEARCH_CODE: &[u8] = &[ + 0xc8, 0x66, 0x40, 0xb9, 0x08, 0x01, 0x00, 0x32, 0xe0, 0x03, 0x17, 0xaa, 0xc8, 0x66, 0x00, 0xb9, +]; + static SELECTED_SKINS: [Mutex>; 8] = [ parking_lot::const_mutex(None), parking_lot::const_mutex(None), @@ -196,7 +202,7 @@ pub struct FighterInfo { fighter_slot: u8, } -#[skyline::hook(offset = 0x66267c, inline)] +#[skyline::hook(offset = FIGHTER_SELECTED_OFFSET, inline)] fn css_fighter_selected(ctx: &InlineCtx) { let infos = unsafe { &*(ctx.registers[0].bindgen_union_field as *const FighterInfo) }; @@ -344,8 +350,24 @@ extern "C" fn chara_6_callback(hash: u64, data: *mut u8, size: usize) -> bool { } } +pub fn search_offsets() { + unsafe { + let text_ptr = getRegionAddress(Region::Text) as *const u8; + let text_size = (getRegionAddress(Region::Rodata) as usize) - (text_ptr as usize); + + let text = std::slice::from_raw_parts(text_ptr, text_size); + + if let Some(offset) = find_subsequence(text, FIGHTER_SELECTED_SEARCH_CODE) { + FIGHTER_SELECTED_OFFSET = offset + 0x10; + } else { + println!("Error: no offset found for 'css_fighter_selected'. Defaulting to 9.0.2 offset. This likely won't work."); + } + } +} + #[skyline::main(name = "minecraft_skins")] pub fn main() { + search_offsets(); skyline::install_hooks!(prepo_add_play_report_hook, css_fighter_selected); unsafe { From 59c0bbac84fc0133dbce7f12cf19bd60a188bc57 Mon Sep 17 00:00:00 2001 From: Raytwo Date: Thu, 24 Dec 2020 18:15:11 +0100 Subject: [PATCH 2/3] Add missing function --- src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 1cc4db9..6223481 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -350,7 +350,11 @@ extern "C" fn chara_6_callback(hash: u64, data: *mut u8, size: usize) -> bool { } } -pub fn search_offsets() { +fn find_subsequence(haystack: &[u8], needle: &[u8]) -> Option { + haystack.windows(needle.len()).position(|window| window == needle) +} + +fn search_offsets() { unsafe { let text_ptr = getRegionAddress(Region::Text) as *const u8; let text_size = (getRegionAddress(Region::Rodata) as usize) - (text_ptr as usize); From 1688baa938c884a821ea8e9cef8baedb0ff2516a Mon Sep 17 00:00:00 2001 From: Raytwo Date: Thu, 24 Dec 2020 18:17:24 +0100 Subject: [PATCH 3/3] Fix using statements --- src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6223481..35d3580 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,11 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use parking_lot::Mutex; use image::DynamicImage; -use skyline::hooks::InlineCtx; +use skyline::hooks::{ + getRegionAddress, + Region, + InlineCtx +}; use smash::lib::lua_const::FIGHTER_KIND_PICKEL; mod keyboard;