From 3c583e91815f677888c17d9e208de6af3252cdbb Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 5 Sep 2023 17:45:34 -0700 Subject: [PATCH] load_roblox module --- src/lib.rs | 1 + src/load_roblox.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/load_roblox.rs diff --git a/src/lib.rs b/src/lib.rs index 0c717499..2b37b9a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ pub mod framework; +pub mod load_roblox; diff --git a/src/load_roblox.rs b/src/load_roblox.rs new file mode 100644 index 00000000..b15a7c06 --- /dev/null +++ b/src/load_roblox.rs @@ -0,0 +1,33 @@ +fn class_is_a(class: &str, superclass: &str) -> bool { + if class==superclass { + return true + } + let class_descriptor=rbx_reflection_database::get().classes.get(class); + if let Some(descriptor) = &class_descriptor { + if let Some(class_super) = &descriptor.superclass { + return class_is_a(&class_super, superclass) + } + } + return false +} + +fn recursive_collect_objects(objects: &mut std::vec::Vec,dom: &rbx_dom_weak::WeakDom, instance: &rbx_dom_weak::Instance, superclass: &str){ + for &referent in instance.children() { + if let Some(c) = dom.get_by_ref(referent) { + if class_is_a(c.class.as_str(), superclass) { + objects.push(c.referent());//copy ref + } + recursive_collect_objects(objects,dom,c,superclass); + } + } +} + +pub fn get_objects(buf_thing: std::io::BufReader<&[u8]>, superclass: &str) -> Result<(rbx_dom_weak::WeakDom,std::vec::Vec), Box> { + // Using buffered I/O is recommended with rbx_binary + let dom = rbx_binary::from_reader(buf_thing)?; + + let mut objects = std::vec::Vec::::new(); + recursive_collect_objects(&mut objects, &dom, dom.root(), superclass); + + return Ok((dom,objects)) +}