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
use unity_asset_binary::asset::SerializedType;
use unity_asset_binary::reader::{BinaryReader, ByteOrder};
use unity_asset_binary::typetree::{TypeTree, TypeTreeNode, TypeTreeSerializer};
use unity_asset_core::UnityValue;

fn push_aligned_string_le(out: &mut Vec<u8>, s: &str) {
    let bytes = s.as_bytes();
    out.extend_from_slice(&(bytes.len() as i32).to_le_bytes());
    out.extend_from_slice(bytes);
    while !out.len().is_multiple_of(4) {
        out.push(0);
    }
}

#[test]
fn referenced_object_data_is_parsed_via_ref_types() {
    // Build a ref type tree: { m_Value: int }
    let mut ref_tree = TypeTree::new();
    let mut ref_root = TypeTreeNode::with_info("MyClass".to_string(), "MyClass".to_string(), -1);
    ref_root.children.push(TypeTreeNode::with_info(
        "int".to_string(),
        "m_Value".to_string(),
        -1,
    ));
    ref_tree.add_node(ref_root);

    let mut ref_type = SerializedType::new(0);
    ref_type.class_name = "MyClass".to_string();
    ref_type.namespace = "MyNS".to_string();
    ref_type.assembly_name = "MyAsm".to_string();
    ref_type.type_tree = ref_tree;

    // Build an object tree containing a ReferencedObject with a `type` object and `data` payload.
    let mut tree = TypeTree::new();
    let mut root = TypeTreeNode::with_info("Root".to_string(), "Root".to_string(), -1);

    let mut ref_obj =
        TypeTreeNode::with_info("ReferencedObject".to_string(), "m_Ref".to_string(), -1);
    let mut type_node = TypeTreeNode::with_info("TypeInfo".to_string(), "type".to_string(), -1);
    type_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "class".to_string(),
        -1,
    ));
    type_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "ns".to_string(),
        -1,
    ));
    type_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "asm".to_string(),
        -1,
    ));
    ref_obj.children.push(type_node);
    ref_obj.children.push(TypeTreeNode::with_info(
        "ReferencedObjectData".to_string(),
        "data".to_string(),
        -1,
    ));

    root.children.push(ref_obj);
    tree.add_node(root);

    let mut bytes = Vec::new();
    push_aligned_string_le(&mut bytes, "MyClass");
    push_aligned_string_le(&mut bytes, "MyNS");
    push_aligned_string_le(&mut bytes, "MyAsm");
    bytes.extend_from_slice(&123i32.to_le_bytes());

    let mut reader = BinaryReader::new(&bytes, ByteOrder::Little);
    let serializer = TypeTreeSerializer::new(&tree);
    let out = serializer
        .parse_object_detailed_with_ref_types(
            &mut reader,
            unity_asset_binary::typetree::TypeTreeParseOptions::default(),
            std::slice::from_ref(&ref_type),
        )
        .unwrap();

    let UnityValue::Object(m_ref) = out.properties.get("m_Ref").expect("m_Ref present") else {
        panic!("m_Ref should be object");
    };

    let UnityValue::Object(typ) = m_ref.get("type").expect("type present") else {
        panic!("type should be object");
    };
    assert_eq!(typ.get("class").and_then(|v| v.as_str()), Some("MyClass"));
    assert_eq!(typ.get("ns").and_then(|v| v.as_str()), Some("MyNS"));
    assert_eq!(typ.get("asm").and_then(|v| v.as_str()), Some("MyAsm"));

    let UnityValue::Object(data) = m_ref.get("data").expect("data present") else {
        panic!("data should be object");
    };
    assert_eq!(data.get("m_Value").and_then(|v| v.as_i64()), Some(123));
}

