forked from rojo-rbx/rojo
Actually generate AppliedPatchSet objects (#250)
* Start actually computing AppliedPatchSet values * Improve patch_apply documentation and flesh out applied patch code * Add file link notes * Stub out where tests for snapshot subsystem will go * Create baseline tests * Fix build failure by silencing Clippy
This commit is contained in:
committed by
GitHub
parent
a70b7ee150
commit
e741f7b557
@@ -5,10 +5,13 @@ use std::collections::HashMap;
|
||||
use rbx_dom_weak::{RbxId, RbxInstanceProperties, RbxValue};
|
||||
|
||||
use super::{
|
||||
patch::{AppliedPatchSet, PatchSet, PatchUpdate},
|
||||
patch::{AppliedPatchSet, AppliedPatchUpdate, PatchSet, PatchUpdate},
|
||||
InstancePropertiesWithMeta, InstanceSnapshot, RojoTree,
|
||||
};
|
||||
|
||||
/// Consumes the input `PatchSet`, applying all of its prescribed changes to the
|
||||
/// tree and returns an `AppliedPatchSet`, which can be used to keep another
|
||||
/// tree in sync with Rojo's.
|
||||
pub fn apply_patch_set(tree: &mut RojoTree, patch_set: PatchSet) -> AppliedPatchSet {
|
||||
let mut context = PatchApplyContext::default();
|
||||
|
||||
@@ -17,9 +20,11 @@ pub fn apply_patch_set(tree: &mut RojoTree, patch_set: PatchSet) -> AppliedPatch
|
||||
}
|
||||
|
||||
for add_patch in patch_set.added_instances {
|
||||
apply_add_child(&mut context, tree, add_patch.parent_id, &add_patch.instance);
|
||||
apply_add_child(&mut context, tree, add_patch.parent_id, add_patch.instance);
|
||||
}
|
||||
|
||||
// Updates need to be applied after additions, which reduces the complexity
|
||||
// of updates significantly.
|
||||
for update_patch in patch_set.updated_instances {
|
||||
apply_update_child(&mut context, tree, update_patch);
|
||||
}
|
||||
@@ -27,10 +32,45 @@ pub fn apply_patch_set(tree: &mut RojoTree, patch_set: PatchSet) -> AppliedPatch
|
||||
finalize_patch_application(context, tree)
|
||||
}
|
||||
|
||||
/// All of the ephemeral state needing during application of a patch.
|
||||
#[derive(Default)]
|
||||
struct PatchApplyContext {
|
||||
/// A map from transient snapshot IDs (generated by snapshot middleware) to
|
||||
/// instance IDs in the actual tree. These are both the same data type so
|
||||
/// that they fit into the same `RbxValue::Ref` type.
|
||||
///
|
||||
/// At this point in the patch process, IDs in instance properties have been
|
||||
/// partially translated from 'snapshot space' into 'tree space' by the
|
||||
/// patch computation process. An ID not existing in this map means either:
|
||||
///
|
||||
/// 1. The ID is already in tree space and refers to an instance that
|
||||
/// existed in the tree before this patch was applied.
|
||||
///
|
||||
/// 2. The ID if in snapshot space, but points to an instance that was not
|
||||
/// part of the snapshot that was put through the patch computation
|
||||
/// function.
|
||||
///
|
||||
/// #2 should not occur in well-formed projects, but is indistinguishable
|
||||
/// from #1 right now. It could happen if two model files try to reference
|
||||
/// eachother.
|
||||
snapshot_id_to_instance_id: HashMap<RbxId, RbxId>,
|
||||
properties_to_apply: HashMap<RbxId, HashMap<String, RbxValue>>,
|
||||
|
||||
/// The properties of instances added by the current `PatchSet`.
|
||||
///
|
||||
/// Instances added to the tree can refer to eachother via Ref properties,
|
||||
/// but we need to make sure they're correctly transformed from snapshot
|
||||
/// space into tree space (via `snapshot_id_to_instance_id`).
|
||||
///
|
||||
/// It's not possible to do that transformation for refs that refer to added
|
||||
/// instances until all the instances have actually been inserted into the
|
||||
/// tree. For simplicity, we defer application of _all_ properties on added
|
||||
/// instances instead of just Refs.
|
||||
///
|
||||
/// This doesn't affect updated instances, since they're always applied
|
||||
/// after we've added all the instances from the patch.
|
||||
added_instance_properties: HashMap<RbxId, HashMap<String, RbxValue>>,
|
||||
|
||||
/// The current applied patch result, describing changes made to the tree.
|
||||
applied_patch_set: AppliedPatchSet,
|
||||
}
|
||||
|
||||
@@ -45,22 +85,24 @@ struct PatchApplyContext {
|
||||
/// where we build up a map of snapshot IDs to instance IDs as they're created,
|
||||
/// then apply properties all at once at the end.
|
||||
fn finalize_patch_application(context: PatchApplyContext, tree: &mut RojoTree) -> AppliedPatchSet {
|
||||
for (id, mut properties) in context.properties_to_apply {
|
||||
for (id, properties) in context.added_instance_properties {
|
||||
// This should always succeed since instances marked as added in our
|
||||
// patch should be added without fail.
|
||||
let mut instance = tree
|
||||
.get_instance_mut(id)
|
||||
.expect("Invalid instance ID in deferred property map");
|
||||
|
||||
for property_value in properties.values_mut() {
|
||||
for (key, mut property_value) in properties {
|
||||
if let RbxValue::Ref { value: Some(id) } = property_value {
|
||||
if let Some(&instance_id) = context.snapshot_id_to_instance_id.get(id) {
|
||||
*property_value = RbxValue::Ref {
|
||||
if let Some(&instance_id) = context.snapshot_id_to_instance_id.get(&id) {
|
||||
property_value = RbxValue::Ref {
|
||||
value: Some(instance_id),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*instance.properties_mut() = properties;
|
||||
instance.properties_mut().insert(key, property_value);
|
||||
}
|
||||
}
|
||||
|
||||
context.applied_patch_set
|
||||
@@ -71,7 +113,7 @@ fn apply_remove_instance(context: &mut PatchApplyContext, tree: &mut RojoTree, r
|
||||
Some(_) => context.applied_patch_set.removed.push(removed_id),
|
||||
None => {
|
||||
log::warn!(
|
||||
"Patch application error: Tried to remove instance {} but it did not exist.",
|
||||
"Patch misapplication: Tried to remove instance {} but it did not exist.",
|
||||
removed_id
|
||||
);
|
||||
}
|
||||
@@ -82,50 +124,64 @@ fn apply_add_child(
|
||||
context: &mut PatchApplyContext,
|
||||
tree: &mut RojoTree,
|
||||
parent_id: RbxId,
|
||||
snapshot: &InstanceSnapshot,
|
||||
snapshot: InstanceSnapshot,
|
||||
) {
|
||||
let properties = InstancePropertiesWithMeta {
|
||||
properties: RbxInstanceProperties {
|
||||
name: snapshot.name.clone().into_owned(),
|
||||
class_name: snapshot.class_name.clone().into_owned(),
|
||||
name: snapshot.name.into_owned(),
|
||||
class_name: snapshot.class_name.into_owned(),
|
||||
|
||||
// Property assignment is deferred until after we know about all
|
||||
// instances in this patch.
|
||||
// instances in this patch. See `PatchApplyContext` for details.
|
||||
properties: HashMap::new(),
|
||||
},
|
||||
metadata: Default::default(), // TODO
|
||||
metadata: snapshot.metadata,
|
||||
};
|
||||
|
||||
let id = tree.insert_instance(properties, parent_id);
|
||||
|
||||
context.applied_patch_set.added.push(id);
|
||||
|
||||
context
|
||||
.properties_to_apply
|
||||
.insert(id, snapshot.properties.clone());
|
||||
.added_instance_properties
|
||||
.insert(id, snapshot.properties);
|
||||
|
||||
if let Some(snapshot_id) = snapshot.snapshot_id {
|
||||
context.snapshot_id_to_instance_id.insert(snapshot_id, id);
|
||||
}
|
||||
|
||||
for child_snapshot in &snapshot.children {
|
||||
for child_snapshot in snapshot.children {
|
||||
apply_add_child(context, tree, id, child_snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_update_child(context: &mut PatchApplyContext, tree: &mut RojoTree, patch: PatchUpdate) {
|
||||
let mut applied_patch = AppliedPatchUpdate::new(patch.id);
|
||||
|
||||
if let Some(metadata) = patch.changed_metadata {
|
||||
tree.update_metadata(patch.id, metadata);
|
||||
tree.update_metadata(patch.id, metadata.clone());
|
||||
applied_patch.changed_metadata = Some(metadata);
|
||||
}
|
||||
|
||||
let mut instance = tree
|
||||
.get_instance_mut(patch.id)
|
||||
.expect("Instance referred to by patch does not exist");
|
||||
let mut instance = match tree.get_instance_mut(patch.id) {
|
||||
Some(instance) => instance,
|
||||
None => {
|
||||
log::warn!(
|
||||
"Patch misapplication: Instance {}, referred to by update patch, did not exist.",
|
||||
patch.id
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(name) = patch.changed_name {
|
||||
*instance.name_mut() = name;
|
||||
*instance.name_mut() = name.clone();
|
||||
applied_patch.changed_name = Some(name);
|
||||
}
|
||||
|
||||
if let Some(class_name) = patch.changed_class_name {
|
||||
*instance.class_name_mut() = class_name;
|
||||
*instance.class_name_mut() = class_name.clone();
|
||||
applied_patch.changed_class_name = Some(class_name);
|
||||
}
|
||||
|
||||
for (key, property_entry) in patch.changed_properties {
|
||||
@@ -134,6 +190,10 @@ fn apply_update_child(context: &mut PatchApplyContext, tree: &mut RojoTree, patc
|
||||
// instance IDs if they referred to an instance that was created as
|
||||
// part of this patch.
|
||||
Some(RbxValue::Ref { value: Some(id) }) => {
|
||||
// If our ID is not found in this map, then it either refers to
|
||||
// an existing instance NOT added by this patch, or there was an
|
||||
// error. See `PatchApplyContext::snapshot_id_to_instance_id`
|
||||
// for more info.
|
||||
let new_id = context
|
||||
.snapshot_id_to_instance_id
|
||||
.get(&id)
|
||||
@@ -141,20 +201,24 @@ fn apply_update_child(context: &mut PatchApplyContext, tree: &mut RojoTree, patc
|
||||
.unwrap_or(id);
|
||||
|
||||
instance.properties_mut().insert(
|
||||
key,
|
||||
key.clone(),
|
||||
RbxValue::Ref {
|
||||
value: Some(new_id),
|
||||
},
|
||||
);
|
||||
}
|
||||
Some(value) => {
|
||||
instance.properties_mut().insert(key, value);
|
||||
Some(ref value) => {
|
||||
instance.properties_mut().insert(key.clone(), value.clone());
|
||||
}
|
||||
None => {
|
||||
instance.properties_mut().remove(&key);
|
||||
}
|
||||
}
|
||||
|
||||
applied_patch.changed_properties.insert(key, property_entry);
|
||||
}
|
||||
|
||||
context.applied_patch_set.updated.push(applied_patch)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user