diff --git a/server/src/rbx.rs b/server/src/rbx.rs index 685db55f..0eb7361a 100644 --- a/server/src/rbx.rs +++ b/server/src/rbx.rs @@ -14,11 +14,9 @@ pub struct RbxInstance { /// Maps to the `ClassName` property on Instance. pub class_name: String, - /// Maps to the `Parent` property on Instance. - pub parent: Option, - /// Contains all other properties of an Instance. pub properties: HashMap, - // TODO: children property, Vec + /// All of the children of this instance. Order is relevant to preserve! + pub children: Vec, } diff --git a/server/src/rbx_session.rs b/server/src/rbx_session.rs index 39b1952f..7af72cb1 100644 --- a/server/src/rbx_session.rs +++ b/server/src/rbx_session.rs @@ -32,19 +32,11 @@ fn file_to_instances( }, }; - let parent = match file_item.get_route().parent() { - Some(parent_route) => match instances_by_route.get(&parent_route) { - Some(parent_id) => Some(*parent_id), - None => None, - }, - None => None, - }; - output.insert(primary_id, RbxInstance { name: route.name(partition).to_string(), class_name: "StringValue".to_string(), - parent, properties, + children: Vec::new(), }); (primary_id, vec![primary_id]) @@ -60,28 +52,14 @@ fn file_to_instances( }, }; - let parent = match file_item.get_route().parent() { - Some(parent_route) => match instances_by_route.get(&parent_route) { - Some(parent_id) => Some(*parent_id), - None => None, - }, - None => None, - }; - - output.insert(primary_id, RbxInstance { - name: route.name(partition).to_string(), - class_name: "Folder".to_string(), - parent, - properties: HashMap::new(), - }); + let mut child_ids = Vec::new(); let mut changed_ids = vec![primary_id]; for child_file_item in children.values() { let (child_id, mut child_changed_ids) = file_to_instances(child_file_item, partition, output, instances_by_route); - output.get_mut(&child_id).unwrap().parent = Some(primary_id); - + child_ids.push(child_id); changed_ids.push(child_id); // TODO: Should I stop using drain on Vecs of Copyable types? @@ -90,6 +68,13 @@ fn file_to_instances( } } + output.insert(primary_id, RbxInstance { + name: route.name(partition).to_string(), + class_name: "Folder".to_string(), + properties: HashMap::new(), + children: child_ids, + }); + (primary_id, changed_ids) }, } @@ -125,6 +110,19 @@ impl RbxSession { } } + pub fn retrieve_instance<'a, 'b>(&'a self, id: Id, output: &'b mut HashMap) { + let requested_instance = match self.instances.get(&id) { + Some(instance) => instance, + None => return, + }; + + output.insert(id, requested_instance); + + for child_id in &requested_instance.children { + self.retrieve_instance(*child_id, output); + } + } + pub fn read_partitions(&mut self) { let vfs_session_arc = self.vfs_session.clone(); let vfs_session = vfs_session_arc.read().unwrap(); diff --git a/server/src/web.rs b/server/src/web.rs index 9f185882..dd8df80c 100644 --- a/server/src/web.rs +++ b/server/src/web.rs @@ -162,19 +162,7 @@ pub fn start(config: WebConfig) { let mut instances = HashMap::new(); for requested_id in &requested_ids { - let requested_instance = match rbx_session.instances.get(requested_id) { - Some(instance) => instance, - None => continue, - }; - - instances.insert(*requested_id, requested_instance); - - // Oops; this needs to be recursive. - for (child_id, instance) in &rbx_session.instances { - if instance.parent == Some(*requested_id) { - instances.insert(*child_id, instance); - } - } + rbx_session.retrieve_instance(*requested_id, &mut instances); } Response::json(&ReadResponse {