Maraiah/maraiah/snd.rs

55 lines
1020 B
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-06-23 04:35:51 -07:00
use std::{collections::BTreeMap, io::prelude::*};
2019-02-17 18:17:35 -08:00
2019-03-01 01:27:14 -08:00
/// Reads all sounds from a Sound file.
2019-06-23 04:35:51 -07:00
pub fn read(fp: &mut impl Read) -> ResultS<Vec<SoundTable>>
2019-02-17 18:17:35 -08:00
{
2019-07-05 20:21:11 -07:00
let mut b = Vec::new();
fp.read_to_end(&mut b)?;
2019-06-23 04:35:51 -07:00
2019-07-05 20:21:11 -07:00
read_data! {
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-17 18:17:35 -08:00
2019-07-05 20:21:11 -07:00
if version != 1 || magic != b"snd2" {
bail!("bad sound header");
}
2019-02-17 18:17:35 -08:00
2019-07-05 20:21:11 -07:00
let mut sc = Vec::with_capacity(src_num);
2019-02-17 18:17:35 -08:00
2019-07-05 20:21:11 -07:00
for i in 0..src_num {
let p = 260 + i * 64;
2019-06-23 04:35:51 -07:00
2019-07-05 20:21:11 -07:00
let mut st = SoundTable::new();
2019-02-17 18:17:35 -08:00
2019-07-05 20:21:11 -07:00
for _ in 0..snd_num {
if let Some((ofs, idx, mut def)) = defs::read(&b[p..p + 64])? {
for &ofs in &ofs {
def.sounds.push(snds::read(&b[ofs..])?);
}
2019-02-17 18:17:35 -08:00
2019-07-05 20:21:11 -07:00
st.insert(idx, def);
}
}
2019-02-17 18:17:35 -08:00
2019-07-05 20:21:11 -07:00
sc.push(st);
}
2019-02-17 18:17:35 -08:00
2019-07-05 20:21:11 -07:00
Ok(sc)
2019-02-17 18:17:35 -08:00
}
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