Fix bin_comp and factor out Patch struct to base lib.

This commit is contained in:
Lyle Mantooth 2022-05-23 09:46:44 -04:00
parent ca1c347386
commit 476f01fad3
Signed by: IslandUsurper
GPG key ID: 6DB52EAE123A5789
5 changed files with 91 additions and 35 deletions

View file

@ -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"

View file

@ -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<u8>,
}
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<PathBuf>, rom: &AsRef<PathBuf>, symbols: &AsRef<PathBuf>) {
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()

View file

@ -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"] }

42
enemize/src/lib.rs Normal file
View file

@ -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<u8>,
}
#[derive(Serialize)]
pub struct PatchSet {
filename: PathBuf,
patches: Vec<Patch>
}
impl PatchSet {
pub fn load(filename: Path) -> Result<PatchSet, anyhow::Error> {
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);
}
}
}

7
enemize/src/rom.rs Normal file
View file

@ -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;