Linedef Reader #4

Merged
wesley merged 1 commits from linedef-reader into main 2025-03-30 19:23:53 -04:00
8 changed files with 125 additions and 3 deletions

19
src/lumps/linedef.rs Normal file
View File

@@ -0,0 +1,19 @@
use crate::types::Linedef;
pub struct LinedefLump {
pub linedefs: Vec<Linedef>,
}
impl LinedefLump {
pub fn get_linedef(&self, pos: usize) -> Linedef {
self.linedefs[pos].to_owned()
}
pub fn get_all_linedefs(&self) -> Vec<Linedef> {
self.linedefs.to_vec()
}
pub fn get_num_linedefs(&self) -> usize {
self.linedefs.len()
}
}

View File

@@ -10,4 +10,5 @@ pub struct Lump {
pub enum LumpType { pub enum LumpType {
Unknown, Unknown,
Vertex, Vertex,
Linedef,
} }

View File

@@ -1,6 +1,8 @@
mod linedef;
mod lump; mod lump;
mod vertex; mod vertex;
pub use linedef::LinedefLump;
pub use lump::Lump; pub use lump::Lump;
pub use lump::LumpType; pub use lump::LumpType;
pub use vertex::VertexLump; pub use vertex::VertexLump;

View File

@@ -5,7 +5,7 @@ pub struct VertexLump {
} }
impl VertexLump { impl VertexLump {
pub fn num_vertexes(&self) -> usize { pub fn get_num_vertexes(&self) -> usize {
self.vertexes.len() self.vertexes.len()
} }

View File

@@ -91,3 +91,59 @@ pub fn get_num_vertex_lumps() {
assert_eq!(vetex_lumps.len(), 9); assert_eq!(vetex_lumps.len(), 9);
} }
#[test]
pub fn get_num_vertexes() {
let wad_file = get_wad();
let vertex_lump = wad_file.get_vertex_lump(&wad_file.directory[10]);
assert_eq!(vertex_lump.get_num_vertexes(), 467);
}
#[test]
pub fn read_first_linedef() {
use crate::types::Linedef;
let wad_file = get_wad();
let linedef_lump = wad_file.get_linedef_lump(&wad_file.directory[8]);
let correct_linedef = Linedef {
vertex1: 0,
vertex2: 1,
flags: 1,
special: 0,
tag: 0,
front_sidedef: 0,
back_sidedef: -1,
};
assert_eq!(linedef_lump.get_linedef(0), correct_linedef);
}
#[test]
pub fn get_num_linedef_lumps() {
use crate::lumps::{Lump, LumpType};
let mut linedef_lumps: Vec<Lump> = Vec::new();
let wad_file = get_wad();
for entry in 0..wad_file.num_lumps as usize {
if let LumpType::Linedef = wad_file.directory[entry].lump_type {
linedef_lumps.push(wad_file.directory[entry].to_owned());
}
}
assert_eq!(linedef_lumps.len(), 9);
}
#[test]
pub fn get_num_linedefs() {
let wad_file = get_wad();
let linedef_lump = wad_file.get_linedef_lump(&wad_file.directory[8]);
assert_eq!(linedef_lump.get_num_linedefs(), 475);
}

10
src/types/linedef.rs Normal file
View File

@@ -0,0 +1,10 @@
#[derive(Clone, Debug, PartialEq)]
pub struct Linedef {
pub vertex1: i16,
pub vertex2: i16,
pub flags: i16,
pub special: i16,
pub tag: i16,
pub front_sidedef: i16,
pub back_sidedef: i16,
}

View File

@@ -1,3 +1,5 @@
mod linedef;
mod vertex; mod vertex;
pub use linedef::Linedef;
pub use vertex::Vertex; pub use vertex::Vertex;

View File

@@ -1,8 +1,8 @@
use std::{fs::File, io::Read, path::Path}; use std::{fs::File, io::Read, path::Path};
use super::header::Header; use super::header::Header;
use crate::lumps::{Lump, LumpType, VertexLump}; use crate::lumps::{LinedefLump, Lump, LumpType, VertexLump};
use crate::types::Vertex; 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, validate_wad};
pub struct WADFile { pub struct WADFile {
@@ -39,6 +39,7 @@ impl WADFile {
let name = read_ascii(&file_buffer[startpos + 8..startpos + 16]); let name = read_ascii(&file_buffer[startpos + 8..startpos + 16]);
let lump_type = match name.as_str() { let lump_type = match name.as_str() {
"VERTEXES" => LumpType::Vertex, "VERTEXES" => LumpType::Vertex,
"LINEDEFS" => LumpType::Linedef,
_ => LumpType::Unknown, _ => LumpType::Unknown,
}; };
lump_dir.push(Lump { lump_dir.push(Lump {
@@ -79,4 +80,35 @@ impl WADFile {
VertexLump { vertexes } 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 }
}
} }