unityrs-export 1.0.0

Unity asset exports beside the textures: sprites, the NGUI atlas cutter and its crops, and the UnityPy extractor tree.
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
// Ported from UnityPy 1.25.2 (MIT): the names a Sprite's or a Texture2D's attribute cannot be set under (UnityPyBoost/TypeTreeHelper.cpp's parse_class, classes/legacy_patch/Sprite.py, classes/legacy_patch/Texture2D.py and classes/Object.py), for SETTERLESS; crunched texture version indexing (export/Texture2DConverter.py and helpers/UnityVersion.py). See THIRD_PARTY_LICENSES.
//! The cutter's reads (`docs/BATCH_SPEC.md` 4.4 steps 1 and 2, section 5):
//! a MonoBehaviour's head, a peeked name, a texture's fields, an atlas's
//! material name, each as the shape the cutter reads or a [`Class`].
//!
//! **The head** (the second carried question). UnityPy reads a
//! MonoBehaviour's head with its TPK's MonoBehaviour tree for the file's
//! version, `check_read` off, whatever typetree the object stores
//! (`parse_monobehaviour_head`). Version 1 configures no typetree provider
//! (`docs/plans/batch.md` A8), so the cutter reads it with fixed rows of
//! its own instead, nothing in them taken from a TPK (`docs/plans/core.md`
//! Q2). UnityPy 1.25.2's MIT class definitions name the four fields
//! (`classes/generated.py`: `MonoBehaviour`'s `m_GameObject`, a
//! `PPtr[GameObject]` it has from `Component`, `m_Enabled`, `m_Script`, a
//! `PPtr[MonoScript]`, and `m_Name`, a `str`), but type `m_Enabled` only as
//! an `int`. Its width and alignment, the rows' type names and their order
//! are format facts from Perfare/AssetStudio's release v0.16.47 (MIT; no
//! AssetStudio code is ported or kept here): `Component` reads
//! `m_GameObject` (`AssetStudio/Classes/Component.cs`; `EditorExtension`
//! before it reads only in an editor file), `Behaviour` one byte,
//! `m_Enabled`, then aligns (`Classes/Behaviour.cs`), and `MonoBehaviour`
//! `m_Script`, then the aligned string `m_Name` (`Classes/MonoBehaviour.cs`);
//! `AssetStudioUtility/SerializedTypeHelper.cs`'s `AddMonoBehaviour` names
//! the rows `PPtr<GameObject> m_GameObject`, `UInt8 m_Enabled` (aligned),
//! `PPtr<MonoScript> m_Script`, `string m_Name` (its `Array` aligned). Each
//! pointer is an `int m_FileID` and an `m_PathID` that is an `SInt64` from
//! SerializedFile format 14 on and an `int` before it, as UnityPy reads an
//! identifier in a file (`files/SerializedFile.py`'s
//! `LocalSerializedObjectIdentifier`) and AssetStudio a pointer
//! (`Classes/PPtr.cs`). Of each row's meta flags only the align bit is set,
//! the one the reader acts on ([`head_rows`]). Two tests pin the rows: one
//! reads them back from a file whose stored typetree is written from those
//! facts, at formats 9 and 22, and the reference's reads of the testkit's
//! atlases (format 22 files), which UnityPy makes with its TPK's tree at
//! run time, are recorded (`tools/fixtures/ngui.py`) and replayed against
//! these rows (X5); no scene has a file before format 14. No UnityPy sample
//! holds a MonoBehaviour to measure them against.
//! A head read that fails is the `m_Script` read's failure.
//! UnityPy chooses the head's tree by the file's Unity version through its
//! bundled typetree package, so the two can differ for a file whose version
//! string disagrees with its format: a divergence (`PARITY.md` 19.3,
//! `docs/BATCH_SPEC.md` 4.4 step 1), which no scene records.
//!
//! **Fields.** UnityPy reads only the `Texture2D` as an object, whose
//! attributes are its children's cleaned names ([`attr`], the core's
//! [`Parsed::attr`]); the atlas, its elements, the Material, the MonoScript,
//! a pointer and a peeked name are typetree dicts, keyed by the children's
//! raw names, a repeated one keeping its last value ([`field`], the core's
//! [`Parsed::field`]; review round 2's C2). Integers are kept wide
//! ([`integer`]).

