From da55c6c35cfea6c396583a6037c5242cbb849f30 Mon Sep 17 00:00:00 2001 From: Jujstme Date: Fri, 8 Nov 2024 20:42:45 +0100 Subject: [PATCH] Reimplemented use of ranges in the scan method --- src/emulator/gba/mednafen.rs | 8 ++++---- src/emulator/gba/nocashgba.rs | 2 +- src/emulator/gba/retroarch.rs | 16 +++++++-------- src/emulator/gba/vba.rs | 22 ++++++++++---------- src/emulator/genesis/blastem.rs | 5 +---- src/emulator/genesis/fusion.rs | 2 +- src/emulator/genesis/gens.rs | 2 +- src/emulator/genesis/retroarch.rs | 13 +++++------- src/emulator/genesis/segaclassics.rs | 4 ++-- src/emulator/ps1/duckstation.rs | 2 +- src/emulator/ps1/epsxe.rs | 2 +- src/emulator/ps1/mednafen.rs | 4 ++-- src/emulator/ps1/pcsx_redux.rs | 9 +++------ src/emulator/ps1/psxfin.rs | 23 ++++++++++----------- src/emulator/ps1/retroarch.rs | 18 +++++++++++------ src/emulator/ps1/xebra.rs | 2 +- src/emulator/ps2/pcsx2.rs | 6 +++--- src/emulator/ps2/retroarch.rs | 2 +- src/emulator/sms/blastem.rs | 5 +---- src/emulator/sms/fusion.rs | 2 +- src/emulator/sms/mednafen.rs | 4 ++-- src/emulator/sms/retroarch.rs | 16 +++++++-------- src/game_engine/unity/il2cpp.rs | 30 ++++++++++++---------------- src/game_engine/unity/mono.rs | 13 +++++++----- src/game_engine/unity/scene.rs | 8 ++++---- src/game_engine/unreal/mod.rs | 18 ++++++++--------- src/signature.rs | 28 ++++++++++---------------- 27 files changed, 125 insertions(+), 141 deletions(-) diff --git a/src/emulator/gba/mednafen.rs b/src/emulator/gba/mednafen.rs index a96fcfa..8032df0 100644 --- a/src/emulator/gba/mednafen.rs +++ b/src/emulator/gba/mednafen.rs @@ -24,7 +24,7 @@ impl State { if self.is_64_bit { self.cached_ewram_pointer = { const SIG: Signature<13> = Signature::new("48 8B 05 ?? ?? ?? ?? 81 E1 FF FF 03 00"); - let ptr: Address = SIG.scan(game, main_module_range.0, main_module_range.1)? + 3; + let ptr: Address = SIG.scan(game, main_module_range)? + 3; let mut addr: Address = ptr + 0x4 + game.read::(ptr).ok()?; if game.read::(ptr + 10).ok()? == 0x48 { @@ -40,7 +40,7 @@ impl State { self.cached_iwram_pointer = { const SIG2: Signature<13> = Signature::new("48 8B 05 ?? ?? ?? ?? 81 E1 FF 7F 00 00"); - let ptr: Address = SIG2.scan(game, main_module_range.0, main_module_range.1)? + 3; + let ptr: Address = SIG2.scan(game, main_module_range)? + 3; let mut addr: Address = ptr + 0x4 + game.read::(ptr).ok()?; if game.read::(ptr + 10).ok()? == 0x48 { @@ -60,13 +60,13 @@ impl State { } else { self.cached_ewram_pointer = { const SIG: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF FF 03 00"); - let ptr = SIG.scan(game, main_module_range.0, main_module_range.1)?; + let ptr = SIG.scan(game, main_module_range)?; game.read::(ptr + 1).ok()?.into() }; self.cached_iwram_pointer = { const SIG2: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF 7F 00 00"); - let ptr = SIG2.scan(game, main_module_range.0, main_module_range.1)?; + let ptr = SIG2.scan(game, main_module_range)?; game.read::(ptr + 1).ok()?.into() }; diff --git a/src/emulator/gba/nocashgba.rs b/src/emulator/gba/nocashgba.rs index 4ef124e..a6b2c77 100644 --- a/src/emulator/gba/nocashgba.rs +++ b/src/emulator/gba/nocashgba.rs @@ -19,7 +19,7 @@ impl State { .find_map(|(name, _)| game.get_module_range(name).ok())?; self.base_addr = game - .read::(SIG.scan(game, main_module_range.0, main_module_range.1)? + 0x2) + .read::(SIG.scan(game, main_module_range)? + 0x2) .ok()? .into(); diff --git a/src/emulator/gba/retroarch.rs b/src/emulator/gba/retroarch.rs index 446e54f..9186127 100644 --- a/src/emulator/gba/retroarch.rs +++ b/src/emulator/gba/retroarch.rs @@ -51,7 +51,7 @@ impl State { const SIG2: Signature<13> = Signature::new("48 8B 05 ?? ?? ?? ?? 81 E1 FF 7F 00 00"); let ewram_pointer = { - let ptr: Address = SIG.scan(game, module_range.0, module_range.1)? + 3; + let ptr: Address = SIG.scan(game, module_range)? + 3; let mut addr: Address = ptr + 0x4 + game.read::(ptr).ok()?; if game.read::(ptr + 10).ok()? == 0x48 { @@ -65,7 +65,7 @@ impl State { }; let iwram_pointer = { - let ptr: Address = SIG2.scan(game, module_range.0, module_range.1)? + 3; + let ptr: Address = SIG2.scan(game, module_range)? + 3; let mut addr: Address = ptr + 0x4 + game.read::(ptr).ok()?; if game.read::(ptr + 10).ok()? == 0x48 { @@ -89,12 +89,12 @@ impl State { } else { let ewram_pointer: Address = { const SIG: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF FF 03 00"); - let ptr = SIG.scan(game, module_range.0, module_range.1)?; + let ptr = SIG.scan(game, module_range)?; game.read::(ptr + 1).ok()?.into() }; let iwram_pointer: Address = { const SIG2: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF 7F 00 00"); - let ptr = SIG2.scan(game, module_range.0, module_range.1)?; + let ptr = SIG2.scan(game, module_range)?; game.read::(ptr + 1).ok()?.into() }; @@ -118,24 +118,24 @@ impl State { let base_addr: Address = match is_64_bit { true => { const SIG: Signature<10> = Signature::new("48 8B 15 ?? ?? ?? ?? 8B 42 40"); - let ptr = SIG.scan(game, self.core_base, module_size)? + 3; + let ptr = SIG.scan(game, (self.core_base, module_size))? + 3; let ptr: Address = ptr + 0x4 + game.read::(ptr).ok()?; game.read::(ptr).ok()?.into() } false => { const SIG: Signature<11> = Signature::new("A3 ?? ?? ?? ?? F7 C5 02 00 00 00"); - let ptr = SIG.scan(game, self.core_base, module_size)? + 1; + let ptr = SIG.scan(game, (self.core_base, module_size))? + 1; game.read::(ptr).ok()?.into() } }; let ewram = { - let offset = SIG_EWRAM.scan(game, self.core_base, module_size)? + 8; + let offset = SIG_EWRAM.scan(game, (self.core_base, module_size))? + 8; base_addr + game.read::(offset).ok()? }; let iwram = { - let offset = SIG_IWRAM.scan(game, self.core_base, module_size)? + 9; + let offset = SIG_IWRAM.scan(game, (self.core_base, module_size))? + 9; base_addr + game.read::(offset).ok()? }; diff --git a/src/emulator/gba/vba.rs b/src/emulator/gba/vba.rs index d4b2172..a421919 100644 --- a/src/emulator/gba/vba.rs +++ b/src/emulator/gba/vba.rs @@ -29,7 +29,7 @@ impl State { const SIG2: Signature<13> = Signature::new("48 8B 05 ?? ?? ?? ?? 81 E3 FF 7F 00 00"); self.cached_ewram_pointer = { - let ptr: Address = SIG.scan(game, main_module_range.0, main_module_range.1)? + 3; + let ptr: Address = SIG.scan(game, main_module_range)? + 3; let mut addr: Address = ptr + 0x4 + game.read::(ptr).ok()?; if game.read::(ptr + 10).ok()? == 0x48 { @@ -43,7 +43,7 @@ impl State { }; self.cached_iwram_pointer = { - let ptr: Address = SIG2.scan(game, main_module_range.0, main_module_range.1)? + 3; + let ptr: Address = SIG2.scan(game, main_module_range)? + 3; let mut addr: Address = ptr + 0x4 + game.read::(ptr).ok()?; if game.read::(ptr + 10).ok()? == 0x48 { @@ -62,13 +62,11 @@ impl State { const SIG_RUNNING2: Signature<16> = Signature::new("48 8B 15 ?? ?? ?? ?? 31 C0 8B 12 85 D2 74 ?? 48"); - if let Some(ptr) = SIG_RUNNING.scan(game, main_module_range.0, main_module_range.1) - { + if let Some(ptr) = SIG_RUNNING.scan(game, main_module_range) { let ptr = ptr + 2; ptr + 0x4 + game.read::(ptr).ok()? + 0x1 } else { - let ptr = - SIG_RUNNING2.scan(game, main_module_range.0, main_module_range.1)? + 3; + let ptr = SIG_RUNNING2.scan(game, main_module_range)? + 3; let ptr = ptr + 0x4 + game.read::(ptr).ok()?; game.read::(ptr).ok()?.into() } @@ -82,11 +80,11 @@ impl State { const SIG: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF FF 03 00"); const SIG_OLD: Signature<12> = Signature::new("81 E6 FF FF 03 00 8B 15 ?? ?? ?? ??"); - if let Some(ptr) = SIG.scan(game, main_module_range.0, main_module_range.1) { + if let Some(ptr) = SIG.scan(game, main_module_range) { self.cached_ewram_pointer = game.read::(ptr + 1).ok()?.into(); self.cached_iwram_pointer = { const SIG2: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF 7F 00 00"); - let ptr = SIG2.scan(game, main_module_range.0, main_module_range.1)?; + let ptr = SIG2.scan(game, main_module_range)?; game.read::(ptr + 1).ok()?.into() }; @@ -97,8 +95,8 @@ impl State { Signature::new("8B 15 ?? ?? ?? ?? 31 C0 85 D2 74 ?? 0F"); let ptr = SIG - .scan(game, main_module_range.0, main_module_range.1) - .or_else(|| SIG_OLD.scan(game, main_module_range.0, main_module_range.1))?; + .scan(game, main_module_range) + .or_else(|| SIG_OLD.scan(game, main_module_range))?; game.read::(ptr + 2).ok()?.into() }; @@ -107,7 +105,7 @@ impl State { let iwram = game.read::(self.cached_iwram_pointer).ok()?; Some([ewram.into(), iwram.into()]) - } else if let Some(ptr) = SIG_OLD.scan(game, main_module_range.0, main_module_range.1) { + } else if let Some(ptr) = SIG_OLD.scan(game, main_module_range) { // This code is for very old versions of VisualBoyAdvance (1.8.0-beta 3) self.cached_ewram_pointer = game.read::(ptr + 8).ok()?.into(); self.cached_iwram_pointer = self.cached_ewram_pointer.add_signed(0x4); @@ -115,7 +113,7 @@ impl State { self.is_emulating = { const SIG_RUNNING: Signature<11> = Signature::new("8B 0D ?? ?? ?? ?? 85 C9 74 ?? 8A"); - let ptr = SIG_RUNNING.scan(game, main_module_range.0, main_module_range.1)? + 2; + let ptr = SIG_RUNNING.scan(game, main_module_range)? + 2; game.read::(ptr).ok()?.into() }; diff --git a/src/emulator/genesis/blastem.rs b/src/emulator/genesis/blastem.rs index 712da2d..e4ebe13 100644 --- a/src/emulator/genesis/blastem.rs +++ b/src/emulator/genesis/blastem.rs @@ -22,10 +22,7 @@ impl State { .contains(MemoryRangeFlags::WRITE) && m.size().unwrap_or_default() == 0x101000 }) - .find_map(|m| { - let (base, size) = m.range().ok()?; - SIG.scan(game, base, size) - })? + .find_map(|m| SIG.scan(game, m.range().ok()?))? + 11; let wram = game.read::(scanned_address).ok()?; diff --git a/src/emulator/genesis/fusion.rs b/src/emulator/genesis/fusion.rs index 582343b..021b608 100644 --- a/src/emulator/genesis/fusion.rs +++ b/src/emulator/genesis/fusion.rs @@ -17,7 +17,7 @@ impl State { .filter(|(_, state)| matches!(state, super::State::Fusion(_))) .find_map(|(name, _)| game.get_module_range(name).ok())?; - let ptr = SIG.scan(game, main_module.0, main_module.1)? + 1; + let ptr = SIG.scan(game, main_module)? + 1; let addr = ptr + game.read::(ptr).ok()? as u64 + 3; let addr = game.read::(addr).ok()?; diff --git a/src/emulator/genesis/gens.rs b/src/emulator/genesis/gens.rs index e8e5a99..231f3c8 100644 --- a/src/emulator/genesis/gens.rs +++ b/src/emulator/genesis/gens.rs @@ -15,7 +15,7 @@ impl State { .filter(|(_, state)| matches!(state, super::State::Gens(_))) .find_map(|(name, _)| game.get_module_range(name).ok())?; - let ptr = SIG.scan(game, main_module.0, main_module.1)? + 11; + let ptr = SIG.scan(game, main_module)? + 11; *endian = if game.read::(ptr + 4).ok()? == 0x86 { Endian::Big diff --git a/src/emulator/genesis/retroarch.rs b/src/emulator/genesis/retroarch.rs index 17bc6ed..f967972 100644 --- a/src/emulator/genesis/retroarch.rs +++ b/src/emulator/genesis/retroarch.rs @@ -47,10 +47,7 @@ impl State { .contains(MemoryRangeFlags::WRITE) && m.size().unwrap_or_default() == 0x101000 }) - .find_map(|m| { - let (base, size) = m.range().ok()?; - SIG.scan(game, base, size) - })? + .find_map(|m| SIG.scan(game, m.range().ok()?))? + 11; let wram = game.read::(scanned_address).ok()?; @@ -64,7 +61,7 @@ impl State { const SIG_64: Signature<10> = Signature::new("48 8D 0D ?? ?? ?? ?? 4C 8B 2D"); let addr = - SIG_64.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 3; + SIG_64.scan(game, (core_address, game.get_module_size(core_name).ok()?))? + 3; let wram = addr + 0x4 + game.read::(addr).ok()?; @@ -73,7 +70,7 @@ impl State { const SIG_32: Signature<7> = Signature::new("A3 ?? ?? ?? ?? 29 F9"); let ptr = - SIG_32.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 1; + SIG_32.scan(game, (core_address, game.get_module_size(core_name).ok()?))? + 1; let wram = game.read::(ptr).ok()?; @@ -87,7 +84,7 @@ impl State { const SIG_64: Signature<9> = Signature::new("48 8D 0D ?? ?? ?? ?? 41 B8"); let addr = - SIG_64.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 3; + SIG_64.scan(game, (core_address, game.get_module_size(core_name).ok()?))? + 3; let wram = addr + 0x4 + game.read::(addr).ok()?; @@ -96,7 +93,7 @@ impl State { const SIG_32: Signature<8> = Signature::new("B9 ?? ?? ?? ?? C1 EF 10"); let ptr = - SIG_32.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 1; + SIG_32.scan(game, (core_address, game.get_module_size(core_name).ok()?))? + 1; let wram = game.read::(ptr).ok()?; diff --git a/src/emulator/genesis/segaclassics.rs b/src/emulator/genesis/segaclassics.rs index d591af7..87ce6ac 100644 --- a/src/emulator/genesis/segaclassics.rs +++ b/src/emulator/genesis/segaclassics.rs @@ -16,14 +16,14 @@ impl State { const GENESISWRAPPERDLL: &str = "GenesisEmuWrapper.dll"; let mut ptr = if let Ok(module) = game.get_module_range(GENESISWRAPPERDLL) { - SIG_GAMEROOM.scan(game, module.0, module.1)? + 2 + SIG_GAMEROOM.scan(game, module)? + 2 } else { let main_module = super::PROCESS_NAMES .iter() .filter(|(_, state)| matches!(state, super::State::SegaClassics(_))) .find_map(|(name, _)| game.get_module_range(name).ok())?; - SIG_SEGACLASSICS.scan(game, main_module.0, main_module.1)? + 8 + SIG_SEGACLASSICS.scan(game, main_module)? + 8 }; ptr = game.read::(ptr).ok()?.into(); diff --git a/src/emulator/ps1/duckstation.rs b/src/emulator/ps1/duckstation.rs index 4f8ad62..1ed9ce5 100644 --- a/src/emulator/ps1/duckstation.rs +++ b/src/emulator/ps1/duckstation.rs @@ -28,7 +28,7 @@ impl State { self.addr = debug_symbol.address; } else { // For older versions of Duckstation, we fall back to regular sigscanning - let addr = SIG.scan(game, main_module_range.0, main_module_range.1)? + 3; + let addr = SIG.scan(game, main_module_range)? + 3; self.addr = addr + 0x4 + game.read::(addr).ok()?; } diff --git a/src/emulator/ps1/epsxe.rs b/src/emulator/ps1/epsxe.rs index 38ff85e..c7c3875 100644 --- a/src/emulator/ps1/epsxe.rs +++ b/src/emulator/ps1/epsxe.rs @@ -15,7 +15,7 @@ impl State { .filter(|(_, state)| matches!(state, super::State::Epsxe(_))) .find_map(|(name, _)| game.get_module_range(name).ok())?; - let ptr = SIG.scan(game, main_module_range.0, main_module_range.1)? + 5; + let ptr = SIG.scan(game, main_module_range)? + 5; Some(game.read::(ptr).ok()?.into()) } diff --git a/src/emulator/ps1/mednafen.rs b/src/emulator/ps1/mednafen.rs index 5dadb60..4304571 100644 --- a/src/emulator/ps1/mednafen.rs +++ b/src/emulator/ps1/mednafen.rs @@ -21,8 +21,8 @@ impl State { pe::MachineType::read(game, main_module_range.0) == Some(pe::MachineType::X86_64); let ptr = match is_64_bit { - true => SIG_64.scan(game, main_module_range.0, main_module_range.1)?, - false => SIG_32.scan(game, main_module_range.0, main_module_range.1)?, + true => SIG_64.scan(game, main_module_range)?, + false => SIG_32.scan(game, main_module_range)?, } + 0x5; Some(game.read::(ptr).ok()?.into()) diff --git a/src/emulator/ps1/pcsx_redux.rs b/src/emulator/ps1/pcsx_redux.rs index 327be8b..2761823 100644 --- a/src/emulator/ps1/pcsx_redux.rs +++ b/src/emulator/ps1/pcsx_redux.rs @@ -27,10 +27,10 @@ impl State { ); const SIG_OFFSET: Signature<9> = Signature::new("89 D1 C1 E9 10 48 8B ?? ??"); - self.addr_base = SIG_BASE.scan(game, main_module_range.0, main_module_range.1)? + 2; + self.addr_base = SIG_BASE.scan(game, main_module_range)? + 2; self.addr = game.read::(self.addr_base).ok()?.into(); - let offset = SIG_OFFSET.scan(game, main_module_range.0, main_module_range.1)? + 8; + let offset = SIG_OFFSET.scan(game, main_module_range)? + 8; let offset = game.read::(offset).ok()? as u64; let addr = game.read::(self.addr + offset).ok()?; @@ -47,10 +47,7 @@ impl State { .unwrap_or_default() .contains(MemoryRangeFlags::WRITE) }) - .find_map(|m| { - let (base, size) = m.range().ok()?; - SIG.scan(game, base, size) - })? + .find_map(|m| SIG.scan(game, m.range().ok()?))? + 2; self.addr = game.read::(self.addr_base).ok()?.into(); diff --git a/src/emulator/ps1/psxfin.rs b/src/emulator/ps1/psxfin.rs index 7e3fe1e..42b6090 100644 --- a/src/emulator/ps1/psxfin.rs +++ b/src/emulator/ps1/psxfin.rs @@ -18,18 +18,17 @@ impl State { .filter(|(_, state)| matches!(state, super::State::PsxFin(_))) .find_map(|(name, _)| game.get_module_range(name).ok())?; - let mut ptr: Address32 = - if let Some(sig) = SIG.scan(game, main_module_range.0, main_module_range.1) { - game.read(sig + 2).ok()? - } else if let Some(sig) = SIG_0.scan(game, main_module_range.0, main_module_range.1) { - game.read(sig + 1).ok()? - } else if let Some(sig) = SIG_1.scan(game, main_module_range.0, main_module_range.1) { - game.read(sig + 1).ok()? - } else if let Some(sig) = SIG_2.scan(game, main_module_range.0, main_module_range.1) { - game.read(sig + 1).ok()? - } else { - return None; - }; + let mut ptr: Address32 = if let Some(sig) = SIG.scan(game, main_module_range) { + game.read(sig + 2).ok()? + } else if let Some(sig) = SIG_0.scan(game, main_module_range) { + game.read(sig + 1).ok()? + } else if let Some(sig) = SIG_1.scan(game, main_module_range) { + game.read(sig + 1).ok()? + } else if let Some(sig) = SIG_2.scan(game, main_module_range) { + game.read(sig + 1).ok()? + } else { + return None; + }; ptr = game.read::(ptr).ok()?; diff --git a/src/emulator/ps1/retroarch.rs b/src/emulator/ps1/retroarch.rs index 4d061af..565fe74 100644 --- a/src/emulator/ps1/retroarch.rs +++ b/src/emulator/ps1/retroarch.rs @@ -37,12 +37,14 @@ impl State { if is_64_bit { const SIG: Signature<14> = Signature::new("48 8B 05 ?? ?? ?? ?? 41 81 E4 FF FF 1F 00"); - let ptr = SIG.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 3; + let ptr = + SIG.scan(game, (core_address, game.get_module_size(core_name).ok()?))? + 3; let ptr = ptr + 0x4 + game.read::(ptr).ok()?; Some(game.read::(ptr).ok()?.into()) } else { const SIG: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 E3 FF FF 1F 00"); - let ptr = SIG.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 1; + let ptr = + SIG.scan(game, (core_address, game.get_module_size(core_name).ok()?))? + 1; let ptr = game.read::(ptr).ok()?; Some(game.read::(ptr).ok()?.into()) } @@ -51,12 +53,14 @@ impl State { if is_64_bit { const SIG: Signature<15> = Signature::new("48 89 0D ?? ?? ?? ?? 89 35 ?? ?? ?? ?? 89 3D"); - let addr = SIG.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 3; + let addr = + SIG.scan(game, (core_address, game.get_module_size(core_name).ok()?))? + 3; let ptr = addr + 0x4 + game.read::(addr).ok()?; Some(game.read::(ptr).ok()?.into()) } else { const SIG: Signature<8> = Signature::new("A1 ?? ?? ?? ?? 23 CB 8B"); - let ptr = SIG.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 1; + let ptr = + SIG.scan(game, (core_address, game.get_module_size(core_name).ok()?))? + 1; let ptr = game.read::(ptr).ok()?; Some(game.read::(ptr).ok()?.into()) } @@ -64,13 +68,15 @@ impl State { // PCSX ReARMed if is_64_bit { const SIG: Signature<9> = Signature::new("48 8B 35 ?? ?? ?? ?? 81 E2"); - let addr = SIG.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 3; + let addr = + SIG.scan(game, (core_address, game.get_module_size(core_name).ok()?))? + 3; let ptr = addr + 0x4 + game.read::(addr).ok()?; let ptr = game.read::(ptr).ok()?; Some(game.read::(ptr).ok()?.into()) } else { const SIG: Signature<9> = Signature::new("FF FF 1F 00 89 ?? ?? ?? A1"); - let ptr = SIG.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 9; + let ptr = + SIG.scan(game, (core_address, game.get_module_size(core_name).ok()?))? + 9; let ptr = game.read::(ptr).ok()?; Some(game.read::(ptr).ok()?.into()) } diff --git a/src/emulator/ps1/xebra.rs b/src/emulator/ps1/xebra.rs index 89f9068..a27987b 100644 --- a/src/emulator/ps1/xebra.rs +++ b/src/emulator/ps1/xebra.rs @@ -15,7 +15,7 @@ impl State { .filter(|(_, state)| matches!(state, super::State::Xebra(_))) .find_map(|(name, _)| game.get_module_range(name).ok())?; - let ptr = SIG.scan(game, main_module_range.0, main_module_range.1)? + 1; + let ptr = SIG.scan(game, main_module_range)? + 1; let addr = ptr + 0x4 + game.read::(ptr).ok()?; let addr = game.read::(addr + 0x16A).ok()?; let addr = game.read::(addr).ok()?; diff --git a/src/emulator/ps2/pcsx2.rs b/src/emulator/ps2/pcsx2.rs index f60e94c..dd35420 100644 --- a/src/emulator/ps2/pcsx2.rs +++ b/src/emulator/ps2/pcsx2.rs @@ -22,15 +22,15 @@ impl State { self.addr_base = if self.is_64_bit { const SIG: Signature<12> = Signature::new("48 8B ?? ?? ?? ?? ?? 25 F0 3F 00 00"); - let ptr = SIG.scan(game, main_module_range.0, main_module_range.1)? + 3; + let ptr = SIG.scan(game, main_module_range)? + 3; ptr + 0x4 + game.read::(ptr).ok()? } else { const SIG: Signature<11> = Signature::new("8B ?? ?? ?? ?? ?? 25 F0 3F 00 00"); const SIG_ALT: Signature<12> = Signature::new("8B ?? ?? ?? ?? ?? 81 ?? F0 3F 00 00"); - let ptr = if let Some(addr) = SIG.scan(game, main_module_range.0, main_module_range.1) { + let ptr = if let Some(addr) = SIG.scan(game, main_module_range) { addr + 2 } else { - SIG_ALT.scan(game, main_module_range.0, main_module_range.1)? + 2 + SIG_ALT.scan(game, main_module_range)? + 2 }; self.read_pointer(game, ptr).ok()? }; diff --git a/src/emulator/ps2/retroarch.rs b/src/emulator/ps2/retroarch.rs index 1aa4f7a..cca62d6 100644 --- a/src/emulator/ps2/retroarch.rs +++ b/src/emulator/ps2/retroarch.rs @@ -35,7 +35,7 @@ impl State { let base_addr = { const SIG: Signature<13> = Signature::new("48 8B ?? ?? ?? ?? ?? 81 ?? F0 3F 00 00"); - let ptr = SIG.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 3; + let ptr = SIG.scan(game, (core_address, game.get_module_size(core_name).ok()?))? + 3; ptr + 0x4 + game.read::(ptr).ok()? }; diff --git a/src/emulator/sms/blastem.rs b/src/emulator/sms/blastem.rs index ef15464..4dcc67d 100644 --- a/src/emulator/sms/blastem.rs +++ b/src/emulator/sms/blastem.rs @@ -19,10 +19,7 @@ impl State { .contains(MemoryRangeFlags::WRITE) && m.size().unwrap_or_default() == 0x101000 }) - .find_map(|m| { - let (base, size) = m.range().ok()?; - SIG.scan(game, base, size) - })? + .find_map(|m| SIG.scan(game, m.range().ok()?))? + 10; let wram: Address = game.read::(scanned_address).ok()?.into(); diff --git a/src/emulator/sms/fusion.rs b/src/emulator/sms/fusion.rs index f903825..6c21e94 100644 --- a/src/emulator/sms/fusion.rs +++ b/src/emulator/sms/fusion.rs @@ -17,7 +17,7 @@ impl State { .filter(|(_, state)| matches!(state, super::State::Fusion(_))) .find_map(|(name, _)| game.get_module_range(name).ok())?; - let ptr = SIG.scan(game, main_module.0, main_module.1)? + 4; + let ptr = SIG.scan(game, main_module)? + 4; self.addr = game.read::(ptr).ok()?.into(); Some(game.read::(self.addr).ok()?.add(0xC000).into()) diff --git a/src/emulator/sms/mednafen.rs b/src/emulator/sms/mednafen.rs index 120efaa..00cb563 100644 --- a/src/emulator/sms/mednafen.rs +++ b/src/emulator/sms/mednafen.rs @@ -21,8 +21,8 @@ impl State { pe::MachineType::read(game, main_module_range.0) == Some(pe::MachineType::X86_64); let ptr = match is_64_bit { - true => SIG_64.scan(game, main_module_range.0, main_module_range.1)? + 8, - false => SIG_32.scan(game, main_module_range.0, main_module_range.1)? + 7, + true => SIG_64.scan(game, main_module_range)? + 8, + false => SIG_32.scan(game, main_module_range)? + 7, }; Some(game.read::(ptr).ok()?.into()) diff --git a/src/emulator/sms/retroarch.rs b/src/emulator/sms/retroarch.rs index 6af64c5..5f4d6dc 100644 --- a/src/emulator/sms/retroarch.rs +++ b/src/emulator/sms/retroarch.rs @@ -50,11 +50,11 @@ impl State { Some( if is_64_bit { const SIG: Signature<9> = Signature::new("48 8D 0D ?? ?? ?? ?? 41 B8"); - let ptr = SIG.scan(game, self.core_base, module_size)? + 3; + let ptr = SIG.scan(game, (self.core_base, module_size))? + 3; ptr + 0x4 + game.read::(ptr).ok()? } else { const SIG: Signature<8> = Signature::new("B9 ?? ?? ?? ?? C1 EF 10"); - let ptr = SIG.scan(game, self.core_base, module_size)? + 1; + let ptr = SIG.scan(game, (self.core_base, module_size))? + 1; game.read::(ptr).ok()?.into() } + 0x20000, ) @@ -65,11 +65,11 @@ impl State { Some(if is_64_bit { const SIG: Signature<10> = Signature::new("48 8D 0D ?? ?? ?? ?? 4C 8B 2D"); - let ptr = SIG.scan(game, self.core_base, module_size)? + 3; + let ptr = SIG.scan(game, (self.core_base, module_size))? + 3; ptr + 0x4 + game.read::(ptr).ok()? } else { const SIG: Signature<7> = Signature::new("A3 ?? ?? ?? ?? 29 F9"); - let ptr = SIG.scan(game, self.core_base, module_size)? + 1; + let ptr = SIG.scan(game, (self.core_base, module_size))? + 1; game.read::(ptr).ok()?.into() }) } @@ -79,11 +79,11 @@ impl State { Some(if is_64_bit { const SIG: Signature<5> = Signature::new("31 F6 48 C7 05"); - let ptr = SIG.scan(game, self.core_base, module_size)? + 5; + let ptr = SIG.scan(game, (self.core_base, module_size))? + 5; ptr + 0x8 + game.read::(ptr).ok()? } else { const SIG: Signature<4> = Signature::new("83 FA 02 B8"); - let ptr = SIG.scan(game, self.core_base, module_size)? + 4; + let ptr = SIG.scan(game, (self.core_base, module_size))? + 4; game.read::(ptr).ok()?.into() }) } @@ -93,7 +93,7 @@ impl State { Some(if is_64_bit { const SIG: Signature<13> = Signature::new("83 ?? 02 75 ?? 48 8B 0D ?? ?? ?? ?? E8"); - let ptr = SIG.scan(game, self.core_base, module_size)? + 8; + let ptr = SIG.scan(game, (self.core_base, module_size))? + 8; let offset = game .read::(ptr + 13 + 0x4 + game.read::(ptr + 13).ok()? + 3) .ok()?; @@ -111,7 +111,7 @@ impl State { } } else { const SIG: Signature<12> = Signature::new("83 ?? 02 75 ?? 8B ?? ?? ?? ?? ?? E8"); - let ptr = SIG.scan(game, self.core_base, module_size)? + 7; + let ptr = SIG.scan(game, (self.core_base, module_size))? + 7; let offset = game .read::(ptr + 12 + 0x4 + game.read::(ptr + 12).ok()? + 2) .ok()?; diff --git a/src/game_engine/unity/il2cpp.rs b/src/game_engine/unity/il2cpp.rs index 1d0c8cf..fbaf22d 100644 --- a/src/game_engine/unity/il2cpp.rs +++ b/src/game_engine/unity/il2cpp.rs @@ -7,8 +7,11 @@ use core::{ }; use crate::{ - file_format::pe, future::retry, signature::Signature, string::ArrayCString, Address, Address64, - Error, PointerSize, Process, + file_format::pe, + future::retry, + signature::{Signature, SignatureScanner}, + string::ArrayCString, + Address, Address64, Error, PointerSize, Process, }; #[cfg(feature = "derive")] @@ -59,14 +62,14 @@ impl Module { const ASSEMBLIES_TRG_SIG: Signature<12> = Signature::new("48 FF C5 80 3C ?? 00 75 ?? 48 8B 1D"); - let addr = ASSEMBLIES_TRG_SIG.scan(process, mono_module.0, mono_module.1)? + 12; + let addr = ASSEMBLIES_TRG_SIG.scan(process, mono_module)? + 12; addr + 0x4 + process.read::(addr).ok()? } PointerSize::Bit32 => { const ASSEMBLIES_TRG_SIG: Signature<9> = Signature::new("8A 07 47 84 C0 75 ?? 8B 35"); - let addr = ASSEMBLIES_TRG_SIG.scan(process, mono_module.0, mono_module.1)? + 9; + let addr = ASSEMBLIES_TRG_SIG.scan(process, mono_module)? + 9; process.read_pointer(addr, pointer_size).ok()? } _ => return None, @@ -77,7 +80,7 @@ impl Module { Signature::new("48 83 3C ?? 00 75 ?? 8B C? E8"); let addr = TYPE_INFO_DEFINITION_TABLE_TRG_SIG - .scan(process, mono_module.0, mono_module.1)? + .scan(process, mono_module)? .add_signed(-4); process @@ -88,8 +91,7 @@ impl Module { const TYPE_INFO_DEFINITION_TABLE_TRG_SIG: Signature<10> = Signature::new("C3 A1 ?? ?? ?? ?? 83 3C ?? 00"); - let addr = - TYPE_INFO_DEFINITION_TABLE_TRG_SIG.scan(process, mono_module.0, mono_module.1)? + 2; + let addr = TYPE_INFO_DEFINITION_TABLE_TRG_SIG.scan(process, mono_module)? + 2; process .read_pointer(process.read_pointer(addr, pointer_size).ok()?, pointer_size) @@ -363,7 +365,7 @@ impl Class { &'a self, process: &'a Process, module: &'a Module, - ) -> impl FusedIterator + '_ { + ) -> impl FusedIterator + 'a { let mut this_class = Some(*self); iter::from_fn(move || { @@ -799,17 +801,14 @@ fn detect_version(process: &Process) -> Option { const SIG_202X: Signature<6> = Signature::new("00 32 30 32 ?? 2E"); const SIG_2019: Signature<6> = Signature::new("00 32 30 31 39 2E"); - if SIG_202X - .scan(process, unity_module.0, unity_module.1) - .is_some() - { + if SIG_202X.scan(process, unity_module).is_some() { let il2cpp_version = { const SIG: Signature<14> = Signature::new("48 2B ?? 48 2B ?? ?? ?? ?? ?? 48 F7 ?? 48"); let address = process.get_module_address("GameAssembly.dll").ok()?; let size = pe::read_size_of_image(process, address)? as u64; let ptr = { - let addr = SIG.scan(process, address, size)? + 6; + let addr = SIG.scan(process, (address, size))? + 6; addr + 0x4 + process.read::(addr).ok()? }; @@ -822,10 +821,7 @@ fn detect_version(process: &Process) -> Option { } else { Version::V2019 }) - } else if SIG_2019 - .scan(process, unity_module.0, unity_module.1) - .is_some() - { + } else if SIG_2019.scan(process, unity_module).is_some() { Some(Version::V2019) } else { Some(Version::Base) diff --git a/src/game_engine/unity/mono.rs b/src/game_engine/unity/mono.rs index 8c7e2bd..c8791b3 100644 --- a/src/game_engine/unity/mono.rs +++ b/src/game_engine/unity/mono.rs @@ -2,8 +2,11 @@ //! backend. use crate::{ - file_format::pe, future::retry, signature::Signature, string::ArrayCString, Address, Address32, - Address64, Error, PointerSize, Process, + file_format::pe, + future::retry, + signature::{Signature, SignatureScanner}, + string::ArrayCString, + Address, Address32, Address64, Error, PointerSize, Process, }; use core::{ array, @@ -63,7 +66,7 @@ impl Module { PointerSize::Bit64 => { const SIG_MONO_64: Signature<3> = Signature::new("48 8B 0D"); let scan_address: Address = - SIG_MONO_64.scan(process, root_domain_function_address, 0x100)? + 3; + SIG_MONO_64.scan(process, (root_domain_function_address, 0x100))? + 3; scan_address + 0x4 + process.read::(scan_address).ok()? } PointerSize::Bit32 => { @@ -72,7 +75,7 @@ impl Module { let ptr = [SIG_32_1, SIG_32_2] .iter() - .find_map(|sig| sig.scan(process, root_domain_function_address, 0x100))? + .find_map(|sig| sig.scan(process, (root_domain_function_address, 0x100)))? + 2; process.read::(ptr).ok()?.into() @@ -916,7 +919,7 @@ fn detect_version(process: &Process) -> Option { const SIG_202X: Signature<6> = Signature::new("00 32 30 32 ?? 2E"); - let Some(addr) = SIG_202X.scan(process, unity_module.0, unity_module.1) else { + let Some(addr) = SIG_202X.scan(process, unity_module) else { return Some(Version::V2); }; diff --git a/src/game_engine/unity/scene.rs b/src/game_engine/unity/scene.rs index 21e8d6f..d7c9e2f 100644 --- a/src/game_engine/unity/scene.rs +++ b/src/game_engine/unity/scene.rs @@ -59,13 +59,13 @@ impl SceneManager { // There are multiple signatures that can be used, depending on the version of Unity // used in the target game. let base_address: Address = if pointer_size == PointerSize::Bit64 { - let addr = SIG_64_BIT.scan(process, unity_player.0, unity_player.1)? + 7; + let addr = SIG_64_BIT.scan(process, unity_player)? + 7; addr + 0x4 + process.read::(addr).ok()? - } else if let Some(addr) = SIG_32_1.scan(process, unity_player.0, unity_player.1) { + } else if let Some(addr) = SIG_32_1.scan(process, unity_player) { process.read::(addr + 5).ok()?.into() - } else if let Some(addr) = SIG_32_2.scan(process, unity_player.0, unity_player.1) { + } else if let Some(addr) = SIG_32_2.scan(process, unity_player) { process.read::(addr.add_signed(-4)).ok()?.into() - } else if let Some(addr) = SIG_32_3.scan(process, unity_player.0, unity_player.1) { + } else if let Some(addr) = SIG_32_3.scan(process, unity_player) { process.read::(addr + 7).ok()?.into() } else { return None; diff --git a/src/game_engine/unreal/mod.rs b/src/game_engine/unreal/mod.rs index b651a8f..95799ed 100644 --- a/src/game_engine/unreal/mod.rs +++ b/src/game_engine/unreal/mod.rs @@ -52,9 +52,9 @@ impl Module { (Signature::new("A8 01 75 ?? C7 05 ??"), 6), ]; - let addr = GENGINE.iter().find_map(|(sig, offset)| { - Some(sig.scan(process, module_range.0, module_range.1)? + *offset) - })?; + let addr = GENGINE + .iter() + .find_map(|(sig, offset)| Some(sig.scan(process, module_range)? + *offset))?; addr + 0x8 + process.read::(addr).ok()? }; @@ -64,9 +64,9 @@ impl Module { 3, )]; - let addr = GWORLD.iter().find_map(|(sig, offset)| { - Some(sig.scan(process, module_range.0, module_range.1)? + *offset) - })?; + let addr = GWORLD + .iter() + .find_map(|(sig, offset)| Some(sig.scan(process, module_range)? + *offset))?; addr + 0x4 + process.read::(addr).ok()? }; @@ -77,9 +77,9 @@ impl Module { (Signature::new("57 0F B7 F8 74 ?? B8 ?? ?? ?? ?? 8B 44"), 7), ]; - let addr = FNAME_POOL.iter().find_map(|(sig, offset)| { - Some(sig.scan(process, module_range.0, module_range.1)? + *offset) - })?; + let addr = FNAME_POOL + .iter() + .find_map(|(sig, offset)| Some(sig.scan(process, module_range)? + *offset))?; addr + 0x4 + process.read::(addr).ok()? }; diff --git a/src/signature.rs b/src/signature.rs index 879c8d9..9fd6fd9 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -307,8 +307,7 @@ pub trait SignatureScanner { /// * `len` - The length of the memory range to scan. /// /// Returns `Some(Address)` of the first match if found, otherwise `None`. - fn scan<'a>(&'a self, process: &Process, addr: impl Into
, len: u64) - -> Option
; + fn scan<'a>(&'a self, process: &Process, range: (impl Into
, u64)) -> Option
; /// Returns an iterator over all occurrences of the signature in the process's memory range. /// @@ -322,8 +321,7 @@ pub trait SignatureScanner { fn scan_process_range<'a>( &'a self, process: &'a Process, - addr: impl Into
, - len: u64, + range: (impl Into
, u64), ) -> impl Iterator + 'a; /// Asynchronously awaits scanning a process for the signature until a match @@ -338,8 +336,7 @@ pub trait SignatureScanner { async fn wait_scan_process_range( &self, process: &Process, - addr: impl Into
, - len: u64, + range: (impl Into
, u64), ) -> Address; } @@ -347,22 +344,20 @@ impl SignatureScanner for Signature { fn scan<'a>( &'a self, process: &'a Process, - addr: impl Into
, - len: u64, + range: (impl Into
, u64), ) -> Option
{ - self.scan_process_range(process, addr, len).next() + self.scan_process_range(process, range).next() } fn scan_process_range<'a>( &'a self, process: &'a Process, - addr: impl Into
, - len: u64, + range: (impl Into
, u64), ) -> impl Iterator + 'a { const MEM_SIZE: usize = 0x1000; - let mut addr: Address = Into::into(addr); - let overall_end = addr.value() + len; + let mut addr: Address = Into::into(range.0); + let overall_end = addr.value() + range.1; // The sigscan essentially works by reading one memory page (0x1000 bytes) // at a time and looking for the signature in each page. We will create a buffer @@ -462,10 +457,9 @@ impl SignatureScanner for Signature { async fn wait_scan_process_range( &self, process: &Process, - addr: impl Into
, - len: u64, + range: (impl Into
, u64), ) -> Address { - let addr = addr.into(); - retry(|| self.scan_process_range(process, addr, len).next()).await + let addr = range.0.into(); + retry(|| self.scan_process_range(process, (addr, range.1)).next()).await } }