#[test]
fn referenced_object_data_resolves_via_unity_field_aliases() {
    // Unity sometimes encodes managed reference type triplets using m_ClassName/m_NameSpace/m_AssemblyName.
    let mut ref_tree = TypeTree::new();
    let mut ref_root = TypeTreeNode::with_info("MyClass".to_string(), "MyClass".to_string(), -1);
    ref_root.children.push(TypeTreeNode::with_info(
        "int".to_string(),
        "m_Value".to_string(),
        -1,
    ));
    ref_tree.add_node(ref_root);

    let mut ref_type = SerializedType::new(0);
    ref_type.class_name = "MyClass".to_string();
    ref_type.namespace = "MyNS".to_string();
    ref_type.assembly_name = "MyAsm".to_string();
    ref_type.type_tree = ref_tree;

    let mut tree = TypeTree::new();
    let mut root = TypeTreeNode::with_info("Root".to_string(), "Root".to_string(), -1);

    let mut ref_obj =
        TypeTreeNode::with_info("ReferencedObject".to_string(), "m_Ref".to_string(), -1);
    let mut type_node = TypeTreeNode::with_info("TypeInfo".to_string(), "type".to_string(), -1);
    type_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "m_ClassName".to_string(),
        -1,
    ));
    type_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "m_NameSpace".to_string(),
        -1,
    ));
    type_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "m_AssemblyName".to_string(),
        -1,
    ));
    ref_obj.children.push(type_node);
    ref_obj.children.push(TypeTreeNode::with_info(
        "ReferencedObjectData".to_string(),
        "data".to_string(),
        -1,
    ));
    root.children.push(ref_obj);
    tree.add_node(root);

    let mut bytes = Vec::new();
    push_aligned_string_le(&mut bytes, "MyClass");
    push_aligned_string_le(&mut bytes, "MyNS");
    push_aligned_string_le(&mut bytes, "MyAsm");
    bytes.extend_from_slice(&456i32.to_le_bytes());

    let mut reader = BinaryReader::new(&bytes, ByteOrder::Little);
    let serializer = TypeTreeSerializer::new(&tree);
    let out = serializer
        .parse_object_detailed_with_ref_types(
            &mut reader,
            unity_asset_binary::typetree::TypeTreeParseOptions::default(),
            std::slice::from_ref(&ref_type),
        )
        .unwrap();

    let UnityValue::Object(m_ref) = out.properties.get("m_Ref").expect("m_Ref present") else {
        panic!("m_Ref should be object");
    };
    let UnityValue::Object(data) = m_ref.get("data").expect("data present") else {
        panic!("data should be object");
    };
    assert_eq!(data.get("m_Value").and_then(|v| v.as_i64()), Some(456));
    assert_eq!(reader.position() as usize, bytes.len());
}

#[test]
fn referenced_object_fallback_marks_unresolved_type() {
    // No ref_types provided; ReferencedObjectData should fall back but remain explainable.
    let mut tree = TypeTree::new();
    let mut root = TypeTreeNode::with_info("Root".to_string(), "Root".to_string(), -1);

    let mut ref_obj =
        TypeTreeNode::with_info("ReferencedObject".to_string(), "m_Ref".to_string(), -1);
    let mut type_node = TypeTreeNode::with_info("TypeInfo".to_string(), "type".to_string(), -1);
    type_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "class".to_string(),
        -1,
    ));
    type_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "ns".to_string(),
        -1,
    ));
    type_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "asm".to_string(),
        -1,
    ));
    ref_obj.children.push(type_node);
    // Placeholder payload: an int field so we can ensure the reader stays in sync.
    let mut data_node =
        TypeTreeNode::with_info("ReferencedObjectData".to_string(), "data".to_string(), -1);
    data_node.children.push(TypeTreeNode::with_info(
        "int".to_string(),
        "m_Value".to_string(),
        -1,
    ));
    ref_obj.children.push(data_node);

    root.children.push(ref_obj);
    tree.add_node(root);

    let mut bytes = Vec::new();
    push_aligned_string_le(&mut bytes, "MissingClass");
    push_aligned_string_le(&mut bytes, "NS");
    push_aligned_string_le(&mut bytes, "ASM");
    bytes.extend_from_slice(&7i32.to_le_bytes());

    let mut reader = BinaryReader::new(&bytes, ByteOrder::Little);
    let serializer = TypeTreeSerializer::new(&tree);
    let out = serializer
        .parse_object_prefix_detailed(
            &mut reader,
            unity_asset_binary::typetree::TypeTreeParseOptions::default(),
            1,
        )
        .unwrap();

    let UnityValue::Object(m_ref) = out.properties.get("m_Ref").expect("m_Ref present") else {
        panic!("m_Ref should be object");
    };
    assert_eq!(
        m_ref
            .get("_referenced_type_unresolved")
            .and_then(|v| v.as_bool()),
        Some(true)
    );
    assert_eq!(
        m_ref.get("_referenced_type_key").and_then(|v| v.as_str()),
        Some("MissingClass|NS|ASM")
    );

    let UnityValue::Object(data) = m_ref.get("data").expect("data present") else {
        panic!("data should be object");
    };
    assert_eq!(data.get("m_Value").and_then(|v| v.as_i64()), Some(7));
    assert_eq!(reader.position() as usize, bytes.len());
}

