From 476f01fad3afe236426eabc4f8dd1ff1f7159340 Mon Sep 17 00:00:00 2001 From: Lyle Mantooth Date: Mon, 23 May 2022 09:46:44 -0400 Subject: [PATCH] Fix bin_comp and factor out Patch struct to base lib. --- bin_comp/Cargo.toml | 2 +- bin_comp/src/main.rs | 73 ++++++++++++++++++++++++-------------------- enemize/Cargo.toml | 2 +- enemize/src/lib.rs | 42 +++++++++++++++++++++++++ enemize/src/rom.rs | 7 +++++ 5 files changed, 91 insertions(+), 35 deletions(-) create mode 100644 enemize/src/lib.rs create mode 100644 enemize/src/rom.rs diff --git a/bin_comp/Cargo.toml b/bin_comp/Cargo.toml index a8bc1ba..5386d6e 100644 --- a/bin_comp/Cargo.toml +++ b/bin_comp/Cargo.toml @@ -7,6 +7,6 @@ edition = "2021" [dependencies] anyhow = "1.0" -serde = { version = "1.0", features = ["derive"] } +enemize = { path = "../enemize" } serde_json = "1.0" tempfile = "3" diff --git a/bin_comp/src/main.rs b/bin_comp/src/main.rs index f52c93a..80ffd30 100644 --- a/bin_comp/src/main.rs +++ b/bin_comp/src/main.rs @@ -1,25 +1,15 @@ use std::env::args; -use std::io::{self, Read, Write}; +use std::ffi::OsString; +use std::fs::File; +use std::io::{Read, Seek, Write}; use std::iter::zip; +use std::path::Path; use std::process::Command; +use std::str::FromStr; -use serde::Serialize; -use tempfile::NamedTempfile; +use tempfile::NamedTempFile; -#[derive(Serialize)] -struct Patch { - pub address: usize, - pub patch_data: Vec, -} - -impl Patch { - fn new() -> Patch { - Patch { - address: 0, - patch_data: vec![], - } - } -} +use enemize::Patch; fn main() -> Result<(), anyhow::Error> { println!("BinComp ---- "); @@ -32,43 +22,55 @@ fn main() -> Result<(), anyhow::Error> { let mut zeroes_patch = vec![]; - let (mut zeroes_file, zeroes_path) = NamedTempfile::new()?; - let (mut ones_file, ones_path) NamedTempfile::new()?; + let mut zeroes_file = NamedTempFile::new()?; + let mut ones_file = NamedTempFile::new()?; - println!("temp file (zeros): {}", zeroes_file.path()); - println!("temp file (0xFF): {}", ones_file.path()); + println!( + "temp file (zeros): {}", + zeroes_file.path().to_string_lossy() + ); + println!("temp file (0xFF): {}", ones_file.path().to_string_lossy()); - run_asar(&asm_fn, zeroes_file.path(), &symbols); + run_asar(asm_fn.as_ref(), zeroes_file.path(), symbols_fn.as_ref()); println!("Reading all 0x00 file."); - zeroes_file.read(&mut zeroes_patch)?; + zeroes_file.read_to_end(&mut zeroes_patch)?; println!("Read all 0x00 file."); println!("Generating all 0xFF file."); - let mut all_ones = vec![0xff; zeroes_patch.len()]; + let all_ones = vec![0xff; zeroes_patch.len()]; ones_file.write_all(&all_ones)?; ones_file.flush()?; println!("Generated all 0xFF file."); - run_asar(&asm_fn, ones_file.path(), &symbols); + run_asar(asm_fn.as_ref(), ones_file.path(), symbols_fn.as_ref()); println!("Reading all 0xFF patched file."); let mut ones_patch = vec![]; - ones_file.rewind()?.read_to_end(&mut ones_patch)?; + ones_file.rewind()?; + ones_file.read_to_end(&mut ones_patch)?; println!("Read all 0xFF patched file."); - assert!(zeroes_patch.len() == ones_patch.len(), "File lengths don't match! Aborting."); + assert!( + zeroes_patch.len() == ones_patch.len(), + "File lengths don't match (0: {}, 1: {})! Aborting.", + zeroes_patch.len(), + ones_patch.len() + ); println!(""); - let list = zip(zeroes_patch, ones_patch).enumerate().fold((true, Vec::new()), |(start, list), &(i, (zero, ff))| { + let (_, list) = zip(zeroes_patch, ones_patch).enumerate().fold((true, Vec::new()), |(start, mut list), (i, (zero, ff))| { if zero == ff { if start { println!("New patch data found at address {:X}", i); let patch = Patch { address: i, patch_data: vec![] }; list.push(patch); } - list.last_mut().and_then(|p| p.patch_data.push(zero)); + list.last_mut().and_then(|p| { + p.patch_data.push(zero); + Some(p) + }); (false, list) } else { @@ -79,21 +81,26 @@ fn main() -> Result<(), anyhow::Error> { }); println!("Writing patch data file."); - let mut bp_file = File::create(&base_patch)?; + let bp_file = File::create(&base_patch)?; serde_json::to_writer(bp_file, &list)?; println!("Cleaning up temp files."); println!("BinComp finished ----"); + + Ok(()) } -fn run_asar(asm: &AsRef, rom: &AsRef, symbols: &AsRef) { - println!("---- Running asar --no-title-check --fix-checksum=off --symbols=wla --symbols-path={} {} {} ----", symbols, asm, rom); +fn run_asar(asm: &Path, rom: &Path, symbols: &Path) { + println!("---- Running asar --no-title-check --fix-checksum=off --symbols=wla --symbols-path={} {} {} ----", symbols.to_string_lossy(), asm.to_string_lossy(), rom.to_string_lossy()); + + let mut symbols_path_arg = OsString::from_str("--symbols-path=").unwrap(); + symbols_path_arg.push(symbols); let status = Command::new("asar") .arg("--no-title-check") .arg("--fix-checksum=off") .arg("--symbols=wla") - .arg(format!("--symbols-path={}", symbols)) + .arg(symbols_path_arg) .arg(asm) .arg(rom) .status() diff --git a/enemize/Cargo.toml b/enemize/Cargo.toml index 311d346..c59205e 100644 --- a/enemize/Cargo.toml +++ b/enemize/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -md5 = "0.7.0" +serde = { version = 1.0, features = ["derive"] } diff --git a/enemize/src/lib.rs b/enemize/src/lib.rs new file mode 100644 index 0000000..d2ba5b0 --- /dev/null +++ b/enemize/src/lib.rs @@ -0,0 +1,42 @@ +use std::io::Read; +use std::path::{Path, PathBuf}; + +use serde::Serialize; + +pub mod rom; + +#[derive(Serialize)] +pub struct Patch { + pub address: usize, + pub patch_data: Vec, +} + +#[derive(Serialize)] +pub struct PatchSet { + filename: PathBuf, + patches: Vec +} + +impl PatchSet { + pub fn load(filename: Path) -> Result { + let patches = { + let mut file = File::open(filename)?; + let mut buffer = std::io::BufReader::new(file); + serde_json::from_reader(buffer)? + }; + PatchSet { + filename: filename.into(), + patches: patches + } + } + + pub fn filename(&self) -> Path { + self.filename.as_path() + } + + pub fn patchRom(&self, &mut rom: RomData) { + for patch in self.patches { + rom.patch_data(patch); + } + } +} diff --git a/enemize/src/rom.rs b/enemize/src/rom.rs new file mode 100644 index 0000000..11a9874 --- /dev/null +++ b/enemize/src/rom.rs @@ -0,0 +1,7 @@ +const ENEMIZER_INFO_SEED_OFFSET: usize = 0; +const ENEMIZER_INFO_SEED_LENGTH: usize = 12; +const ENEMIZER_INFO_VERSION_OFFSET: usize = ENEMIZER_INFO_SEED_OFFSET + ENEMIZER_INFO_SEED_LENGTH; +const ENEMIZER_INFO_VERSION_LENGTH: usize = 8; +const ENEMIZER_INFO_FLAGS_OFFSET: usize = ENEMIZER_INFO_VERSION_OFFSET + ENEMIZER_INFO_VERSION_LENGTH; +const ENEMIZER_INFO_FLAGS_LENGTH: usize = 0x50; +