agb/agb-gbafix/src/main.rs

62 lines
1.7 KiB
Rust
Raw Normal View History

2023-04-07 06:09:44 +10:00
use std::{
fs,
2023-04-07 06:54:26 +10:00
io::{BufWriter, Write},
2023-04-07 06:09:44 +10:00
path::PathBuf,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let path = PathBuf::from("tests/text_render");
let file_data = fs::read(path)?;
let file_data = file_data.as_slice();
let elf_file = elf::ElfBytes::<elf::endian::AnyEndian>::minimal_parse(file_data)?;
2023-04-07 06:54:26 +10:00
let section_headers = elf_file
.section_headers()
.expect("Expected section headers");
2023-04-07 06:09:44 +10:00
2023-04-07 06:54:26 +10:00
let mut output = BufWriter::new(fs::File::create("out.gba")?);
2023-04-07 06:09:44 +10:00
2023-04-07 06:54:26 +10:00
let mut header = gbafix::GBAHeader::default();
2023-04-07 06:09:44 +10:00
2023-04-07 06:54:26 +10:00
let mut written_header = false;
for section_header in section_headers.iter() {
2023-04-07 06:09:44 +10:00
const SHT_NOBITS: u32 = 8;
const SHT_NULL: u32 = 0;
const SHF_ALLOC: u64 = 2;
if (section_header.sh_type == SHT_NOBITS || section_header.sh_type == SHT_NULL)
|| section_header.sh_flags & SHF_ALLOC == 0
{
continue;
}
2023-04-07 06:54:26 +10:00
let (mut data, compression) = elf_file.section_data(&section_header)?;
2023-04-07 06:09:44 +10:00
if let Some(compression) = compression {
2023-04-07 06:10:03 +10:00
panic!("Cannot decompress elf content, but got compression header {compression:?}");
2023-04-07 06:09:44 +10:00
}
2023-04-07 06:54:26 +10:00
if !written_header {
assert!(
data.len() > 192,
"first section must be at least as big as the gba header"
);
header.start_code = data[0..4].try_into().unwrap();
header.update_checksum();
let header_bytes = bytemuck::bytes_of(&header);
output.write_all(header_bytes)?;
data = &data[192..];
written_header = true;
}
output.write_all(data)?;
2023-04-07 06:09:44 +10:00
}
2023-04-07 06:54:26 +10:00
output.flush()?;
2023-04-07 06:09:44 +10:00
Ok(())
}