Maraiah/source/marathon/wad.rs

212 lines
6.7 KiB
Rust

//! Marathon Wad format handling.
use crate::{durandal::{bin::*, err::*, image},
marathon::{map, phy, pict, text::mac_roman_cstr}};
use std::{collections::BTreeMap, convert::TryFrom};
/// Reads all chunks in an entry.
pub fn read_chunks(b: &[u8], old: bool, siz_cnk: usize) -> ResultS<Vec<Chunk>>
{
let mut chunks = Vec::new();
let mut p = 0;
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! {
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 = &b[beg..end];
chunks.push(match &iden.0 {
b"PICT" => Chunk::Pict(pict::load_pict(data)?),
b"Minf" => Chunk::Minf(read_chunk_minf(data)?),
b"iidx" => Chunk::Iidx(rd_array(data, map::iidx::read)?),
b"EPNT" => Chunk::Epnt(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, 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, 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, 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)?),
b"PXpx" => Chunk::Pxpx(rd_array(data, phy::pxpx::read)?),
b"WPpx" => Chunk::Wppx(rd_array(data, phy::wppx::read)?),
_ => Chunk::Data{iden, data: data.to_vec()},
});
p = end;
}
Ok(chunks)
}
/// Reads all entries in a `Wad`.
pub fn read_entries(b: &[u8],
old_wad: bool,
old_dat: bool,
siz_app: usize,
siz_ent: usize,
siz_cnk: usize)
-> ResultS<BTreeMap<u16, Entry>>
{
read_data! {
endian: BIG, buf: b, size: 128, start: 0, data {
let dirofs = u32[72] usize;
let numents = u16[76] usize;
}
}
let mut entries = BTreeMap::new();
let mut p = dirofs;
for i in 0..numents {
read_data! {
endian: BIG, buf: b, size: siz_ent, start: p, data {
let offset = u32[0] usize;
let size = u32[4] usize;
let index = u16[8];
}
}
let index = if old_wad {i as u16} else {index};
let chunks = read_chunks(&b[offset..offset + size], old_dat, siz_cnk)?;
let appdata = b[p..p + siz_app].to_vec();
entries.insert(index, Entry{chunks, appdata});
p += siz_ent + siz_app;
}
Ok(entries)
}
/// Reads a Map file.
pub fn read_wad(b: &[u8]) -> ResultS<Wad>
{
read_data! {
endian: BIG, buf: b, size: 128, start: 0, data {
let ver_wad = u16[0] enum Ver;
let ver_dat = u16[2];
let name = mac_roman_cstr[4; 64] no_try;
let siz_app = u16[78] usize;
let siz_wcnk = u16[80] usize;
let siz_went = u16[82] usize;
}
}
let old_dat = ver_dat == 0;
let old_wad = match ver_wad {
Ver::Base => true,
_ => false,
};
let siz_ent = if old_wad {8 } else {10};
let siz_cnk = if old_wad {12} else {16};
if !old_wad && siz_ent != siz_went {
bail!("invalid entry size");
}
if !old_wad && siz_cnk != siz_wcnk {
bail!("invalid chunk size");
}
let entries = read_entries(b, old_wad, old_dat, siz_app, siz_ent, siz_cnk)?;
Ok(Wad{name, siz_app, entries})
}
/// Any kind of chunk in an `Entry`.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Debug, Eq, PartialEq)]
pub enum Chunk
{
/** A `PICT` chunk. */ Pict(image::Image8),
/** A `Minf` chunk. */ Minf(map::minf::Minf),
/** An `iidx` chunk. */ Iidx(Vec<u16>),
/** An `EPNT` chunk. */ Epnt(Vec<map::epnt::Endpoint>),
/** A `PNTS` chunk. */ Pnts(Vec<map::pnts::Point>),
/** A `LINS` chunk. */ Lins(Vec<map::lins::Line>),
/** A `SIDS` chunk. */ Sids(Vec<map::sids::Side>),
/** A `POLY` chunk. */ Poly(Vec<map::poly::Polygon>),
/** A `LITE` chunk. */ Lite(Vec<map::lite::Light>),
/** An `OBJS` chunk. */ Objs(Vec<map::objs::Object>),
/** A `plac` chunk. */ Plac(Vec<map::plac::ObjectFreq>),
/** An `ambi` chunk. */ Ambi(Vec<map::ambi::SoundAmbi>),
/** A `bonk` chunk. */ Bonk(Vec<map::bonk::SoundRand>),
/** A `medi` chunk. */ Medi(Vec<map::medi::Media>),
/** A `plat` chunk. */ Plat(Vec<map::plat::Platform>),
/** A `NOTE` chunk. */ Note(Vec<map::note::Note>),
/** A `term` chunk. */ Term(Vec<map::term::Terminal>),
/** A `FXpx` chunk. */ Fxpx(Vec<phy::fxpx::Effect>),
/** A `MNpx` chunk. */ Mnpx(Vec<phy::mnpx::Monster>),
/** A `PRpx` chunk. */ Prpx(Vec<phy::prpx::Projectile>),
/** A `PXpx` chunk. */ Pxpx(Vec<phy::pxpx::Physics>),
/** A `WPpx` chunk. */ Wppx(Vec<phy::wppx::Weapon>),
/// Any other type of chunk, which may have arbitrary data in it.
Data{/** The name of the chunk. */ iden: Ident,
/** The data. */ data: Vec<u8>},
}
/// An entry containing chunks and application-specific data.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Debug, Eq, PartialEq)]
pub struct Entry
{
/// All of the chunks in this `Entry`.
pub chunks: Vec<Chunk>,
/// The application specific data for this Entry.
pub appdata: Vec<u8>,
}
/// A Map file containing entries.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Debug, Eq, PartialEq)]
pub struct Wad
{
/// The original name of this file.
pub name: String,
/// The size of each `Entry`'s `appdata` field.
pub siz_app: usize,
/// All of the entries in this `Wad`.
pub entries: BTreeMap<u16, Entry>,
}
c_enum! {
/// The version of a `Wad`.
#[derive(Debug)]
enum Ver: u16
{
Base = 0,
Dir = 1,
Over = 2,
Inf = 4,
}
}
// EOF