diff --git a/Cargo.lock b/Cargo.lock index 77fdf28..5de52f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,16 @@ version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc" +[[package]] +name = "base_patch_generator" +version = "0.1.0" +dependencies = [ + "anyhow", + "enemize", + "md5", + "serde_json", +] + [[package]] name = "bin_comp" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 16d8175..636360e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,4 +3,5 @@ members = [ "enemize", "bin_comp", + "base_patch_generator", ] diff --git a/base_patch_generator/Cargo.toml b/base_patch_generator/Cargo.toml new file mode 100644 index 0000000..746828c --- /dev/null +++ b/base_patch_generator/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "base_patch_generator" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0" +enemize = { path = "../enemize" } +md5 = "0.7.0" +serde_json = "1.0" diff --git a/base_patch_generator/src/main.rs b/base_patch_generator/src/main.rs new file mode 100644 index 0000000..4abf411 --- /dev/null +++ b/base_patch_generator/src/main.rs @@ -0,0 +1,56 @@ +use std::env; +use std::fs::File; +use std::io::Read; +use std::path::Path; + +use enemize::PatchSet; +use enemize::asar; +use enemize::rom::RomData; + +fn main() -> Result<(), anyhow::Error> { + let mut args = env::args(); + + let output_path = args.next_back().expect("No output file"); + let symbols_path = args.next_back().expect("No symbols file"); + let patch_path = args.next_back().expect("No patch file"); + let rom_path = args.next_back().expect("No ROM file"); + + let mut rom_bytes = vec![]; + let mut rom_file = File::open(&rom_path)?; + rom_file.read_to_end(&mut rom_bytes)?; + + rom_bytes = if rom_bytes.len() % 1024 == 512 { + rom_bytes.into_iter().skip(512).collect() + } else { + rom_bytes + }; + + let expected = [ + 0x03, 0xa6, 0x39, 0x45, 0x39, 0x81, 0x91, 0x33, 0x7e, 0x89, 0x6e, 0x57, 0x71, 0xf7, 0x71, + 0x73, + ]; + let actual: [u8; 16] = md5::compute(&rom_bytes).into(); + assert_eq!(expected, actual, "Invalid rom file"); + + println!("Applying Patch to rom"); + + rom_bytes.resize(4 * 1024 * 1024, 0); + + let symbols = asar::load_symbols(Path::new(&symbols_path))?; + + let mut rom = RomData::new(symbols, rom_bytes); + + let mut patches = PatchSet::load(Path::new(&patch_path))?; + patches.patch_rom(&mut rom); + + rom.move_room_headers(); + + let rom_patches = rom.generate_patch(); + patches.add_patches(rom_patches); + + println!("Writing output file {}", output_path); + let out_file = File::create(&output_path)?; + serde_json::to_writer(out_file, &patches)?; + + Ok(()) +}