unity-asset-decode 0.2.0

Decode/export helpers for Unity assets (Texture/Audio/Sprite/Mesh) built on unity-asset-binary
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
//! Audio Processing Tests
//!
//! This file tests audio processing functionality including AudioClip parsing,
//! format detection, and audio data extraction.

#![cfg(feature = "audio")]
#![allow(unused_imports)]
#![allow(dead_code)]

use std::fs;
use std::path::Path;
use unity_asset_decode::audio::{AudioClip, AudioCompressionFormat, AudioProcessor};
use unity_asset_decode::bundle::load_bundle_from_memory;
use unity_asset_decode::object::UnityObject;

const SAMPLES_DIR: &str = "tests/samples";

/// Get all sample files in the samples directory
fn get_sample_files() -> Vec<std::path::PathBuf> {
    let samples_path = Path::new(SAMPLES_DIR);
    if !samples_path.exists() {
        return Vec::new();
    }

    let mut files = Vec::new();
    if let Ok(entries) = fs::read_dir(samples_path) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_file() {
                files.push(path);
            }
        }
    }
    files
}

/// Test audio format detection and classification
#[test]
fn test_audio_format_detection() {
    println!("=== Audio Format Detection Test ===");

    // Test all supported audio formats
    let formats = [
        (AudioCompressionFormat::PCM, "PCM", "wav", false, false),
        (
            AudioCompressionFormat::Vorbis,
            "Ogg Vorbis",
            "ogg",
            true,
            true,
        ),
        (AudioCompressionFormat::ADPCM, "ADPCM", "wav", true, true),
        (AudioCompressionFormat::MP3, "MP3", "mp3", true, true),
        (AudioCompressionFormat::AAC, "AAC", "aac", true, true),
        (
            AudioCompressionFormat::VAG,
            "PlayStation VAG",
            "vag",
            true,
            true,
        ),
        (AudioCompressionFormat::XMA, "Xbox XMA", "xma", true, true),
        (
            AudioCompressionFormat::ATRAC9,
            "PlayStation ATRAC9",
            "at9",
            true,
            true,
        ),
    ];

    for (format, expected_name, expected_ext, expected_compressed, expected_lossy) in formats {
        let info = format.info();
        println!("  Testing format: {:?}", format);
        println!("    Name: {} (expected: {})", info.name, expected_name);
        println!(
            "    Extension: {} (expected: {})",
            info.extension, expected_ext
        );
        println!(
            "    Compressed: {} (expected: {})",
            info.compressed, expected_compressed
        );
        println!("    Lossy: {} (expected: {})", info.lossy, expected_lossy);

        assert_eq!(info.name, expected_name);
        assert_eq!(info.extension, expected_ext);
        assert_eq!(info.compressed, expected_compressed);
        assert_eq!(info.lossy, expected_lossy);
        assert_eq!(format.extension(), expected_ext);
        assert_eq!(format.is_compressed(), expected_compressed);
        assert_eq!(format.is_lossy(), expected_lossy);
    }

    println!("  ✓ All audio format tests passed");
}

/// Test AudioClip object creation and manipulation
#[test]
fn test_audioclip_creation() {
    println!("=== AudioClip Creation Test ===");

    // Test default AudioClip
    let default_clip = AudioClip::default();
    println!("  Default AudioClip:");
    println!("    Name: '{}'", default_clip.name);
    println!("    Data size: {} bytes", default_clip.data.len());

    assert_eq!(default_clip.name, "");
    assert_eq!(default_clip.data.len(), 0);

    // Test AudioClip with specific format
    let formats_to_test = [
        AudioCompressionFormat::PCM,
        AudioCompressionFormat::Vorbis,
        AudioCompressionFormat::MP3,
    ];

    for format in formats_to_test {
        let clip = AudioClip::new(format!("test_{:?}", format), format);
        println!("  AudioClip with {:?} format:", format);
        println!("    Name: '{}'", clip.name);

        // Check that the format is correctly set
        if let unity_asset_decode::audio::types::AudioClipMeta::Modern {
            compression_format, ..
        } = &clip.meta
        {
            assert_eq!(*compression_format, format);
            println!("    Format correctly set: {:?}", compression_format);
        }
    }

    println!("  ✓ AudioClip creation tests passed");
}

