Maraiah/src/marathon/wad.rs

129 lines
2.9 KiB
Rust
Raw Normal View History

2018-09-06 09:01:52 -07:00
//! Marathon Wad format handling.
use std::collections::BTreeMap;
use std::fmt;
2018-12-08 14:54:42 -08:00
use crate::durandal::bin::*;
use crate::durandal::machead::try_mac_header;
use crate::durandal::text::mac_roman_conv;
2018-09-06 09:01:52 -07:00
type Chunk <'a> = &'a[u8];
type ChunkMap<'a> = BTreeMap<Ident, Chunk<'a>>;
type EntryMap<'a> = BTreeMap<u16 , Entry<'a>>;
pub struct Entry<'a>
{
2018-09-09 15:21:17 -07:00
pub map: ChunkMap<'a>,
pub ext: &'a[u8],
2018-09-06 09:01:52 -07:00
}
#[derive(Debug)]
pub struct Wad<'a>
{
2018-09-09 15:21:17 -07:00
ver: Ver,
dvr: u16,
ext: usize,
nam: String,
pub ent: EntryMap<'a>,
}
#[derive(Debug)]
pub enum Ver
{
MI,
2018-09-11 01:20:54 -07:00
M2,
M1Dir,
2018-09-09 15:21:17 -07:00
M1
2018-09-06 09:01:52 -07:00
}
impl<'a> fmt::Debug for Entry<'a>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
2018-09-11 01:20:54 -07:00
write!(f, "Entry{{ ")?;
2018-09-06 09:01:52 -07:00
for (k, _) in &self.map {write!(f, "{} ", mac_roman_conv(&k[..]))?}
write!(f, "}}")
}
}
impl<'a> Wad<'a>
{
2018-09-11 12:07:42 -07:00
pub fn new(b: &[u8]) -> ResultS<Wad>
2018-09-06 09:01:52 -07:00
{
2018-09-11 01:20:54 -07:00
const SIZE_ENTRY_NEW: usize = 10;
const SIZE_ENTRY_OLD: usize = 8;
2018-09-11 12:07:42 -07:00
let b = &b[try_mac_header(b)..];
if b.len() < 128 {return Err("not enough data for header")}
let ver = b.c_u16b( 0)?;
let dvr = b.c_u16b( 2)?;
let nam = &b[4..68];
// crc = b.c_u32b(68)?;
let dir = b.c_u32b(72)? as usize;
let num = b.c_u16b(76)? as usize;
let ext = b.c_u16b(78)? as usize;
// hdr = b.c_u16b(80)?;
// bsz = b.c_u16b(82)?;
// pck = b.c_u32b(84)?;
2018-09-09 15:21:17 -07:00
let ver = match ver {
4 => Ver::MI,
2018-09-11 01:20:54 -07:00
2 => Ver::M2,
1 => Ver::M1Dir,
2018-09-10 07:30:38 -07:00
0 => Ver::M1,
2018-09-11 12:07:42 -07:00
_ => return Err("invalid wad version"),
2018-09-09 15:21:17 -07:00
};
let mut map = EntryMap::new();
let mut p = dir;
2018-09-11 01:20:54 -07:00
let o = match ver {Ver::M1 | Ver::M1Dir => true, _ => false};
let h = if o {SIZE_ENTRY_OLD} else {SIZE_ENTRY_NEW};
2018-09-09 15:21:17 -07:00
for i in 0..num
{
2018-09-11 12:07:42 -07:00
let ofs = b.c_u32b(p )? as usize;
let len = b.c_u32b(p+4)? as usize;
2018-09-11 01:20:54 -07:00
let ind = if o {i as u16}
2018-09-11 12:07:42 -07:00
else {b.c_u16b(p+8)?};
if ofs + len > b.len() {return Err("not enough data for entry")}
let ent = Entry{map: get_chunks(&b[ofs..ofs+len], o)?,
2018-09-09 15:21:17 -07:00
ext: &b[p+h..p+h+ext]};
map.insert(ind, ent);
p += h + ext;
}
2018-09-06 09:01:52 -07:00
2018-09-11 12:07:42 -07:00
Ok(Wad{ver, dvr, ext,
nam: mac_roman_conv(nam),
ent: map})
2018-09-06 09:01:52 -07:00
}
}
2018-09-11 12:07:42 -07:00
fn get_chunks(b: &[u8], o: bool) -> ResultS<ChunkMap>
2018-09-06 09:01:52 -07:00
{
2018-09-11 01:20:54 -07:00
const SIZE_CHUNK_NEW: usize = 16;
const SIZE_CHUNK_OLD: usize = 12;
2018-09-06 09:01:52 -07:00
let mut map = ChunkMap::new();
2018-09-11 01:20:54 -07:00
let mut p = 0;
let h = if o {SIZE_CHUNK_OLD} else {SIZE_CHUNK_NEW};
2018-09-06 09:01:52 -07:00
while p < b.len()
{
2018-09-11 12:07:42 -07:00
let k = b.c_iden(p )?;
// nx = b.c_u32b(p+ 4)?;
let l = b.c_u32b(p+ 8)? as usize;
// o = b.c_u32b(p+12)?;
2018-09-10 07:30:38 -07:00
map.insert(k, &b[p+h ..p+h+l]);
p += l + h;
2018-09-06 09:01:52 -07:00
}
2018-09-11 12:07:42 -07:00
Ok(map)
2018-09-06 09:01:52 -07:00
}
// EOF