Merge pull request 'Directory Loading' (#4) from dir-loader into master

Reviewed-on: https://git.batesirvintech.net/wesley/doom-oxidized/pulls/4
This commit is contained in:
Wesley Irvin
2023-04-26 19:50:31 -04:00
4 changed files with 99 additions and 3 deletions

View File

@@ -9,10 +9,29 @@ fn main() {
Header:
Identifier: {}
Lump Count: {}
Initial Offset: {}",
Initial Offset: {}
Number of Found Lumps: {}",
wadutils::get_wad_path(&wad_file),
wadutils::get_wad_type(&wad_file),
wadutils::get_num_lumps(&wad_file),
wadutils::get_init_offset(&wad_file)
wadutils::get_init_offset(&wad_file),
wadutils::get_directory_len(&wad_file)
);
println!("First 15 Directory Entries:");
for i in 0..15 {
println!(
"\t{}\tName {}\tPosition {}\tSize {}",
i,
wadutils::get_lump_name(&wad_file, i),
wadutils::get_lump_offset(&wad_file, i),
wadutils::get_lump_size(&wad_file, i)
);
}
println!(
"Index of e1m1 is: {}",
wadutils::get_index_from_name(&wad_file, "e1m1").unwrap()
);
}

View File

@@ -21,3 +21,35 @@ pub fn get_num_lumps(wad_file: &WADFile) -> u32 {
pub fn get_init_offset(wad_file: &WADFile) -> u32 {
wad_file.init_offset
}
pub fn get_lump_name(wad_file: &WADFile, index: u32) -> String {
wad_file.lump_directory[index as usize].lump_name.to_owned()
}
pub fn get_lump_offset(wad_file: &WADFile, index: u32) -> u32 {
wad_file.lump_directory[index as usize].lump_pos.to_owned()
}
pub fn get_lump_size(wad_file: &WADFile, index: u32) -> u32 {
wad_file.lump_directory[index as usize].lump_size.to_owned()
}
pub fn get_directory_len(wad_file: &WADFile) -> usize {
wad_file.lump_directory.len()
}
pub fn get_index_from_name(wad_file: &WADFile, lump_name: &str) -> Option<u32> {
let mut search_lump = lump_name.to_ascii_uppercase();
for _ in 0..8 - search_lump.len() {
search_lump.push(0 as char);
}
for i in 0..wad_file.num_lumps {
if wad_file.lump_directory[i as usize].lump_name == search_lump {
return Some(i);
}
}
None
}

View File

@@ -1,25 +1,65 @@
use std::{fs::File, io::Read};
pub mod directory;
use directory::Directory;
use std::{
fs::File,
io::{Read, Seek, SeekFrom},
};
pub struct WADFile {
pub(super) wad_path: String,
pub(super) identifier: String,
pub(super) num_lumps: u32,
pub(super) init_offset: u32,
pub(super) lump_directory: Vec<Directory>,
}
impl WADFile {
pub fn from_path(path: &str) -> Self {
let header_data = get_header_data(path);
let mut lump_dir: Vec<Directory> = Vec::with_capacity(header_data.1 as usize);
get_lump_dir(path, &header_data.2, &header_data.1, &mut lump_dir);
Self {
wad_path: path.to_string(),
identifier: header_data.0,
num_lumps: header_data.1,
init_offset: header_data.2,
lump_directory: lump_dir,
}
}
}
fn get_lump_dir(path: &str, dir_offset: &u32, num_lumps: &u32, lump_buffer: &mut Vec<Directory>) {
let mut wad_data = File::open(&path).unwrap();
wad_data
.seek(SeekFrom::Start(dir_offset.to_owned() as u64))
.unwrap();
for _ in 0..num_lumps.to_owned() {
let mut lump_pos = [0; 4];
let mut lump_size = [0; 4];
let mut lump_name = [0; 8];
let mut name = String::with_capacity(8);
wad_data.read(&mut lump_pos).unwrap();
wad_data.read(&mut lump_size).unwrap();
wad_data.read(&mut lump_name).unwrap();
for letter in 0..lump_name.len() {
name.push(lump_name[letter] as char);
}
lump_buffer.push(Directory {
lump_pos: u32::from_le_bytes(lump_pos),
lump_size: u32::from_le_bytes(lump_size),
lump_name: name,
})
}
}
fn get_header_data(path: &str) -> (String, u32, u32) {
let mut wad_data = File::open(&path).unwrap();

View File

@@ -0,0 +1,5 @@
pub(in crate::wadutils) struct Directory {
pub(in crate::wadutils) lump_pos: u32,
pub(in crate::wadutils) lump_size: u32,
pub(in crate::wadutils) lump_name: String,
}