From b3ff145f635b52a96cb15dc658ee2319fa43f86d Mon Sep 17 00:00:00 2001 From: Marrub Date: Mon, 1 Apr 2019 05:09:01 -0400 Subject: [PATCH] move trm to map module --- source/marathon.rs | 1 - source/marathon/map.rs | 3 + source/marathon/map/term.rs | 54 +++++++++++++ source/marathon/map/trmf.rs | 29 +++++++ source/marathon/{trm.rs => map/trmg.rs} | 100 ++++-------------------- source/marathon/wad.rs | 24 +++--- 6 files changed, 112 insertions(+), 99 deletions(-) create mode 100644 source/marathon/map/term.rs create mode 100644 source/marathon/map/trmf.rs rename source/marathon/{trm.rs => map/trmg.rs} (53%) diff --git a/source/marathon.rs b/source/marathon.rs index d01f3c6..a11cddc 100644 --- a/source/marathon.rs +++ b/source/marathon.rs @@ -10,7 +10,6 @@ pub mod shp; pub mod snd; pub mod text; pub mod tga; -pub mod trm; pub mod wad; pub mod wav; pub mod xfer; diff --git a/source/marathon/map.rs b/source/marathon/map.rs index 05fcb99..6514101 100644 --- a/source/marathon/map.rs +++ b/source/marathon/map.rs @@ -17,6 +17,9 @@ pub mod pnts; pub mod poly; pub mod sids; pub mod stex; +pub mod term; +pub mod trmf; +pub mod trmg; /// The number of game ticks per second. pub const TICKS_PER_SECOND: u16 = 30; diff --git a/source/marathon/map/term.rs b/source/marathon/map/term.rs new file mode 100644 index 0000000..522b09b --- /dev/null +++ b/source/marathon/map/term.rs @@ -0,0 +1,54 @@ +//! `Terminal` type. + +use crate::{durandal::{bin, err::*}, marathon::text}; +use super::{trmf, trmg}; + +/// Reads a `term` chunk. +pub fn read(b: &[u8]) -> ResultS<(Terminal, usize)> +{ + read_data! { + endian: BIG, buf: b, size: 10, start: 0, data { + let end = u16[0] usize; + let encoded = u16[2]; + let lines = u16[4]; + let group_n = u16[6] usize; + let face_n = u16[8] usize; + } + } + + let encoded = encoded != 0; + + let (i_grp, x) = bin::rd_array_num(&b[10..], group_n, trmg::read)?; + let (faces, y) = bin::rd_array_num(&b[10 + x..], face_n, trmf::read)?; + + let text = ok!(b.get(10 + x + y..end), "not enough data")?; + let text = if encoded {text::fuck_string(text)} else {text.to_vec()}; + + let mut groups = Vec::with_capacity(group_n); + + for grp in &i_grp { + let flags = grp.flags; + let ttype = grp.ttype; + let lines = grp.lines; + let beg = grp.beg; + let len = grp.len; + let text = ok!(text.get(beg..beg + len), "bad offset")?; + let text = text::mac_roman_cstr(text); + + groups.push(trmg::Group{flags, ttype, lines, text}); + } + + Ok((Terminal{lines, groups, faces}, end)) +} + +/// A terminal definition, with collections of groups and faces. +#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] +#[derive(Debug, Eq, PartialEq)] +pub struct Terminal +{ + pub lines: u16, + pub groups: Vec, + pub faces: Vec, +} + +// EOF diff --git a/source/marathon/map/trmf.rs b/source/marathon/map/trmf.rs new file mode 100644 index 0000000..a8e1031 --- /dev/null +++ b/source/marathon/map/trmf.rs @@ -0,0 +1,29 @@ +//! `Face` type. + +use crate::durandal::err::*; + +/// Reads a `Face`. +pub fn read(b: &[u8]) -> ResultS<(Face, usize)> +{ + read_data! { + endian: BIG, buf: b, size: 6, start: 0, data { + let start = u16[0] usize; + let face = u16[2]; + let color = u16[4]; + } + } + + Ok((Face{start, face, color}, 6)) +} + +/// A text face. +#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] +#[derive(Debug, Eq, PartialEq)] +pub struct Face +{ + pub start: usize, + pub face: u16, + pub color: u16, +} + +// EOF diff --git a/source/marathon/trm.rs b/source/marathon/map/trmg.rs similarity index 53% rename from source/marathon/trm.rs rename to source/marathon/map/trmg.rs index 3181dec..2001114 100644 --- a/source/marathon/trm.rs +++ b/source/marathon/map/trmg.rs @@ -1,10 +1,10 @@ -//! Structures used by Marathon's Map format's terminal definitions. +//! `Group` type. -use crate::{durandal::{bin::*, err::*}, marathon::text::*}; +use crate::durandal::err::*; use bitflags::bitflags; /// Reads an `InterGroup`. -pub fn read_group(b: &[u8]) -> ResultS<(InterGroup, usize)> +pub fn read(b: &[u8]) -> ResultS<(InterGroup, usize)> { read_data! { endian: BIG, buf: b, size: 12, start: 0, data { @@ -41,94 +41,11 @@ pub fn read_group(b: &[u8]) -> ResultS<(InterGroup, usize)> Ok((InterGroup{flags, ttype, lines, beg, len}, 12)) } -/// Reads a `Face`. -pub fn read_face(b: &[u8]) -> ResultS<(Face, usize)> -{ - read_data! { - endian: BIG, buf: b, size: 6, start: 0, data { - let start = u16[0] usize; - let face = u16[2]; - let color = u16[4]; - } - } - - Ok((Face{start, face, color}, 6)) -} - -/// Reads a `term` chunk. -pub fn read_term(b: &[u8]) -> ResultS<(Terminal, usize)> -{ - read_data! { - endian: BIG, buf: b, size: 10, start: 0, data { - let end = u16[0] usize; - let encoded = u16[2]; - let lines = u16[4]; - let group_n = u16[6] usize; - let face_n = u16[8] usize; - } - } - - let encoded = encoded != 0; - - let (i_grp, x) = rd_array_num(&b[10..], group_n, read_group)?; - let (faces, y) = rd_array_num(&b[10 + x..], face_n, read_face)?; - - let text = ok!(b.get(10 + x + y..end), "not enough data")?; - let text = if encoded {fuck_string(text)} else {text.to_vec()}; - - let mut groups = Vec::with_capacity(group_n); - - for grp in &i_grp { - let flags = grp.flags; - let ttype = grp.ttype; - let lines = grp.lines; - let beg = grp.beg; - let len = grp.len; - let text = ok!(text.get(beg..beg + len), "bad offset")?; - let text = mac_roman_cstr(text); - - groups.push(Group{flags, ttype, lines, text}); - } - - Ok((Terminal{lines, groups, faces}, end)) -} - impl Default for GroupType { fn default() -> Self {GroupType::Unfinished} } -/// A terminal definition, with collections of groups and faces. -#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] -#[derive(Debug, Eq, PartialEq)] -pub struct Terminal -{ - pub lines: u16, - pub groups: Vec, - pub faces: Vec, -} - -/// A text face. -#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] -#[derive(Debug, Eq, PartialEq)] -pub struct Face -{ - pub start: usize, - pub face: u16, - pub color: u16, -} - -/// A terminal command grouping. -#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] -#[derive(Debug, Eq, PartialEq)] -pub struct Group -{ - pub flags: GroupFlags, - pub ttype: GroupType, - pub lines: u16, - pub text: String, -} - /// Interim structure. #[derive(Debug, Eq, PartialEq)] pub struct InterGroup @@ -140,6 +57,17 @@ pub struct InterGroup pub len: usize, } +/// A terminal command grouping. +#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] +#[derive(Debug, Eq, PartialEq)] +pub struct Group +{ + pub flags: GroupFlags, + pub ttype: GroupType, + pub lines: u16, + pub text: String, +} + /// The command of a `Group`. #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] #[derive(Clone, Copy, Debug, Eq, PartialEq)] diff --git a/source/marathon/wad.rs b/source/marathon/wad.rs index 3fb718b..fa98889 100644 --- a/source/marathon/wad.rs +++ b/source/marathon/wad.rs @@ -1,20 +1,20 @@ //! Marathon Wad format handling. use crate::{durandal::{bin::*, err::*, image}, - marathon::{map, phy, pict, text::mac_roman_cstr, trm}}; + marathon::{map, phy, pict, text::mac_roman_cstr}}; use std::collections::BTreeMap; /// Reads all chunks in an entry. -pub fn read_chunks(b: &[u8], old_dat: bool, siz_cnk: usize) +pub fn read_chunks(b: &[u8], old: bool, siz_cnk: usize) -> ResultS> { let mut chunks = Vec::new(); let mut p = 0; - let rd_minf = if old_dat {map::minf::read_old} else {map::minf::read}; - let rd_sids = if old_dat {map::sids::read_old} else {map::sids::read}; - let rd_poly = if old_dat {map::poly::read_old} else {map::poly::read}; - let rd_lite = if old_dat {map::lite::read_old} else {map::lite::read}; + let read_chunk_minf = if old {map::minf::read_old} else {map::minf::read}; + let read_chunk_sids = if old {map::sids::read_old} else {map::sids::read}; + let read_chunk_poly = if old {map::poly::read_old} else {map::poly::read}; + let read_chunk_lite = if old {map::lite::read_old} else {map::lite::read}; while p < b.len() { read_data! { @@ -30,22 +30,22 @@ pub fn read_chunks(b: &[u8], old_dat: bool, siz_cnk: usize) chunks.push(match &iden.0 { b"PICT" => Chunk::Pict(pict::load_pict(data)?), - b"Minf" => Chunk::Minf(rd_minf(data)?), + b"Minf" => Chunk::Minf(read_chunk_minf(data)?), b"iidx" => Chunk::Iidx(rd_array(data, map::iidx::read)?), b"EPNT" => Chunk::Pnts(rd_array(data, map::epnt::read)?), b"PNTS" => Chunk::Pnts(rd_array(data, map::pnts::read)?), b"LINS" => Chunk::Lins(rd_array(data, map::lins::read)?), - b"SIDS" => Chunk::Sids(rd_array(data, rd_sids)?), - b"POLY" => Chunk::Poly(rd_array(data, rd_poly)?), + b"SIDS" => Chunk::Sids(rd_array(data, read_chunk_sids)?), + b"POLY" => Chunk::Poly(rd_array(data, read_chunk_poly)?), b"OBJS" => Chunk::Objs(rd_array(data, map::objs::read)?), - b"LITE" => Chunk::Lite(rd_array(data, rd_lite)?), + b"LITE" => Chunk::Lite(rd_array(data, read_chunk_lite)?), b"plac" => Chunk::Plac(rd_array(data, map::plac::read)?), b"ambi" => Chunk::Ambi(rd_array(data, map::ambi::read)?), b"bonk" => Chunk::Bonk(rd_array(data, map::bonk::read)?), b"medi" => Chunk::Medi(rd_array(data, map::medi::read)?), b"plat" => Chunk::Plat(rd_array(data, map::plat::read)?), b"NOTE" => Chunk::Note(rd_array(data, map::note::read)?), - b"term" => Chunk::Term(rd_array(data, trm::read_term)?), + b"term" => Chunk::Term(rd_array(data, map::term::read)?), b"FXpx" => Chunk::Fxpx(rd_array(data, phy::fxpx::read)?), b"MNpx" => Chunk::Mnpx(rd_array(data, phy::mnpx::read)?), b"PRpx" => Chunk::Prpx(rd_array(data, phy::prpx::read)?), @@ -157,7 +157,7 @@ pub enum Chunk /** A `medi` chunk. */ Medi(Vec), /** A `plat` chunk. */ Plat(Vec), /** A `NOTE` chunk. */ Note(Vec), - /** A `term` chunk. */ Term(Vec), + /** A `term` chunk. */ Term(Vec), /** A `FXpx` chunk. */ Fxpx(Vec), /** A `MNpx` chunk. */ Mnpx(Vec), /** A `PRpx` chunk. */ Prpx(Vec),