unity-asset-binary 0.2.0

Unity binary file format parser (AssetBundle, SerializedFile)
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! Unity asset metadata processing module
//!
//! This module provides comprehensive metadata extraction and analysis capabilities
//! for Unity assets, organized following best practices for maintainability.
//!
//! # Architecture
//!
//! The module is organized into several sub-modules:
//! - `types` - Core data structures for metadata representation
//! - `extractor` - Main metadata extraction functionality
//! - `analyzer` - Advanced dependency and relationship analysis
//!
//! # Examples
//!
//! ```rust,no_run
//! use unity_asset_binary::metadata::{MetadataExtractor, ExtractionConfig};
//! use unity_asset_binary::asset::{SerializedFile, SerializedFileHeader};
//!
//! // Create extractor with custom configuration
//! let config = ExtractionConfig {
//!     include_dependencies: true,
//!     include_hierarchy: true,
//!     max_objects: Some(1000),
//!     include_performance: true,
//!     include_object_details: true,
//! };
//! let extractor = MetadataExtractor::with_config(config);
//!
//! // Note: In real usage, you would load a SerializedFile from actual data
//! // For demonstration, we'll just show the extractor creation
//! println!("Extractor created with config");
//! println!("Metadata extracted successfully");
//! # Ok::<(), unity_asset_binary::error::BinaryError>(())
//! ```

pub mod analyzer;
pub mod extractor;
pub mod types;

// Re-export main types for easy access
pub use analyzer::{DependencyAnalyzer, RelationshipAnalyzer};
pub use extractor::MetadataExtractor;
pub use types::{
    // Core metadata types
    AssetMetadata,
    AssetReference,
    // Relationship types
    AssetRelationships,
    ComponentRelationship,
    DependencyGraph,
    // Dependency types
    DependencyInfo,
    ExternalObjectRef,
    ExternalReference,
    ExtractionConfig,
    ExtractionResult,
    ExtractionStats,
    FileInfo,
    GameObjectHierarchy,
    InternalReference,
    MemoryUsage,
    ObjectStatistics,
    ObjectSummary,
    // Performance and configuration
    PerformanceMetrics,
    // Constants
    class_ids,
};

use crate::asset::SerializedFile;
use crate::bundle::AssetBundle;

/// Main metadata processing facade
///
/// This struct provides a high-level interface for metadata processing,
/// combining extraction and analysis functionality.
pub struct MetadataProcessor {
    extractor: MetadataExtractor,
    dependency_analyzer: Option<DependencyAnalyzer>,
    relationship_analyzer: Option<RelationshipAnalyzer>,
}

fn apply_dependency_info_to_relationships(
    dependencies: &DependencyInfo,
    relationships: &mut AssetRelationships,
) {
    let mut by_from: std::collections::HashMap<i64, Vec<i64>> = std::collections::HashMap::new();
    for r in &dependencies.internal_references {
        by_from.entry(r.from_object).or_default().push(r.to_object);
    }
    for v in by_from.values_mut() {
        v.sort_unstable();
        v.dedup();
    }

    let mut by_from_external: std::collections::HashMap<i64, Vec<ExternalObjectRef>> =
        std::collections::HashMap::new();
    for r in &dependencies.external_references {
        let ext = ExternalObjectRef {
            file_id: r.file_id,
            path_id: r.path_id,
            file_path: r.file_path.clone(),
            guid: r.guid,
        };
        for from in &r.referenced_by {
            by_from_external.entry(*from).or_default().push(ext.clone());
        }
    }
    for v in by_from_external.values_mut() {
        v.sort_by_key(|e| (e.file_id, e.path_id));
        v.dedup_by_key(|e| (e.file_id, e.path_id));
    }

    for rel in &mut relationships.component_relationships {
        rel.dependencies = by_from.get(&rel.component_id).cloned().unwrap_or_default();
        rel.external_dependencies = by_from_external
            .get(&rel.component_id)
            .cloned()
            .unwrap_or_default();
    }

    // Build asset reference summary (internal targets + external targets).
    let gameobject_ids: std::collections::HashSet<i64> = relationships
        .gameobject_hierarchy
        .iter()
        .map(|h| h.gameobject_id)
        .collect();
    let component_ids: std::collections::HashSet<i64> = relationships
        .component_relationships
        .iter()
        .map(|c| c.component_id)
        .collect();

    let mut referenced_by_internal: std::collections::HashMap<i64, Vec<i64>> =
        std::collections::HashMap::new();
    for r in &dependencies.internal_references {
        referenced_by_internal
            .entry(r.to_object)
            .or_default()
            .push(r.from_object);
    }
    for v in referenced_by_internal.values_mut() {
        v.sort_unstable();
        v.dedup();
    }

    let mut refs: Vec<AssetReference> = Vec::new();

    for (asset_id, referenced_by) in referenced_by_internal {
        let asset_type = if gameobject_ids.contains(&asset_id) {
            "GameObject".to_string()
        } else if component_ids.contains(&asset_id) {
            "Component".to_string()
        } else {
            "Object".to_string()
        };
        refs.push(AssetReference {
            asset_id,
            asset_type,
            referenced_by,
            file_path: None,
        });
    }

    for r in &dependencies.external_references {
        refs.push(AssetReference {
            asset_id: r.path_id,
            asset_type: format!("ExternalObject(file_id={})", r.file_id),
            referenced_by: r.referenced_by.clone(),
            file_path: r.file_path.clone(),
        });
    }

    refs.sort_by(|a, b| {
        // Prefer refs with known file path, then by ref count desc, then asset_id asc.
        match (b.file_path.is_some()).cmp(&a.file_path.is_some()) {
            std::cmp::Ordering::Equal => {}
            ord => return ord,
        }
        match b.referenced_by.len().cmp(&a.referenced_by.len()) {
            std::cmp::Ordering::Equal => {}
            ord => return ord,
        }
        a.asset_id.cmp(&b.asset_id)
    });

    relationships.asset_references = refs;
}

