Maraiah/maraiah/map/entr.rs

61 lines
1.4 KiB
Rust
Raw Normal View History

2019-06-21 18:34:10 -07:00
//! Map file entry type.
2019-04-11 21:04:39 -07:00
use crate::err::*;
2019-06-21 18:34:10 -07:00
use super::head;
2019-04-11 21:04:39 -07:00
use std::collections::BTreeMap;
2019-06-21 18:34:10 -07:00
/// Read an entry from a Map file.
pub fn read(map: &head::Map, i: usize) -> ResultS<(u16, Entry<'_>)>
2019-04-11 21:04:39 -07:00
{
2019-06-21 18:34:10 -07:00
let size = map.head().size_entry();
2019-04-11 21:04:39 -07:00
read_data! {
2019-06-21 18:34:10 -07:00
endian: BIG, buf: map.dir(), size: size, start: size * i, data {
let offset = u32[0] usize;
let dsize = u32[4] usize;
let index = u16[8];
let app_data = u8[10; map.head().size_appl()];
2019-04-11 21:04:39 -07:00
}
}
2019-06-21 18:34:10 -07:00
let data = &map.data()[offset..offset + dsize];
let index = if map.head().old_wad() {i as u16} else {index};
Ok((index, Entry{data, app_data}))
}
2019-04-11 21:04:39 -07:00
2019-06-21 18:34:10 -07:00
/// Reads all entries in a Map file.
pub fn read_all(map: &head::Map) -> ResultS<EntryMap<'_>>
{
let mut entries = EntryMap::new();
2019-04-11 21:04:39 -07:00
2019-06-21 18:34:10 -07:00
for i in 0..map.num_ent() {
let (index, entry) = read(map, i)?;
2019-04-11 21:04:39 -07:00
2019-06-21 18:34:10 -07:00
if entries.contains_key(&index) {
bail!("entry index already exists");
}
2019-04-11 21:04:39 -07:00
2019-06-21 18:34:10 -07:00
entries.insert(index, entry);
2019-04-11 21:04:39 -07:00
}
Ok(entries)
}
2019-06-21 18:34:10 -07:00
/// An entry containing chunked data and application-specific data.
2019-04-11 21:04:39 -07:00
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Debug, Eq, PartialEq)]
2019-06-21 18:34:10 -07:00
pub struct Entry<'a>
2019-04-11 21:04:39 -07:00
{
2019-06-21 18:34:10 -07:00
/// The data in this `Entry`.
pub data: &'a [u8],
2019-04-11 21:04:39 -07:00
/// The application specific data for this Entry.
2019-06-21 18:34:10 -07:00
pub app_data: &'a [u8],
2019-04-11 21:04:39 -07:00
}
/// A map of indexed entries.
2019-06-21 18:34:10 -07:00
pub type EntryMap<'a> = BTreeMap<u16, Entry<'a>>;
2019-04-11 21:04:39 -07:00
// EOF