1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
mod error;
mod header;
mod state;
use std::{io::Read, str};
use rbx_dom_weak::WeakDom;
use rbx_reflection::ReflectionDatabase;
use self::state::DeserializerState;
pub(crate) use self::header::FileHeader;
pub use self::error::Error;
/// A configurable deserializer for Roblox binary models and places.
///
/// ## Example
/// ```no_run
/// use std::fs::File;
/// use std::io::BufReader;
///
/// use rbx_binary::Deserializer;
///
/// let input = BufReader::new(File::open("File.rbxm")?);
///
/// let deserializer = Deserializer::new();
/// let dom = deserializer.deserialize(input)?;
///
/// // rbx_binary always returns a DOM with a DataModel at the top level.
/// // To get to the instances from our file, we need to go one level deeper.
///
/// println!("Root instances in file:");
/// for &referent in dom.root().children() {
/// let instance = dom.get_by_ref(referent).unwrap();
/// println!("- {}", instance.name);
/// }
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// ## Configuration
///
/// A custom [`ReflectionDatabase`][ReflectionDatabase] can be specified via
/// [`reflection_database`][reflection_database].
///
/// [ReflectionDatabase]: rbx_reflection::ReflectionDatabase
/// [reflection_database]: Deserializer#method.reflection_database
pub struct Deserializer<'db> {
database: &'db ReflectionDatabase<'db>,
}
impl<'db> Deserializer<'db> {
/// Create a new `Deserializer` with the default settings.
pub fn new() -> Self {
Self {
database: rbx_reflection_database::get(),
}
}
/// Sets what reflection database for the deserializer to use.
#[inline]
pub fn reflection_database(self, database: &'db ReflectionDatabase<'db>) -> Self {
Self { database }
}
/// Deserialize a Roblox binary model or place from the given stream using
/// this deserializer.
pub fn deserialize<R: Read>(&self, reader: R) -> Result<WeakDom, Error> {
profiling::scope!("rbx_binary::deserialize");
let mut deserializer = DeserializerState::new(self, reader)?;
loop {
let chunk = deserializer.next_chunk()?;
match &chunk.name {
b"META" => deserializer.decode_meta_chunk(&chunk.data)?,
b"SSTR" => deserializer.decode_sstr_chunk(&chunk.data)?,
b"INST" => deserializer.decode_inst_chunk(&chunk.data)?,
b"PROP" => deserializer.decode_prop_chunk(&chunk.data)?,
b"PRNT" => deserializer.decode_prnt_chunk(&chunk.data)?,
b"END\0" => {
deserializer.decode_end_chunk(&chunk.data)?;
break;
}
_ => match str::from_utf8(&chunk.name) {
Ok(name) => log::info!("Unknown binary chunk name {}", name),
Err(_) => log::info!("Unknown binary chunk name {:?}", chunk.name),
},
}
}
Ok(deserializer.finish())
}
}
impl<'db> Default for Deserializer<'db> {
fn default() -> Self {
Self::new()
}
}