/// Test audio processing from real Unity files
#[test]
fn test_audio_processing_from_files() {
    println!("=== Audio Processing from Files Test ===");

    let sample_files = get_sample_files();
    let mut total_objects = 0;
    let mut audio_objects = 0;
    let mut processed_audio = 0;

    for file_path in sample_files {
        let file_name = file_path.file_name().unwrap_or_default().to_string_lossy();
        println!("  Processing file: {}", file_name);

        if let Ok(data) = fs::read(&file_path) {
            match load_bundle_from_memory(data) {
                Ok(bundle) => {
                    for asset in &bundle.assets {
                        for asset_object_info in &asset.objects {
                            total_objects += 1;

                            let unity_object =
                                UnityObject::from_serialized_file(asset, asset_object_info)
                                    .unwrap_or_else(|_| {
                                        let fallback_data = asset
                                            .object_bytes(asset_object_info)
                                            .map(|b| b.to_vec())
                                            .unwrap_or_default();
                                        UnityObject::from_raw(
                                            asset_object_info.type_id,
                                            asset_object_info.path_id,
                                            fallback_data,
                                        )
                                    });
                            let class_name = unity_object.class_name().to_string();

                            // Look for AudioClip objects (Class ID 83) or any audio-related objects
                            if unity_object.class_id() == 83
                                || class_name.contains("Audio")
                                || class_name == "Object"
                            {
                                // Many audio objects are classified as generic "Object"
                                audio_objects += 1;
                                println!(
                                    "    Found audio object: {} (ID:{}, PathID:{})",
                                    class_name,
                                    unity_object.class_id(),
                                    unity_object.path_id()
                                );

                                processed_audio += 1;
                                let unity_class = unity_object.as_unity_class();

                                // Try to get audio properties
                                if let Some(unity_asset_core::UnityValue::String(name)) =
                                    unity_class.get("m_Name")
                                {
                                    println!("      Audio name: '{}'", name);
                                }

                                // Look for audio format information
                                if let Some(unity_asset_core::UnityValue::Integer(format_id)) =
                                    unity_class.get("m_CompressionFormat")
                                {
                                    let format = AudioCompressionFormat::from(*format_id as i32);
                                    println!("      Format: {:?} ({})", format, format.info().name);
                                }

                                // Look for audio data size
                                if let Some(unity_asset_core::UnityValue::Integer(size)) =
                                    unity_class.get("m_Size")
                                {
                                    println!("      Data size: {} bytes", size);
                                }
                            }
                        }
                    }
                }
                Err(e) => {
                    println!("    Failed to load bundle: {}", e);
                }
            }
        }
    }

    println!("\nAudio Processing Results:");
    println!("  Total objects: {}", total_objects);
    println!("  Audio objects found: {}", audio_objects);
    println!("  Successfully processed: {}", processed_audio);

    if audio_objects > 0 {
        let processing_rate = (processed_audio as f64 / audio_objects as f64) * 100.0;
        println!("  Processing success rate: {:.1}%", processing_rate);
        assert!(
            processing_rate >= 50.0,
            "Should process at least 50% of audio objects"
        );
    }

    println!("  ✓ Audio processing test completed");
}

/// Test audio format conversion capabilities
#[test]
fn test_audio_format_conversion() {
    println!("=== Audio Format Conversion Test ===");

    // Test MIME type detection
    let mime_tests = [
        (AudioCompressionFormat::PCM, "audio/wav"),
        (AudioCompressionFormat::ADPCM, "audio/wav"),
        (AudioCompressionFormat::Vorbis, "audio/ogg"),
        (AudioCompressionFormat::MP3, "audio/mpeg"),
        (AudioCompressionFormat::AAC, "audio/aac"),
        (AudioCompressionFormat::VAG, "application/octet-stream"),
    ];

    for (format, expected_mime) in mime_tests {
        let mime_type = format.mime_type();
        println!(
            "  Format {:?} -> MIME: {} (expected: {})",
            format, mime_type, expected_mime
        );
        assert_eq!(mime_type, expected_mime);
    }

    // Test format support detection
    let supported_formats = [
        AudioCompressionFormat::PCM,
        AudioCompressionFormat::Vorbis,
        AudioCompressionFormat::ADPCM,
        AudioCompressionFormat::MP3,
        AudioCompressionFormat::AAC,
    ];

    let unsupported_formats = [
        AudioCompressionFormat::VAG,
        AudioCompressionFormat::HEVAG,
        AudioCompressionFormat::XMA,
        AudioCompressionFormat::GCADPCM,
        AudioCompressionFormat::ATRAC9,
    ];

    for format in supported_formats {
        assert!(
            format.is_supported(),
            "Format {:?} should be supported",
            format
        );
        println!("  ✓ Format {:?} is supported", format);
    }

    for format in unsupported_formats {
        assert!(
            !format.is_supported(),
            "Format {:?} should not be supported",
            format
        );
        println!(
            "  ⚠ Format {:?} is not supported (requires specialized decoder)",
            format
        );
    }

    println!("  ✓ Audio format conversion tests passed");
}

