1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use crate::resource::ResourceReference;
use crate::uuid::UuidV4;
use rkyv::Archive;
use ron::ser::PrettyConfig;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::SystemTime;
use uuid::Uuid;
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AssetType {
Mesh,
Texture,
Audio,
Material,
Scene,
Script,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AssetEntry {
pub uuid: Uuid,
pub name: String,
pub asset_type: AssetType,
pub location: ResourceReference,
pub compiled_path: Option<PathBuf>,
pub source_hash: [u8; 32],
pub import_time: SystemTime,
pub dependencies: Vec<Uuid>,
}
impl AssetEntry {
pub fn to_reference(&self, project_root: &Path) -> Option<ResourceReference> {
match &self.location {
ResourceReference::File(relative) => {
Some(ResourceReference::File(project_root.join(relative)))
}
ResourceReference::Procedural => Some(ResourceReference::Procedural),
ResourceReference::Embedded(_) | ResourceReference::Packed { .. } => None,
}
}
pub fn to_embedded_reference(&self, bytes: &'static [u8]) -> ResourceReference {
ResourceReference::Embedded(Arc::from(bytes))
}
pub fn is_stale(&self, current_hash: &[u8; 32]) -> bool {
&self.source_hash != current_hash
}
}
pub fn detect_asset_type(path: &Path) -> Option<AssetType> {
match path.extension()?.to_str()?.to_ascii_lowercase().as_str() {
"obj" | "gltf" | "glb" | "fbx" | "eucmdl" | "eucbin" => Some(AssetType::Mesh),
"png" | "jpg" | "jpeg" | "webp" | "hdr" | "tga" | "bmp" | "exr" => Some(AssetType::Texture),
"wav" | "ogg" | "flac" | "mp3" => Some(AssetType::Audio),
"eucs" => Some(AssetType::Scene),
"kt" | "kts" => Some(AssetType::Script),
_ => None,
}
}
fn hash_file(path: &Path) -> anyhow::Result<[u8; 32]> {
let bytes = fs::read(path)?;
let digest = Sha256::digest(&bytes);
let mut hash = [0u8; 32];
hash.copy_from_slice(&digest);
Ok(hash)
}
pub fn generate_eucmeta(source_path: &Path, project_root: &Path) -> anyhow::Result<AssetEntry> {
let meta_path = PathBuf::from(format!("{}.eucmeta", source_path.display()));
if meta_path.exists() {
let ron_str = fs::read_to_string(&meta_path)?;
let entry: AssetEntry = ron::de::from_str(&ron_str)
.map_err(|e| anyhow::anyhow!("Failed to parse {}: {}", meta_path.display(), e))?;
return Ok(entry);
}
let asset_type = detect_asset_type(source_path)
.ok_or_else(|| anyhow::anyhow!("Unknown asset type for: {}", source_path.display()))?;
let name = source_path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("unnamed")
.to_string();
let relative = source_path
.strip_prefix(project_root)
.unwrap_or(source_path)
.to_path_buf();
let source_hash = hash_file(source_path)?;
let entry = AssetEntry {
uuid: Uuid::new_v4(),
name,
asset_type,
location: ResourceReference::File(relative),
compiled_path: None,
source_hash,
import_time: SystemTime::now(),
dependencies: vec![],
};
let ron_str = ron::ser::to_string_pretty(&entry, PrettyConfig::default())
.map_err(|e| anyhow::anyhow!("RON serialization error: {}", e))?;
fs::write(&meta_path, &ron_str)?;
log::info!("Generated .eucmeta for {}", source_path.display());
Ok(entry)
}
#[derive(Clone, Debug, PartialEq, Eq, Archive, rkyv::Serialize, rkyv::Deserialize)]
pub enum PackedAssetType {
Mesh,
Texture,
Audio,
Material,
Scene,
Script,
}
#[derive(Clone, Debug, Archive, rkyv::Serialize, rkyv::Deserialize)]
pub struct PackedAssetEntry {
pub uuid: UuidV4,
pub asset_type: PackedAssetType,
pub offset: u64,
pub length: u64,
pub dependencies: Vec<UuidV4>,
}
pub fn scan_and_generate_eucmeta(project_root: &Path) -> usize {
let resources_dir = project_root.join("resources");
let mut created = 0usize;
fn walk(dir: &Path, project_root: &Path, created: &mut usize) {
let Ok(read) = std::fs::read_dir(dir) else {
return;
};
for entry in read.flatten() {
let path = entry.path();
if path.is_dir() {
walk(&path, project_root, created);
continue;
}
if path.extension().and_then(|e| e.to_str()) == Some("eucmeta") {
continue;
}
if detect_asset_type(&path).is_none() {
continue;
}
let meta_path = PathBuf::from(format!("{}.eucmeta", path.display()));
if meta_path.exists() {
continue;
}
match generate_eucmeta(&path, project_root) {
Ok(_) => *created += 1,
Err(e) => log::warn!(
"Failed to generate .eucmeta for '{}': {}",
path.display(),
e
),
}
}
}
walk(&resources_dir, project_root, &mut created);
if created > 0 {
log::info!(
"Generated {} .eucmeta sidecar(s) during project scan",
created
);
}
created
}
pub fn find_asset_by_uuid(project_root: &Path, uuid: Uuid) -> anyhow::Result<AssetEntry> {
fn scan_dir(dir: &Path, uuid: Uuid) -> Option<AssetEntry> {
let read = std::fs::read_dir(dir).ok()?;
for entry in read.flatten() {
let path = entry.path();
if path.is_dir() {
if let Some(found) = scan_dir(&path, uuid) {
return Some(found);
}
} else if path.extension().and_then(|e| e.to_str()) == Some("eucmeta") {
if let Ok(s) = std::fs::read_to_string(&path) {
if let Ok(entry) = ron::de::from_str::<AssetEntry>(&s) {
if entry.uuid == uuid {
return Some(entry);
}
}
}
}
}
None
}
let resources_dir = project_root.join("resources");
scan_dir(&resources_dir, uuid)
.ok_or_else(|| anyhow::anyhow!("No .eucmeta file found for UUID {}", uuid))
}