Maraiah/src/marathon/wad.rs

114 lines
2.9 KiB
Rust
Raw Normal View History

2018-09-06 09:01:52 -07:00
//! Marathon Wad format handling.
2018-12-11 00:08:23 -08:00
use crate::durandal::{bin::*, err::*, machead::try_mac_header, text::mac_roman_conv};
2018-12-10 23:33:38 -08:00
use std::{collections::BTreeMap, fmt};
2018-09-06 09:01:52 -07:00
2018-12-10 23:33:38 -08:00
impl Wad<'_>
2018-09-06 09:01:52 -07:00
{
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 12:07:42 -07:00
let b = &b[try_mac_header(b)..];
2018-12-11 16:06:51 -08:00
if b.len() < 128 {return err_msg("not enough data for Wad header");}
2018-12-10 23:32:59 -08:00
let wadver = b.c_u16b( 0)?;
let dataver = b.c_u16b( 2)?;
let origname = mac_roman_conv(&b[4..68]);
// crc = b.c_u32b(68)?;
let dirofs = b.c_u32b(72)? as usize;
let numents = b.c_u16b(76)? as usize;
let appsize = b.c_u16b(78)? as usize;
// chnksize = b.c_u16b(80)?;
// entsize = b.c_u16b(82)?;
// parent = b.c_u32b(84)?;
2018-12-13 01:06:57 -08:00
let wadver = Ver::from_repr(wadver)?;
2018-12-10 23:32:59 -08:00
let is_old = match wadver {Ver::Base => true, _ => false};
let entsize = if is_old {8} else {10};
let mut entries = EntryMap::new();
let mut p = dirofs;
2018-09-09 15:21:17 -07:00
2018-12-11 16:06:51 -08:00
for i in 0..numents {
2018-12-10 23:32:59 -08:00
let offset = b.c_u32b(p )? as usize;
let size = b.c_u32b(p+4)? as usize;
let index = if !is_old {b.c_u16b(p+8)?} else {i as u16};
2018-09-11 12:07:42 -07:00
2018-12-13 01:06:57 -08:00
if offset + size > b.len() {return err_msg("not enough data for entry");}
2018-09-11 12:07:42 -07:00
2018-12-10 23:32:59 -08:00
let chunks = get_chunks(&b[offset..offset+size], is_old)?;
let appdata = &b[p+entsize..p+entsize+appsize];
2018-09-09 15:21:17 -07:00
2018-12-10 23:32:59 -08:00
entries.insert(index, Entry{chunks, appdata});
2018-09-09 15:21:17 -07:00
2018-12-10 23:32:59 -08:00
p += entsize + appsize;
2018-09-09 15:21:17 -07:00
}
2018-09-06 09:01:52 -07:00
2018-12-10 23:32:59 -08:00
Ok(Wad{wadver, dataver, appsize, origname, entries})
2018-09-06 09:01:52 -07:00
}
}
2018-12-10 23:32:59 -08:00
fn get_chunks(b: &[u8], is_old: bool) -> ResultS<ChunkMap>
2018-09-06 09:01:52 -07:00
{
2018-12-10 23:32:59 -08:00
let chnksize = if !is_old {16} else {12};
2018-09-11 01:20:54 -07:00
2018-12-10 23:32:59 -08:00
let mut chunks = ChunkMap::new();
let mut p = 0;
2018-09-06 09:01:52 -07:00
2018-12-11 16:06:51 -08:00
while p < b.len() {
2018-12-10 23:32:59 -08:00
let ident = b.c_iden(p )?;
// offset = b.c_u32b(p+ 4)?;
let size = b.c_u32b(p+ 8)? as usize;
// patchofs = b.c_u32b(p+12)?;
let beg = p + chnksize;
let end = beg + size;
chunks.insert(ident, &b[beg..end]);
p = end;
2018-09-06 09:01:52 -07:00
}
2018-12-10 23:32:59 -08:00
Ok(chunks)
2018-09-06 09:01:52 -07:00
}
2018-12-13 01:06:57 -08:00
type Chunk <'a> = &'a[u8];
type ChunkMap<'a> = BTreeMap<Ident, Chunk<'a>>;
type EntryMap<'a> = BTreeMap<u16 , Entry<'a>>;
pub struct Entry<'a>
{
pub chunks: ChunkMap<'a>,
pub appdata: &'a[u8],
}
#[derive(Debug)]
pub struct Wad<'a>
{
wadver: Ver,
dataver: u16,
origname: String,
appsize: usize,
pub entries: EntryMap<'a>,
}
c_enum! {
#[derive(Debug)]
pub enum Ver: u16
{
0 => Base,
1 => Dir,
2 => Over,
4 => MI,
}
}
impl fmt::Debug for Entry<'_>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
write!(f, "Entry{{ ")?;
for (ident, _) in &self.chunks {write!(f, "{} ", mac_roman_conv(ident))?;}
if self.appdata.len() != 0{write!(f, "\nappdata: {:?} ", self.appdata)?;}
write!(f, "}}")
}
}
2018-09-06 09:01:52 -07:00
// EOF