use std::sync::Arc;

use unityrs_core::env::{DerefError, PPtr};
use unityrs_core::files::{BundleFile, ParentKind, SerializedFile};
use unityrs_core::limits::{Limits, Meter, MeteredVec};
use unityrs_core::typetree::{NodeList, NodeRow, Tree};
use unityrs_core::{Bytes, Map, ObjectRef, Parsed, PrimArray, RawStr, Value};
use unityrs_texture::{Format, Formats, SWITCH, Selection, Skip, TextureFields, select_in};

use super::classify::{self, Class, Reason};
use super::seam::Seam;
use super::table;

/// The format from which a pointer's `m_PathID` is 64 bits.
const WIDE_PATH_ID: u32 = 14;

/// A value of another shape than the cutter reads: a `ParseError`'s `Read`
/// (4.4 step 2's rule).
#[must_use]
pub fn shape() -> Class {
    Class::Skip(Reason::decode_error("read"))
}

/// The bundle a SerializedFile or bundle's parent link names, while alive.
#[must_use]
pub fn parent_bundle(kind: Option<&ParentKind>) -> Option<Arc<BundleFile>> {
    match kind? {
        ParentKind::Bundle { bundle, .. } => bundle.upgrade(),
        ParentKind::Web { .. } => None,
    }
}

/// The top-level bundle that holds `file`, held while a read uses it
/// (`docs/plans/batch.md` 2.6 step 7).
#[must_use]
pub fn top_bundle(file: &SerializedFile) -> Option<Arc<BundleFile>> {
    let mut top = parent_bundle(file.parent().map(|p| &p.kind))?;
    while let Some(up) = parent_bundle(top.parent().map(|p| &p.kind)) {
        top = up;
    }
    Some(top)
}

/// A dict's field by its raw name, as `parse_as_dict` keys it: of children
/// of one name, the last one's value.
#[must_use]
pub fn field<'a>(parsed: &'a Parsed, value: &'a Value, name: &[u8]) -> Option<&'a Value> {
    parsed.field(value.as_dict()?, name)
}

/// An object's attribute, as UnityPy's object mode keys it: the last child
/// whose cleaned name is `name` ([`Parsed::attr`]). The `Texture2D`'s only.
#[must_use]
pub fn attr<'a>(parsed: &'a Parsed, value: &'a Value, name: &[u8]) -> Option<&'a Value> {
    parsed.attr(value.as_dict()?, name)
}

/// A pointer struct: an integer `m_FileID` and `m_PathID`.
#[must_use]
pub fn pptr(parsed: &Parsed, value: &Value) -> Option<PPtr> {
    let file_id = field(parsed, value, b"m_FileID")?.as_i64()?;
    let path_id = field(parsed, value, b"m_PathID")?.as_i64()?;
    Some(PPtr {
        file_id: i32::try_from(file_id).ok()?,
        path_id,
    })
}

fn string(value: &Value) -> Option<&RawStr> {
    match value {
        Value::Str(s) => Some(s),
        _ => None,
    }
}

/// A MonoBehaviour's head: `m_Script` and `m_Name`.
#[derive(Clone, Debug)]
pub struct Head {
    /// `m_Script`.
    pub script: PPtr,
    /// `m_Name`.
    pub name: RawStr,
}

/// `m_MetaFlag`'s align bit, the one bit of a row the core's reader acts on
/// (UnityPy's `m_MetaFlag & 0x4000`, the core's [`NodeRow::aligned`]).
const ALIGN: i64 = 0x4000;

