//! Wad file chunk type. use crate::{bin::*, err::*, image::{self, pict}, map}; /// Reads all chunks in an entry. pub fn read(b: &[u8], siz_cnk: usize) -> ResultS> { let mut chunks = Vec::new(); let mut p = 0; while p < b.len() { read_data! { endian: BIG, buf: b, size: siz_cnk, start: p, data { let iden = Ident[0]; let size = u32[8] usize; } } let beg = p + siz_cnk; let end = beg + size; let data = ok!(b.get(beg..end), "not enough data")?.to_vec(); chunks.push(ChunkData{iden, data}); p = end; } Ok(chunks) } /// Loads structured data from chunk data. pub fn load(chunk_data: &[ChunkData], old: bool) -> ResultS> { let mut chunks = Vec::new(); 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}; for chunk in chunk_data.iter() { let data = &chunk.data; let chunk = match &chunk.iden.0 { b"EPNT" => Chunk::Epnt(rd_array(data, map::epnt::read)?), b"FXpx" => Chunk::Fxpx(rd_array(data, map::fxpx::read)?), b"LINS" => Chunk::Lins(rd_array(data, map::lins::read)?), b"LITE" => Chunk::Lite(rd_array(data, read_chunk_lite)?), b"MNpx" => Chunk::Mnpx(rd_array(data, map::mnpx::read)?), b"Minf" => Chunk::Minf(read_chunk_minf(data)?), b"NAME" => Chunk::Name(rd_array(data, map::name::read)?), b"NOTE" => Chunk::Note(rd_array(data, map::note::read)?), b"OBJS" => Chunk::Objs(rd_array(data, map::objs::read)?), b"PICT" => Chunk::Pict(pict::read(data)?), b"PNTS" => Chunk::Pnts(rd_array(data, map::pnts::read)?), b"POLY" => Chunk::Poly(rd_array(data, read_chunk_poly)?), b"PRpx" => Chunk::Prpx(rd_array(data, map::prpx::read)?), b"PXpx" => Chunk::Pxpx(rd_array(data, map::pxpx::read)?), b"SIDS" => Chunk::Sids(rd_array(data, read_chunk_sids)?), b"WPpx" => Chunk::Wppx(rd_array(data, map::wppx::read)?), b"ambi" => Chunk::Ambi(rd_array(data, map::ambi::read)?), b"bonk" => Chunk::Bonk(rd_array(data, map::bonk::read)?), b"iidx" => Chunk::Iidx(rd_array(data, map::iidx::read)?), b"medi" => Chunk::Medi(rd_array(data, map::medi::read)?), b"plac" => Chunk::Plac(rd_array(data, map::plac::read)?), b"plat" => Chunk::Plat(rd_array(data, map::plat::read)?), b"term" => Chunk::Term(rd_array(data, map::term::read)?), _ => Chunk::Data(chunk.clone()), }; chunks.push(chunk); } Ok(chunks) } /// The data of a Chunk read from a file. #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] #[derive(Clone, Debug, Eq, PartialEq)] pub struct ChunkData { /// The name of the chunk. pub iden: Ident, /// The data. pub data: Vec, } /// Any kind of chunk in an `Entry`. #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] #[derive(Debug, Eq, PartialEq)] pub enum Chunk { /** `EPNT` chunks. */ Epnt(Vec), /** `FXpx` chunks. */ Fxpx(Vec), /** `LINS` chunks. */ Lins(Vec), /** `LITE` chunks. */ Lite(Vec), /** `MNpx` chunks. */ Mnpx(Vec), /** `Minf` chunks. */ Minf(map::minf::Info), /** `NAME` chunks. */ Name(Vec), /** `NOTE` chunks. */ Note(Vec), /** `OBJS` chunks. */ Objs(Vec), /** `PICT` chunks. */ Pict(image::Image8), /** `PNTS` chunks. */ Pnts(Vec), /** `POLY` chunks. */ Poly(Vec), /** `PRpx` chunks. */ Prpx(Vec), /** `PXpx` chunks. */ Pxpx(Vec), /** `SIDS` chunks. */ Sids(Vec), /** `WPpx` chunks. */ Wppx(Vec), /** `ambi` chunks. */ Ambi(Vec), /** `bonk` chunks. */ Bonk(Vec), /** `iidx` chunks. */ Iidx(Vec), /** `medi` chunks. */ Medi(Vec), /** `plac` chunks. */ Plac(Vec), /** `plat` chunks. */ Plat(Vec), /** `term` chunks. */ Term(Vec), /** Unknown chunk. */ Data(ChunkData), } // EOF