WAD Refactor #5

Merged
wesley merged 1 commits from wad-refactor into main 2025-03-31 19:33:16 -04:00
7 changed files with 93 additions and 87 deletions

View File

@@ -1,4 +1,4 @@
use crate::utils::*;
use crate::wad::wadfile::validate_wad;
#[test]
pub fn valid_wad() {

View File

@@ -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<bool> {
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("")

View File

@@ -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;

View File

@@ -1,4 +1,4 @@
mod header;
mod wadfile;
pub mod wadfile;
pub use wadfile::WADFile;

View File

@@ -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<Vertex> = 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<Linedef> = 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 }
}
}

View File

@@ -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<Vertex> = 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<Linedef> = 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 }
}
}

26
src/wad/wadfile/utils.rs Normal file
View File

@@ -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<bool> {
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")
}