/// The head's rows (the module docs' **The head**): each a level, a type
/// and a name, its `m_MetaFlag` the align bit where the value is aligned
/// (`m_Enabled`, and a string's `Array`) and 0 elsewhere, its `m_ByteSize`
/// left 0 as UnityPy's `from_list` leaves it (the reader reads neither the
/// other bits nor the size).
fn head_rows(wide: bool) -> Vec<NodeRow> {
    let id_ty = if wide { "SInt64" } else { "int" };
    let row = |level, ty: &str, name: &str| NodeRow::new(level, ty, name).with_meta_flag(0);
    let aligned = |level, ty: &str, name: &str| NodeRow::new(level, ty, name).with_meta_flag(ALIGN);
    vec![
        row(0, "MonoBehaviour", "Base"),
        row(1, "PPtr<GameObject>", "m_GameObject"),
        row(2, "int", "m_FileID"),
        row(2, id_ty, "m_PathID"),
        aligned(1, "UInt8", "m_Enabled"),
        row(1, "PPtr<MonoScript>", "m_Script"),
        row(2, "int", "m_FileID"),
        row(2, id_ty, "m_PathID"),
        row(1, "string", "m_Name"),
        aligned(2, "Array", "Array"),
        row(3, "int", "size"),
        row(3, "char", "data"),
    ]
}

/// A peek tree kept for one type of X's own files, under its full tree's
/// address: that tree (kept alive, so the address is not reused while the
/// entry lives) and its peek tree, or none.
type Peek = (Arc<Tree>, Option<Arc<Tree>>);

/// The cutter's reader: the head trees, and the peek tree of each type of
/// X's own files, built once, in a hash table by the type's tree, charged
/// to `M_X` (one entry per type a name is read of: as many as the input
/// has objects, so a sorted table's inserts would be quadratic, B3's review
/// round 1). A dependency's tree is never kept: its peek tree is built,
/// read with and dropped in the read (`docs/plans/batch.md` 2.6 step 7;
/// review round 2's S3).
#[derive(Debug)]
pub struct Reader {
    wide: Arc<Tree>,
    narrow: Arc<Tree>,
    peeks: table::Map<usize, Peek>,
}

impl Reader {
    /// The reader, its head trees compiled. They are fixed rows, so their
    /// failing is the cutter's own fault, `internal`, whatever the core says.
    pub fn new(meter: &Arc<Meter>) -> Result<Self, Class> {
        fn internal<E>(_: E) -> Class {
            Class::Run(Reason::plain("internal"))
        }
        let tree = |wide| {
            let list = NodeList::trusted(head_rows(wide)).map_err(internal)?;
            Tree::compile(list.into_shared().map_err(internal)?, &Limits::default())
                .map_err(internal)
        };
        Ok(Self {
            wide: tree(true)?,
            narrow: tree(false)?,
            peeks: table::Map::new(meter),
        })
    }

    /// `obj`'s head, read with the fixed rows ([`head_rows`]), `check_read` off.
    pub fn head(&self, obj: &ObjectRef) -> Result<Head, Class> {
        let tree = if obj.file().header().version >= WIDE_PATH_ID {
            &self.wide
        } else {
            &self.narrow
        };
        let parsed = obj
            .read_typetree(Some(tree), false)
            .map_err(|e| classify::parse(&e))?;
        let root = parsed.value();
        let script = field(&parsed, root, b"m_Script")
            .and_then(|v| pptr(&parsed, v))
            .ok_or_else(shape)?;
        let name = field(&parsed, root, b"m_Name")
            .and_then(string)
            .ok_or_else(shape)?
            .clone();
        Ok(Head { script, name })
    }

