Add collaborative authored-file safety
This commit is contained in:
1 parent
b4aa61e394
commit
5a82a9ee29
23 files changed
+2857
-89
No files matched your search
@@ -20,6 +20,10 @@ use crate::assets::{
|
||||
AssetSelection, AssetSubAssetKind, EditorAsset, EditorAssetKind, EditorAssets, ASSETS_ROOT,
|
||||
BUILTINS_FOLDER,
|
||||
};
|
||||
use crate::project::collaboration::{
|
||||
file_status_indicator_ui, publish_authored_file, CollaborationState, FileSnapshot,
|
||||
FileWriteIntent,
|
||||
};
|
||||
use crate::scene_io::{SceneIo, SceneIoRequest};
|
||||
|
||||
use super::state::{
|
||||
@@ -1857,7 +1861,7 @@ fn top_level_asset_details_panel(
|
||||
selected_entities: &SelectedEntities,
|
||||
asset: &EditorAsset,
|
||||
) {
|
||||
asset_details_header(ui, asset);
|
||||
asset_details_header(world, ui, asset);
|
||||
ui.add_space(8.0);
|
||||
detail_row(ui, "Path", asset.path.as_deref().unwrap_or("Built-in"));
|
||||
detail_row(ui, "Folder", asset.folder_path.as_str());
|
||||
@@ -1946,7 +1950,7 @@ fn top_level_asset_details_panel(
|
||||
asset_action_buttons(world, ui, selected_entities, asset);
|
||||
}
|
||||
|
||||
fn asset_details_header(ui: &mut egui::Ui, asset: &EditorAsset) {
|
||||
fn asset_details_header(world: &World, ui: &mut egui::Ui, asset: &EditorAsset) {
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
ui.label(
|
||||
egui::RichText::new(kind_icon(&asset.kind).as_str())
|
||||
@@ -1959,6 +1963,13 @@ fn asset_details_header(ui: &mut egui::Ui, asset: &EditorAsset) {
|
||||
ui.vertical(|ui| {
|
||||
ui.strong(asset.label.as_str());
|
||||
ui.small(egui::RichText::new(kind_label(&asset.kind)).color(TEXT_DIM));
|
||||
if let (Some(state), Some(path)) = (
|
||||
world.get_resource::<CollaborationState>(),
|
||||
asset.path.as_deref(),
|
||||
) {
|
||||
let status = state.file_status(Path::new(path));
|
||||
let _ = file_status_indicator_ui(ui, &status, Path::new(path));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -2434,10 +2445,19 @@ fn ensure_material_draft(world: &mut World, path: &str, fallback_label: &str) {
|
||||
if !needs_load {
|
||||
return;
|
||||
}
|
||||
let draft = match MaterialAsset::load_from_path(path) {
|
||||
Ok(asset) => MaterialAssetDraft {
|
||||
let loaded = fs::read_to_string(path)
|
||||
.map_err(|error| format!("could not read {path}: {error}"))
|
||||
.and_then(|text| {
|
||||
let snapshot = FileSnapshot::from_loaded_bytes(Path::new(path), text.as_bytes());
|
||||
ron::from_str::<MaterialAsset>(&text)
|
||||
.map(|asset| (asset, snapshot))
|
||||
.map_err(|error| format!("invalid material RON in {path}: {error}"))
|
||||
});
|
||||
let draft = match loaded {
|
||||
Ok((asset, disk_snapshot)) => MaterialAssetDraft {
|
||||
path: path.to_string(),
|
||||
asset,
|
||||
disk_snapshot,
|
||||
error: None,
|
||||
},
|
||||
Err(error) => MaterialAssetDraft {
|
||||
@@ -2450,6 +2470,8 @@ fn ensure_material_draft(world: &mut World, path: &str, fallback_label: &str) {
|
||||
render_state: shared::MaterialRenderState::default(),
|
||||
material: MaterialDesc::default(),
|
||||
},
|
||||
disk_snapshot: FileSnapshot::capture(Path::new(path))
|
||||
.unwrap_or_else(|_| FileSnapshot::missing()),
|
||||
error: Some(error),
|
||||
},
|
||||
};
|
||||
@@ -2467,17 +2489,23 @@ fn save_material_draft(world: &mut World) {
|
||||
let result = ron::ser::to_string_pretty(&draft.asset, ron::ser::PrettyConfig::default())
|
||||
.map_err(|error| format!("could not serialize material {}: {error}", draft.path))
|
||||
.and_then(|text| {
|
||||
fs::write(&draft.path, text)
|
||||
.map_err(|error| format!("could not write material {}: {error}", draft.path))
|
||||
publish_authored_file(
|
||||
world,
|
||||
Path::new(&draft.path),
|
||||
text.as_bytes(),
|
||||
&draft.disk_snapshot,
|
||||
FileWriteIntent::Material,
|
||||
)
|
||||
});
|
||||
match result {
|
||||
Ok(()) => {
|
||||
Ok(disk_snapshot) => {
|
||||
if let Some(current) = world
|
||||
.resource_mut::<AssetBrowserUiState>()
|
||||
.material_draft
|
||||
.as_mut()
|
||||
{
|
||||
current.error = None;
|
||||
current.disk_snapshot = disk_snapshot;
|
||||
}
|
||||
world.resource_mut::<EditorAssets>().refresh();
|
||||
invalidate_on_catalog_refresh(world);
|
||||
@@ -2556,8 +2584,13 @@ fn create_material_instance_from_base(world: &mut World, base_path: &str, base_l
|
||||
let result = ron::ser::to_string_pretty(&instance, ron::ser::PrettyConfig::default())
|
||||
.map_err(|error| format!("could not serialize material instance: {error}"))
|
||||
.and_then(|text| {
|
||||
fs::write(&path, text)
|
||||
.map_err(|error| format!("could not write {}: {error}", path.display()))
|
||||
publish_authored_file(
|
||||
world,
|
||||
&path,
|
||||
text.as_bytes(),
|
||||
&FileSnapshot::missing(),
|
||||
FileWriteIntent::MaterialInstance,
|
||||
)
|
||||
});
|
||||
if let Err(error) = result {
|
||||
world.resource_mut::<SceneIo>().status = error;
|
||||
@@ -2845,10 +2878,19 @@ fn ensure_material_instance_draft(world: &mut World, path: &str, fallback_label:
|
||||
if !needs_load {
|
||||
return;
|
||||
}
|
||||
let draft = match MaterialInstanceAsset::load_from_path(path) {
|
||||
Ok(asset) => MaterialInstanceAssetDraft {
|
||||
let loaded = fs::read_to_string(path)
|
||||
.map_err(|error| format!("could not read {path}: {error}"))
|
||||
.and_then(|text| {
|
||||
let snapshot = FileSnapshot::from_loaded_bytes(Path::new(path), text.as_bytes());
|
||||
ron::from_str::<MaterialInstanceAsset>(&text)
|
||||
.map(|asset| (asset, snapshot))
|
||||
.map_err(|error| format!("invalid material-instance RON in {path}: {error}"))
|
||||
});
|
||||
let draft = match loaded {
|
||||
Ok((asset, disk_snapshot)) => MaterialInstanceAssetDraft {
|
||||
path: path.to_string(),
|
||||
asset,
|
||||
disk_snapshot,
|
||||
error: None,
|
||||
},
|
||||
Err(error) => MaterialInstanceAssetDraft {
|
||||
@@ -2860,6 +2902,8 @@ fn ensure_material_instance_draft(world: &mut World, path: &str, fallback_label:
|
||||
parameters: Vec::new(),
|
||||
textures: Vec::new(),
|
||||
},
|
||||
disk_snapshot: FileSnapshot::capture(Path::new(path))
|
||||
.unwrap_or_else(|_| FileSnapshot::missing()),
|
||||
error: Some(error),
|
||||
},
|
||||
};
|
||||
@@ -2884,18 +2928,24 @@ fn save_material_instance_draft(world: &mut World) {
|
||||
ron::ser::to_string_pretty(&draft.asset, ron::ser::PrettyConfig::default())
|
||||
.map_err(|error| format!("could not serialize material instance: {error}"))
|
||||
.and_then(|text| {
|
||||
fs::write(&draft.path, text)
|
||||
.map_err(|error| format!("could not write {}: {error}", draft.path))
|
||||
publish_authored_file(
|
||||
world,
|
||||
Path::new(&draft.path),
|
||||
text.as_bytes(),
|
||||
&draft.disk_snapshot,
|
||||
FileWriteIntent::MaterialInstance,
|
||||
)
|
||||
})
|
||||
};
|
||||
match result {
|
||||
Ok(()) => {
|
||||
Ok(disk_snapshot) => {
|
||||
if let Some(current) = world
|
||||
.resource_mut::<AssetBrowserUiState>()
|
||||
.material_instance_draft
|
||||
.as_mut()
|
||||
{
|
||||
current.error = None;
|
||||
current.disk_snapshot = disk_snapshot;
|
||||
}
|
||||
world.resource_mut::<EditorAssets>().refresh();
|
||||
invalidate_on_catalog_refresh(world);
|
||||
@@ -4380,10 +4430,181 @@ fn dedup_strings(values: &mut Vec<String>) {
|
||||
values.retain(|value| seen.insert(value.clone()));
|
||||
}
|
||||
|
||||
pub(crate) fn validate_material_conflict_destination(
|
||||
world: &World,
|
||||
destination: &Path,
|
||||
) -> Result<String, String> {
|
||||
let root = PathBuf::from(&world.resource::<crate::project_io::ProjectWorkspace>().root);
|
||||
let root = root.canonicalize().unwrap_or(root);
|
||||
let absolute = if destination.is_absolute() {
|
||||
destination.to_path_buf()
|
||||
} else {
|
||||
root.join(destination)
|
||||
};
|
||||
let absolute = absolute
|
||||
.parent()
|
||||
.and_then(|parent| parent.canonicalize().ok())
|
||||
.and_then(|parent| destination.file_name().map(|name| parent.join(name)))
|
||||
.unwrap_or(absolute);
|
||||
let relative = absolute.strip_prefix(&root).map_err(|_| {
|
||||
format!(
|
||||
"Material assets must stay inside {}",
|
||||
root.join("assets").display()
|
||||
)
|
||||
})?;
|
||||
if !relative.starts_with("assets") {
|
||||
return Err(format!(
|
||||
"Material assets must stay inside {}",
|
||||
root.join("assets").display()
|
||||
));
|
||||
}
|
||||
Ok(relative.to_string_lossy().replace('\\', "/"))
|
||||
}
|
||||
|
||||
pub(crate) fn reload_material_after_file_conflict(
|
||||
world: &mut World,
|
||||
path: &Path,
|
||||
intent: &FileWriteIntent,
|
||||
) -> Result<String, String> {
|
||||
let path = path.to_string_lossy();
|
||||
{
|
||||
let mut state = world.resource_mut::<AssetBrowserUiState>();
|
||||
match intent {
|
||||
FileWriteIntent::Material => {
|
||||
if state
|
||||
.material_draft
|
||||
.as_ref()
|
||||
.is_some_and(|draft| draft.path == path)
|
||||
{
|
||||
state.material_draft = None;
|
||||
}
|
||||
}
|
||||
FileWriteIntent::MaterialInstance => {
|
||||
if state
|
||||
.material_instance_draft
|
||||
.as_ref()
|
||||
.is_some_and(|draft| draft.path == path)
|
||||
{
|
||||
state.material_instance_draft = None;
|
||||
}
|
||||
}
|
||||
_ => return Err("the conflict is not an editable material document".into()),
|
||||
}
|
||||
}
|
||||
world.resource_mut::<EditorAssets>().refresh();
|
||||
invalidate_on_catalog_refresh(world);
|
||||
Ok(format!("Reloaded {} from disk", path))
|
||||
}
|
||||
|
||||
pub(crate) fn adopt_material_conflict_save_as(
|
||||
world: &mut World,
|
||||
original_path: &Path,
|
||||
catalog_path: String,
|
||||
disk_snapshot: FileSnapshot,
|
||||
intent: &FileWriteIntent,
|
||||
) -> Result<String, String> {
|
||||
let original_path = original_path.to_string_lossy();
|
||||
{
|
||||
let mut state = world.resource_mut::<AssetBrowserUiState>();
|
||||
match intent {
|
||||
FileWriteIntent::Material => {
|
||||
if let Some(draft) = state
|
||||
.material_draft
|
||||
.as_mut()
|
||||
.filter(|draft| draft.path == original_path)
|
||||
{
|
||||
draft.path.clone_from(&catalog_path);
|
||||
draft.disk_snapshot = disk_snapshot;
|
||||
draft.error = None;
|
||||
} else {
|
||||
let asset = MaterialAsset::load_from_path(&catalog_path)?;
|
||||
state.material_draft = Some(MaterialAssetDraft {
|
||||
path: catalog_path.clone(),
|
||||
asset,
|
||||
disk_snapshot,
|
||||
error: None,
|
||||
});
|
||||
}
|
||||
}
|
||||
FileWriteIntent::MaterialInstance => {
|
||||
if let Some(draft) = state
|
||||
.material_instance_draft
|
||||
.as_mut()
|
||||
.filter(|draft| draft.path == original_path)
|
||||
{
|
||||
draft.path.clone_from(&catalog_path);
|
||||
draft.disk_snapshot = disk_snapshot;
|
||||
draft.error = None;
|
||||
} else {
|
||||
let asset = MaterialInstanceAsset::load_from_path(&catalog_path)?;
|
||||
state.material_instance_draft = Some(MaterialInstanceAssetDraft {
|
||||
path: catalog_path.clone(),
|
||||
asset,
|
||||
disk_snapshot,
|
||||
error: None,
|
||||
});
|
||||
}
|
||||
}
|
||||
_ => return Err("the conflict is not an editable material document".into()),
|
||||
}
|
||||
}
|
||||
{
|
||||
let mut assets = world.resource_mut::<EditorAssets>();
|
||||
assets.refresh();
|
||||
assets.select(AssetSelection::File(catalog_path.clone()));
|
||||
}
|
||||
invalidate_on_catalog_refresh(world);
|
||||
Ok(format!("Saved material copy to {catalog_path}"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn material_apply_refuses_an_external_revision() {
|
||||
let root = std::env::temp_dir().join(format!(
|
||||
"blacksite-material-external-write-{}",
|
||||
uuid::Uuid::new_v4()
|
||||
));
|
||||
let path = root.join("assets/materials/test.ron");
|
||||
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
fs::write(&path, b"loaded").unwrap();
|
||||
let disk_snapshot = FileSnapshot::capture(&path).unwrap();
|
||||
let draft = MaterialAssetDraft {
|
||||
path: path.to_string_lossy().into_owned(),
|
||||
asset: MaterialAsset {
|
||||
schema_version: shared::MATERIAL_ASSET_SCHEMA_VERSION,
|
||||
label: "Test".into(),
|
||||
shader: None,
|
||||
shader_ref: None,
|
||||
render_state: shared::MaterialRenderState::default(),
|
||||
material: MaterialDesc::default(),
|
||||
},
|
||||
disk_snapshot,
|
||||
error: None,
|
||||
};
|
||||
let state = AssetBrowserUiState {
|
||||
material_draft: Some(draft),
|
||||
..Default::default()
|
||||
};
|
||||
let mut world = World::new();
|
||||
world.insert_resource(state);
|
||||
world.insert_resource(SceneIo::default());
|
||||
fs::write(&path, b"external").unwrap();
|
||||
|
||||
save_material_draft(&mut world);
|
||||
|
||||
assert_eq!(fs::read(&path).unwrap(), b"external");
|
||||
assert!(world
|
||||
.resource::<AssetBrowserUiState>()
|
||||
.material_draft
|
||||
.as_ref()
|
||||
.and_then(|draft| draft.error.as_deref())
|
||||
.is_some_and(|error| error.contains("changed outside Blacksite")));
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn audio_filter_matches_only_audio_clips() {
|
||||
let audio = EditorAsset {
|
||||
|
||||
@@ -7,6 +7,7 @@ use shared::{MaterialAsset, MaterialInstanceAsset};
|
||||
|
||||
use crate::asset_db::ImportSettings;
|
||||
use crate::assets::{AssetSelection, ASSETS_ROOT, BUILTINS_FOLDER};
|
||||
use crate::project::collaboration::FileSnapshot;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum AssetBrowserView {
|
||||
@@ -69,6 +70,7 @@ pub(crate) struct ImportSettingsDraft {
|
||||
pub(crate) struct MaterialAssetDraft {
|
||||
pub(crate) path: String,
|
||||
pub(crate) asset: MaterialAsset,
|
||||
pub(crate) disk_snapshot: FileSnapshot,
|
||||
pub(crate) error: Option<String>,
|
||||
}
|
||||
|
||||
@@ -76,6 +78,7 @@ pub(crate) struct MaterialAssetDraft {
|
||||
pub(crate) struct MaterialInstanceAssetDraft {
|
||||
pub(crate) path: String,
|
||||
pub(crate) asset: MaterialInstanceAsset,
|
||||
pub(crate) disk_snapshot: FileSnapshot,
|
||||
pub(crate) error: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user