Added ability to read WAD directory

This commit is contained in:
2025-03-27 17:43:52 -04:00
parent b2028e0207
commit 98cfdb93a6
15 changed files with 208 additions and 0 deletions

54
src/wad/wadfile.rs Normal file
View File

@@ -0,0 +1,54 @@
use std::{fs::File, io::Read, path::Path};
use super::{directory::Directory, header::Header};
use crate::utils::{read_ascii, read_u32_le, validate_wad};
pub struct WADFile {
pub path: String,
pub wad_id: String,
pub num_lumps: u32,
pub dir_offset: u32,
pub directory: Vec<Directory>,
pub wad_data: Vec<u8>,
}
impl WADFile {
pub fn new(path: String) -> Self {
if !(validate_wad(path.as_str()).unwrap()) {
panic!();
}
let file_path = Path::new(path.as_str());
let mut wad_file = File::open(file_path).unwrap();
let file_size = wad_file.metadata().unwrap().len() as usize;
let mut file_buffer: Vec<u8> = Vec::with_capacity(file_size);
wad_file.read_to_end(&mut file_buffer).unwrap();
let wad_header = Header::read_data(&file_buffer[0..12]);
let mut wad_dir: Vec<Directory> = Vec::with_capacity(wad_header.num_lumps as usize);
let offset = wad_header.dir_offset;
for entry in 0..wad_header.num_lumps {
let startpos = (offset + 16 * entry) as usize;
let filepos = read_u32_le(&file_buffer[startpos..startpos + 4]);
let size = read_u32_le(&file_buffer[startpos + 4..startpos + 8]);
let name = read_ascii(&file_buffer[startpos + 8..startpos + 16]);
wad_dir.push(Directory {
filepos,
name,
size,
});
}
Self {
path,
wad_id: wad_header.wad_id,
num_lumps: wad_header.num_lumps,
dir_offset: wad_header.dir_offset,
directory: wad_dir,
wad_data: file_buffer,
}
}
}