    /// `peek_name` (4.4 step 1): the object's root's children up to and
    /// including the first named `m_Name` or `name`, read `check_read` off
    /// over the object's own nodes, and that child's value under its raw
    /// name (`parse_as_dict(peek_node)[key]`; review round 2's C3); a type
    /// with no typetree fails as its object read does (`asset_bundle`), one
    /// with no name field, or a name that is no string, as a `read`. Each
    /// core call is under the refusal probe on its own (review round 2's
    /// S4).
    pub(super) fn peek_name(&mut self, seam: &Seam<'_>, obj: &ObjectRef) -> Result<RawStr, Class> {
        self.peek(seam, obj)?.ok_or_else(shape)
    }

    /// [`Self::peek`] of an atlas's name for the sprites kind's tag walk
    /// (`sprites::atlas`): a frame of its own, so a reservation refused in
    /// it is named for the walk (the tests' `site`, B3's review round 5, Q6).
    #[inline(never)]
    pub(super) fn atlas_name(
        &mut self,
        seam: &Seam<'_>,
        obj: &ObjectRef,
    ) -> Result<Option<RawStr>, Class> {
        self.peek(seam, obj)
    }

    /// `peek_name` as a `SpriteAtlas`'s is compared with a Sprite's tag
    /// (`sprites::atlas`): as [`Self::peek_name`], but a type
    /// with no name field (UnityPy's `None`) and a name that is no string
    /// (no tag equals it) are `None`, no failure.
    pub(super) fn peek(
        &mut self,
        seam: &Seam<'_>,
        obj: &ObjectRef,
    ) -> Result<Option<RawStr>, Class> {
        let _sample = Sample(obj.file().meter(), seam.hooks);
        let found = seam.probe(|| obj.tree(None).map_err(|e| classify::parse(&e)))?;
        let Some(tree) = found else {
            return Err(Class::Skip(Reason::decode_error("asset_bundle")));
        };
        let own = top_bundle(obj.file()).is_some_and(|b| Arc::ptr_eq(&b, &seam.registered.own));
        let peek = if own {
            self.kept(seam, &tree.tree, obj.file())?
        } else {
            peek_tree(seam, &tree.tree, obj.file())?
        };
        drop(tree);
        let Some(peek) = peek else {
            return Ok(None);
        };
        let parsed = seam.probe(|| {
            obj.read_typetree(Some(&peek), false)
                .map_err(|e| classify::parse(&e))
        })?;
        // The peek tree ends with the selected child: the root's last.
        let list = peek.list();
        let key = list
            .children(list.root())
            .last()
            .map(|c| list.name(*c))
            .ok_or_else(shape)?;
        Ok(field(&parsed, parsed.value(), key)
            .and_then(string)
            .cloned())
    }

    /// The peek tree of one of X's own types, built once and kept.
    fn kept(
        &mut self,
        seam: &Seam<'_>,
        tree: &Arc<Tree>,
        file: &SerializedFile,
    ) -> Result<Option<Arc<Tree>>, Class> {
        let at = Arc::as_ptr(tree) as usize;
        if let Some((_, peek)) = self.peeks.get(&at) {
            return Ok(peek.clone());
        }
        let peek = peek_tree(seam, tree, file)?;
        let peeks = &mut self.peeks;
        seam.probe(|| peeks.insert(at, (tree.clone(), peek.clone())))?;
        Ok(peek)
    }
}

/// The peek tree of `tree` (UnityPy's `get_name_peek_node`), or none when
/// its root has no `m_Name` or `name` child: the rows up to the end of the
/// first such child's subtree, so that child is the peek root's last. Its
/// four core calls (the rows' charge and copy, the list, its compile) are
/// each under the probe on its own (review round 4's S3).
fn peek_tree(
    seam: &Seam<'_>,
    tree: &Arc<Tree>,
    file: &SerializedFile,
) -> Result<Option<Arc<Tree>>, Class> {
    let list = tree.list();
    let root = list.root();
    let children = list.children(root);
    let Some(pos) = children
        .iter()
        .position(|c| matches!(list.name(*c), b"m_Name" | b"name"))
    else {
        return Ok(None);
    };
    let end = children
        .get(pos + 1)
        .map_or(list.rows().len(), |next| next.index());
    let rows = list.rows().get(..end).unwrap_or_default();
    let mut kept = seam.probe(|| {
        MeteredVec::with_capacity(file.meter(), rows.len()).map_err(|e| classify::charge(&e))
    })?;
    seam.probe(|| {
        kept.extend_from_slice(rows)
            .map_err(|e| classify::charge(&e))
    })?;
    let peek = seam.probe(|| NodeList::from_rows(kept).map_err(|e| classify::tree(&e)))?;
    let limits = &file.context().limits;
    let compiled = seam.probe(|| {
        Tree::compile(peek.into_shared().map_err(|e| classify::tree(&e))?, limits)
            .map_err(|e| classify::tree(&e))
    })?;
    Ok(Some(compiled))
}

