### Complete Game Trainer Example in Rust Source: https://context7.com/frankvdstam/mem-rs/llms.txt A comprehensive example demonstrating the creation of a game trainer using mem-rs. It covers attaching to a process, scanning for game data managers and player stats via byte patterns, and continuously monitoring and modifying player HP and playtime. ```rust use std::thread::sleep; use std::time::Duration; use mem_rs::prelude::*; struct GameTrainer { process: Process, game_data_man: Pointer, player_stats: Pointer, } impl GameTrainer { pub fn new() -> Self { GameTrainer { process: Process::new("DarkSoulsRemastered.exe"), game_data_man: Pointer::default(), player_stats: Pointer::default(), } } pub fn refresh(&mut self) -> Result<(), String> { if !self.process.is_attached() { // Initial attachment - scan for patterns self.process.refresh()?; self.game_data_man = self.process.scan_rel( "GameDataMan", "48 8b 05 ? ? ? ? 48 8b 50 10 48 89 54 24 60", 3, 7, vec![0] )?; self.player_stats = self.process.scan_rel( "PlayerStats", "48 8b 0d ? ? ? ? 48 85 c9 74 0e 48 83 c1 28", 3, 7, vec![0] )?; println!("Attached to process"); } else { // Verify process still running self.process.refresh()?; } Ok(()) } pub fn get_play_time_ms(&self) -> u32 { self.game_data_man.read_u32_rel(Some(0xa4)) } pub fn get_player_hp(&self) -> i32 { self.player_stats.read_i32_rel(Some(0x3E8)) } pub fn set_player_hp(&self, hp: i32) { self.player_stats.write_i32_rel(Some(0x3E8), hp); } } fn main() { let mut trainer = GameTrainer::new(); loop { match trainer.refresh() { Ok(()) => { let time_ms = trainer.get_play_time_ms(); let hp = trainer.get_player_hp(); println!("Time: {}ms, HP: {}", time_ms, hp); } Err(e) => println!("Error: {}", e) } sleep(Duration::from_secs(1)); } } ``` -------------------------------- ### Rust Example: Interacting with Dark Souls Remastered Memory Source: https://github.com/frankvdstam/mem-rs/blob/main/README.md This Rust code demonstrates how to use the mem-rs library to attach to the 'DarkSoulsRemastered.exe' process, scan for specific memory patterns to locate game data, and read values like in-game time and AI timer. It includes error handling for process attachment and refreshing. ```rust struct Ds1 { process: Process, game_data_man: Pointer, ai_timer: Pointer, } impl Ds1 { pub fn new() -> Self { Ds1 { process: Process::new("DarkSoulsRemastered.exe"), game_data_man: Pointer::default(), ai_timer: Pointer::default(), } } pub fn get_in_game_time_milliseconds(&self) -> u32 { return self.game_data_man.read_u32_rel(Some(0xa4)); } pub fn get_ai_timer(&self) -> f32 { return self.ai_timer.read_f32_rel(Some(0x24)); } pub fn refresh(&mut self) -> Result<(), String> { if !self.process.is_attached() { self.process.refresh()?; self.game_data_man = self.process.scan_rel("GameDataMan", "48 8b 05 ? ? ? ? 48 8b 50 10 48 89 54 24 60", 3, 7, vec![0])?; self.ai_timer = self.process.scan_rel("GameDataMan", "48 8b 0d ? ? ? ? 48 85 c9 74 0e 48 83 c1 28", 3, 7, vec![0])?; } else { self.process.refresh()?; } Ok(()) } } fn main() { let mut ds1 = Ds1::new(); loop { match ds1.refresh() { Ok(()) => {} Err(e) => println!("{}", e) } println!("igt: {}", ds1.get_in_game_time_milliseconds()); println!("ai: {}", ds1.get_ai_timer()); sleep(Duration::from_secs(1)); } } ``` -------------------------------- ### Initialize Process Instance Source: https://context7.com/frankvdstam/mem-rs/llms.txt Creates a new Process wrapper for a target executable. Note that this does not immediately attach to the process; a subsequent call to refresh() is required. ```rust use mem_rs::prelude::*; // Create a process wrapper for the target executable let mut process = Process::new("DarkSoulsRemastered.exe"); // Check attachment status (will be false until refresh is called) println!("Attached: {}", process.is_attached()); // false ``` -------------------------------- ### Configure Memory Access Mode Source: https://context7.com/frankvdstam/mem-rs/llms.txt Initializes a Process instance with a specific memory access strategy. Win32Api is used for external memory access, while Direct is intended for injected DLLs. ```rust use mem_rs::prelude::*; // For external tools - uses ReadProcessMemory/WriteProcessMemory let external_process = Process::new_with_memory_type("game.exe", MemoryType::Win32Api); // For injected DLLs - uses direct pointer access let injected_process = Process::new_with_memory_type("game.exe", MemoryType::Direct); ``` -------------------------------- ### Attach and Refresh Process Source: https://context7.com/frankvdstam/mem-rs/llms.txt Establishes a connection to the target process, opens necessary handles, and caches module information. This method must be called to enable memory operations. ```rust use mem_rs::prelude::*; let mut process = Process::new("game.exe"); // Attach to the process - returns error if not running match process.refresh() { Ok(()) => { println!("Successfully attached!"); println!("Process path: {}", process.get_path()); println!("Is 64-bit: {}", process.is_64_bit()); println!("Process ID: {}", process.get_id()); } Err(e) => println!("Failed to attach: {}", e) } // Check if we're attached if process.is_attached() { // Subsequent refresh calls verify process is still alive process.refresh().expect("Process exited"); } ``` -------------------------------- ### Inject DLL into Process using Rust Source: https://context7.com/frankvdstam/mem-rs/llms.txt Injects a Dynamic Link Library (DLL) into the target process using the `LoadLibraryW` technique. This involves allocating memory within the target process, writing the DLL's path to that memory, and then creating a remote thread to execute `LoadLibraryW` with the path as an argument, effectively loading the DLL. ```rust use mem_rs::prelude::*; let mut process = Process::new("game.exe"); process.refresh().expect("Process not running"); // Inject a DLL using LoadLibraryW match process.inject_dll(r#"C:\\mods\\cheat_engine.dll"#) { Ok(()) => println!("DLL injected successfully"), Err(e) => println!("Injection failed: {}", e) } // Supports Unicode paths process.inject_dll(r#"C:\\projects\\mods\\native.dll"#) .expect("Failed to inject DLL"); ``` -------------------------------- ### Access Loaded Modules and Exports in Rust Source: https://context7.com/frankvdstam/mem-rs/llms.txt Demonstrates how to access loaded modules within a process and enumerate their exported functions using the mem-rs library. This is useful for understanding a process's structure and available functionalities. ```rust use mem_rs::prelude::*; let mut process = Process::new("game.exe"); process.refresh().expect("Process not running"); // Get the main module (cached during refresh) let main_module = process.get_main_module(); println!("Module: {}", main_module.name); println!("Base address: 0x{:X}", main_module.base_address); println!("Size: {} bytes", main_module.size); println!("Path: {}", main_module.path); // Get all loaded modules let modules = process.get_modules(); for module in modules { println!(" {} at 0x{:X}", module.name, module.base_address); // Get exported functions from a module let exports = module.get_exports(); for (name, address) in exports { println!(" Export: {} -> 0x{:X}", name, address); } } ``` -------------------------------- ### Enable Pointer Debug Mode for Tracing in Rust Source: https://context7.com/frankvdstam/mem-rs/llms.txt Shows how to enable debug output for pointer resolution in mem-rs. This feature is invaluable for debugging complex pointer chains by visualizing the step-by-step resolution path. ```rust use mem_rs::prelude::*; let mut process = Process::new("game.exe"); process.refresh().expect("Process not running"); let mut pointer = process.scan_rel( "PlayerData", "48 8b 05 ? ? ? ? 48 8b 50 10", 3, 7, vec![0x10, 0x20, 0x30] ).expect("Scan failed"); // Enable debug output for pointer resolution pointer.debug = true; // This will print the full pointer path during resolution: // 0x7FF00000 // [0x7FF00000 + 0x10]: 0x12345678 // [0x12345678 + 0x20]: 0xABCDEF00 // 0xABCDEF00 + 0x30: 0xABCDEF30 let value = pointer.read_u32_rel(None); ``` -------------------------------- ### Create Pointer from Address in Rust Source: https://context7.com/frankvdstam/mem-rs/llms.txt Creates a Pointer object from a known base address and a chain of offsets. This is useful when the target address is not found via pattern scanning but is known or calculated. The pointer can then be used to read or write values at the resolved address. ```rust use mem_rs::prelude::*; let mut process = Process::new("game.exe"); process.refresh().expect("Process not running"); // Create pointer from a known base address with offset chain let network_ptr = process.create_pointer( 0x141D151B0, // Base address vec![0xc, 0x6c978] // Offsets to follow ); // The pointer will resolve: [[[0x141D151B0] + 0xc] + 0x6c978] let session_id = network_ptr.read_u32_rel(None); println!("Session ID: {}", session_id); ``` -------------------------------- ### Read Data Types using Pointer Offsets in Rust Source: https://context7.com/frankvdstam/mem-rs/llms.txt Demonstrates how to read various primitive data types (integers, floats, booleans) from memory using a Pointer object. It shows both relative reads, which follow the pointer's offset chain and can include an additional offset, and absolute reads, which access memory directly at a specified address, ignoring the pointer chain. ```rust use mem_rs::prelude::*; let mut process = Process::new("game.exe"); process.refresh().expect("Process not running"); let player_data = process.create_pointer(0x1234567, vec![0x10, 0x20]); // Relative reads - follows pointer chain, adds optional offset let health: i32 = player_data.read_i32_rel(Some(0x100)); let stamina: f32 = player_data.read_f32_rel(Some(0x104)); let level: u32 = player_data.read_u32_rel(Some(0x108)); let alive: bool = player_data.read_bool_rel(Some(0x10C)); let position_x: f64 = player_data.read_f64_rel(Some(0x110)); // Read at resolved pointer address without additional offset let base_value: u8 = player_data.read_u8_rel(None); // Absolute reads - ignores pointer chain, reads directly let absolute_value: u64 = player_data.read_u64_abs(0x7FF00000); // Available types: i8, i32, i64, u8, u32, u64, f32, f64, bool println!("Health: {}, Stamina: {:.1}, Level: {}", health, stamina, level); ``` -------------------------------- ### Write Data Types using Pointer Offsets in Rust Source: https://context7.com/frankvdstam/mem-rs/llms.txt Illustrates how to write various primitive data types to memory using a Pointer object. Similar to reading, it supports both relative writes, which modify memory along the pointer's offset chain with an optional additional offset, and absolute writes, which directly target a specified memory address. ```rust use mem_rs::prelude::*; let mut process = Process::new("game.exe"); process.refresh().expect("Process not running"); let player_data = process.create_pointer(0x1234567, vec![0x10, 0x20]); // Relative writes - follows pointer chain player_data.write_i32_rel(Some(0x100), 9999); // Set health to 9999 player_data.write_f32_rel(Some(0x104), 150.0); // Set stamina player_data.write_u32_rel(Some(0x108), 99); // Set level player_data.write_u8_rel(Some(0x10C), 1); // Set alive flag // Absolute writes - direct address player_data.write_u64_abs(0x7FF00000, 0xDEADBEEF); // Available: write_i8, write_i32, write_i64, write_u8, write_u32, // write_u64, write_f32, write_f64 (both _rel and _abs variants) ``` -------------------------------- ### Raw Memory Read/Write Operations in Rust Source: https://context7.com/frankvdstam/mem-rs/llms.txt Provides methods for low-level memory access using byte buffers. This is useful for reading or writing custom data structures or performing bulk memory operations. It supports both relative access (following pointer offsets) and absolute access (direct memory addresses) for reading and writing raw bytes. ```rust use mem_rs::prelude::*; let mut process = Process::new("game.exe"); process.refresh().expect("Process not running"); let pointer = process.create_pointer(0x1234567, vec![0x10]); // Read raw bytes relatively let mut buffer: [u8; 16] = [0; 16]; let success = pointer.read_memory_rel(Some(0x50), &mut buffer); if success { println!("Read {} bytes: {:?}", buffer.len(), buffer); } // Write raw bytes relatively let data: [u8; 4] = [0x90, 0x90, 0x90, 0x90]; // NOP sled pointer.write_memory_rel(Some(0x100), &data); // Absolute memory access let mut abs_buffer: [u8; 8] = [0; 8]; pointer.read_memory_abs(0x7FF00000, &mut abs_buffer); pointer.write_memory_abs(0x7FF00000, &[0xFF, 0xFF, 0xFF, 0xFF]); ``` -------------------------------- ### Perform Relative Pattern Scan Source: https://context7.com/frankvdstam/mem-rs/llms.txt Scans for byte patterns in x64 processes where pointers are instruction-relative. It calculates absolute addresses based on instruction offsets and follows pointer chains. ```rust use mem_rs::prelude::*; let mut process = Process::new("DarkSoulsRemastered.exe"); process.refresh().expect("Process not running"); let game_data_man = process.scan_rel( "GameDataMan", "48 8b 05 ? ? ? ? 48 8b 50 10 48 89 54 24 60", 3, 7, vec![0] ).expect("Pattern not found"); // Read data from the found pointer let in_game_time = game_data_man.read_u32_rel(Some(0xa4)); println!("In-game time: {} ms", in_game_time); ``` -------------------------------- ### Perform Absolute Pattern Scan Source: https://context7.com/frankvdstam/mem-rs/llms.txt Scans for byte patterns in x86 processes or code pointers where the memory address is stored directly. Useful for locating static addresses or 32-bit game data. ```rust use mem_rs::prelude::*; let mut process = Process::new("DarkSouls.exe"); process.refresh().expect("Process not running"); // Absolute scan - address at pattern offset is the direct pointer value let pointer = process.scan_abs( "PlayerHP", "56 8B F1 8B 46 1C 50 A1 ? ? ? ? 32 C9", 8, vec![0, 0, 0] ).expect("Pattern not found"); let health = pointer.read_i32_rel(Some(0x10)); println!("Player HP: {}", health); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.