Skip to content

Commit

Permalink
Reimplemented to return an iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Jujstme committed Oct 12, 2024
1 parent 8af2c3e commit d722877
Show file tree
Hide file tree
Showing 28 changed files with 519 additions and 348 deletions.
14 changes: 9 additions & 5 deletions src/emulator/gba/mednafen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{file_format::pe, signature::Signature, Address, Address32, Address64, Error, Process};
use crate::{
file_format::pe,
signature::{Signature, SignatureScanner},
Address, Address32, Address64, Error, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -20,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_process_range(game, main_module_range)? + 3;
let ptr: Address = SIG.scan(game, main_module_range.0, main_module_range.1)? + 3;
let mut addr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -36,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_process_range(game, main_module_range)? + 3;
let ptr: Address = SIG2.scan(game, main_module_range.0, main_module_range.1)? + 3;
let mut addr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -56,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_process_range(game, main_module_range)?;
let ptr = SIG.scan(game, main_module_range.0, main_module_range.1)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};

self.cached_iwram_pointer = {
const SIG2: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF 7F 00 00");
let ptr = SIG2.scan_process_range(game, main_module_range)?;
let ptr = SIG2.scan(game, main_module_range.0, main_module_range.1)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};

Expand Down
7 changes: 5 additions & 2 deletions src/emulator/gba/nocashgba.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{signature::Signature, Address, Address32, Process};
use crate::{
signature::{Signature, SignatureScanner},
Address, Address32, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -16,7 +19,7 @@ impl State {
.find_map(|(name, _)| game.get_module_range(name).ok())?;

self.base_addr = game
.read::<Address32>(SIG.scan_process_range(game, main_module_range)? + 0x2)
.read::<Address32>(SIG.scan(game, main_module_range.0, main_module_range.1)? + 0x2)
.ok()?
.into();

Expand Down
22 changes: 13 additions & 9 deletions src/emulator/gba/retroarch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{file_format::pe, signature::Signature, Address, Address32, Address64, Process};
use crate::{
file_format::pe,
signature::{Signature, SignatureScanner},
Address, Address32, Address64, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand Down Expand Up @@ -47,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_process_range(game, module_range)? + 3;
let ptr: Address = SIG.scan(game, module_range.0, module_range.1)? + 3;
let mut addr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -61,7 +65,7 @@ impl State {
};

let iwram_pointer = {
let ptr: Address = SIG2.scan_process_range(game, module_range)? + 3;
let ptr: Address = SIG2.scan(game, module_range.0, module_range.1)? + 3;
let mut addr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -85,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_process_range(game, module_range)?;
let ptr = SIG.scan(game, module_range.0, module_range.1)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};
let iwram_pointer: Address = {
const SIG2: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF 7F 00 00");
let ptr = SIG2.scan_process_range(game, module_range)?;
let ptr = SIG2.scan(game, module_range.0, module_range.1)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};

Expand All @@ -114,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_process_range(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::<i32>(ptr).ok()?;
game.read::<Address64>(ptr).ok()?.into()
}
false => {
const SIG: Signature<11> = Signature::new("A3 ?? ?? ?? ?? F7 C5 02 00 00 00");
let ptr = SIG.scan_process_range(game, (self.core_base, module_size))? + 1;
let ptr = SIG.scan(game, self.core_base, module_size)? + 1;
game.read::<Address32>(ptr).ok()?.into()
}
};

let ewram = {
let offset = SIG_EWRAM.scan_process_range(game, (self.core_base, module_size))? + 8;
let offset = SIG_EWRAM.scan(game, self.core_base, module_size)? + 8;
base_addr + game.read::<i32>(offset).ok()?
};

let iwram = {
let offset = SIG_IWRAM.scan_process_range(game, (self.core_base, module_size))? + 9;
let offset = SIG_IWRAM.scan(game, self.core_base, module_size)? + 9;
base_addr + game.read::<i32>(offset).ok()?
};

Expand Down
28 changes: 17 additions & 11 deletions src/emulator/gba/vba.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{file_format::pe, signature::Signature, Address, Address32, Address64, Error, Process};
use crate::{
file_format::pe,
signature::{Signature, SignatureScanner},
Address, Address32, Address64, Error, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -25,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_process_range(game, main_module_range)? + 3;
let ptr: Address = SIG.scan(game, main_module_range.0, main_module_range.1)? + 3;
let mut addr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -39,7 +43,7 @@ impl State {
};

self.cached_iwram_pointer = {
let ptr: Address = SIG2.scan_process_range(game, main_module_range)? + 3;
let ptr: Address = SIG2.scan(game, main_module_range.0, main_module_range.1)? + 3;
let mut addr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -58,11 +62,13 @@ 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_process_range(game, main_module_range) {
if let Some(ptr) = SIG_RUNNING.scan(game, main_module_range.0, main_module_range.1)
{
let ptr = ptr + 2;
ptr + 0x4 + game.read::<i32>(ptr).ok()? + 0x1
} else {
let ptr = SIG_RUNNING2.scan_process_range(game, main_module_range)? + 3;
let ptr =
SIG_RUNNING2.scan(game, main_module_range.0, main_module_range.1)? + 3;
let ptr = ptr + 0x4 + game.read::<i32>(ptr).ok()?;
game.read::<Address64>(ptr).ok()?.into()
}
Expand All @@ -76,11 +82,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_process_range(game, main_module_range) {
if let Some(ptr) = SIG.scan(game, main_module_range.0, main_module_range.1) {
self.cached_ewram_pointer = game.read::<Address32>(ptr + 1).ok()?.into();
self.cached_iwram_pointer = {
const SIG2: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF 7F 00 00");
let ptr = SIG2.scan_process_range(game, main_module_range)?;
let ptr = SIG2.scan(game, main_module_range.0, main_module_range.1)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};

Expand All @@ -91,8 +97,8 @@ impl State {
Signature::new("8B 15 ?? ?? ?? ?? 31 C0 85 D2 74 ?? 0F");

let ptr = SIG
.scan_process_range(game, main_module_range)
.or_else(|| SIG_OLD.scan_process_range(game, main_module_range))?;
.scan(game, main_module_range.0, main_module_range.1)
.or_else(|| SIG_OLD.scan(game, main_module_range.0, main_module_range.1))?;

game.read::<Address32>(ptr + 2).ok()?.into()
};
Expand All @@ -101,15 +107,15 @@ impl State {
let iwram = game.read::<Address32>(self.cached_iwram_pointer).ok()?;

Some([ewram.into(), iwram.into()])
} else if let Some(ptr) = SIG_OLD.scan_process_range(game, main_module_range) {
} else if let Some(ptr) = SIG_OLD.scan(game, main_module_range.0, main_module_range.1) {
// This code is for very old versions of VisualBoyAdvance (1.8.0-beta 3)
self.cached_ewram_pointer = game.read::<Address32>(ptr + 8).ok()?.into();
self.cached_iwram_pointer = self.cached_ewram_pointer.add_signed(0x4);

self.is_emulating = {
const SIG_RUNNING: Signature<11> =
Signature::new("8B 0D ?? ?? ?? ?? 85 C9 74 ?? 8A");
let ptr = SIG_RUNNING.scan_process_range(game, main_module_range)? + 2;
let ptr = SIG_RUNNING.scan(game, main_module_range.0, main_module_range.1)? + 2;
game.read::<Address32>(ptr).ok()?.into()
};

Expand Down
11 changes: 9 additions & 2 deletions src/emulator/genesis/blastem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{runtime::MemoryRangeFlags, signature::Signature, Address, Address32, Endian, Process};
use crate::{
runtime::MemoryRangeFlags,
signature::{Signature, SignatureScanner},
Address, Address32, Endian, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State;
Expand All @@ -18,7 +22,10 @@ impl State {
.contains(MemoryRangeFlags::WRITE)
&& m.size().unwrap_or_default() == 0x101000
})
.find_map(|m| SIG.scan_process_range(game, m.range().ok()?))?
.find_map(|m| {
let (base, size) = m.range().ok()?;
SIG.scan(game, base, size)
})?
+ 11;

let wram = game.read::<Address32>(scanned_address).ok()?;
Expand Down
7 changes: 5 additions & 2 deletions src/emulator/genesis/fusion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{signature::Signature, Address, Address32, Endian, Process};
use crate::{
signature::{Signature, SignatureScanner},
Address, Address32, Endian, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -14,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_process_range(game, main_module)? + 1;
let ptr = SIG.scan(game, main_module.0, main_module.1)? + 1;

let addr = ptr + game.read::<u8>(ptr).ok()? as u64 + 3;
let addr = game.read::<Address32>(addr).ok()?;
Expand Down
7 changes: 5 additions & 2 deletions src/emulator/genesis/gens.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{signature::Signature, Address, Address32, Endian, Process};
use crate::{
signature::{Signature, SignatureScanner},
Address, Address32, Endian, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State;
Expand All @@ -12,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_process_range(game, main_module)? + 11;
let ptr = SIG.scan(game, main_module.0, main_module.1)? + 11;

*endian = if game.read::<u8>(ptr + 4).ok()? == 0x86 {
Endian::Big
Expand Down
33 changes: 15 additions & 18 deletions src/emulator/genesis/retroarch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{
file_format::pe, signature::Signature, Address, Address32, Endian, MemoryRangeFlags, Process,
file_format::pe,
signature::{Signature, SignatureScanner},
Address, Address32, Endian, MemoryRangeFlags, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -45,7 +47,10 @@ impl State {
.contains(MemoryRangeFlags::WRITE)
&& m.size().unwrap_or_default() == 0x101000
})
.find_map(|m| SIG.scan_process_range(game, m.range().ok()?))?
.find_map(|m| {
let (base, size) = m.range().ok()?;
SIG.scan(game, base, size)
})?
+ 11;

let wram = game.read::<Address32>(scanned_address).ok()?;
Expand All @@ -58,21 +63,17 @@ impl State {
if is_x86_64 {
const SIG_64: Signature<10> = Signature::new("48 8D 0D ?? ?? ?? ?? 4C 8B 2D");

let addr = SIG_64.scan_process_range(
game,
(core_address, game.get_module_size(core_name).ok()?),
)? + 3;
let addr =
SIG_64.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 3;

let wram = addr + 0x4 + game.read::<i32>(addr).ok()?;

Some(wram)
} else {
const SIG_32: Signature<7> = Signature::new("A3 ?? ?? ?? ?? 29 F9");

let ptr = SIG_32.scan_process_range(
game,
(core_address, game.get_module_size(core_name).ok()?),
)? + 1;
let ptr =
SIG_32.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 1;

let wram = game.read::<Address32>(ptr).ok()?;

Expand All @@ -85,21 +86,17 @@ impl State {
if is_x86_64 {
const SIG_64: Signature<9> = Signature::new("48 8D 0D ?? ?? ?? ?? 41 B8");

let addr = SIG_64.scan_process_range(
game,
(core_address, game.get_module_size(core_name).ok()?),
)? + 3;
let addr =
SIG_64.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 3;

let wram = addr + 0x4 + game.read::<i32>(addr).ok()?;

Some(wram)
} else {
const SIG_32: Signature<8> = Signature::new("B9 ?? ?? ?? ?? C1 EF 10");

let ptr = SIG_32.scan_process_range(
game,
(core_address, game.get_module_size(core_name).ok()?),
)? + 1;
let ptr =
SIG_32.scan(game, core_address, game.get_module_size(core_name).ok()?)? + 1;

let wram = game.read::<Address32>(ptr).ok()?;

Expand Down
9 changes: 6 additions & 3 deletions src/emulator/genesis/segaclassics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{signature::Signature, Address, Address32, Endian, Process};
use crate::{
signature::{Signature, SignatureScanner},
Address, Address32, Endian, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -13,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_process_range(game, module)? + 2
SIG_GAMEROOM.scan(game, module.0, module.1)? + 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_process_range(game, main_module)? + 8
SIG_SEGACLASSICS.scan(game, main_module.0, main_module.1)? + 8
};

ptr = game.read::<Address32>(ptr).ok()?.into();
Expand Down
8 changes: 6 additions & 2 deletions src/emulator/ps1/duckstation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{file_format::pe, signature::Signature, Address, Address64, Process};
use crate::{
file_format::pe,
signature::{Signature, SignatureScanner},
Address, Address64, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -24,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_process_range(game, main_module_range)? + 3;
let addr = SIG.scan(game, main_module_range.0, main_module_range.1)? + 3;
self.addr = addr + 0x4 + game.read::<i32>(addr).ok()?;
}

Expand Down
Loading

0 comments on commit d722877

Please sign in to comment.