Maraiah/maraiah/snd.rs

53 lines
1.0 KiB
Rust
Raw Normal View History

2019-02-17 18:17:35 -08:00
//! Marathon Sounds format handling.
2019-04-11 21:04:39 -07:00
pub mod defs;
pub mod snds;
2019-02-17 18:17:35 -08:00
2019-06-13 18:09:07 -07:00
use crate::{bin::Ident, err::*};
2019-04-11 21:04:39 -07:00
use std::collections::BTreeMap;
2019-02-17 18:17:35 -08:00
2019-03-01 01:27:14 -08:00
/// Reads all sounds from a Sound file.
2019-04-11 21:04:39 -07:00
pub fn read(b: &[u8]) -> ResultS<Vec<SoundTable>>
2019-02-17 18:17:35 -08:00
{
2019-02-18 20:06:34 -08:00
read_data! {
2019-03-18 12:31:14 -07:00
endian: BIG, buf: b, size: 260, start: 0, data {
let version = u32[0];
let magic = Ident[4];
let src_num = u16[8] usize;
let snd_num = u16[10] usize;
}
2019-02-18 20:06:34 -08:00
}
2019-02-17 18:17:35 -08:00
2019-03-13 08:32:46 -07:00
if version != 1 || magic != b"snd2" {
2019-02-17 18:17:35 -08:00
bail!("bad sound header");
}
let mut sc = Vec::with_capacity(src_num);
let mut p = 260;
for _ in 0..src_num {
2019-04-11 21:04:39 -07:00
let mut st = SoundTable::new();
2019-02-17 18:17:35 -08:00
for _ in 0..snd_num {
2019-04-11 21:04:39 -07:00
if let Some((ofs, idx, mut def)) = defs::read(&b[p..p + 64])? {
2019-02-17 18:17:35 -08:00
for &ofs in &ofs {
2019-04-11 21:04:39 -07:00
def.sounds.push(snds::read(&b[ofs..])?);
2019-02-17 18:17:35 -08:00
}
st.insert(idx, def);
}
p += 64;
}
sc.push(st);
}
Ok(sc)
}
2019-03-01 01:27:14 -08:00
/// A table of sound definitions.
2019-04-11 21:04:39 -07:00
pub type SoundTable = BTreeMap<u16, defs::SoundDef>;
2019-02-17 18:17:35 -08:00
// EOF