#[test]
fn managed_references_registry_is_consumed_without_affecting_following_fields() {
    // The parser should consume `ManagedReferencesRegistry` bytes without allocating, and keep the
    // reader in sync for following fields.
    let mut tree = TypeTree::new();
    let mut root = TypeTreeNode::with_info("Root".to_string(), "Root".to_string(), -1);

    let mut registry = TypeTreeNode::with_info(
        "ManagedReferencesRegistry".to_string(),
        "m_Registry".to_string(),
        -1,
    );
    let mut vec_node = TypeTreeNode::with_info("vector".to_string(), "m_Data".to_string(), -1);
    let mut array_node = TypeTreeNode::with_info("Array".to_string(), "Array".to_string(), -1);
    array_node.meta_flags = 0x4000; // align to 4 after the array payload
    array_node.children.push(TypeTreeNode::with_info(
        "int".to_string(),
        "size".to_string(),
        -1,
    ));
    array_node.children.push(TypeTreeNode::with_info(
        "UInt8".to_string(),
        "data".to_string(),
        -1,
    ));
    vec_node.children.push(array_node);
    registry.children.push(vec_node);

    root.children.push(registry);
    root.children.push(TypeTreeNode::with_info(
        "int".to_string(),
        "m_Next".to_string(),
        -1,
    ));
    tree.add_node(root);

    // Registry bytes:
    // - array size=1
    // - one byte
    // - 3 bytes padding (Array node has align flag)
    // Then m_Next (int).
    let mut bytes = Vec::new();
    bytes.extend_from_slice(&1i32.to_le_bytes());
    bytes.push(0xAA);
    bytes.extend_from_slice(&[0u8; 3]);
    bytes.extend_from_slice(&0x11223344i32.to_le_bytes());

    let mut reader = BinaryReader::new(&bytes, ByteOrder::Little);
    let serializer = TypeTreeSerializer::new(&tree);
    let out = serializer
        .parse_object_detailed(
            &mut reader,
            unity_asset_binary::typetree::TypeTreeParseOptions::default(),
        )
        .unwrap();

    assert!(
        matches!(out.properties.get("m_Registry"), Some(UnityValue::Null)),
        "ManagedReferencesRegistry should be skipped (Null) to avoid large allocations"
    );
    assert_eq!(
        out.properties.get("m_Next").and_then(|v| v.as_i64()),
        Some(0x11223344)
    );
    assert_eq!(reader.position() as usize, bytes.len());
}

#[test]
fn managed_references_registry_skips_large_byte_arrays_and_keeps_reader_in_sync() {
    let mut tree = TypeTree::new();
    let mut root = TypeTreeNode::with_info("Root".to_string(), "Root".to_string(), -1);

    let mut registry = TypeTreeNode::with_info(
        "ManagedReferencesRegistry".to_string(),
        "m_Registry".to_string(),
        -1,
    );
    let mut vec_node = TypeTreeNode::with_info("vector".to_string(), "m_Data".to_string(), -1);
    let mut array_node = TypeTreeNode::with_info("Array".to_string(), "Array".to_string(), -1);
    array_node.meta_flags = 0x4000; // align to 4 after the array payload
    array_node.children.push(TypeTreeNode::with_info(
        "int".to_string(),
        "size".to_string(),
        -1,
    ));
    array_node.children.push(TypeTreeNode::with_info(
        "UInt8".to_string(),
        "data".to_string(),
        -1,
    ));
    vec_node.children.push(array_node);
    registry.children.push(vec_node);

    root.children.push(registry);
    root.children.push(TypeTreeNode::with_info(
        "int".to_string(),
        "m_Next".to_string(),
        -1,
    ));
    tree.add_node(root);

    let n: i32 = 128 * 1024;
    let mut bytes = Vec::new();
    bytes.extend_from_slice(&n.to_le_bytes());
    bytes.extend(std::iter::repeat_n(0xABu8, n as usize));
    while bytes.len() % 4 != 0 {
        bytes.push(0);
    }
    bytes.extend_from_slice(&0x55667788i32.to_le_bytes());

    let mut reader = BinaryReader::new(&bytes, ByteOrder::Little);
    let serializer = TypeTreeSerializer::new(&tree);
    let out = serializer
        .parse_object_detailed(
            &mut reader,
            unity_asset_binary::typetree::TypeTreeParseOptions::default(),
        )
        .unwrap();

    assert!(matches!(
        out.properties.get("m_Registry"),
        Some(UnityValue::Null)
    ));
    assert_eq!(
        out.properties.get("m_Next").and_then(|v| v.as_i64()),
        Some(0x55667788)
    );
    assert_eq!(reader.position() as usize, bytes.len());
}

