WAD Refactor

Refactored some of the code for the wadfile reading. Moved out the
readers into their own separate file, and moved some utilities around to
create wad utilities for more modularity.
This commit is contained in:
2025-03-31 19:30:26 -04:00
parent 8de9d56c2d
commit fb802a8d2b
7 changed files with 93 additions and 87 deletions

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 }
}
}