/// Shared streamed texture spans and Python slicing.
pub use unityrs_texture::fetch::{Span, Stream, python_slice};

/// `m_PlatformBlob`, any list (4.4 step 2): what `is_switch_swizzled` reads
/// of it, its length and its elements 8 to 12 as bytes, and no copy.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Blob {
    /// The element count (`len(platform_blob)`).
    pub len: usize,
    /// Elements 8 to 12 as the bytes `int.from_bytes` takes them for, when
    /// the blob has 12: each an integer in `0..=255` (or a bool, which
    /// Python's `bytes()` reads as 0 or 1); `None` when one is not
    /// ([`BLOB_ELEMENT`]).
    pub gobs: Option<[u8; 4]>,
}

/// Section 3's rule for a Switch blob element among bytes 8 to 12 that is
/// not a byte: UnityPy's `int.from_bytes(platform_blob[8:12], "little")`
/// raises, so the texture's answer is `decode_error`, detail `read`.
pub const BLOB_ELEMENT: &str = "read";

/// Element `i` of a primitive run as a byte, when it is one (a float is
/// not: `bytes()` refuses it).
fn prim_byte(p: &PrimArray, i: usize) -> Option<u8> {
    let int = |v: Option<i128>| v.and_then(|v| u8::try_from(v).ok());
    match p {
        PrimArray::Bool(v) => v.get(i).map(|b| u8::from(*b)),
        PrimArray::U8(v) => v.get(i).copied(),
        PrimArray::I8(v) => int(v.get(i).map(|x| i128::from(*x))),
        PrimArray::U16(v) => int(v.get(i).map(|x| i128::from(*x))),
        PrimArray::I16(v) => int(v.get(i).map(|x| i128::from(*x))),
        PrimArray::U32(v) => int(v.get(i).map(|x| i128::from(*x))),
        PrimArray::I32(v) => int(v.get(i).map(|x| i128::from(*x))),
        PrimArray::U64(v) => int(v.get(i).map(|x| i128::from(*x))),
        PrimArray::I64(v) => int(v.get(i).map(|x| i128::from(*x))),
        PrimArray::F32(_) | PrimArray::F64(_) => None,
    }
}

impl Blob {
    fn of(value: &Value) -> Option<Self> {
        let byte = |v: &Value| match v {
            Value::Bool(b) => Some(u8::from(*b)),
            other => u8::try_from(other.as_i64()?).ok(),
        };
        let (len, gobs) = match value {
            Value::Prims(p) => {
                let gobs = (8..12)
                    .map(|i| prim_byte(p, i))
                    .collect::<Option<Vec<u8>>>();
                (p.len(), gobs)
            }
            Value::List(items) => {
                let gobs = items
                    .get(8..12)
                    .map(|four| four.iter().map(byte).collect::<Option<Vec<u8>>>())
                    .unwrap_or_default();
                (items.len(), gobs)
            }
            _ => return None,
        };
        let gobs = gobs.and_then(|g| <[u8; 4]>::try_from(g).ok());
        Some(Self { len, gobs })
    }
}

