Error Rework / Code Refactor

Added in being able to return an error when the path to a WAD is
not a valid path. This patch is to address issue #6.
This commit is contained in:
Wesley Irvin
2023-04-30 19:37:31 -04:00
parent 6939ac9c8d
commit 12c6d7122c
2 changed files with 22 additions and 26 deletions

View File

@@ -5,7 +5,7 @@ use doomlevel::DoomLevel;
use wadfile::WADFile;
fn main() {
let wad_file = WADFile::from_path("WADs/doom1.wad");
let wad_file = WADFile::from_path("WADs/doom1.wad").unwrap();
let level = DoomLevel::load_level(&wad_file, wad_file.get_index_from_name("e1m1").unwrap());
println!(
@@ -66,7 +66,7 @@ Flags:
\tSecret: {}
\tBlocks Sound: {}
\tNever Shows on Automap: {}
\tAlways Shows on Automap {}",
\tAlways Shows on Automap: {}",
level.linedefs[test_linedef].start_vertex,
level.linedefs[test_linedef].end_vertex,
level.linedefs[test_linedef].flags,

View File

@@ -4,6 +4,7 @@ use directory::Directory;
use std::{
fs::File,
io::{Read, Seek, SeekFrom},
path::Path,
};
pub struct WADFile {
@@ -15,20 +16,26 @@ pub struct WADFile {
}
impl WADFile {
pub fn from_path(path: &str) -> Self {
pub fn from_path(path: &str) -> Result<Self, &str> {
let wad_file = Path::new(path);
if !(wad_file.exists()) {
return Err("Could not open file!");
}
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 {
Ok(Self {
wad_path: path.to_string(),
identifier: header_data.0,
num_lumps: header_data.1,
init_offset: header_data.2,
lump_directory: lump_dir,
}
})
}
pub fn get_lump_name(&self, index: u32) -> String {
@@ -74,20 +81,15 @@ fn get_lump_dir(path: &str, dir_offset: &u32, num_lumps: &u32, lump_buffer: &mut
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,
lump_name: String::from_utf8(lump_name.to_vec()).unwrap(),
})
}
}
@@ -95,23 +97,17 @@ fn get_lump_dir(path: &str, dir_offset: &u32, num_lumps: &u32, lump_buffer: &mut
fn get_header_data(path: &str) -> (String, u32, u32) {
let mut wad_data = File::open(&path).unwrap();
let mut header = [0; 12];
let mut wad_id = [0; 4];
let mut dir_size = [0; 4];
let mut dir_offset = [0; 4];
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_data.read(&mut wad_id).unwrap();
wad_data.read(&mut dir_size).unwrap();
wad_data.read(&mut dir_offset).unwrap();
(
wad_type,
u32::from_le_bytes(lump_bytes),
u32::from_le_bytes(offset_bytes),
String::from_utf8(wad_id.to_vec()).unwrap(),
u32::from_le_bytes(dir_size),
u32::from_le_bytes(dir_offset),
)
}