diff --git a/src/main.rs b/src/main.rs index 48e41d9..340c957 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,14 @@ -extern crate clap; - use std::fs::File; -use std::io::{BufReader, Error, Read}; - -//use std::path::Path; +use std::io::{Error, Read}; +extern crate clap; use clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; +mod parsers { + pub mod bhd; +} +use parsers::*; + fn main() { let matches = App::new("Rusted Iron Ring") .setting(AppSettings::ArgRequiredElseHelp) @@ -34,11 +36,10 @@ fn cmd_bhd(args: &ArgMatches) -> Result::<(), Error> { println!("File: {:?}", filepath); println!("Output: {:?}", outputpath); - let bhd_data: File = File::open(filepath)?; - let mut bhd_reader = BufReader::new(bhd_data); - let mut magic: [u8; 4] = [0; 4]; - bhd_reader.read(&mut magic)?; + let mut bhd_file: File = File::open(filepath)?; + let mut bhd_data = vec![0u8; bhd_file.metadata()?.len() as usize]; + bhd_file.read_to_end(&mut bhd_data)?; - println!("First byte: 0x{:X}", magic[0]); + bhd::parse(&bhd_data); Ok(()) } diff --git a/src/parsers/bhd.rs b/src/parsers/bhd.rs index 5ff94b2..ff5569b 100644 --- a/src/parsers/bhd.rs +++ b/src/parsers/bhd.rs @@ -1,13 +1,23 @@ extern crate nom; +use nom::{IResult, named, verify}; +use nom::number::complete::*; #[derive(Debug)] pub struct BhdHeader { - pub magic: u8[4]; - pub unk04: i8; - pub unk05: i8; - pub unk06: i8; - pub unk07: i8; - pub unk08: u32; - pub num_buckets: u32; - pub ofs_buckets: u32; + pub magic: u32, +// pub unk04: i8, +// pub unk05: i8, +// pub unk06: i8, +// pub unk07: i8, +// pub unk08: u32, +// pub num_buckets: u32, +// pub ofs_buckets: u32, +} + +const MAGIC: u32 = 0x35444842; +named!(check_magic<&[u8], u32>, verify!(le_u32, |m| *m == MAGIC)); + +pub fn parse(i: &[u8]) -> IResult<&[u8], BhdHeader> { + let (i, magic) = check_magic(i)?; + Ok((i, BhdHeader { magic })) }