diff --git a/src/tests/helpers.rs b/src/tests/helpers.rs index 2f0e41a..5c7209f 100644 --- a/src/tests/helpers.rs +++ b/src/tests/helpers.rs @@ -1,4 +1,4 @@ -use crate::utils::*; +use crate::wad::wadfile::validate_wad; #[test] pub fn valid_wad() { diff --git a/src/utils/helpers.rs b/src/utils/helpers.rs index 98c5066..959fd44 100644 --- a/src/utils/helpers.rs +++ b/src/utils/helpers.rs @@ -1,32 +1,3 @@ -use std::{ - fs::File, - io::{self, Read}, - path::Path, -}; - -/// Validates a WAD file to make sure that it is a legitimate file -/// -/// Parameters: -/// - path: &str - Path to the WAD to validate -pub fn validate_wad(path: &str) -> io::Result { - let wad_file = Path::new(path); - - // Check to see if the WAD exists - if !(wad_file.exists()) { - // Return back false because we didn't pass a valid file - return Ok(false); - } - - // If the file exists open it and read the first 4 bytes - // of the file and see if we get "IWAD" or "PWAD" - let mut file = File::open(wad_file)?; - let mut magic = [0u8; 4]; - file.read_exact(&mut magic)?; - - // Now we return based on what we found - Ok(magic == *b"IWAD" || magic == *b"PWAD") -} - pub fn read_ascii(bytes: &[u8]) -> String { std::str::from_utf8(&bytes[..bytes.len()]) .unwrap_or("") diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 2784022..2c3146e 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -4,4 +4,3 @@ mod helpers; pub use helpers::read_ascii; pub use helpers::read_i16_le; pub use helpers::read_u32_le; -pub use helpers::validate_wad; diff --git a/src/wad/mod.rs b/src/wad/mod.rs index cd01cbc..dc3a438 100644 --- a/src/wad/mod.rs +++ b/src/wad/mod.rs @@ -1,4 +1,4 @@ mod header; -mod wadfile; +pub mod wadfile; pub use wadfile::WADFile; diff --git a/src/wad/wadfile.rs b/src/wad/wadfile.rs index bf56521..c5a65f8 100644 --- a/src/wad/wadfile.rs +++ b/src/wad/wadfile.rs @@ -1,9 +1,13 @@ +mod readers; +mod utils; + +pub use utils::validate_wad; + +use crate::lumps::{Lump, LumpType}; use std::{fs::File, io::Read, path::Path}; use super::header::Header; -use crate::lumps::{LinedefLump, Lump, LumpType, VertexLump}; -use crate::types::{Linedef, Vertex}; -use crate::utils::{read_ascii, read_i16_le, read_u32_le, validate_wad}; +use crate::utils::{read_ascii, read_i16_le, read_u32_le}; pub struct WADFile { pub path: String, @@ -59,56 +63,4 @@ impl WADFile { data: file_buffer, } } - - pub fn get_vertex_lump(&self, lump: &Lump) -> VertexLump { - let lump_offset = lump.offset as usize; - let lump_size = lump.size as usize; - let lump_data = &self.data[lump_offset..lump_offset + lump_size]; - let lump_entries: usize = lump_size / 4; - let mut vertexes: Vec = Vec::with_capacity(lump_entries); - - for entry in 0..lump_entries { - let startpos = entry * 4; - let vertex_x = read_i16_le(&lump_data[startpos..startpos + 2]); - let vertex_y = read_i16_le(&lump_data[startpos + 2..startpos + 4]); - - vertexes.push(Vertex { - x: vertex_x, - y: vertex_y, - }); - } - - VertexLump { vertexes } - } - - pub fn get_linedef_lump(&self, lump: &Lump) -> LinedefLump { - let lump_offset = lump.offset as usize; - let lump_size = lump.size as usize; - let lump_data = &self.data[lump_offset..lump_offset + lump_size]; - let lump_entries = lump_size / 14; - let mut linedefs: Vec = Vec::with_capacity(lump_entries); - - for entry in 0..lump_entries { - let startpos = entry * 14; - let vertex1 = read_i16_le(&lump_data[startpos..startpos + 2]); - let vertex2 = read_i16_le(&lump_data[startpos + 2..startpos + 4]); - let flags = read_i16_le(&lump_data[startpos + 4..startpos + 6]); - let special = read_i16_le(&lump_data[startpos + 6..startpos + 8]); - let tag = read_i16_le(&lump_data[startpos + 8..startpos + 10]); - let front_sidedef = read_i16_le(&lump_data[startpos + 10..startpos + 12]); - let back_sidedef = read_i16_le(&lump_data[startpos + 12..startpos + 14]); - - linedefs.push(Linedef { - vertex1, - vertex2, - flags, - special, - tag, - front_sidedef, - back_sidedef, - }); - } - - LinedefLump { linedefs } - } } diff --git a/src/wad/wadfile/readers.rs b/src/wad/wadfile/readers.rs new file mode 100644 index 0000000..62ecf92 --- /dev/null +++ b/src/wad/wadfile/readers.rs @@ -0,0 +1,58 @@ +use crate::lumps::{LinedefLump, Lump, VertexLump}; +use crate::types::{Linedef, Vertex}; +use crate::wad::WADFile; +use crate::wad::wadfile::read_i16_le; + +impl WADFile { + pub fn get_vertex_lump(&self, lump: &Lump) -> VertexLump { + let lump_offset = lump.offset as usize; + let lump_size = lump.size as usize; + let lump_data = &self.data[lump_offset..lump_offset + lump_size]; + let lump_entries: usize = lump_size / 4; + let mut vertexes: Vec = Vec::with_capacity(lump_entries); + + for entry in 0..lump_entries { + let startpos = entry * 4; + let vertex_x = read_i16_le(&lump_data[startpos..startpos + 2]); + let vertex_y = read_i16_le(&lump_data[startpos + 2..startpos + 4]); + + vertexes.push(Vertex { + x: vertex_x, + y: vertex_y, + }); + } + + VertexLump { vertexes } + } + + pub fn get_linedef_lump(&self, lump: &Lump) -> LinedefLump { + let lump_offset = lump.offset as usize; + let lump_size = lump.size as usize; + let lump_data = &self.data[lump_offset..lump_offset + lump_size]; + let lump_entries = lump_size / 14; + let mut linedefs: Vec = Vec::with_capacity(lump_entries); + + for entry in 0..lump_entries { + let startpos = entry * 14; + let vertex1 = read_i16_le(&lump_data[startpos..startpos + 2]); + let vertex2 = read_i16_le(&lump_data[startpos + 2..startpos + 4]); + let flags = read_i16_le(&lump_data[startpos + 4..startpos + 6]); + let special = read_i16_le(&lump_data[startpos + 6..startpos + 8]); + let tag = read_i16_le(&lump_data[startpos + 8..startpos + 10]); + let front_sidedef = read_i16_le(&lump_data[startpos + 10..startpos + 12]); + let back_sidedef = read_i16_le(&lump_data[startpos + 12..startpos + 14]); + + linedefs.push(Linedef { + vertex1, + vertex2, + flags, + special, + tag, + front_sidedef, + back_sidedef, + }); + } + + LinedefLump { linedefs } + } +} diff --git a/src/wad/wadfile/utils.rs b/src/wad/wadfile/utils.rs new file mode 100644 index 0000000..51d1711 --- /dev/null +++ b/src/wad/wadfile/utils.rs @@ -0,0 +1,26 @@ +use std::fs::File; +use std::io::{self, Read}; +use std::path::Path; + +/// Validates a WAD file to make sure that it is a legitimate file +/// +/// Parameters: +/// - path: &str - Path to the WAD to validate +pub fn validate_wad(path: &str) -> io::Result { + let wad_file = Path::new(path); + + // Check to see if the WAD exists + if !(wad_file.exists()) { + // Return back false because we didn't pass a valid file + return Ok(false); + } + + // If the file exists open it and read the first 4 bytes + // of the file and see if we get "IWAD" or "PWAD" + let mut file = File::open(wad_file)?; + let mut magic = [0u8; 4]; + file.read_exact(&mut magic)?; + + // Now we return based on what we found + Ok(magic == *b"IWAD" || magic == *b"PWAD") +}