From d9fc333644d825020903b3e64b79de7a5278f151 Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Wed, 26 Jul 2023 10:21:54 +0100 Subject: [PATCH] Implement the padding flag --- agb-gbafix/src/lib.rs | 16 ++++++++++++++-- agb-gbafix/src/main.rs | 13 ++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/agb-gbafix/src/lib.rs b/agb-gbafix/src/lib.rs index 7d65b6d2..36b0389f 100644 --- a/agb-gbafix/src/lib.rs +++ b/agb-gbafix/src/lib.rs @@ -60,7 +60,19 @@ impl GbaHeader { } } -pub fn write_gba_file(input: &[u8], mut header: GbaHeader, output: &mut W) -> Result<()> { +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub enum PaddingBehaviour { + Pad, + #[default] + DoNotPad, +} + +pub fn write_gba_file( + input: &[u8], + mut header: GbaHeader, + padding_behaviour: PaddingBehaviour, + output: &mut W, +) -> Result<()> { let elf_file = elf::ElfBytes::::minimal_parse(input)?; let section_headers = elf_file @@ -111,7 +123,7 @@ pub fn write_gba_file(input: &[u8], mut header: GbaHeader, output: &mu bytes_written += data.len() as u64; } - if !bytes_written.is_power_of_two() { + if !bytes_written.is_power_of_two() && padding_behaviour == PaddingBehaviour::Pad { let required_padding = bytes_written.next_power_of_two() - bytes_written; for _ in 0..required_padding { diff --git a/agb-gbafix/src/main.rs b/agb-gbafix/src/main.rs index 94a7c697..3ae0f25c 100644 --- a/agb-gbafix/src/main.rs +++ b/agb-gbafix/src/main.rs @@ -7,7 +7,7 @@ use std::{ path::PathBuf, }; -use agb_gbafix::{write_gba_file, GbaHeader}; +use agb_gbafix::{write_gba_file, GbaHeader, PaddingBehaviour}; fn main() -> Result<()> { let matches = clap::Command::new("agb-gbafix") @@ -18,7 +18,7 @@ fn main() -> Result<()> { .arg(arg!(-c --gamecode "Sets the game code, 4 bytes")) .arg(arg!(-m --makercode "Set the maker code, 2 bytes")) .arg(arg!(-r --gameversion "Set the version of the game, 0-255").value_parser(value_parser!(u8))) - .arg(arg!(-p --padding "Ignored for compatibility with gbafix")) + .arg(arg!(-p --padding "Pad the ROM to the next power of 2 in size")) .get_matches(); let input = matches.get_one::("INPUT").unwrap(); @@ -70,10 +70,17 @@ fn main() -> Result<()> { } } + let pad = matches.get_flag("padding"); + let pad = if pad { + PaddingBehaviour::Pad + } else { + PaddingBehaviour::DoNotPad + }; + let mut output = BufWriter::new(fs::File::create(output)?); let file_data = fs::read(input)?; - write_gba_file(file_data.as_slice(), header, &mut output)?; + write_gba_file(file_data.as_slice(), header, pad, &mut output)?; output.flush()?;