unity_asset_binary/
lib.rs

1//! Unity Binary Asset Parser
2//!
3//! This crate provides functionality to parse Unity binary file formats including:
4//! - AssetBundle files (.bundle, .unity3d)
5//! - Serialized Asset files (.assets)
6//! - Resource files
7//!
8//! # Features
9//!
10//! - **AssetBundle parsing**: Support for UnityFS format
11//! - **Compression support**: LZ4, LZMA, and other compression formats
12//! - **TypeTree parsing**: Dynamic type information for objects
13//! - **Object extraction**: Extract Unity objects from binary data
14//!
15//! ## Feature Flags
16//!
17//! - `texture`: Texture processing (basic formats + PNG export)
18//! - `audio`: Audio processing (all formats including Vorbis, MP3)
19//! - `mesh`: Mesh processing (parsing + basic export)
20//! - `sprite`: Sprite processing (requires texture support)
21//! - `texture-advanced`: Advanced texture formats (DXT, ETC, ASTC) - requires texture2ddecoder
22//! - `mesh-export`: Advanced mesh export (OBJ format)
23//!
24//! # Example
25//!
26//! ```rust,no_run
27//! use unity_asset_binary::load_bundle_from_memory;
28//! use std::fs;
29//!
30//! // Load an AssetBundle file
31//! let data = fs::read("example.bundle")?;
32//! let bundle = load_bundle_from_memory(data)?;
33//!
34//! // Access contained assets
35//! for asset in &bundle.assets {
36//!     println!("Asset with {} objects", asset.object_count());
37//!     // Access objects in the asset
38//!     for object in &asset.objects {
39//!         println!("  Object: {} (type_id: {})", object.path_id, object.type_id);
40//!     }
41//! }
42//! # Ok::<(), Box<dyn std::error::Error>>(())
43//! ```
44
45// Core modules (always available)
46pub mod asset;
47pub mod bundle;
48pub mod compression;
49pub mod error;
50pub mod metadata;
51pub mod object;
52pub mod performance;
53pub mod reader;
54pub mod typetree;
55pub mod unity_objects;
56pub mod unity_version;
57pub mod webfile;
58
59// Feature-gated modules
60#[cfg(feature = "texture")]
61pub mod texture;
62
63#[cfg(feature = "audio")]
64pub mod audio;
65
66#[cfg(feature = "sprite")]
67pub mod sprite;
68
69#[cfg(feature = "mesh")]
70pub mod mesh;
71
72// Re-export core types (always available)
73pub use asset::{
74    Asset, // Legacy compatibility alias
75    AssetFileInfo,
76    // Processing
77    AssetProcessor,
78    FileIdentifier,
79    FileStatistics,
80    // Information and validation
81    HeaderFormatInfo,
82    HeaderValidation,
83    ObjectInfo,
84    ParsingOptions,
85    ParsingStats,
86    // Core types
87    SerializedFile,
88    SerializedFileHeader,
89    SerializedFileParser,
90    SerializedType,
91    TypeRegistry,
92    // Constants
93    class_ids,
94    // Convenience functions
95    create_processor as create_asset_processor,
96    get_file_info as get_asset_file_info,
97    get_parsing_options as get_asset_parsing_options,
98    get_supported_versions as get_supported_asset_versions,
99    is_valid_serialized_file,
100    is_version_supported as is_asset_version_supported,
101    parse_serialized_file,
102    parse_serialized_file_from_path,
103};
104pub use bundle::{
105    // Core types
106    AssetBundle,
107    // Compression
108    BundleCompression,
109    BundleFileInfo,
110    BundleFormatInfo,
111    BundleHeader,
112    BundleInfo,
113    BundleLoadOptions,
114    BundleLoader,
115    BundleParser,
116    // Processing
117    BundleProcessor,
118    BundleResourceManager,
119    BundleStatistics,
120    CompressionOptions,
121    CompressionStats,
122    DirectoryNode,
123    // Statistics and info
124    LoaderStatistics,
125    ParsingComplexity,
126    // Convenience functions (with bundle prefix to avoid conflicts)
127    create_processor as create_bundle_processor,
128    extract_file_from_bundle,
129    get_bundle_info,
130    get_supported_formats as get_supported_bundle_formats,
131    is_valid_bundle,
132    list_bundle_contents,
133    load_bundle,
134    load_bundle_from_memory,
135    load_bundle_with_options,
136};
137pub use error::{BinaryError, Result};
138
139// Re-export async support
140pub use metadata::{
141    // Core metadata types
142    AssetMetadata,
143    AssetReference,
144    // Relationship types
145    AssetRelationships,
146    // Statistics and options
147    AssetStatistics,
148    ComponentRelationship,
149    DependencyAnalyzer,
150    DependencyGraph,
151    // Dependency types
152    DependencyInfo,
153    ExternalReference,
154    // Configuration and results
155    ExtractionConfig,
156    ExtractionResult,
157    ExtractionStats,
158    FileInfo,
159    GameObjectHierarchy,
160    InternalReference,
161    MemoryUsage,
162    MetadataExtractor,
163    // Processing
164    MetadataProcessor,
165    ObjectStatistics,
166    ObjectSummary,
167    PerformanceMetrics,
168    ProcessingOptions,
169    RelationshipAnalyzer,
170    create_comprehensive_processor,
171    create_performance_processor,
172    // Convenience functions
173    create_processor as create_metadata_processor,
174    extract_basic_metadata,
175    extract_metadata_with_config,
176    get_asset_statistics,
177    get_recommended_config,
178    is_extraction_supported,
179};
180pub use object::{ObjectInfo as UnityObjectInfo, UnityObject};
181pub use reader::{BinaryReader, ByteOrder};
182pub use typetree::{
183    ParsingStats as TypeTreeParsingStats,
184    TypeInfo,
185    TypeRegistry as TypeTreeRegistry,
186    // Core types
187    TypeTree,
188    TypeTreeBuilder,
189    // Information
190    TypeTreeInfo,
191    TypeTreeNode,
192    TypeTreeParser,
193    // Processing
194    TypeTreeProcessor,
195    TypeTreeSerializer,
196    TypeTreeStatistics,
197    TypeTreeValidator,
198    ValidationReport,
199    build_common_typetree,
200    // Convenience functions
201    create_processor as create_typetree_processor,
202    get_parsing_method as get_typetree_parsing_method,
203    get_typetree_info,
204    is_version_supported as is_typetree_version_supported,
205    parse_object_with_typetree,
206    parse_typetree,
207    serialize_object_with_typetree,
208    validate_typetree,
209};
210pub use unity_objects::{GameObject, ObjectRef, Quaternion, Transform, Vector3};
211pub use unity_version::{UnityFeature, UnityVersion, UnityVersionType, VersionCompatibility};
212pub use webfile::{WebFile, WebFileCompression};
213
214// Re-export feature-gated types
215#[cfg(feature = "texture")]
216pub use texture::{
217    BasicDecoder,
218    CompressedDecoder,
219    CrunchDecoder,
220    ExportOptions,
221    GLTextureSettings,
222    MobileDecoder,
223    StreamingInfo,
224    // Core types
225    Texture2D,
226    Texture2DConverter,
227    // Processors and converters
228    Texture2DProcessor,
229    // Decoders
230    TextureDecoder,
231    // Helpers
232    TextureExporter,
233    TextureFormat,
234    TextureProcessor,
235    TextureSwizzler,
236    // Convenience functions
237    create_processor,
238    decode_texture_data,
239    export_image,
240    get_supported_formats,
241    is_format_supported,
242};
243
244#[cfg(feature = "audio")]
245pub use audio::{
246    AudioAnalysis,
247    // Core types
248    AudioClip,
249    AudioClipConverter,
250    AudioClipMeta,
251    // Processors and converters
252    AudioClipProcessor,
253    AudioCompressionFormat,
254    // Decoder
255    AudioDecoder,
256    // Export
257    AudioExporter,
258    AudioFormat,
259    AudioFormatInfo,
260    AudioInfo,
261    AudioProcessor,
262    AudioProperties,
263    DecodedAudio,
264    FMODSoundType,
265    // Convenience functions (with audio prefix to avoid conflicts)
266    create_processor as create_audio_processor,
267    decode_audio_data,
268    export_audio,
269    get_supported_formats as get_supported_audio_formats,
270    is_format_supported as is_audio_format_supported,
271};
272
273#[cfg(feature = "sprite")]
274pub use sprite::{
275    ProcessingOptions as SpriteProcessingOptions,
276    // Core sprite types
277    Sprite,
278    SpriteAtlas,
279    SpriteBorder,
280    // Configuration and results
281    SpriteConfig,
282    SpriteInfo,
283    // Processing
284    SpriteManager,
285    SpriteOffset,
286    SpriteParser,
287    SpritePivot,
288    SpriteProcessor,
289    SpriteRect,
290    SpriteRenderData,
291    SpriteResult,
292    SpriteSettings,
293    SpriteStats,
294    create_full_manager as create_full_sprite_manager,
295    // Convenience functions
296    create_manager as create_sprite_manager,
297    create_performance_manager as create_performance_sprite_manager,
298    extract_sprite_image,
299    get_recommended_config as get_recommended_sprite_config,
300    get_sprite_area,
301    get_sprite_aspect_ratio,
302    is_atlas_sprite,
303    is_nine_slice_sprite,
304    is_sprite_feature_supported,
305    parse_sprite,
306    validate_sprite,
307};
308
309#[cfg(feature = "mesh")]
310pub use mesh::{
311    AABB,
312    BlendShape,
313    BlendShapeChannel,
314    // Blend shape types
315    BlendShapeData,
316    BlendShapeVertex,
317    ChannelInfo,
318    // Compression types
319    CompressedMesh,
320    // Core mesh types
321    Mesh,
322    // Configuration and results
323    MeshConfig,
324    MeshInfo,
325    // Processing
326    MeshManager,
327    MeshParser,
328    MeshProcessor,
329    MeshResult,
330    MeshStats,
331    PackedFloatVector,
332    PackedIntVector,
333    ProcessingOptions as MeshProcessingOptions,
334    // Streaming and info
335    StreamingInfo as MeshStreamingInfo,
336    SubMesh,
337    VertexData,
338    create_full_manager as create_full_mesh_manager,
339    // Convenience functions
340    create_manager as create_mesh_manager,
341    create_performance_manager as create_performance_mesh_manager,
342    export_mesh_to_obj,
343    get_mesh_bounds,
344    get_recommended_config as get_recommended_mesh_config,
345    get_triangle_count,
346    get_vertex_count,
347    has_blend_shapes,
348    has_streaming_data,
349    is_compressed_mesh,
350    is_mesh_feature_supported,
351    parse_mesh,
352    validate_mesh,
353};
354
355#[cfg(test)]
356mod tests {
357    #[test]
358    fn test_basic_functionality() {
359        // Basic smoke test
360        assert_eq!(2 + 2, 4);
361    }
362
363    #[cfg(feature = "async")]
364    #[tokio::test]
365    async fn test_async_functionality() {
366        // Test that async features compile
367        let dummy_data = [0u8; 32];
368
369        // Test basic async functionality - for now just verify the feature compiles
370        // TODO: Implement actual async methods when needed
371        let _result = tokio::task::yield_now().await;
372
373        println!("✅ Async feature compiles correctly");
374
375        // Note: AssetBundle::from_bytes_async and SerializedFile::from_bytes_async
376        // are not yet implemented. They would be added when async support is needed.
377        assert!(!dummy_data.is_empty());
378    }
379}