Files
rojo/src/snapshot_middleware/rbxmx.rs
Lucien Greathouse 10341e3776 Major Performance Improvements (#548)
* Use WeakDom::into_raw for faster snapshot generation from models

* Make compute_patch_set take snapshots by value

* Stop deferring property application in apply_patch_set

* Use InstanceBuilder::empty to avoid extra name allocations

* Git dependencies, skip dropping ServeSession

* Use std::mem::forget instead of ManuallyDrop

* Switch to latest rbx-dom crates.io dependencies

* Update other dependencies
2022-06-05 17:47:31 -04:00

88 lines
2.4 KiB
Rust

use std::path::Path;
use anyhow::Context;
use memofs::Vfs;
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
use super::util::PathExt;
pub fn snapshot_rbxmx(
context: &InstanceContext,
vfs: &Vfs,
path: &Path,
) -> anyhow::Result<Option<InstanceSnapshot>> {
let name = path.file_name_trim_end(".rbxmx")?;
let options = rbx_xml::DecodeOptions::new()
.property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown);
let temp_tree = rbx_xml::from_reader(vfs.read(path)?.as_slice(), options)
.with_context(|| format!("Malformed rbxm file: {}", path.display()))?;
let root_instance = temp_tree.root();
let children = root_instance.children();
if children.len() == 1 {
let child = children[0];
let snapshot = InstanceSnapshot::from_tree(temp_tree, child)
.name(name)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf()])
.context(context),
);
Ok(Some(snapshot))
} else {
anyhow::bail!(
"Rojo currently only supports model files with one top-level instance.\n\n \
Check the model file at path {}",
path.display()
);
}
}
#[cfg(test)]
mod test {
use super::*;
use memofs::{InMemoryFs, VfsSnapshot};
#[test]
fn plain_folder() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot(
"/foo.rbxmx",
VfsSnapshot::file(
r#"
<roblox version="4">
<Item class="Folder" referent="0">
<Properties>
<string name="Name">THIS NAME IS IGNORED</string>
</Properties>
</Item>
</roblox>
"#,
),
)
.unwrap();
let mut vfs = Vfs::new(imfs);
let instance_snapshot = snapshot_rbxmx(
&InstanceContext::default(),
&mut vfs,
Path::new("/foo.rbxmx"),
)
.unwrap()
.unwrap();
assert_eq!(instance_snapshot.name, "foo");
assert_eq!(instance_snapshot.class_name, "Folder");
assert_eq!(instance_snapshot.properties, Default::default());
assert_eq!(instance_snapshot.children, Vec::new());
}
}