Skip to content

Commit

Permalink
Reimplemented use of ranges in the scan method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jujstme committed Nov 8, 2024
1 parent ca2e5f3 commit da55c6c
Show file tree
Hide file tree
Showing 27 changed files with 125 additions and 141 deletions.
8 changes: 4 additions & 4 deletions src/emulator/gba/mednafen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -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::<i32>(ptr).ok()?;

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

Expand Down
2 changes: 1 addition & 1 deletion src/emulator/gba/nocashgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl State {
.find_map(|(name, _)| game.get_module_range(name).ok())?;

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

Expand Down
16 changes: 8 additions & 8 deletions src/emulator/gba/retroarch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -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::<i32>(ptr).ok()?;

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

Expand All @@ -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::<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(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(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(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
22 changes: 10 additions & 12 deletions src/emulator/gba/vba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -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::<i32>(ptr).ok()?;

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

Expand All @@ -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::<Address32>(ptr + 2).ok()?.into()
};
Expand All @@ -107,15 +105,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(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::<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(game, main_module_range.0, main_module_range.1)? + 2;
let ptr = SIG_RUNNING.scan(game, main_module_range)? + 2;
game.read::<Address32>(ptr).ok()?.into()
};

Expand Down
5 changes: 1 addition & 4 deletions src/emulator/genesis/blastem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Address32>(scanned_address).ok()?;
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/genesis/fusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>(ptr).ok()? as u64 + 3;
let addr = game.read::<Address32>(addr).ok()?;
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/genesis/gens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>(ptr + 4).ok()? == 0x86 {
Endian::Big
Expand Down
13 changes: 5 additions & 8 deletions src/emulator/genesis/retroarch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Address32>(scanned_address).ok()?;
Expand All @@ -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::<i32>(addr).ok()?;

Expand All @@ -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::<Address32>(ptr).ok()?;

Expand All @@ -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::<i32>(addr).ok()?;

Expand All @@ -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::<Address32>(ptr).ok()?;

Expand Down
4 changes: 2 additions & 2 deletions src/emulator/genesis/segaclassics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Address32>(ptr).ok()?.into();
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/ps1/duckstation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32>(addr).ok()?;
}

Expand Down
2 changes: 1 addition & 1 deletion src/emulator/ps1/epsxe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Address32>(ptr).ok()?.into())
}
Expand Down
4 changes: 2 additions & 2 deletions src/emulator/ps1/mednafen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Address32>(ptr).ok()?.into())
Expand Down
9 changes: 3 additions & 6 deletions src/emulator/ps1/pcsx_redux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Address64>(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::<u8>(offset).ok()? as u64;

let addr = game.read::<Address64>(self.addr + offset).ok()?;
Expand All @@ -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::<Address32>(self.addr_base).ok()?.into();
Expand Down
23 changes: 11 additions & 12 deletions src/emulator/ps1/psxfin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Address32>(ptr).ok()?;

Expand Down
Loading

0 comments on commit da55c6c

Please sign in to comment.