This commit is contained in:
commit
f29712d158
259 files changed
+52069
No files matched your search
@@ -0,0 +1,257 @@
|
||||
//! Project asset registry (stable IDs + import metadata). Phase 5 foundation.
|
||||
|
||||
use bevy::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Stable asset identity for dependency tracking and prefab references.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
|
||||
pub struct AssetId(pub Uuid);
|
||||
|
||||
impl AssetId {
|
||||
pub fn new() -> Self {
|
||||
Self(Uuid::new_v4())
|
||||
}
|
||||
|
||||
pub fn as_string(&self) -> String {
|
||||
self.0.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AssetId {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum ModelPlacementMode {
|
||||
StaticAsset,
|
||||
SceneInstance,
|
||||
}
|
||||
|
||||
impl Default for ModelPlacementMode {
|
||||
fn default() -> Self {
|
||||
Self::StaticAsset
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum ModelHierarchyMode {
|
||||
SingleActor,
|
||||
SourceHierarchy,
|
||||
}
|
||||
|
||||
impl Default for ModelHierarchyMode {
|
||||
fn default() -> Self {
|
||||
Self::SingleActor
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum MaterialImportPolicy {
|
||||
SourceMaterials,
|
||||
AuthoringOverride,
|
||||
}
|
||||
|
||||
impl Default for MaterialImportPolicy {
|
||||
fn default() -> Self {
|
||||
Self::SourceMaterials
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct ImportSettings {
|
||||
pub scale: f32,
|
||||
pub generate_collider: bool,
|
||||
pub lod0_only: bool,
|
||||
#[serde(default)]
|
||||
pub placement_mode: ModelPlacementMode,
|
||||
#[serde(default)]
|
||||
pub hierarchy_mode: ModelHierarchyMode,
|
||||
#[serde(default)]
|
||||
pub material_policy: MaterialImportPolicy,
|
||||
#[serde(default)]
|
||||
pub static_mesh_manifest_path: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for ImportSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
scale: 1.0,
|
||||
generate_collider: true,
|
||||
lod0_only: true,
|
||||
placement_mode: ModelPlacementMode::default(),
|
||||
hierarchy_mode: ModelHierarchyMode::default(),
|
||||
material_policy: MaterialImportPolicy::default(),
|
||||
static_mesh_manifest_path: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct AssetRecord {
|
||||
pub id: AssetId,
|
||||
pub path: String,
|
||||
pub label: String,
|
||||
pub kind_tag: String,
|
||||
#[serde(default)]
|
||||
pub import_settings: ImportSettings,
|
||||
#[serde(default)]
|
||||
pub dependencies: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Resource, Debug, Default)]
|
||||
pub struct AssetRegistry {
|
||||
pub records: Vec<AssetRecord>,
|
||||
pub index_dirty: bool,
|
||||
}
|
||||
|
||||
pub struct AssetDbPlugin;
|
||||
|
||||
impl Plugin for AssetDbPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_resource(load_registry())
|
||||
.add_systems(Update, sync_registry_from_browser);
|
||||
}
|
||||
}
|
||||
|
||||
fn sync_registry_from_browser(
|
||||
assets: Res<crate::assets::EditorAssets>,
|
||||
mut registry: ResMut<AssetRegistry>,
|
||||
) {
|
||||
if !assets.is_changed() {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut existing: HashMap<String, AssetRecord> = registry
|
||||
.records
|
||||
.drain(..)
|
||||
.map(|record| (record.path.clone(), record))
|
||||
.collect();
|
||||
|
||||
let mut next_records = Vec::new();
|
||||
let mut seen_paths = HashMap::new();
|
||||
|
||||
for asset in &assets.assets {
|
||||
let Some(path) = asset.path.clone() else {
|
||||
continue;
|
||||
};
|
||||
if path.is_empty() || seen_paths.contains_key(&path) {
|
||||
continue;
|
||||
}
|
||||
seen_paths.insert(path.clone(), ());
|
||||
|
||||
let kind_tag = format!("{:?}", asset.kind);
|
||||
let mut record = if let Some(mut prior) = existing.remove(&path) {
|
||||
prior.label = asset.label.clone();
|
||||
prior.kind_tag = kind_tag;
|
||||
prior
|
||||
} else {
|
||||
AssetRecord {
|
||||
id: AssetId::new(),
|
||||
path: path.clone(),
|
||||
label: asset.label.clone(),
|
||||
kind_tag,
|
||||
import_settings: ImportSettings::default(),
|
||||
dependencies: Vec::new(),
|
||||
}
|
||||
};
|
||||
if record.kind_tag == "Model" {
|
||||
if let Err(error) = super::static_mesh::refresh_static_mesh_artifact(&mut record) {
|
||||
warn!(
|
||||
"Static mesh processor failed for {} (asset id {}): {error}",
|
||||
record.path,
|
||||
record.id.as_string()
|
||||
);
|
||||
}
|
||||
}
|
||||
next_records.push(record);
|
||||
}
|
||||
|
||||
let changed = registry.records != next_records;
|
||||
registry.records = next_records;
|
||||
if changed {
|
||||
registry.index_dirty = true;
|
||||
}
|
||||
|
||||
if registry.index_dirty {
|
||||
if let Err(error) = save_registry(®istry) {
|
||||
warn!("Asset registry save failed: {error}");
|
||||
} else {
|
||||
registry.index_dirty = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_asset_by_path(registry: &AssetRegistry, path: &str) -> Option<AssetRecord> {
|
||||
registry
|
||||
.records
|
||||
.iter()
|
||||
.find(|record| record.path == path)
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub fn find_asset_by_id(registry: &AssetRegistry, id: &str) -> Option<AssetRecord> {
|
||||
registry
|
||||
.records
|
||||
.iter()
|
||||
.find(|record| record.id.as_string() == id)
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub fn find_asset_mut_by_path<'a>(
|
||||
registry: &'a mut AssetRegistry,
|
||||
path: &str,
|
||||
) -> Option<&'a mut AssetRecord> {
|
||||
registry
|
||||
.records
|
||||
.iter_mut()
|
||||
.find(|record| record.path == path)
|
||||
}
|
||||
|
||||
pub fn update_import_settings(
|
||||
registry: &mut AssetRegistry,
|
||||
path: &str,
|
||||
settings: ImportSettings,
|
||||
) -> bool {
|
||||
let Some(record) = registry
|
||||
.records
|
||||
.iter_mut()
|
||||
.find(|record| record.path == path)
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
record.import_settings = settings;
|
||||
registry.index_dirty = true;
|
||||
true
|
||||
}
|
||||
|
||||
pub fn registry_index_path() -> &'static str {
|
||||
"assets/.index/registry.ron"
|
||||
}
|
||||
|
||||
pub fn save_registry(registry: &AssetRegistry) -> Result<(), String> {
|
||||
let path = registry_index_path();
|
||||
if let Some(parent) = std::path::Path::new(path).parent() {
|
||||
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
|
||||
}
|
||||
let text = ron::ser::to_string_pretty(®istry.records, ron::ser::PrettyConfig::default())
|
||||
.map_err(|e| e.to_string())?;
|
||||
std::fs::write(path, text).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
pub fn load_registry() -> AssetRegistry {
|
||||
let path = registry_index_path();
|
||||
match std::fs::read_to_string(path) {
|
||||
Ok(text) => {
|
||||
let records: Vec<AssetRecord> = ron::from_str(&text).unwrap_or_default();
|
||||
AssetRegistry {
|
||||
records,
|
||||
index_dirty: false,
|
||||
}
|
||||
}
|
||||
Err(_) => AssetRegistry::default(),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user