Maraiah/maraiah/shp.rs

60 lines
1.4 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-06-23 04:28:56 -07:00
use std::io::prelude::*;
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
{
2019-07-05 20:21:11 -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)?))
} else {
Ok(None)
}
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-06-23 04:28:56 -07:00
pub fn read(fp: &mut impl Read) -> ResultS<Collections>
2019-02-09 11:02:23 -08:00
{
2019-07-05 20:21:11 -07:00
let mut b = Vec::new();
fp.read_to_end(&mut b)?;
2019-02-12 02:31:20 -08:00
2019-07-05 20:21:11 -07:00
let mut collections = vec![(None, None); 32];
2019-06-23 04:28:56 -07:00
2019-07-05 20:21:11 -07:00
for (i, co) in collections.iter_mut().enumerate() {
read_data! {
endian: BIG, buf: &b, size: 32, start: i * 32, data {
let lo_ofs = u32[4];
let lo_len = u32[8] usize;
let hi_ofs = u32[12];
let hi_len = u32[16] usize;
}
}
2019-02-12 02:31:20 -08:00
2019-07-05 20:21:11 -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-07-05 20:21:11 -07:00
*co = (lo, hi);
}
2019-02-10 20:29:12 -08:00
2019-07-05 20:21:11 -07:00
Ok(collections)
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-06-23 04:28:56 -07:00
/// The set of all collections in a Shapes file.
pub type Collections = Vec<CollectionDef>;
2019-06-23 04:28:56 -07:00
2019-02-09 11:02:23 -08:00
// EOF