Maraiah/maraiah/shp.rs

61 lines
1.6 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
{
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-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-06-23 04:28:56 -07:00
let mut b = Vec::new();
fp.read_to_end(&mut b)?;
2019-02-12 02:31:20 -08:00
2019-06-23 04:28:56 -07:00
// hush little rustc, don't say a word...
let mut collections: Collections = unsafe {std::mem::uninitialized()};
for (i, co) in collections.iter_mut().enumerate() {
2019-02-18 20:06:34 -08:00
read_data! {
2019-06-23 04:28:56 -07:00
endian: BIG, buf: &b, size: 32, start: i * 32, 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-06-23 04:28:56 -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-06-23 04:28:56 -07:00
*co = (lo, hi);
2019-02-10 20:29:12 -08:00
}
2019-06-23 04:28:56 -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 = [CollectionDef; 32];
2019-02-09 11:02:23 -08:00
// EOF