impl MetadataProcessor {
    /// Create a new metadata processor with default settings
    pub fn new() -> Self {
        Self {
            extractor: MetadataExtractor::new(),
            dependency_analyzer: None,
            relationship_analyzer: None,
        }
    }

    /// Create a metadata processor with custom configuration
    pub fn with_config(config: ExtractionConfig) -> Self {
        let enable_advanced = config.include_dependencies || config.include_hierarchy;

        Self {
            extractor: MetadataExtractor::with_config(config),
            dependency_analyzer: if enable_advanced {
                Some(DependencyAnalyzer::new())
            } else {
                None
            },
            relationship_analyzer: if enable_advanced {
                Some(RelationshipAnalyzer::new())
            } else {
                None
            },
        }
    }

    /// Process metadata from a SerializedFile
    pub fn process_asset(
        &mut self,
        asset: &SerializedFile,
    ) -> crate::error::Result<ExtractionResult> {
        let mut result = self.extractor.extract_from_asset(asset)?;

        // Enhanced dependency analysis if analyzer is available
        if let Some(ref mut analyzer) = self.dependency_analyzer
            && self.extractor.config().include_dependencies
        {
            let objects: Vec<&crate::asset::ObjectInfo> =
                if let Some(max) = self.extractor.config().max_objects {
                    asset.objects.iter().take(max).collect()
                } else {
                    asset.objects.iter().collect()
                };

            match analyzer.analyze_dependencies_in_asset(asset, &objects) {
                Ok(deps) => {
                    if self.extractor.config().include_object_details {
                        let mut by_from: std::collections::HashMap<i64, Vec<i64>> =
                            std::collections::HashMap::new();
                        for r in &deps.internal_references {
                            by_from.entry(r.from_object).or_default().push(r.to_object);
                        }
                        for v in by_from.values_mut() {
                            v.sort_unstable();
                            v.dedup();
                        }
                        for summary in &mut result.metadata.object_stats.largest_objects {
                            summary.dependencies =
                                by_from.get(&summary.path_id).cloned().unwrap_or_default();
                        }
                    }
                    result.metadata.dependencies = deps;
                }
                Err(e) => {
                    result.add_warning(format!("Enhanced dependency analysis failed: {}", e));
                }
            }
        }

        // Enhanced relationship analysis if analyzer is available
        if let Some(ref mut analyzer) = self.relationship_analyzer
            && self.extractor.config().include_hierarchy
        {
            let objects: Vec<&crate::asset::ObjectInfo> =
                if let Some(max) = self.extractor.config().max_objects {
                    asset.objects.iter().take(max).collect()
                } else {
                    asset.objects.iter().collect()
                };

            match analyzer.analyze_relationships_in_asset(asset, &objects) {
                Ok(mut rels) => {
                    if self.extractor.config().include_dependencies {
                        apply_dependency_info_to_relationships(
                            &result.metadata.dependencies,
                            &mut rels,
                        );
                    }
                    result.metadata.relationships = rels;
                }
                Err(e) => {
                    result.add_warning(format!("Enhanced relationship analysis failed: {}", e));
                }
            }
        }

        Ok(result)
    }

    /// Process metadata from an AssetBundle
    pub fn process_bundle(
        &mut self,
        bundle: &AssetBundle,
    ) -> crate::error::Result<Vec<ExtractionResult>> {
        let mut results = Vec::new();

        for asset in &bundle.assets {
            let result = self.process_asset(asset)?;
            results.push(result);
        }

        Ok(results)
    }