/// Test advanced audio extraction and analysis
#[test]
fn test_advanced_audio_extraction() {
    println!("=== Advanced Audio Extraction Test ===");

    let sample_files = get_sample_files();
    let mut total_objects = 0;
    let mut potential_audio = 0;
    let mut analyzed_objects = 0;

    for file_path in sample_files {
        let file_name = file_path.file_name().unwrap_or_default().to_string_lossy();
        println!("  Analyzing file: {}", file_name);

        if let Ok(data) = fs::read(&file_path) {
            match load_bundle_from_memory(data) {
                Ok(bundle) => {
                    for asset in &bundle.assets {
                        for asset_object_info in &asset.objects {
                            total_objects += 1;

                            let unity_object =
                                UnityObject::from_serialized_file(asset, asset_object_info)
                                    .unwrap_or_else(|_| {
                                        let fallback_data = asset
                                            .object_bytes(asset_object_info)
                                            .map(|b| b.to_vec())
                                            .unwrap_or_default();
                                        UnityObject::from_raw(
                                            asset_object_info.type_id,
                                            asset_object_info.path_id,
                                            fallback_data,
                                        )
                                    });
                            let class_name = unity_object.class_name().to_string();
                            let unity_class = unity_object.as_unity_class();
                            analyzed_objects += 1;

                            // Look for audio-related properties
                            let has_audio_props = unity_class.properties().keys().any(|key| {
                                let lower = key.to_lowercase();
                                lower.contains("audio")
                                    || lower.contains("sound")
                                    || lower.contains("clip")
                                    || key.contains("m_CompressionFormat")
                                    || key.contains("m_Frequency")
                                    || key.contains("m_Channels")
                                    || key.contains("m_BitsPerSample")
                            });

                            if has_audio_props {
                                potential_audio += 1;
                                println!(
                                    "    Potential audio object: {} (ID:{}, PathID:{})",
                                    class_name,
                                    unity_object.class_id(),
                                    unity_object.path_id()
                                );

                                if let Some(unity_asset_core::UnityValue::String(name)) =
                                    unity_class.get("m_Name")
                                {
                                    println!("      Name: '{}'", name);
                                }

                                if let Some(unity_asset_core::UnityValue::Integer(format_id)) =
                                    unity_class.get("m_CompressionFormat")
                                {
                                    let format = AudioCompressionFormat::from((*format_id) as i32);
                                    println!("      Format: {:?} ({})", format, format.info().name);
                                }

                                if let Some(unity_asset_core::UnityValue::Integer(freq)) =
                                    unity_class.get("m_Frequency")
                                {
                                    println!("      Frequency: {} Hz", freq);
                                }

                                if let Some(unity_asset_core::UnityValue::Integer(channels)) =
                                    unity_class.get("m_Channels")
                                {
                                    println!("      Channels: {}", channels);
                                }

                                if let Some(unity_asset_core::UnityValue::Integer(bits)) =
                                    unity_class.get("m_BitsPerSample")
                                {
                                    println!("      Bits per sample: {}", bits);
                                }

                                if let Some(unity_asset_core::UnityValue::Integer(size)) =
                                    unity_class.get("m_Size")
                                {
                                    println!("      Data size: {} bytes", size);
                                }

                                if unity_class.get("m_Resource").is_some() {
                                    println!("      Has streaming resource");
                                }
                            }

                            // Also check for large binary data that might be audio
                            let raw_len = unity_object.raw_data().len();
                            if raw_len > 1024 && class_name == "Object" {
                                let preview_len = 32.min(raw_len);
                                let data_preview = &unity_object.raw_data()[..preview_len];
                                let has_audio_signature = data_preview.iter().any(|&b| b != 0)
                                    && data_preview.len() >= 16;

                                if has_audio_signature {
                                    println!(
                                        "    Large binary object (potential audio): {} bytes (ID:{}, PathID:{})",
                                        raw_len,
                                        unity_object.class_id(),
                                        unity_object.path_id()
                                    );
                                }
                            }
                        }
                    }
                }
                Err(e) => {
                    println!("    Failed to load bundle: {}", e);
                }
            }
        }
    }

    println!("\nAdvanced Audio Analysis Results:");
    println!("  Total objects: {}", total_objects);
    println!("  Analyzed objects: {}", analyzed_objects);
    println!("  Potential audio objects: {}", potential_audio);

    if analyzed_objects > 0 {
        let analysis_rate = (analyzed_objects as f64 / total_objects as f64) * 100.0;
        println!("  Analysis success rate: {:.1}%", analysis_rate);
        assert!(
            analysis_rate >= 80.0,
            "Should analyze at least 80% of objects"
        );
    }

    println!("  ✓ Advanced audio extraction test completed");
}