Added `is_key_down` example using the games input system

The "ResolveJmp" and "ResolveRip" operation enum names were also renamed to "Jmp" and "Rip" for simplicity.
This commit is contained in:
a2x 2023-11-09 22:50:19 +10:00
parent 92db16a3d1
commit a1639bea1c
3 changed files with 61 additions and 30 deletions

View File

@ -6,7 +6,7 @@
"pattern": "8B 05 ? ? ? ? C3 CC CC CC CC CC CC CC CC CC 48 8B 0D ? ? ? ? 48 8D 05", "pattern": "8B 05 ? ? ? ? C3 CC CC CC CC CC CC CC CC CC 48 8B 0D ? ? ? ? 48 8D 05",
"operations": [ "operations": [
{ {
"type": "resolveRip", "type": "rip",
"offset": 2, "offset": 2,
"length": 6 "length": 6
} }
@ -18,7 +18,7 @@
"pattern": "48 8B 0D ? ? ? ? 48 89 7C 24 ? 8B FA C1 EB", "pattern": "48 8B 0D ? ? ? ? 48 89 7C 24 ? 8B FA C1 EB",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -28,7 +28,7 @@
"pattern": "48 8D 0D ? ? ? ? E9 E4 44 B4 FF", "pattern": "48 8D 0D ? ? ? ? E9 E4 44 B4 FF",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
}, },
{ {
"type": "add", "type": "add",
@ -42,7 +42,7 @@
"pattern": "48 8D 0D ? ? ? ? E9 F4 44 B4 FF", "pattern": "48 8D 0D ? ? ? ? E9 F4 44 B4 FF",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
}, },
{ {
"type": "add", "type": "add",
@ -56,7 +56,7 @@
"pattern": "48 8B 05 ? ? ? ? 4C 8D 1D", "pattern": "48 8B 05 ? ? ? ? 4C 8D 1D",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
}, },
{ {
"type": "add", "type": "add",
@ -70,7 +70,7 @@
"pattern": "48 8B 05 ? ? ? ? 4C 8D 3D ? ? ? ? 48 89 45", "pattern": "48 8B 05 ? ? ? ? 4C 8D 3D ? ? ? ? 48 89 45",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
}, },
{ {
"type": "add", "type": "add",
@ -84,7 +84,7 @@
"pattern": "48 8B 05 ? ? ? ? 4C 8D 0D ? ? ? ? 48 89 45", "pattern": "48 8B 05 ? ? ? ? 4C 8D 0D ? ? ? ? 48 89 45",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
}, },
{ {
"type": "add", "type": "add",
@ -98,7 +98,7 @@
"pattern": "48 8B 05 ? ? ? ? 48 8D 1D ? ? ? ? 48 89 45", "pattern": "48 8B 05 ? ? ? ? 48 8D 1D ? ? ? ? 48 89 45",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
}, },
{ {
"type": "add", "type": "add",
@ -112,7 +112,7 @@
"pattern": "48 8B 05 ? ? ? ? 48 8D 0D ? ? ? ? 44 8B 15", "pattern": "48 8B 05 ? ? ? ? 48 8D 0D ? ? ? ? 44 8B 15",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
}, },
{ {
"type": "add", "type": "add",
@ -126,7 +126,7 @@
"pattern": "48 8B 05 ? ? ? ? 48 8D 15 ? ? ? ? 48 89 45", "pattern": "48 8B 05 ? ? ? ? 48 8D 15 ? ? ? ? 48 89 45",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
}, },
{ {
"type": "add", "type": "add",
@ -140,7 +140,7 @@
"pattern": "48 8B 1D ? ? ? ? 48 89 1D", "pattern": "48 8B 1D ? ? ? ? 48 89 1D",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -162,7 +162,7 @@
"pattern": "48 89 0D ? ? ? ? 8B 0D", "pattern": "48 89 0D ? ? ? ? 8B 0D",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -172,7 +172,7 @@
"pattern": "48 89 0D ? ? ? ? 48 89 41", "pattern": "48 89 0D ? ? ? ? 48 89 41",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -182,7 +182,7 @@
"pattern": "48 8B 05 ? ? ? ? C3 CC CC CC CC CC CC CC CC 48 89 5C 24 ? 48 89 6C 24", "pattern": "48 8B 05 ? ? ? ? C3 CC CC CC CC CC CC CC CC 48 89 5C 24 ? 48 89 6C 24",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -192,7 +192,7 @@
"pattern": "48 89 05 ? ? ? ? 48 8D 05", "pattern": "48 89 05 ? ? ? ? 48 8D 05",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -202,7 +202,7 @@
"pattern": "4C 8B 0D ? ? ? ? 4C 8B D2 4C 8B D9", "pattern": "4C 8B 0D ? ? ? ? 4C 8B D2 4C 8B D9",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -212,7 +212,7 @@
"pattern": "48 8B 05 ? ? ? ? 48 85 C0 74 4F", "pattern": "48 8B 05 ? ? ? ? 48 85 C0 74 4F",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -222,7 +222,7 @@
"pattern": "48 8D 05 ? ? ? ? C3 CC CC CC CC CC CC CC CC 48 83 EC ? 8B 0D", "pattern": "48 8D 05 ? ? ? ? C3 CC CC CC CC CC CC CC CC 48 83 EC ? 8B 0D",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
}, },
{ {
"type": "add", "type": "add",
@ -236,7 +236,7 @@
"pattern": "48 89 3D ? ? ? ? 48 8D 15", "pattern": "48 89 3D ? ? ? ? 48 8D 15",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -286,7 +286,7 @@
"pattern": "48 8B 15 ? ? ? ? FF C0 48 8D 4C 24", "pattern": "48 8B 15 ? ? ? ? FF C0 48 8D 4C 24",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -296,7 +296,7 @@
"pattern": "48 8D 05 ? ? ? ? C3 CC CC CC CC CC CC CC CC 48 83 EC ? 8B 0D", "pattern": "48 8D 05 ? ? ? ? C3 CC CC CC CC CC CC CC CC 48 83 EC ? 8B 0D",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -306,7 +306,7 @@
"pattern": "48 8B 0D ? ? ? ? 48 8B 01 48 FF 60 30", "pattern": "48 8B 0D ? ? ? ? 48 8B 01 48 FF 60 30",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
}, },
{ {
"type": "dereference" "type": "dereference"
@ -323,7 +323,7 @@
"pattern": "48 8D 0D ? ? ? ? 48 C1 E0 06", "pattern": "48 8D 0D ? ? ? ? 48 C1 E0 06",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -333,7 +333,7 @@
"pattern": "48 89 05 ? ? ? ? 48 8B C8 48 85 C0", "pattern": "48 89 05 ? ? ? ? 48 8B C8 48 85 C0",
"operations": [ "operations": [
{ {
"type": "resolveRip" "type": "rip"
} }
] ]
}, },
@ -343,7 +343,7 @@
"pattern": "8B 05 ? ? ? ? 89 03", "pattern": "8B 05 ? ? ? ? 89 03",
"operations": [ "operations": [
{ {
"type": "resolveRip", "type": "rip",
"offset": 2, "offset": 2,
"length": 6 "length": 6
} }
@ -355,7 +355,7 @@
"pattern": "8B 05 ? ? ? ? 89 07", "pattern": "8B 05 ? ? ? ? 89 07",
"operations": [ "operations": [
{ {
"type": "resolveRip", "type": "rip",
"offset": 2, "offset": 2,
"length": 6 "length": 6
} }

View File

@ -24,7 +24,7 @@ pub enum Operation {
/// ///
/// `offset` is the offset of the displacement value. If `None`, the offset will be `0x1`. /// `offset` is the offset of the displacement value. If `None`, the offset will be `0x1`.
/// `length` is the length of the instruction. If `None`, the length will be `0x5`. /// `length` is the length of the instruction. If `None`, the length will be `0x5`.
ResolveJmp { Jmp {
offset: Option<usize>, offset: Option<usize>,
length: Option<usize>, length: Option<usize>,
}, },
@ -34,7 +34,7 @@ pub enum Operation {
/// ///
/// `offset` is the offset of the displacement value. If `None`, the offset will be `0x3`. /// `offset` is the offset of the displacement value. If `None`, the offset will be `0x3`.
/// `length` is the length of the instruction. If `None`, the length will be `0x7`. /// `length` is the length of the instruction. If `None`, the length will be `0x7`.
ResolveRip { Rip {
offset: Option<usize>, offset: Option<usize>,
length: Option<usize>, length: Option<usize>,
}, },

View File

@ -74,12 +74,12 @@ pub fn dump_offsets(
)?; )?;
} }
} }
ResolveJmp { offset, length } => { Jmp { offset, length } => {
address = process address = process
.resolve_jmp(address, offset.unwrap_or(0x1), length.unwrap_or(0x5))? .resolve_jmp(address, offset.unwrap_or(0x1), length.unwrap_or(0x5))?
.into() .into()
} }
ResolveRip { offset, length } => { Rip { offset, length } => {
address = process address = process
.resolve_rip(address, offset.unwrap_or(0x3), length.unwrap_or(0x7))? .resolve_rip(address, offset.unwrap_or(0x3), length.unwrap_or(0x7))?
.into() .into()
@ -141,8 +141,12 @@ pub fn dump_offsets(
mod tests { mod tests {
use super::*; use super::*;
use core::arch::x86_64::_bittest;
use std::ffi::{c_char, c_void}; use std::ffi::{c_char, c_void};
use std::mem::offset_of; use std::mem::offset_of;
use std::thread::sleep;
use std::time::Duration;
fn setup() -> Result<Process> { fn setup() -> Result<Process> {
let mut process = Process::new("cs2.exe")?; let mut process = Process::new("cs2.exe")?;
@ -267,6 +271,33 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn is_key_down() -> Result<()> {
let process = setup()?;
let input_system_base = process
.get_module_by_name("inputsystem.dll")
.expect("Failed to find inputsystem.dll")
.base();
let input_system = input_system_base + 0x35770;
let is_key_down = |key_code: i32| -> bool {
let key_map_element = process
.read_memory::<i32>((input_system + 0x4 * (key_code as usize / 32) + 0x12A0).into())
.unwrap_or(0);
unsafe { _bittest(&key_map_element, key_code & 0x1F) != 0 }
};
sleep(Duration::from_secs(1));
// See https://www.unknowncheats.me/forum/3855779-post889.html for button codes.
println!("Insert down: {}", is_key_down(73));
Ok(())
}
#[test] #[test]
fn local_player_controller() -> Result<()> { fn local_player_controller() -> Result<()> {
let process = setup()?; let process = setup()?;