    /// Get the current extraction configuration
    pub fn config(&self) -> &ExtractionConfig {
        self.extractor.config()
    }

    /// Update the extraction configuration
    pub fn set_config(&mut self, config: ExtractionConfig) {
        let enable_advanced = config.include_dependencies || config.include_hierarchy;

        self.extractor.set_config(config);

        // Initialize analyzers if needed
        if enable_advanced {
            if self.dependency_analyzer.is_none() {
                self.dependency_analyzer = Some(DependencyAnalyzer::new());
            }
            if self.relationship_analyzer.is_none() {
                self.relationship_analyzer = Some(RelationshipAnalyzer::new());
            }
        }
    }

    /// Clear internal caches
    pub fn clear_caches(&mut self) {
        if let Some(ref mut analyzer) = self.dependency_analyzer {
            analyzer.clear_cache();
        }
        if let Some(ref mut analyzer) = self.relationship_analyzer {
            analyzer.clear_cache();
        }
    }

    /// Check if advanced analysis is enabled
    pub fn has_advanced_analysis(&self) -> bool {
        self.dependency_analyzer.is_some() || self.relationship_analyzer.is_some()
    }
}

impl Default for MetadataProcessor {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience functions for common operations
/// Create a metadata processor with default settings
pub fn create_processor() -> MetadataProcessor {
    MetadataProcessor::default()
}

/// Create a metadata processor with performance-focused configuration
pub fn create_performance_processor() -> MetadataProcessor {
    let config = ExtractionConfig {
        include_dependencies: false,
        include_hierarchy: false,
        max_objects: Some(1000),
        include_performance: true,
        include_object_details: false,
    };
    MetadataProcessor::with_config(config)
}

/// Create a metadata processor with comprehensive analysis
pub fn create_comprehensive_processor() -> MetadataProcessor {
    let config = ExtractionConfig {
        include_dependencies: true,
        include_hierarchy: true,
        max_objects: None,
        include_performance: true,
        include_object_details: true,
    };
    MetadataProcessor::with_config(config)
}

/// Extract basic metadata from an asset
pub fn extract_basic_metadata(asset: &SerializedFile) -> crate::error::Result<AssetMetadata> {
    let mut processor = MetadataProcessor::with_config(ExtractionConfig::default());
    let result = processor.process_asset(asset)?;
    Ok(result.metadata)
}

/// Extract metadata with custom configuration
pub fn extract_metadata_with_config(
    asset: &SerializedFile,
    config: ExtractionConfig,
) -> crate::error::Result<ExtractionResult> {
    let mut processor = MetadataProcessor::with_config(config);
    processor.process_asset(asset)
}

/// Get quick statistics for an asset
pub fn get_asset_statistics(asset: &SerializedFile) -> AssetStatistics {
    AssetStatistics {
        object_count: asset.objects.len(),
        type_count: asset.types.len(),
        external_count: asset.externals.len(),
        file_size: asset.header.file_size,
        unity_version: asset.unity_version.clone(),
        format_version: asset.header.version,
    }
}

/// Quick asset statistics
#[derive(Debug, Clone)]
pub struct AssetStatistics {
    pub object_count: usize,
    pub type_count: usize,
    pub external_count: usize,
    pub file_size: u64,
    pub unity_version: String,
    pub format_version: u32,
}

/// Metadata processing options
#[derive(Debug, Clone)]
pub struct ProcessingOptions {
    pub enable_caching: bool,
    pub max_cache_size: usize,
    pub parallel_processing: bool,
    pub memory_limit_mb: Option<usize>,
}

impl Default for ProcessingOptions {
    fn default() -> Self {
        Self {
            enable_caching: true,
            max_cache_size: 1000,
            parallel_processing: false,
            memory_limit_mb: None,
        }
    }
}

/// Check if metadata extraction is supported for an asset
pub fn is_extraction_supported(asset: &SerializedFile) -> bool {
    // Support Unity 5.0+ (version 10+)
    asset.header.version >= 10
}

/// Get recommended extraction configuration for an asset
pub fn get_recommended_config(asset: &SerializedFile) -> ExtractionConfig {
    let object_count = asset.objects.len();

    if object_count > 10000 {
        // Large asset - performance focused
        ExtractionConfig {
            include_dependencies: false,
            include_hierarchy: false,
            max_objects: Some(5000),
            include_performance: true,
            include_object_details: false,
        }
    } else if object_count > 1000 {
        // Medium asset - balanced
        ExtractionConfig {
            include_dependencies: true,
            include_hierarchy: false,
            max_objects: Some(2000),
            include_performance: true,
            include_object_details: true,
        }
    } else {
        // Small asset - comprehensive
        ExtractionConfig::default()
    }
}

#[cfg(test)]
mod processor_tests {
    use super::*;

