Maraiah/source/map/chnk.rs

121 lines
4.4 KiB
Rust

//! 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<Vec<ChunkData>>
{
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<Vec<Chunk>>
{
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<u8>,
}
/// 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<map::epnt::Endpoint>),
/** `FXpx` chunks. */ Fxpx(Vec<map::fxpx::Effect>),
/** `LINS` chunks. */ Lins(Vec<map::lins::Line>),
/** `LITE` chunks. */ Lite(Vec<map::lite::Light>),
/** `MNpx` chunks. */ Mnpx(Vec<map::mnpx::Monster>),
/** `Minf` chunks. */ Minf(map::minf::Minf),
/** `NAME` chunks. */ Name(Vec<String>),
/** `NOTE` chunks. */ Note(Vec<map::note::Note>),
/** `OBJS` chunks. */ Objs(Vec<map::objs::Object>),
/** `PICT` chunks. */ Pict(image::Image8),
/** `PNTS` chunks. */ Pnts(Vec<map::pnts::Point>),
/** `POLY` chunks. */ Poly(Vec<map::poly::Polygon>),
/** `PRpx` chunks. */ Prpx(Vec<map::prpx::Projectile>),
/** `PXpx` chunks. */ Pxpx(Vec<map::pxpx::Physics>),
/** `SIDS` chunks. */ Sids(Vec<map::sids::Side>),
/** `WPpx` chunks. */ Wppx(Vec<map::wppx::Weapon>),
/** `ambi` chunks. */ Ambi(Vec<map::ambi::SoundAmbi>),
/** `bonk` chunks. */ Bonk(Vec<map::bonk::SoundRand>),
/** `iidx` chunks. */ Iidx(Vec<u16>),
/** `medi` chunks. */ Medi(Vec<map::medi::Media>),
/** `plac` chunks. */ Plac(Vec<map::plac::ObjectFreq>),
/** `plat` chunks. */ Plat(Vec<map::plat::Platform>),
/** `term` chunks. */ Term(Vec<map::term::Terminal>),
/** Unknown chunk. */ Data(ChunkData),
}
// EOF