/// A `Texture2D`'s fields, as the cutter reads them: the integers wide, as
/// read, narrowed only where section 3's answers use them ([`answer`]).
#[derive(Clone, Debug)]
pub struct Texture {
    /// `m_Width`.
    pub width: i128,
    /// `m_Height`.
    pub height: i128,
    /// `m_TextureFormat`.
    pub format: i128,
    /// `image_data`.
    pub image_data: Bytes,
    /// `m_StreamData`, when present.
    pub stream: Option<Stream>,
    /// `m_PlatformBlob`, when present.
    pub platform_blob: Option<Blob>,
}

impl Texture {
    /// Shared decoder fields, with the blob's checked bytes held on the stack.
    pub(crate) fn fields<'a>(
        &self,
        format: i32,
        platform: i32,
        blob: &'a mut [u8; 12],
    ) -> TextureFields<'a> {
        let platform_blob = self
            .platform_blob
            .filter(|b| b.len >= 12)
            .and_then(|b| b.gobs)
            .map(|g| {
                blob.fill(0);
                blob[8..].copy_from_slice(&g);
                blob.as_slice()
            });
        let (width, height) = self.sides();
        TextureFields {
            texture_format: format,
            width,
            height,
            platform,
            platform_blob,
        }
    }
    /// The sides as the decoder takes them: a side outside `i32` is clamped
    /// to it, which keeps section 3's order (a negative side is answer 6,
    /// a side past `i32::MAX` more pixels than answer 7's bound).
    #[must_use]
    pub fn sides(&self) -> (i32, i32) {
        let narrow = |v: i128| {
            i32::try_from(v.clamp(i128::from(i32::MIN), i128::from(i32::MAX))).unwrap_or(i32::MAX)
        };
        (narrow(self.width), narrow(self.height))
    }
}

/// Section 3's answers 1 to 3 for `t` on `platform`, exporting the formats
/// of `allowed` (the spec's, `docs/BATCH_SPEC.md` 1.5): its format, or the
/// skip. A format outside `i32` is `unsupported_format(<decimal>)`; a Switch
/// blob is read as bytes ([`Blob`], [`BLOB_ELEMENT`]).
pub fn answer(
    t: &Texture,
    platform: i32,
    allowed: Formats,
    unity_version: (u16, u16),
) -> Result<Format, Class> {
    if t.width == 0 || t.height == 0 {
        return Err(Class::Skip(Reason::plain(Skip::EmptyImage.to_string())));
    }
    formats(t, platform, allowed, unity_version)
}

/// Select without indexing the version; data retrieval must precede that read.
pub(crate) fn object_answer(
    t: &Texture,
    obj: Option<&ObjectRef>,
    allowed: Formats,
) -> Result<Format, Class> {
    let platform = obj
        .and_then(|o| o.file().target_platform())
        .unwrap_or(super::attempt::UNKNOWN_PLATFORM);
    answer(t, platform, allowed, (u16::MAX, u16::MAX))
}

mod crunch;
pub(super) use crunch::data_format;

/// Section 3's answers 2 and 3 for `t`, whose sides are not 0, on
/// `platform`: [`answer`] past its first.
pub(crate) fn formats(
    t: &Texture,
    platform: i32,
    allowed: Formats,
    unity_version: (u16, u16),
) -> Result<Format, Class> {
    let Ok(format) = i32::try_from(t.format) else {
        return Err(Class::Skip(Reason::plain(format!(
            "unsupported_format({})",
            t.format
        ))));
    };
    let mut blob = [0u8; 12];
    match select_in(
        &t.fields(format, platform, &mut blob),
        allowed,
        unity_version,
    ) {
        Selection::Export(_)
            if platform == SWITCH
                && t.platform_blob
                    .is_some_and(|b| b.len >= 12 && b.gobs.is_none()) =>
        {
            Err(Class::Skip(Reason::decode_error(BLOB_ELEMENT)))
        }
        Selection::Export(f) => Ok(f),
        Selection::Skip(s) => Err(Class::Skip(Reason::plain(s.to_string()))),
    }
}

