unity_asset_binary/metadata/
types.rs

1//! Metadata type definitions
2//!
3//! This module defines all the data structures used for Unity asset metadata extraction.
4
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8/// Comprehensive metadata for a Unity asset
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct AssetMetadata {
11    /// Basic file information
12    pub file_info: FileInfo,
13    /// Object statistics
14    pub object_stats: ObjectStatistics,
15    /// Dependency information
16    pub dependencies: DependencyInfo,
17    /// Asset relationships
18    pub relationships: AssetRelationships,
19    /// Performance metrics
20    pub performance: PerformanceMetrics,
21}
22
23/// Basic file information
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct FileInfo {
26    pub file_size: u64,
27    pub unity_version: String,
28    pub target_platform: String,
29    pub compression_type: String,
30    pub file_format_version: u32,
31}
32
33/// Object statistics within the asset
34#[derive(Debug, Clone, Serialize, Deserialize, Default)]
35pub struct ObjectStatistics {
36    pub total_objects: usize,
37    pub objects_by_type: HashMap<String, usize>,
38    pub largest_objects: Vec<ObjectSummary>,
39    pub memory_usage: MemoryUsage,
40}
41
42/// Summary of an individual object
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct ObjectSummary {
45    pub path_id: i64,
46    pub class_name: String,
47    pub name: Option<String>,
48    pub byte_size: u32,
49    pub dependencies: Vec<i64>,
50}
51
52/// Memory usage statistics
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct MemoryUsage {
55    pub total_bytes: u64,
56    pub by_type: HashMap<String, u64>,
57    pub largest_type: Option<String>,
58    pub average_object_size: f64,
59}
60
61impl Default for MemoryUsage {
62    fn default() -> Self {
63        Self {
64            total_bytes: 0,
65            by_type: HashMap::new(),
66            largest_type: None,
67            average_object_size: 0.0,
68        }
69    }
70}
71
72/// Dependency information
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct DependencyInfo {
75    pub external_references: Vec<ExternalReference>,
76    pub internal_references: Vec<InternalReference>,
77    pub dependency_graph: DependencyGraph,
78    pub circular_dependencies: Vec<Vec<i64>>,
79}
80
81/// External file reference
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct ExternalReference {
84    pub file_id: i32,
85    pub path_id: i64,
86    pub referenced_by: Vec<i64>,
87}
88
89/// Internal object reference
90#[derive(Debug, Clone, Serialize, Deserialize)]
91pub struct InternalReference {
92    pub from_object: i64,
93    pub to_object: i64,
94    pub reference_type: String,
95}
96
97/// Dependency graph representation
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct DependencyGraph {
100    pub nodes: Vec<i64>,
101    pub edges: Vec<(i64, i64)>,
102    pub root_objects: Vec<i64>,
103    pub leaf_objects: Vec<i64>,
104}
105
106/// Asset relationships and hierarchy
107#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct AssetRelationships {
109    pub gameobject_hierarchy: Vec<GameObjectHierarchy>,
110    pub component_relationships: Vec<ComponentRelationship>,
111    pub asset_references: Vec<AssetReference>,
112}
113
114/// GameObject hierarchy information
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct GameObjectHierarchy {
117    pub gameobject_id: i64,
118    pub name: String,
119    pub parent_id: Option<i64>,
120    pub children_ids: Vec<i64>,
121    pub transform_id: i64,
122    pub components: Vec<i64>,
123    pub depth: u32,
124}
125
126/// Component relationship information
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct ComponentRelationship {
129    pub component_id: i64,
130    pub component_type: String,
131    pub gameobject_id: i64,
132    pub dependencies: Vec<i64>,
133}
134
135/// Asset reference information
136#[derive(Debug, Clone, Serialize, Deserialize)]
137pub struct AssetReference {
138    pub asset_id: i64,
139    pub asset_type: String,
140    pub referenced_by: Vec<i64>,
141    pub file_path: Option<String>,
142}
143
144/// Performance metrics
145#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct PerformanceMetrics {
147    pub parse_time_ms: f64,
148    pub memory_peak_mb: f64,
149    pub object_parse_rate: f64, // objects per second
150    pub complexity_score: f64,
151}
152
153/// Metadata extraction configuration
154#[derive(Debug, Clone)]
155pub struct ExtractionConfig {
156    /// Whether to include dependency analysis
157    pub include_dependencies: bool,
158    /// Whether to include hierarchy analysis
159    pub include_hierarchy: bool,
160    /// Maximum number of objects to analyze (0 = no limit)
161    pub max_objects: Option<usize>,
162    /// Whether to include performance metrics
163    pub include_performance: bool,
164    /// Whether to extract detailed object summaries
165    pub include_object_details: bool,
166}
167
168impl Default for ExtractionConfig {
169    fn default() -> Self {
170        Self {
171            include_dependencies: true,
172            include_hierarchy: true,
173            max_objects: None,
174            include_performance: true,
175            include_object_details: true,
176        }
177    }
178}
179
180/// Metadata extraction result
181#[derive(Debug, Clone)]
182pub struct ExtractionResult {
183    pub metadata: AssetMetadata,
184    pub warnings: Vec<String>,
185    pub errors: Vec<String>,
186}
187
188impl ExtractionResult {
189    pub fn new(metadata: AssetMetadata) -> Self {
190        Self {
191            metadata,
192            warnings: Vec::new(),
193            errors: Vec::new(),
194        }
195    }
196
197    pub fn add_warning(&mut self, warning: String) {
198        self.warnings.push(warning);
199    }
200
201    pub fn add_error(&mut self, error: String) {
202        self.errors.push(error);
203    }
204
205    pub fn has_warnings(&self) -> bool {
206        !self.warnings.is_empty()
207    }
208
209    pub fn has_errors(&self) -> bool {
210        !self.errors.is_empty()
211    }
212}
213
214/// Statistics about the extraction process
215#[derive(Debug, Clone)]
216pub struct ExtractionStats {
217    pub objects_processed: usize,
218    pub dependencies_found: usize,
219    pub relationships_found: usize,
220    pub processing_time_ms: f64,
221    pub memory_used_mb: f64,
222}
223
224impl Default for ExtractionStats {
225    fn default() -> Self {
226        Self {
227            objects_processed: 0,
228            dependencies_found: 0,
229            relationships_found: 0,
230            processing_time_ms: 0.0,
231            memory_used_mb: 0.0,
232        }
233    }
234}
235
236/// Unity class ID constants for metadata extraction
237pub mod class_ids {
238    pub const GAME_OBJECT: i32 = 1;
239    pub const COMPONENT: i32 = 2;
240    pub const BEHAVIOUR: i32 = 3;
241    pub const TRANSFORM: i32 = 4;
242    pub const MATERIAL: i32 = 21;
243    pub const TEXTURE_2D: i32 = 28;
244    pub const MESH: i32 = 43;
245    pub const SHADER: i32 = 48;
246    pub const ANIMATION_CLIP: i32 = 74;
247    pub const AUDIO_CLIP: i32 = 83;
248    pub const ANIMATOR_CONTROLLER: i32 = 91;
249    pub const MONO_BEHAVIOUR: i32 = 114;
250    pub const SPRITE: i32 = 213;
251}
252
253/// Helper functions for metadata types
254impl AssetMetadata {
255    /// Create a new empty metadata structure
256    pub fn new() -> Self {
257        Self {
258            file_info: FileInfo {
259                file_size: 0,
260                unity_version: String::new(),
261                target_platform: String::new(),
262                compression_type: String::new(),
263                file_format_version: 0,
264            },
265            object_stats: ObjectStatistics::default(),
266            dependencies: DependencyInfo {
267                external_references: Vec::new(),
268                internal_references: Vec::new(),
269                dependency_graph: DependencyGraph {
270                    nodes: Vec::new(),
271                    edges: Vec::new(),
272                    root_objects: Vec::new(),
273                    leaf_objects: Vec::new(),
274                },
275                circular_dependencies: Vec::new(),
276            },
277            relationships: AssetRelationships {
278                gameobject_hierarchy: Vec::new(),
279                component_relationships: Vec::new(),
280                asset_references: Vec::new(),
281            },
282            performance: PerformanceMetrics {
283                parse_time_ms: 0.0,
284                memory_peak_mb: 0.0,
285                object_parse_rate: 0.0,
286                complexity_score: 0.0,
287            },
288        }
289    }
290
291    /// Get total number of objects
292    pub fn total_objects(&self) -> usize {
293        self.object_stats.total_objects
294    }
295
296    /// Get total memory usage
297    pub fn total_memory_bytes(&self) -> u64 {
298        self.object_stats.memory_usage.total_bytes
299    }
300
301    /// Check if the asset has dependencies
302    pub fn has_dependencies(&self) -> bool {
303        !self.dependencies.external_references.is_empty()
304            || !self.dependencies.internal_references.is_empty()
305    }
306
307    /// Check if the asset has hierarchy information
308    pub fn has_hierarchy(&self) -> bool {
309        !self.relationships.gameobject_hierarchy.is_empty()
310    }
311}
312
313impl Default for AssetMetadata {
314    fn default() -> Self {
315        Self::new()
316    }
317}