Maraiah/maraiah/shp.rs

56 lines
1.3 KiB
Rust
Raw Normal View History

2019-02-09 11:02:23 -08:00
//! Marathon Shapes format handling.
2019-04-11 21:04:39 -07:00
pub mod bmap;
pub mod clut;
pub mod coll;
pub mod fram;
pub mod sequ;
2019-06-13 18:09:07 -07:00
use crate::{bin::usize_from_u32, err::*};
2019-04-11 21:04:39 -07:00
/// Reads a collection at an offset provided by the Shapes header.
2019-05-30 18:29:37 -07:00
pub fn read_coll_at_offset(b: &[u8],
ofs: u32,
len: usize) -> ResultS<Option<coll::Collection>>
2019-04-11 21:04:39 -07:00
{
if ofs != u32::max_value() {
let ofs = usize_from_u32(ofs);
let dat = ok!(b.get(ofs..ofs + len), "bad offset")?;
Ok(Some(coll::read(dat)?))
2019-02-12 02:31:20 -08:00
} else {
2019-04-11 21:04:39 -07:00
Ok(None)
2019-02-11 03:29:01 -08:00
}
2019-02-12 02:31:20 -08:00
}
2019-02-09 11:02:23 -08:00
2019-03-01 01:27:14 -08:00
/// Read all of the collections in a Shapes file.
2019-04-11 21:04:39 -07:00
pub fn read(b: &[u8]) -> ResultS<Vec<CollectionDef>>
2019-02-09 11:02:23 -08:00
{
2019-02-12 02:31:20 -08:00
let mut cl = Vec::with_capacity(32);
let mut p = 0;
for _ in 0..32 {
2019-02-18 20:06:34 -08:00
read_data! {
2019-03-18 12:31:14 -07:00
endian: BIG, buf: b, size: 32, start: p, data {
2019-04-11 21:04:39 -07:00
let lo_ofs = u32[4];
2019-03-18 12:31:14 -07:00
let lo_len = u32[8] usize;
2019-04-11 21:04:39 -07:00
let hi_ofs = u32[12];
2019-03-18 12:31:14 -07:00
let hi_len = u32[16] usize;
}
2019-02-18 20:06:34 -08:00
}
2019-02-12 02:31:20 -08:00
2019-04-11 21:04:39 -07:00
let lo = read_coll_at_offset(b, lo_ofs, lo_len)?;
let hi = read_coll_at_offset(b, hi_ofs, hi_len)?;
2019-02-12 02:31:20 -08:00
2019-04-11 21:04:39 -07:00
cl.push((lo, hi));
2019-02-12 02:31:20 -08:00
p += 32;
2019-02-10 20:29:12 -08:00
}
2019-02-12 02:31:20 -08:00
Ok(cl)
2019-02-10 20:29:12 -08:00
}
2019-03-01 01:27:14 -08:00
/// A collection, which may have low- and high-definition variations, or none.
2019-04-11 21:04:39 -07:00
pub type CollectionDef = (Option<coll::Collection>, Option<coll::Collection>);
2019-02-12 03:32:10 -08:00
2019-02-09 11:02:23 -08:00
// EOF