From 3f526cd12735e26920c008b9abb2ee5dd8e09582 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Thu, 17 Jan 2019 15:33:17 -0800 Subject: [PATCH] Metadata per instance is a go --- server/src/rbx_session.rs | 20 ++++++++-------- server/src/rbx_snapshot.rs | 32 ++++++++++++++++--------- server/src/snapshot_reconciler.rs | 40 +++++++++++++------------------ server/src/web.rs | 21 +++++++++++++--- 4 files changed, 65 insertions(+), 48 deletions(-) diff --git a/server/src/rbx_session.rs b/server/src/rbx_session.rs index 50036db5..21b033df 100644 --- a/server/src/rbx_session.rs +++ b/server/src/rbx_session.rs @@ -1,7 +1,7 @@ use std::{ borrow::Cow, collections::HashMap, - path::Path, + path::{Path, PathBuf}, str, sync::{Arc, Mutex}, }; @@ -23,17 +23,17 @@ const INIT_SCRIPT: &str = "init.lua"; const INIT_SERVER_SCRIPT: &str = "init.server.lua"; const INIT_CLIENT_SCRIPT: &str = "init.client.lua"; -#[derive(Debug, Default, Serialize, Deserialize)] +#[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct MetadataPerPath { pub instance_id: Option, pub instance_name: Option, } -// #[derive(Debug, Default, Serialize, Deserialize)] -// pub struct MetadataPerInstance { -// pub source_path: Option, -// pub ignore_unknown_instances: bool, -// } +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct MetadataPerInstance { + pub source_path: Option, + pub ignore_unknown_instances: bool, +} pub struct RbxSession { tree: RbxTree, @@ -45,7 +45,7 @@ pub struct RbxSession { // TODO: Hold this map, inverted from metadata_per_path // metadata_per_instance: PathMap, - instance_metadata_map: HashMap, + instance_metadata_map: HashMap, message_queue: Arc>, imfs: Arc>, } @@ -182,7 +182,7 @@ impl RbxSession { &self.tree } - pub fn get_instance_metadata(&self, id: RbxId) -> Option<&InstanceProjectNodeMetadata> { + pub fn get_instance_metadata(&self, id: RbxId) -> Option<&MetadataPerInstance> { self.instance_metadata_map.get(&id) } @@ -201,7 +201,7 @@ fn reify_initial_tree( project: &Project, imfs: &Imfs, metadata_per_path: &mut PathMap, - instance_metadata_map: &mut HashMap, + instance_metadata_map: &mut HashMap, ) -> RbxTree { let mut meta = SnapshotMetadata { metadata_per_path, diff --git a/server/src/rbx_snapshot.rs b/server/src/rbx_snapshot.rs index 76cc63c5..191b8544 100644 --- a/server/src/rbx_snapshot.rs +++ b/server/src/rbx_snapshot.rs @@ -30,7 +30,7 @@ use crate::{ }, path_map::PathMap, // TODO: Move MetadataPerPath into this module? - rbx_session::MetadataPerPath, + rbx_session::{MetadataPerPath, MetadataPerInstance}, }; const INIT_MODULE_NAME: &str = "init.lua"; @@ -120,8 +120,10 @@ fn snapshot_instance_node<'source>( name: instance_name, properties: node.properties.clone(), children, - source_path: None, - metadata: Some(node.metadata.clone()), + metadata: MetadataPerInstance { + source_path: None, + ignore_unknown_instances: node.metadata.ignore_unknown_instances, + }, })) } @@ -202,8 +204,10 @@ fn snapshot_imfs_directory<'source>( name: snapshot_name, properties: HashMap::new(), children: Vec::new(), - source_path: Some(directory.path.to_owned()), - metadata: None, + metadata: MetadataPerInstance { + source_path: Some(directory.path.to_owned()), + ignore_unknown_instances: false, + }, } }; @@ -285,8 +289,10 @@ fn snapshot_lua_file<'source>( }, }, children: Vec::new(), - metadata: None, - source_path: Some(file.path.to_path_buf()), + metadata: MetadataPerInstance { + source_path: Some(file.path.to_path_buf()), + ignore_unknown_instances: false, + }, })) } @@ -321,8 +327,10 @@ fn snapshot_txt_file<'source>( }, }, children: Vec::new(), - metadata: None, - source_path: Some(file.path.to_path_buf()), + metadata: MetadataPerInstance { + source_path: Some(file.path.to_path_buf()), + ignore_unknown_instances: false, + }, })) } @@ -352,8 +360,10 @@ fn snapshot_csv_file<'source>( }, }, children: Vec::new(), - metadata: None, - source_path: Some(file.path.to_path_buf()), + metadata: MetadataPerInstance { + source_path: Some(file.path.to_path_buf()), + ignore_unknown_instances: false, + }, })) } diff --git a/server/src/snapshot_reconciler.rs b/server/src/snapshot_reconciler.rs index 5f482733..201c1ef0 100644 --- a/server/src/snapshot_reconciler.rs +++ b/server/src/snapshot_reconciler.rs @@ -11,8 +11,7 @@ use serde_derive::{Serialize, Deserialize}; use crate::{ path_map::PathMap, - rbx_session::MetadataPerPath, - project::InstanceProjectNodeMetadata, + rbx_session::{MetadataPerPath, MetadataPerInstance}, }; #[derive(Debug, Clone, Default, Serialize, Deserialize)] @@ -63,10 +62,7 @@ pub struct RbxSnapshotInstance<'a> { pub class_name: Cow<'a, str>, pub properties: HashMap, pub children: Vec>, - - // TODO: Replace source_path and metadata with MetadataPerInstance - pub source_path: Option, - pub metadata: Option, + pub metadata: MetadataPerInstance, } pub fn snapshot_from_tree(tree: &RbxTree, id: RbxId) -> Option> { @@ -82,29 +78,29 @@ pub fn snapshot_from_tree(tree: &RbxTree, id: RbxId) -> Option, - instance_metadata_map: &mut HashMap, + instance_metadata_map: &mut HashMap, changes: &mut InstanceChanges, ) -> RbxTree { let instance = reify_core(snapshot); let mut tree = RbxTree::new(instance); let root_id = tree.get_root_id(); - if let Some(source_path) = &snapshot.source_path { + if let Some(source_path) = &snapshot.metadata.source_path { let path_meta = metadata_per_path.entry(source_path.to_owned()).or_default(); path_meta.instance_id = Some(root_id); } - if let Some(metadata) = &snapshot.metadata { - instance_metadata_map.insert(root_id, metadata.clone()); - } + instance_metadata_map.insert(root_id, snapshot.metadata.clone()); changes.added.insert(root_id); @@ -120,20 +116,18 @@ pub fn reify_subtree( tree: &mut RbxTree, parent_id: RbxId, metadata_per_path: &mut PathMap, - instance_metadata_map: &mut HashMap, + instance_metadata_map: &mut HashMap, changes: &mut InstanceChanges, ) { let instance = reify_core(snapshot); let id = tree.insert_instance(instance, parent_id); - if let Some(source_path) = &snapshot.source_path { + if let Some(source_path) = &snapshot.metadata.source_path { let path_meta = metadata_per_path.entry(source_path.clone()).or_default(); path_meta.instance_id = Some(id); } - if let Some(metadata) = &snapshot.metadata { - instance_metadata_map.insert(id, metadata.clone()); - } + instance_metadata_map.insert(id, snapshot.metadata.clone()); changes.added.insert(id); @@ -147,17 +141,15 @@ pub fn reconcile_subtree( id: RbxId, snapshot: &RbxSnapshotInstance, metadata_per_path: &mut PathMap, - instance_metadata_map: &mut HashMap, + instance_metadata_map: &mut HashMap, changes: &mut InstanceChanges, ) { - if let Some(source_path) = &snapshot.source_path { + if let Some(source_path) = &snapshot.metadata.source_path { let path_meta = metadata_per_path.entry(source_path.to_owned()).or_default(); path_meta.instance_id = Some(id); } - if let Some(metadata) = &snapshot.metadata { - instance_metadata_map.insert(id, metadata.clone()); - } + instance_metadata_map.insert(id, snapshot.metadata.clone()); if reconcile_instance_properties(tree.get_instance_mut(id).unwrap(), snapshot) { changes.updated.insert(id); @@ -244,7 +236,7 @@ fn reconcile_instance_children( id: RbxId, snapshot: &RbxSnapshotInstance, metadata_per_path: &mut PathMap, - instance_metadata_map: &mut HashMap, + instance_metadata_map: &mut HashMap, changes: &mut InstanceChanges, ) { let mut visited_snapshot_indices = HashSet::new(); diff --git a/server/src/web.rs b/server/src/web.rs index 2ff1a92c..2bf8c0f9 100644 --- a/server/src/web.rs +++ b/server/src/web.rs @@ -20,8 +20,23 @@ use crate::{ project::InstanceProjectNodeMetadata, snapshot_reconciler::InstanceChanges, visualize::{VisualizeRbxSession, VisualizeImfs, graphviz_to_svg}, + rbx_session::{MetadataPerInstance}, }; +/// Contains the instance metadata relevant to Rojo clients. +#[derive(Debug, Serialize, Deserialize)] +pub struct InstanceMetadata { + ignore_unknown_instances: bool, +} + +impl InstanceMetadata { + fn from_session_metadata(meta: &MetadataPerInstance) -> InstanceMetadata { + InstanceMetadata { + ignore_unknown_instances: meta.ignore_unknown_instances, + } + } +} + /// Used to attach metadata specific to Rojo to instances, which come from the /// rbx_tree crate. /// @@ -33,7 +48,7 @@ pub struct InstanceWithMetadata<'a> { pub instance: Cow<'a, RbxInstance>, #[serde(rename = "Metadata")] - pub metadata: Option>, + pub metadata: Option, } #[derive(Debug, Serialize, Deserialize)] @@ -163,7 +178,7 @@ impl Server { for &requested_id in &requested_ids { if let Some(instance) = tree.get_instance(requested_id) { let metadata = rbx_session.get_instance_metadata(requested_id) - .map(Cow::Borrowed); + .map(InstanceMetadata::from_session_metadata); instances.insert(instance.get_id(), InstanceWithMetadata { instance: Cow::Borrowed(instance), @@ -172,7 +187,7 @@ impl Server { for descendant in tree.descendants(requested_id) { let descendant_meta = rbx_session.get_instance_metadata(descendant.get_id()) - .map(Cow::Borrowed); + .map(InstanceMetadata::from_session_metadata); instances.insert(descendant.get_id(), InstanceWithMetadata { instance: Cow::Borrowed(descendant),