Implement the padding flag

This commit is contained in:
Gwilym Inzani 2023-07-26 10:21:54 +01:00
parent 9eee5a03f2
commit d9fc333644
2 changed files with 24 additions and 5 deletions

View file

@ -60,7 +60,19 @@ impl GbaHeader {
} }
} }
pub fn write_gba_file<W: Write>(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<W: Write>(
input: &[u8],
mut header: GbaHeader,
padding_behaviour: PaddingBehaviour,
output: &mut W,
) -> Result<()> {
let elf_file = elf::ElfBytes::<elf::endian::AnyEndian>::minimal_parse(input)?; let elf_file = elf::ElfBytes::<elf::endian::AnyEndian>::minimal_parse(input)?;
let section_headers = elf_file let section_headers = elf_file
@ -111,7 +123,7 @@ pub fn write_gba_file<W: Write>(input: &[u8], mut header: GbaHeader, output: &mu
bytes_written += data.len() as u64; 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; let required_padding = bytes_written.next_power_of_two() - bytes_written;
for _ in 0..required_padding { for _ in 0..required_padding {

View file

@ -7,7 +7,7 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
use agb_gbafix::{write_gba_file, GbaHeader}; use agb_gbafix::{write_gba_file, GbaHeader, PaddingBehaviour};
fn main() -> Result<()> { fn main() -> Result<()> {
let matches = clap::Command::new("agb-gbafix") let matches = clap::Command::new("agb-gbafix")
@ -18,7 +18,7 @@ fn main() -> Result<()> {
.arg(arg!(-c --gamecode <GAME_CODE> "Sets the game code, 4 bytes")) .arg(arg!(-c --gamecode <GAME_CODE> "Sets the game code, 4 bytes"))
.arg(arg!(-m --makercode <MAKER_CODE> "Set the maker code, 2 bytes")) .arg(arg!(-m --makercode <MAKER_CODE> "Set the maker code, 2 bytes"))
.arg(arg!(-r --gameversion <VERSION> "Set the version of the game, 0-255").value_parser(value_parser!(u8))) .arg(arg!(-r --gameversion <VERSION> "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(); .get_matches();
let input = matches.get_one::<PathBuf>("INPUT").unwrap(); let input = matches.get_one::<PathBuf>("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 mut output = BufWriter::new(fs::File::create(output)?);
let file_data = fs::read(input)?; 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()?; output.flush()?;