Build production-ready editor authoring workflows
CI / Format, lint, test, build (push) Waiting to run
CI / Format, lint, test, build (push) Waiting to run
This commit is contained in:
1 parent
cc1fa6ea3b
commit
0553a85220
163 files changed
+34547
-1565
No files matched your search
@@ -2,7 +2,7 @@
|
||||
|
||||
use bevy::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Stable asset identity for dependency tracking and prefab references.
|
||||
@@ -59,6 +59,8 @@ pub struct ImportSettings {
|
||||
pub material_policy: MaterialImportPolicy,
|
||||
#[serde(default)]
|
||||
pub static_mesh_manifest_path: Option<String>,
|
||||
#[serde(default)]
|
||||
pub animation_manifest_path: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for ImportSettings {
|
||||
@@ -71,6 +73,7 @@ impl Default for ImportSettings {
|
||||
hierarchy_mode: ModelHierarchyMode::default(),
|
||||
material_policy: MaterialImportPolicy::default(),
|
||||
static_mesh_manifest_path: None,
|
||||
animation_manifest_path: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -115,6 +118,11 @@ fn sync_registry_from_browser(
|
||||
.drain(..)
|
||||
.map(|record| (record.path.clone(), record))
|
||||
.collect();
|
||||
let current_paths = assets
|
||||
.assets
|
||||
.iter()
|
||||
.filter_map(|asset| asset.path.clone())
|
||||
.collect::<HashSet<_>>();
|
||||
|
||||
let mut next_records = Vec::new();
|
||||
let mut seen_paths = HashMap::new();
|
||||
@@ -129,7 +137,18 @@ fn sync_registry_from_browser(
|
||||
seen_paths.insert(path.clone(), ());
|
||||
|
||||
let kind_tag = format!("{:?}", asset.kind);
|
||||
let mut record = if let Some(mut prior) = existing.remove(&path) {
|
||||
let mut record = if let Some(mut prior) = existing.remove(&path).or_else(|| {
|
||||
take_uniquely_moved_model_record(&mut existing, ¤t_paths, &path, &kind_tag)
|
||||
}) {
|
||||
if prior.path != path {
|
||||
info!(
|
||||
"Asset registry preserved model identity {} across move {} -> {}",
|
||||
prior.id.as_string(),
|
||||
prior.path,
|
||||
path
|
||||
);
|
||||
prior.path = path.clone();
|
||||
}
|
||||
prior.label = asset.label.clone();
|
||||
prior.kind_tag = kind_tag;
|
||||
prior
|
||||
@@ -144,9 +163,9 @@ fn sync_registry_from_browser(
|
||||
}
|
||||
};
|
||||
if record.kind_tag == "Model" {
|
||||
if let Err(error) = super::static_mesh::refresh_static_mesh_artifact(&mut record) {
|
||||
if let Err(error) = super::refresh_model_artifacts(&mut record) {
|
||||
warn!(
|
||||
"Static mesh processor failed for {} (asset id {}): {error}",
|
||||
"Model artifact refresh failed for {} (asset id {}): {error}",
|
||||
record.path,
|
||||
record.id.as_string()
|
||||
);
|
||||
@@ -170,6 +189,34 @@ fn sync_registry_from_browser(
|
||||
}
|
||||
}
|
||||
|
||||
fn take_uniquely_moved_model_record(
|
||||
existing: &mut HashMap<String, AssetRecord>,
|
||||
current_paths: &HashSet<String>,
|
||||
new_path: &str,
|
||||
kind_tag: &str,
|
||||
) -> Option<AssetRecord> {
|
||||
if kind_tag != "Model" {
|
||||
return None;
|
||||
}
|
||||
let bytes = std::fs::read(new_path).ok()?;
|
||||
let content_hash = blake3::hash(&bytes).to_hex().to_string();
|
||||
let candidates = existing
|
||||
.iter()
|
||||
.filter_map(|(old_path, record)| {
|
||||
if current_paths.contains(old_path) || record.kind_tag != "Model" {
|
||||
return None;
|
||||
}
|
||||
let manifest_path = record.import_settings.animation_manifest_path.as_deref()?;
|
||||
let manifest = super::animation::load_animation_manifest(manifest_path).ok()?;
|
||||
(manifest.source.fingerprint.content_hash == content_hash).then(|| old_path.clone())
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if candidates.len() != 1 {
|
||||
return None;
|
||||
}
|
||||
existing.remove(&candidates[0])
|
||||
}
|
||||
|
||||
pub fn find_asset_by_path(registry: &AssetRegistry, path: &str) -> Option<AssetRecord> {
|
||||
registry
|
||||
.records
|
||||
@@ -196,6 +243,34 @@ pub fn find_asset_mut_by_path<'a>(
|
||||
.find(|record| record.path == path)
|
||||
}
|
||||
|
||||
pub fn ensure_asset_record(
|
||||
registry: &mut AssetRegistry,
|
||||
path: impl Into<String>,
|
||||
label: impl Into<String>,
|
||||
kind_tag: impl Into<String>,
|
||||
) -> Result<AssetRecord, String> {
|
||||
let path = path.into();
|
||||
if let Some(record) = find_asset_by_path(registry, &path) {
|
||||
return Ok(record);
|
||||
}
|
||||
let record = AssetRecord {
|
||||
id: AssetId::new(),
|
||||
path,
|
||||
label: label.into(),
|
||||
kind_tag: kind_tag.into(),
|
||||
import_settings: ImportSettings::default(),
|
||||
dependencies: Vec::new(),
|
||||
};
|
||||
registry.records.push(record.clone());
|
||||
registry.index_dirty = true;
|
||||
if let Err(error) = save_registry(registry) {
|
||||
registry.records.pop();
|
||||
return Err(error);
|
||||
}
|
||||
registry.index_dirty = false;
|
||||
Ok(record)
|
||||
}
|
||||
|
||||
pub fn update_import_settings(
|
||||
registry: &mut AssetRegistry,
|
||||
path: &str,
|
||||
@@ -240,3 +315,98 @@ pub fn load_registry() -> AssetRegistry {
|
||||
Err(_) => AssetRegistry::default(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use shared::{
|
||||
AnimationManifest, AnimationManifestSource, AnimationSourceFingerprint,
|
||||
ANIMATION_MANIFEST_SCHEMA_VERSION,
|
||||
};
|
||||
|
||||
fn moved_model_fixture(bytes: &[u8]) -> (std::path::PathBuf, std::path::PathBuf, AssetRecord) {
|
||||
let key = Uuid::new_v4();
|
||||
let source_path = std::env::temp_dir().join(format!("blacksite-moved-model-{key}.glb"));
|
||||
let manifest_path =
|
||||
std::env::temp_dir().join(format!("blacksite-moved-model-{key}.animation.ron"));
|
||||
std::fs::write(&source_path, bytes).unwrap();
|
||||
let id = AssetId::new();
|
||||
let old_path = format!("assets/models/old-{key}.glb");
|
||||
let manifest = AnimationManifest {
|
||||
schema_version: ANIMATION_MANIFEST_SCHEMA_VERSION,
|
||||
asset_id: id.as_string(),
|
||||
label: "Moved Model".into(),
|
||||
source: AnimationManifestSource {
|
||||
path: old_path.clone(),
|
||||
format: "glb".into(),
|
||||
fingerprint: AnimationSourceFingerprint {
|
||||
byte_len: bytes.len() as u64,
|
||||
modified_unix_secs: 0,
|
||||
content_hash: blake3::hash(bytes).to_hex().to_string(),
|
||||
},
|
||||
dependencies: Vec::new(),
|
||||
},
|
||||
runtime_supported: true,
|
||||
skeletons: Vec::new(),
|
||||
clips: Vec::new(),
|
||||
diagnostics: Vec::new(),
|
||||
};
|
||||
std::fs::write(
|
||||
&manifest_path,
|
||||
ron::ser::to_string_pretty(&manifest, ron::ser::PrettyConfig::default()).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let record = AssetRecord {
|
||||
id,
|
||||
path: old_path,
|
||||
label: "Moved Model".into(),
|
||||
kind_tag: "Model".into(),
|
||||
import_settings: ImportSettings {
|
||||
animation_manifest_path: Some(manifest_path.to_string_lossy().into_owned()),
|
||||
..Default::default()
|
||||
},
|
||||
dependencies: Vec::new(),
|
||||
};
|
||||
(source_path, manifest_path, record)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unique_content_match_preserves_model_record_across_move() {
|
||||
let (source_path, manifest_path, record) = moved_model_fixture(b"stable model bytes");
|
||||
let expected_id = record.id.clone();
|
||||
let mut existing = HashMap::from([(record.path.clone(), record)]);
|
||||
|
||||
let moved = take_uniquely_moved_model_record(
|
||||
&mut existing,
|
||||
&HashSet::new(),
|
||||
&source_path.to_string_lossy(),
|
||||
"Model",
|
||||
)
|
||||
.expect("unique moved model should retain its registry record");
|
||||
|
||||
assert_eq!(moved.id, expected_id);
|
||||
assert!(existing.is_empty());
|
||||
let _ = std::fs::remove_file(source_path);
|
||||
let _ = std::fs::remove_file(manifest_path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn existing_source_path_is_not_reconciled_as_a_copy_move() {
|
||||
let (source_path, manifest_path, record) = moved_model_fixture(b"copied model bytes");
|
||||
let old_path = record.path.clone();
|
||||
let mut existing = HashMap::from([(old_path.clone(), record)]);
|
||||
let current_paths = HashSet::from([old_path]);
|
||||
|
||||
let moved = take_uniquely_moved_model_record(
|
||||
&mut existing,
|
||||
¤t_paths,
|
||||
&source_path.to_string_lossy(),
|
||||
"Model",
|
||||
);
|
||||
|
||||
assert!(moved.is_none());
|
||||
assert_eq!(existing.len(), 1);
|
||||
let _ = std::fs::remove_file(source_path);
|
||||
let _ = std::fs::remove_file(manifest_path);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user