#[test]
fn managed_references_registry_skips_nested_string_vectors_and_keeps_reader_in_sync() {
    let mut tree = TypeTree::new();
    let mut root = TypeTreeNode::with_info("Root".to_string(), "Root".to_string(), -1);

    let mut registry = TypeTreeNode::with_info(
        "ManagedReferencesRegistry".to_string(),
        "m_Registry".to_string(),
        -1,
    );
    registry.children.push(TypeTreeNode::with_info(
        "int".to_string(),
        "m_Version".to_string(),
        -1,
    ));

    let mut vec_node = TypeTreeNode::with_info("vector".to_string(), "m_Names".to_string(), -1);
    let mut array_node = TypeTreeNode::with_info("Array".to_string(), "Array".to_string(), -1);
    array_node.meta_flags = 0x4000; // align to 4 after the array payload
    array_node.children.push(TypeTreeNode::with_info(
        "int".to_string(),
        "size".to_string(),
        -1,
    ));
    array_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "data".to_string(),
        -1,
    ));
    vec_node.children.push(array_node);
    registry.children.push(vec_node);

    root.children.push(registry);
    root.children.push(TypeTreeNode::with_info(
        "int".to_string(),
        "m_Next".to_string(),
        -1,
    ));
    tree.add_node(root);

    let mut bytes = Vec::new();
    bytes.extend_from_slice(&2i32.to_le_bytes()); // m_Version
    bytes.extend_from_slice(&2i32.to_le_bytes()); // size
    push_aligned_string_le(&mut bytes, "a");
    push_aligned_string_le(&mut bytes, "bc");
    while bytes.len() % 4 != 0 {
        bytes.push(0);
    }
    bytes.extend_from_slice(&0x01020304i32.to_le_bytes());

    let mut reader = BinaryReader::new(&bytes, ByteOrder::Little);
    let serializer = TypeTreeSerializer::new(&tree);
    let out = serializer
        .parse_object_detailed(
            &mut reader,
            unity_asset_binary::typetree::TypeTreeParseOptions::default(),
        )
        .unwrap();

    assert!(matches!(
        out.properties.get("m_Registry"),
        Some(UnityValue::Null)
    ));
    assert_eq!(
        out.properties.get("m_Next").and_then(|v| v.as_i64()),
        Some(0x01020304)
    );
    assert_eq!(reader.position() as usize, bytes.len());
}

#[test]
fn scan_pptrs_can_traverse_managed_reference_payloads_via_ref_types() {
    // Build a ref type tree: { m_Ptr: PPtr<Object> }.
    let mut ref_tree = TypeTree::new();
    let mut ref_root = TypeTreeNode::with_info("MyClass".to_string(), "MyClass".to_string(), -1);
    let mut pptr = TypeTreeNode::with_info("PPtr<Object>".to_string(), "m_Ptr".to_string(), -1);
    pptr.children.push(TypeTreeNode::with_info(
        "int".to_string(),
        "m_FileID".to_string(),
        -1,
    ));
    pptr.children.push(TypeTreeNode::with_info(
        "long long".to_string(),
        "m_PathID".to_string(),
        -1,
    ));
    ref_root.children.push(pptr);
    ref_tree.add_node(ref_root);

    let mut ref_type = SerializedType::new(0);
    ref_type.class_name = "MyClass".to_string();
    ref_type.namespace = "MyNS".to_string();
    ref_type.assembly_name = "MyAsm".to_string();
    ref_type.type_tree = ref_tree;

    // Root contains ReferencedObject -> type triplet + data payload.
    let mut tree = TypeTree::new();
    let mut root = TypeTreeNode::with_info("Root".to_string(), "Root".to_string(), -1);
    let mut ref_obj =
        TypeTreeNode::with_info("ReferencedObject".to_string(), "m_Ref".to_string(), -1);
    let mut type_node = TypeTreeNode::with_info("TypeInfo".to_string(), "type".to_string(), -1);
    type_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "class".to_string(),
        -1,
    ));
    type_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "ns".to_string(),
        -1,
    ));
    type_node.children.push(TypeTreeNode::with_info(
        "string".to_string(),
        "asm".to_string(),
        -1,
    ));
    ref_obj.children.push(type_node);
    ref_obj.children.push(TypeTreeNode::with_info(
        "ReferencedObjectData".to_string(),
        "data".to_string(),
        -1,
    ));
    root.children.push(ref_obj);
    tree.add_node(root);

    let mut bytes = Vec::new();
    push_aligned_string_le(&mut bytes, "MyClass");
    push_aligned_string_le(&mut bytes, "MyNS");
    push_aligned_string_le(&mut bytes, "MyAsm");
    bytes.extend_from_slice(&0i32.to_le_bytes()); // m_FileID
    bytes.extend_from_slice(&1234i64.to_le_bytes()); // m_PathID

    let mut reader = BinaryReader::new(&bytes, ByteOrder::Little);
    let serializer = TypeTreeSerializer::new(&tree);
    let scan = serializer
        .scan_pptrs_with_ref_types(&mut reader, Some(std::slice::from_ref(&ref_type)))
        .unwrap();

    assert_eq!(scan.internal, vec![1234]);
    assert!(scan.external.is_empty());
    assert_eq!(reader.position() as usize, bytes.len());
}