/// The fields UnityPy 1.25.2's `Texture2D` needs (`classes/generated.py`).
const TEXTURE_FIELDS: [&[u8]; 11] = [
    b"image_data",
    b"m_CompleteImageSize",
    b"m_Height",
    b"m_ImageCount",
    b"m_IsReadable",
    b"m_LightmapFormat",
    b"m_Name",
    b"m_TextureDimension",
    b"m_TextureFormat",
    b"m_TextureSettings",
    b"m_Width",
];

/// Any integer, signed or not (not a bool, which UnityPy's dict keeps
/// apart), as read.
#[must_use]
pub fn integer(value: &Value) -> Option<i128> {
    match value {
        Value::Int(i) => Some(i128::from(*i)),
        Value::UInt(u) => Some(i128::from(*u)),
        _ => None,
    }
}

/// The names an extra field of a `Sprite` or a `Texture2D` cannot be set
/// under, so that `parse_class`'s set of it fails and the value is an
/// `UnknownObject` (`UnityPyBoost/TypeTreeHelper.cpp:538-574`; B3's review
/// rounds 3 and 4, S3b and R4): the data descriptors on each class and its
/// bases that refuse the set, `image` (`classes/legacy_patch/Sprite.py:10`,
/// a property of no setter; `classes/legacy_patch/Texture2D.py:78`, whose
/// setter raises for any value but an image or a path Pillow opens,
/// `classes/legacy_patch/Texture2D.py:25-26`: unityrs refuses every value,
/// since a sealed attempt never opens a host path a file's data names, a
/// stated divergence, `PARITY.md`, B3's review round 5, Q2) and `assets_file`
/// (`classes/Object.py:23-27`), and the three every instance of a class with
/// a `__dict__` refuses, `__class__`, `__dict__` and `__weakref__` (Python's
/// own). `SpriteAtlas` refuses the same but `image`; its class changes
/// nothing (an atlas is used whatever its class). `test_sprites_kind.py`
/// checks the list against UnityPy's classes.
pub(crate) const SETTERLESS: [&[u8]; 5] = [
    b"image",
    b"assets_file",
    b"__class__",
    b"__dict__",
    b"__weakref__",
];

/// Whether a child of `map`'s node cleans to one of [`SETTERLESS`].
#[must_use]
pub(crate) fn setterless(parsed: &Parsed, map: &Map) -> bool {
    SETTERLESS.iter().any(|n| parsed.attr(map, n).is_some())
}

/// Whether a child of `map`'s node cleans to `set_object_reader`: UnityPy
/// sets the child as an attribute of whatever class it builds, then its
/// object read calls that attribute as the method it shadows
/// (`files/ObjectReader.py`'s `read`), so the read fails, `read` (B3's
/// review round 3, S3b), for any object read.
#[must_use]
pub(crate) fn shadowed(parsed: &Parsed, map: &Map) -> bool {
    parsed.attr(map, b"set_object_reader").is_some()
}