    #[test]
    fn test_apply_dependency_info_to_relationships_fills_component_dependencies() {
        let dependencies = DependencyInfo {
            external_references: vec![ExternalReference {
                file_id: 2,
                path_id: 999,
                referenced_by: vec![11],
                file_path: Some("library/external.assets".to_string()),
                guid: Some([7u8; 16]),
            }],
            internal_references: vec![
                InternalReference {
                    from_object: 10,
                    to_object: 1,
                    reference_type: "Direct".to_string(),
                },
                InternalReference {
                    from_object: 10,
                    to_object: 2,
                    reference_type: "Direct".to_string(),
                },
                InternalReference {
                    from_object: 11,
                    to_object: 3,
                    reference_type: "Direct".to_string(),
                },
            ],
            dependency_graph: DependencyGraph {
                nodes: Vec::new(),
                edges: Vec::new(),
                root_objects: Vec::new(),
                leaf_objects: Vec::new(),
            },
            circular_dependencies: Vec::new(),
        };

        let mut relationships = AssetRelationships {
            gameobject_hierarchy: Vec::new(),
            component_relationships: vec![
                ComponentRelationship {
                    component_id: 10,
                    component_type: "Transform".to_string(),
                    gameobject_id: 100,
                    dependencies: Vec::new(),
                    external_dependencies: Vec::new(),
                },
                ComponentRelationship {
                    component_id: 11,
                    component_type: "MeshRenderer".to_string(),
                    gameobject_id: 100,
                    dependencies: Vec::new(),
                    external_dependencies: Vec::new(),
                },
                ComponentRelationship {
                    component_id: 12,
                    component_type: "Unknown".to_string(),
                    gameobject_id: 100,
                    dependencies: Vec::new(),
                    external_dependencies: Vec::new(),
                },
            ],
            asset_references: Vec::new(),
        };

        apply_dependency_info_to_relationships(&dependencies, &mut relationships);

        assert_eq!(
            relationships.component_relationships[0].dependencies,
            vec![1, 2]
        );
        assert_eq!(
            relationships.component_relationships[1].dependencies,
            vec![3]
        );
        assert_eq!(
            relationships.component_relationships[2].dependencies,
            Vec::<i64>::new()
        );

        assert_eq!(
            relationships.component_relationships[0]
                .external_dependencies
                .len(),
            0
        );
        assert_eq!(
            relationships.component_relationships[1]
                .external_dependencies
                .len(),
            1
        );
        assert_eq!(
            relationships.component_relationships[1].external_dependencies[0],
            ExternalObjectRef {
                file_id: 2,
                path_id: 999,
                file_path: Some("library/external.assets".to_string()),
                guid: Some([7u8; 16]),
            }
        );

        let ext_ref = relationships
            .asset_references
            .iter()
            .find(|r| r.asset_id == 999)
            .expect("external asset reference exists");
        assert_eq!(ext_ref.asset_type, "ExternalObject(file_id=2)");
        assert_eq!(
            ext_ref.file_path,
            Some("library/external.assets".to_string())
        );
        assert_eq!(ext_ref.referenced_by, vec![11]);

        let internal_ref_1 = relationships
            .asset_references
            .iter()
            .find(|r| r.asset_id == 1)
            .expect("internal asset reference 1 exists");
        assert_eq!(internal_ref_1.file_path, None);
        assert_eq!(internal_ref_1.referenced_by, vec![10]);

        let internal_ref_3 = relationships
            .asset_references
            .iter()
            .find(|r| r.asset_id == 3)
            .expect("internal asset reference 3 exists");
        assert_eq!(internal_ref_3.referenced_by, vec![11]);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_processor_creation() {
        let processor = create_processor();
        assert!(!processor.has_advanced_analysis());
    }

    #[test]
    fn test_comprehensive_processor() {
        let processor = create_comprehensive_processor();
        assert!(processor.has_advanced_analysis());
        assert!(processor.config().include_dependencies);
        assert!(processor.config().include_hierarchy);
    }

    #[test]
    fn test_performance_processor() {
        let processor = create_performance_processor();
        assert!(!processor.config().include_dependencies);
        assert!(!processor.config().include_hierarchy);
        assert_eq!(processor.config().max_objects, Some(1000));
    }

    #[test]
    fn test_extraction_support() {
        // This would need a mock SerializedFile for proper testing
        // For now, just test that the function exists
    }
}