Skip to content

Commit

Permalink
feat: add patcher
Browse files Browse the repository at this point in the history
  • Loading branch information
7ap committed Nov 19, 2023
1 parent 4611fe3 commit e8f7fae
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ version = "0.0.0"
edition = "2021"
publish = false

[profile.release]
strip = true
opt-level = "z"
lto = true
codegen-units = 1

[dependencies]
clap = { version = "4.4", features = ["derive"] }
winreg = "0.51"
56 changes: 55 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
use std::fs;
use std::path::PathBuf;
use std::time::Instant;

use clap::Parser;
use winreg::{enums::HKEY_CLASSES_ROOT, RegKey};

const SIGNATURE: &[u8] = &[
0x41, 0x80, 0xbe, 0x50, 0x01, 0x00, 0x00, 0x00, 0x74, 0x05, 0xe8,
];

const PATCH: &[u8] = &[
0x41, 0x80, 0xbe, 0x50, 0x01, 0x00, 0x00, 0x00, 0x90, 0x90, 0xe8,
];

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
input: Option<PathBuf>,
output: Option<PathBuf>,
}

fn patch(input: &PathBuf, output: &PathBuf) {
let mut binary = fs::read(input).expect("Could not read input file.");

let offset = binary
.windows(SIGNATURE.len())
.position(|window| window == SIGNATURE)
.expect("Could not find signature.");

binary[offset..offset + PATCH.len()].copy_from_slice(PATCH);
fs::write(output, binary).expect("Could not write output file.");
}

fn main() {
println!("Hello, world!");
let Cli { input, output } = Cli::parse();

#[rustfmt::skip]
let input = input.unwrap_or_else(|| {
let path: String = RegKey::predef(HKEY_CLASSES_ROOT)
.open_subkey("roblox-studio").unwrap()
.open_subkey("DefaultIcon").unwrap()
.get_value("").unwrap();

PathBuf::from(path)
});

let output = if cfg!(debug_assertions) {
output.unwrap_or_else(|| input.with_file_name("RobloxStudioBeta_INTERNAL.exe"))
} else {
output.unwrap_or_else(|| input.clone()) // TODO: Is convenience really worth it?
};

let now = Instant::now();
patch(&input, &output);
println!("Patched in {:?}.", now.elapsed());
}

0 comments on commit e8f7fae

Please sign in to comment.