/// A texture's object read (4.4 step 2): the checked read, then the class
/// UnityPy builds (a `Texture2D` lacking a field of no default, or with a
/// field it cannot be given, is an `UnknownObject`; one that shadows
/// `set_object_reader` fails its read), then the shape.
pub fn texture(obj: &ObjectRef) -> Result<Texture, Class> {
    let parsed = obj
        .read_typetree(None, true)
        .map_err(|e| classify::parse(&e))?;
    let root = parsed.value();
    let map: &Map = root.as_dict().ok_or_else(shape)?;
    if TEXTURE_FIELDS.iter().any(|f| parsed.attr(map, f).is_none())
        || setterless(&parsed, map)
        || shadowed(&parsed, map)
    {
        return Err(shape());
    }
    let get = |name: &[u8]| parsed.attr(map, name);
    let width = get(b"m_Width").and_then(integer).ok_or_else(shape)?;
    let height = get(b"m_Height").and_then(integer).ok_or_else(shape)?;
    let format = get(b"m_TextureFormat")
        .and_then(integer)
        .ok_or_else(shape)?;
    let stream = match get(b"m_StreamData") {
        None => None,
        Some(s) => {
            let path = attr(&parsed, s, b"path")
                .and_then(string)
                .ok_or_else(shape)?;
            let int = |name: &[u8]| attr(&parsed, s, name).and_then(integer);
            Some(Stream {
                path: path.clone(),
                offset: int(b"offset").ok_or_else(shape)?,
                size: int(b"size").ok_or_else(shape)?,
            })
        }
    };
    let image_data = match get(b"image_data") {
        Some(Value::Bytes(b)) => b.clone(),
        _ => return Err(shape()),
    };
    let platform_blob = match get(b"m_PlatformBlob") {
        None => None,
        Some(v) => Some(Blob::of(v).ok_or_else(shape)?),
    };
    Ok(Texture {
        width,
        height,
        format,
        image_data,
        stream,
        platform_blob,
    })
}

/// Dereferences `pptr` from `from`: an Environment call, checked as
/// 2.6 step 7 says ([`Seam::env_call`]).
pub(super) fn deref(
    seam: &Seam<'_>,
    from: &Arc<SerializedFile>,
    pptr: PPtr,
) -> Result<ObjectRef, Class> {
    seam.env_call(|| {
        seam.env()
            .deref(from, pptr)
            .map_err(|e: DerefError| classify::deref(&e))
    })
}

/// Section 5 step 1: the name of the texture the atlas's `material` names
/// as its `_MainTex`, or none. A failure is returned for the caller, which
/// swallows an object's skip.
pub(super) fn material_name(
    seam: &Seam<'_>,
    reader: &mut Reader,
    atlas: &ObjectRef,
    parsed: &Parsed,
) -> Result<Option<RawStr>, Class> {
    let Some(material) = field(parsed, parsed.value(), b"material") else {
        return Ok(None);
    };
    let at = pptr(parsed, material).ok_or_else(shape)?;
    let target = deref(seam, atlas.file(), at)?;
    let _top = top_bundle(target.file());
    let _sample = Sample(target.file().meter(), seam.hooks);
    let mat = seam.probe(|| {
        target
            .read_typetree(None, true)
            .map_err(|e| classify::parse(&e))
    })?;
    let envs = field(&mat, mat.value(), b"m_SavedProperties")
        .and_then(|p| field(&mat, p, b"m_TexEnvs"))
        .ok_or_else(shape)?;
    let Value::List(pairs) = envs else {
        return match envs {
            Value::Prims(p) if p.is_empty() => Ok(None),
            _ => Err(shape()),
        };
    };
    for pair in pairs {
        // A pair, or a two-element list (section 5 step 1).
        let two: &[Value] = match pair {
            Value::Tuple(t) => t,
            Value::List(l) => l,
            _ => return Err(shape()),
        };
        let [first, second] = two else {
            return Err(shape());
        };
        let key = match first {
            Value::Str(s) => Some(s),
            Value::Dict(_) => field(&mat, first, b"name").and_then(string),
            _ => None,
        };
        if key.map(RawStr::as_bytes) != Some(b"_MainTex".as_slice()) {
            continue;
        }
        let tex = field(&mat, second, b"m_Texture")
            .and_then(|t| pptr(&mat, t))
            .ok_or_else(shape)?;
        let texture = deref(seam, target.file(), tex)?;
        let _held = top_bundle(texture.file());
        return reader.peek_name(seam, &texture).map(Some);
    }
    Ok(None)
}

/// Observation ends before the file handle drops; no retained dependency pin.
pub(super) struct Sample<'a>(pub &'a Meter, pub &'a dyn super::Hooks);
impl Drop for Sample<'_> {
    fn drop(&mut self) {
        self.1.meter(self.0);
    }
}

#[cfg(test)]
mod tests;