diff --git a/src/main.rs b/src/main.rs index e7a11a9..4ac67e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,18 @@ +mod wadutils; + fn main() { - println!("Hello, world!"); + let wad_file = wadutils::load_wad("WADs/doom1.wad"); + + println!( + "WAD Path: {} + +Header: + Identifier: {} + Lump Count: {} + Initial Offset: {}", + wadutils::get_wad_path(&wad_file), + wadutils::get_wad_type(&wad_file), + wadutils::get_num_lumps(&wad_file), + wadutils::get_init_offset(&wad_file) + ); } diff --git a/src/wadutils.rs b/src/wadutils.rs new file mode 100644 index 0000000..81fdc79 --- /dev/null +++ b/src/wadutils.rs @@ -0,0 +1,23 @@ +mod wadfile; + +use wadfile::WADFile; + +pub fn load_wad(path: &str) -> WADFile { + WADFile::from_path(path) +} + +pub fn get_wad_path(wad_file: &WADFile) -> String { + wad_file.wad_path.to_string() +} + +pub fn get_wad_type(wad_file: &WADFile) -> String { + wad_file.identifier.to_string() +} + +pub fn get_num_lumps(wad_file: &WADFile) -> u32 { + wad_file.num_lumps +} + +pub fn get_init_offset(wad_file: &WADFile) -> u32 { + wad_file.init_offset +} diff --git a/src/wadutils/wadfile.rs b/src/wadutils/wadfile.rs new file mode 100644 index 0000000..a9a796b --- /dev/null +++ b/src/wadutils/wadfile.rs @@ -0,0 +1,45 @@ +use std::{fs::File, io::Read}; + +pub struct WADFile { + pub(super) wad_path: String, + pub(super) identifier: String, + pub(super) num_lumps: u32, + pub(super) init_offset: u32, +} + +impl WADFile { + pub fn from_path(path: &str) -> Self { + let header_data = get_header_data(path); + + Self { + wad_path: path.to_string(), + identifier: header_data.0, + num_lumps: header_data.1, + init_offset: header_data.2, + } + } +} + +fn get_header_data(path: &str) -> (String, u32, u32) { + let mut wad_data = File::open(&path).unwrap(); + + let mut header = [0; 12]; + + wad_data.read(&mut header).unwrap(); + + let mut wad_type = String::with_capacity(4); + + wad_type.push(header[0] as char); + wad_type.push(header[1] as char); + wad_type.push(header[2] as char); + wad_type.push(header[3] as char); + + let lump_bytes: [u8; 4] = [header[4], header[5], header[6], header[7]]; + let offset_bytes: [u8; 4] = [header[8], header[9], header[10], header[11]]; + + ( + wad_type, + u32::from_le_bytes(lump_bytes), + u32::from_le_bytes(offset_bytes), + ) +}