Skip to content

Commit

Permalink
Feature/update windows crate (#2)
Browse files Browse the repository at this point in the history
* Upgrade win crate to latest version
* fix warnings
* Replace K32GetModuleFileNameExA with K32GetModuleFileNameExW. Abstract windows widechar to string conversion.
* Split process into files. Remove all ASCII win32 functions and replace them with widechar version
* Bump version
  • Loading branch information
FrankvdStam authored Apr 27, 2024
1 parent cc0e01d commit 1a7f65d
Show file tree
Hide file tree
Showing 14 changed files with 499 additions and 407 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

[package]
name = "mem-rs"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
readme = "README.md"
homepage = "https://github.com/FrankvdStam/mem-rs"
Expand All @@ -30,7 +30,7 @@ description = "pattern scanning and abstraction for pointers in memory of runnin
[dependencies]

[dependencies.windows]
version = "0.42.0"
version = "0.56.0"
features = [
"Win32_Foundation",
"Win32_System_Memory",
Expand Down
43 changes: 28 additions & 15 deletions examples/dsr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use std::thread::sleep;
use std::time::Duration;
use mem_rs::helpers::{get_w32str_from_str, vec_u16_to_u8};
use mem_rs::prelude::*;

struct Ds1
Expand Down Expand Up @@ -61,15 +62,34 @@ impl Ds1
}
Ok(())
}

#[allow(dead_code)]
pub fn inject_soulmemory_rs(&self)
{
//self.process.inject_dll(r#"C:\soulmemory\soulmemory_rs.dll"#);
//self.process.inject_dll(r#"C:\projects\soulmemory-rs\target\x86_64-pc-windows-msvc\release\soulmemory_rs.dll"#);
self.process.inject_dll(r#"C:\projects\soulmemory-rs\target\x86_64-pc-windows-msvc\å\soulmemory_rs.dll"#).expect("TODO: panic message");
}
}

fn main()
{
//let processes = Process::get_running_process_names();
//for p in &processes
//{
// println!("{}", p);
//}
let str = r#"C:\soulmemory\soulmemory_rs.dll"#;
let w32_str = get_w32str_from_str(str);
println!("{:?}", w32_str);
println!("{:?}", vec_u16_to_u8(&w32_str));

let alloated_str = String::from(str);
let collected: Vec<u16> = alloated_str.encode_utf16().collect();
println!("{:?}", collected);
unsafe { println!("{:?}", collected.align_to::<u8>()); }


let processes = Process::get_running_process_names();
for p in &processes
{
println!("{}", p);
}

let mut ds1 = Ds1::new();

Expand All @@ -81,17 +101,10 @@ fn main()
Err(e) => println!("{}", e)
}

//ds1.inject_soulmemory_rs();

println!("igt: {}", ds1.get_in_game_time_milliseconds());
println!("ai: {}", ds1.get_ai_timer());
sleep(Duration::from_secs(1));
}
}

#[allow(dead_code)]
fn inject()
{
let mut process = Process::new("DarkSoulsRemastered.exe");
process.refresh().unwrap();
process.inject_dll(r#"C:\projects\soulmemory-rs\target\x86_64-pc-windows-msvc\debug\soulmemory_rs.dll"#);
}

}
33 changes: 33 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::path::Path;
use windows::core::{PCSTR, PCWSTR};

pub fn scan(code: &[u8], pattern: &[Option<u8>]) -> Option<usize>
{
if code.len() == 0
Expand Down Expand Up @@ -58,4 +61,34 @@ pub fn to_pattern(str: &str) -> Vec<Option<u8>>
}
}
return vec;
}

pub fn vec_u16_to_u8(vec_u16: &Vec<u16>) -> Vec<u8>
{
return unsafe { vec_u16.align_to::<u8>().1.to_vec() };
}

pub fn w32str_to_string(w32str: &Vec<u16>) -> String
{
return w32str.iter().map(|&v| (v & 0xFF) as u8).take_while(|&c| c != 0).map(|c| c as char).collect();
}

pub fn get_file_name_from_string(str: &String) -> String
{
return String::from(Path::new(&str).file_name().unwrap().to_str().unwrap());
}

pub fn get_w32str_from_str(str: &str) -> Vec<u16>
{
return str.encode_utf16().collect();
}
pub fn get_pcwstr_from_str(str: &str) -> PCWSTR
{
let vec: Vec<u16> = str.encode_utf16().collect();
return PCWSTR(vec.as_ptr());
}

pub fn get_pcstr_from_str(str: &str) -> PCSTR
{
return PCSTR(str.as_ptr());
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

mod process_data;
mod process_module;
mod helpers;
pub mod helpers;
pub mod read_write;
pub mod process;
pub mod pointer;
Expand Down
Loading

0 comments on commit 1a7f65d

Please sign in to comment.