wgtk/space/
mod.rs

1//! Compiled space codec, use it to open and read sections of a compiled space binaries.
2
3pub mod section;
4
5use std::io::{self, Read, Seek, SeekFrom};
6
7use section::{Section, BWTB};
8
9
10/// A structure representing a full compiled space.
11pub struct CompiledSpace<R> {
12    pub inner: R,
13    pub bwtb: BWTB,
14}
15
16impl<R: Read + Seek> CompiledSpace<R> {
17
18    /// Create a new lazy compiled space from a seekable read implementor.
19    /// This function will only read the BWTB header section  before
20    /// actually returning the object.
21    pub fn new(mut inner: R) -> io::Result<Self> {
22
23        let bwtb = BWTB::decode(&mut inner)?;
24
25        Ok(CompiledSpace {
26            inner,
27            bwtb,
28        })
29
30    }
31
32    /// Decode a section from this compiled space.
33    pub fn decode_section<S: Section>(&mut self) -> Option<S> {
34        let meta = self.bwtb.get_section_meta(S::ID)?;
35        self.inner.seek(SeekFrom::Start(meta.off as u64)).ok()?;
36        Some(S::decode(&mut self.inner).unwrap())
37    }
38
39}