use tempfile::NamedTempFile; use std::fs::File; use std::io::{self, Read, Seek, SeekFrom, Write}; use std::{env, fs}; // Offset within the bank file to the version number within the BKHD section. // TODO: Check if this may change. const OFFSET_VERSION_NUMBER: u64 = 52; // Retrieved by philipdestroyer by inspecting the game's sound banks in memory at runtime. const BANK_VERSION: [u8; 4] = [0x28, 0xbc, 0x11, 0x92]; fn main() { let path = env::args().nth(1).expect("Path to sound bank required"); let tmp_path = { let mut f = File::open(&path).expect("failed to open sound bank file"); let mut tmp = NamedTempFile::new().expect("failed to open temporary file"); // Copy everything until the version number io::copy( &mut std::io::Read::by_ref(&mut f).take(OFFSET_VERSION_NUMBER), &mut tmp, ) .expect("failed to copy to tempfile"); // Write the hard coded version number tmp.write(&BANK_VERSION).expect("failed to write version number"); // Copy the rest of the file f.seek(SeekFrom::Start(OFFSET_VERSION_NUMBER + 4)) .expect("failed to skip to rest of content"); io::copy(&mut f, &mut tmp).expect("failed to copy to tempfile"); tmp.into_temp_path() }; if let Err(err) = fs::rename(&tmp_path, &path) { let tmp_path = tmp_path.keep().expect("failed to persist temporary file"); eprintln!( "failed to rename temporary file to override the sound bank {}.\n\ Please manually copy the file from {} to {}", err, tmp_path.display(), path ); } }