Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meta 2 way sync #618

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
40 changes: 38 additions & 2 deletions src/change_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ use rbx_dom_weak::types::{Ref, Variant};

use crate::{
message_queue::MessageQueue,
resolution::UnresolvedValue,
snapshot::{
apply_patch_set, compute_patch_set, AppliedPatchSet, InstigatingSource, PatchSet, RojoTree,
},
snapshot_middleware::{snapshot_from_vfs, snapshot_project_node},
snapshot_middleware::{meta_file::AdjacentMetadata, snapshot_from_vfs, snapshot_project_node},
};

/// Processes file change events, updates the DOM, and sends those updates
Expand Down Expand Up @@ -193,6 +194,13 @@ impl JobThreadContext {
}

for (key, changed_value) in &update.changed_properties {
let mut meta_path = None;
for path in instance.metadata().relevant_paths.iter() {
if path.to_str().unwrap().ends_with(".meta.json") {
meta_path = Some(path);
break;
}
}
if key == "Source" {
if let Some(instigating_source) =
&instance.metadata().instigating_source
Expand All @@ -218,8 +226,36 @@ impl JobThreadContext {
id
);
}
} else if let Some(meta_path) = meta_path {
if let Some(meta_contents) =
self.vfs.read(&meta_path).with_not_found().unwrap()
{
let mut metadata =
AdjacentMetadata::from_slice(&meta_contents, meta_path.clone())
.unwrap();
metadata.properties.insert(
key.clone(),
UnresolvedValue::FullyQualified(
changed_value.as_ref().unwrap().clone(),
),
);
let data = serde_json::to_string_pretty(&metadata).unwrap();
fs::write(meta_path, data).unwrap();
} else {
let mut metadata = AdjacentMetadata::new(meta_path.clone());
metadata.properties.insert(
key.clone(),
UnresolvedValue::FullyQualified(
changed_value.as_ref().unwrap().clone(),
),
);
let data = serde_json::to_string_pretty(&metadata).unwrap();
fs::write(meta_path, data).unwrap();
}
} else {
log::warn!("Cannot change properties besides BaseScript.Source.");
log::warn!(
"Cannot change properties of instances with no meta support"
);
}
}
} else {
Expand Down
9 changes: 8 additions & 1 deletion src/snapshot_middleware/meta_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ pub struct AdjacentMetadata {
#[serde(skip)]
pub path: PathBuf,
}

impl AdjacentMetadata {
pub fn new(path: PathBuf) -> AdjacentMetadata {
AdjacentMetadata {
ignore_unknown_instances: None,
properties: HashMap::new(),
attributes: HashMap::new(),
path: path,
}
}
pub fn from_slice(slice: &[u8], path: PathBuf) -> anyhow::Result<Self> {
let mut meta: Self = serde_json::from_slice(slice).with_context(|| {
format!(
Expand Down
2 changes: 1 addition & 1 deletion src/snapshot_middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod dir;
mod json;
mod json_model;
mod lua;
mod meta_file;
pub mod meta_file;
mod project;
mod rbxm;
mod rbxmx;
Expand Down