unityrs-core 1.0.0

Read and write Unity asset bundles, serialized files and typetrees.
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
// Ported from UnityPy 1.25.2 (MIT): UnityPy/environment.py, UnityPy/files/ObjectReader.py, UnityPy/helpers/ContainerHelper.py. See THIRD_PARTY_LICENSES.
//! The container: [`Environment::container`], [`Environment::container_path`]
//! and the pass that builds their index (UnityPy's
//! `_build_container_index`), the walk of each file's preload table.

use std::collections::HashMap;
use std::fmt;
use std::mem::size_of;
use std::sync::Arc;
use std::sync::atomic::Ordering;

use super::deref::Reached;
use super::registry::{Rank, SlotKind, load_keys_here};
use super::revisit::Pending;
use super::watch::{FILES, Flag, REGISTRY, Walk};
use super::{Cab, DerefError, Environment, Found, LoadMode, PPtr, never_evicts, pptr};
use crate::budget::Reservation;
use crate::container::{Container, ContainerPath, ContainerRow};
use crate::error::{Error, Unsupported};
use crate::files::{BundleFile, File, SerializedFile};
use crate::limits::{Charge, GrantScope, HASH_TABLE_SHARES, Meter, MeteredVec};
use crate::object::ObjectRef;
use crate::once::CellError;
use crate::pystr::{clean_external_path, py_lower, simplify_name};
use crate::raw::RawStr;
use crate::walk::STEP_WORK;

/// The iterator [`Environment::container`] returns, CABs in registry order.
pub struct ContainerItems<'a> {
    env: &'a Environment,
    after: Option<Rank>,
    current: Option<(Arc<SerializedFile>, usize)>,
}

const _: () = crate::send_sync::<ContainerItems<'static>>();

impl Iterator for ContainerItems<'_> {
    type Item = Result<ContainerItem, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if let Some((sf, k)) = self.current.take() {
                match sf.container() {
                    Ok(c) => {
                        if let Some(row) = c.rows().get(k) {
                            let row = row.clone();
                            self.current = Some((sf.clone(), k + 1));
                            return Some(Ok(ContainerItem { row, file: sf }));
                        }
                    }
                    Err(e) => return Some(Err(e)),
                }
            }
            // UnityPy environment.py:221-224: every non-dependency CAB.
            let (rank, slot) = self.env.registry().slot_after(self.after.as_ref())?;
            self.after = Some(rank);
            if slot.is_dependency() {
                continue;
            }
            match self.env.registry().resolve(&slot) {
                Ok(Some(Cab::Serialized(sf))) => self.current = Some((sf, 0)),
                Ok(_) => {}
                Err(e) => return Some(Err(e)),
            }
        }
    }
}

impl fmt::Debug for ContainerItems<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ContainerItems").finish_non_exhaustive()
    }
}

/// A container row with the object it points at, as `env.container` yields.
#[derive(Clone, Debug)]
pub struct ContainerItem {
    /// The row.
    pub row: ContainerRow,
    /// The file whose container it is.
    pub file: Arc<SerializedFile>,
}

const _: () = crate::send_sync::<ContainerItem>();

/// A container pass's index of each bundle's entry names, lower-cased, to
/// the entry the first of them names (rule 4's scan of a bundle's `files`
/// as one lookup): built once for each bundle the pass meets, charged to the
/// bundle's meter, and dropped with the pass.
#[derive(Default)]
pub(super) struct Siblings {
    bundles: HashMap<usize, SiblingNames>,
}

struct SiblingNames {
    /// Keeps the bundle, and so the address that keys it, for the pass.
    _bundle: Arc<BundleFile>,
    names: HashMap<RawStr, usize>,
    _charge: Charge,
}

impl Siblings {
    /// The directory index of `bundle`'s first `files` entry whose name
    /// lowers to `clean`.
    pub(super) fn find(
        &mut self,
        bundle: &Arc<BundleFile>,
        clean: &RawStr,
    ) -> Result<Option<usize>, Error> {
        let key = Arc::as_ptr(bundle).addr();
        let entry = match self.bundles.entry(key) {
            std::collections::hash_map::Entry::Occupied(o) => o.into_mut(),
            std::collections::hash_map::Entry::Vacant(v) => {
                let meter = bundle.meter();
                let slots = bundle.files();
                let n = u64::try_from(slots.len()).unwrap_or(u64::MAX);
                let charge = meter.charge(
                    HASH_TABLE_SHARES
                        .saturating_mul((size_of::<RawStr>() + size_of::<usize>() + 1) as u64)
                        .saturating_mul(n.saturating_add(1)),
                )?;
                let mut names = HashMap::with_capacity(slots.len());
                for s in slots {
                    names
                        .entry(py_lower(&s.name, meter)?)
                        .or_insert(s.dir_index);
                }
                v.insert(SiblingNames {
                    _bundle: bundle.clone(),
                    names,
                    _charge: charge,
                })
            }
        };
        Ok(entry.names.get(clean.as_bytes()).copied())
    }
}

/// What one external costs a preload walk's map of answers:
/// [`HASH_TABLE_SHARES`] buckets.
const TARGET_SHARE: u64 = HASH_TABLE_SHARES * (size_of::<i32>() + size_of::<Known>() + 1) as u64;

/// No table index: the end of a list in [`Targets::next`].
const NONE: usize = usize::MAX;

/// A preload walk's answers for its file's externals, each kept only while
/// UnityPy's next dereference would give the same one: a bundle sibling, or
/// a file id past the externals' end, for the walk; an answer of `files` or
/// the registry (found or not) until a change to a key its lookup read
/// ([`super::watch`]: a load's registration, any thread's, the resolver's
/// included, and under [`LoadMode::Lazy`] the entries a reach registers),
/// so the answer UnityPy's next lookup could find changed is asked again
/// and every other is kept; and the resolver's load not at all (the next
/// dereference looks again, and finds it if the load registered the name
/// asked for, or loads again if not, as UnityPy's `find_file` does when its
/// walk matched a longer file name by suffix). An external is looked up
/// once, and once more per change to one of its keys (one the resolver
/// loads under another name, once per dereference), so a walk costs its
/// table and its externals' names (plan decision 11), plus those lookups
/// again, each lookup past an external's first counted as work on the
/// walking file's meter (`STEP_WORK` and the external's path's bytes;
/// review round 4: every change, any thread's, expired every answer, so a
/// table of pointers to long names cost the table times the names).
///
/// Each answer with a flag (one of `files`, the registry or the resolver's
/// load) lists the table indexes dereferenced under it (`answers`, linked
/// through `next`), so when a load sets its flag, the walk's own or another
/// thread's ([`Walk::fired`]), those indexes are dereferenced again at the
/// next row whose span reaches them, or later in the row the walk is in
/// (review round 13: a walk that dereferenced each index once never gave a
/// later row's path to the file a load in between put under the key the
/// index's external resolves through; review round 14: only a load of the
/// walk's own put them back, and not after another thread's had set the
/// flag first).
///
/// [`LoadMode::Lazy`]: super::LoadMode::Lazy
///
/// Each charge is released after the memory it pays for (plan section 3.15):
/// fields drop in order, so `known` and `answers` go first, then the walk's
/// watches with their flags and their own charge, then `known`'s charge
/// (review round 5: the watches' charge was released before their entries
/// left the map).
struct Targets<'a> {
    known: HashMap<i32, Known>,
    /// Every flag the walk set up, by its number, with the head of the list
    /// of table indexes dereferenced under its answer.
    answers: MeteredVec<Answer>,
    /// Each table index's next in its answer's list, made at the first
    /// link: an index is in one list at most, the one of the answer it last
    /// got, and leaves it when the list is taken.
    next: Option<MeteredVec<usize>>,
    /// After `known`, whose flags drop before the walk's watches end.
    walk: Walk<'a>,
    /// `known`'s table, after it.
    charge: Charge,
}

/// One external's answer in [`Targets`].
struct Known {
    found: Option<Found>,
    /// Whether the walk keeps it: not the resolver's load, which the next
    /// dereference asks again.
    kept: bool,
    /// Set when a key its lookup read changes; `None`: kept for the walk (a
    /// sibling, a file id past the end).
    flag: Option<Arc<Flag>>,
}

/// The table indexes dereferenced under one flag's answer: the head of a
/// list linked through [`Targets::next`] (the walk holds the flag itself,
/// [`Walk`]).
struct Answer {
    head: usize,
}

/// The work a preload walk counts on its file's meter for each index it
/// puts back to be dereferenced again ([`Targets::reopen`]): a step for the
/// dereference, and one per level of the [`Pending`] set for putting the
/// index back (the find and the removal that dereference makes read no
/// more words than a first dereference's).
fn reopen_work(pending: &Pending) -> u64 {
    STEP_WORK.saturating_mul(1 + u64::try_from(pending.levels()).unwrap_or(u64::MAX))
}

impl Targets<'_> {
    /// The answer kept for `file_id`, if it holds.
    fn kept(&self, file_id: i32) -> Option<&Known> {
        self.known
            .get(&file_id)
            .filter(|k| k.kept && k.flag.as_ref().is_none_or(|f| !f.changed()))
    }

    /// Records `found` for `file_id`, kept (`kept`) while `flag` is unset
    /// (`None`: for the walk).
    fn keep(
        &mut self,
        file_id: i32,
        found: Option<Found>,
        kept: bool,
        flag: Option<Arc<Flag>>,
    ) -> Result<(), Error> {
        if !self.known.contains_key(&file_id) {
            self.charge.grow(TARGET_SHARE)?;
        }
        self.known.insert(file_id, Known { found, kept, flag });
        Ok(())
    }

    /// Lists table index `i` (of a table of `len`) under the answer of flag
    /// `id`.
    fn link(&mut self, i: usize, id: usize, len: usize, meter: &Arc<Meter>) -> Result<(), Error> {
        if self.next.is_none() {
            let mut n = MeteredVec::with_capacity(meter, len)?;
            for _ in 0..len {
                n.push(NONE)?;
            }
            self.next = Some(n);
        }
        if let (Some(answer), Some(slot)) = (
            self.answers.as_mut_slice().get_mut(id),
            self.next.as_mut().and_then(|n| n.as_mut_slice().get_mut(i)),
        ) {
            *slot = answer.head;
            answer.head = i;
        }
        Ok(())
    }

    /// Puts back into `pending` every table index listed under an answer
    /// whose flag a load set since the last call, any thread's, each
    /// counted on `meter` first ([`reopen_work`]); the lists are taken, so
    /// an index is put back once per dereference.
    fn reopen(&mut self, pending: &mut Pending, meter: &Meter) -> Result<(), Error> {
        let Self {
            walk,
            answers,
            next,
            ..
        } = self;
        let links = next.as_ref().map(MeteredVec::as_slice);
        for id in walk.fired() {
            let Some(answer) = answers.as_mut_slice().get_mut(id) else {
                continue;
            };
            let mut i = std::mem::replace(&mut answer.head, NONE);
            while i != NONE {
                meter.count_work(reopen_work(pending))?;
                pending.insert(i);
                i = links.and_then(|l| l.get(i)).copied().unwrap_or(NONE);
            }
        }
        Ok(())
    }
}

/// A dereference failure as a file-level error (the container preload's
/// raise): a load failure as it is, any other as [`Error::Deref`].
fn deref_error(e: DerefError) -> Error {
    match e {
        DerefError::Load(e) => e,
        other => Error::Deref(Arc::new(other)),
    }
}

impl Environment {
    /// `container`: every non-dependency CAB's container rows, CABs in
    /// registry order, after the preload index is built (`environment.py:208-226`),
    /// as an iterator. It walks the registry, as UnityPy walks `env.cabs`, so
    /// a CAB a dependency's replaced under its key is not listed.
    pub fn container(&self) -> Result<ContainerItems<'_>, Error> {
        self.build_container_index()?;
        Ok(ContainerItems {
            env: self,
            after: None,
            current: None,
        })
    }

    /// `obj.container` (`files/ObjectReader.py:199-204`): the container index
    /// built first ([`Self::container`]'s pass, whose failure is this call's),
    /// then the path the object's file's `path_dict` holds for its path id
    /// ([`crate::container`]): a row of the file's own container whose asset
    /// is the object, the last such row winning, else the first row whose
    /// preload span reached it. The paths live in the file the object was
    /// read from, as UnityPy's live in its `assets_file`, so a second handle
    /// a caller opened on the same bytes holds only its own rows'.
    ///
    /// Where a dependency may be evicted (every option but
    /// [`EnvOptions::unitypy`]'s, [`EnvOptions::dep_cache_bytes`]), an
    /// object of a dependency's file is refused with
    /// [`Unsupported::EvictableContainerPath`]: an eviction drops the paths
    /// the preload set in that file and its reopen does not walk again, so
    /// the answer would depend on when the LRU evicted (`PARITY.md` section
    /// 19.3). The preload sets no path in such a file.
    ///
    /// [`EnvOptions::unitypy`]: super::EnvOptions::unitypy
    /// [`EnvOptions::dep_cache_bytes`]: super::EnvOptions::dep_cache_bytes
    pub fn container_path(&self, obj: &ObjectRef) -> Result<Option<ContainerPath>, Error> {
        let sf = obj.file();
        if self.may_reopen(sf) {
            return Err(Error::Unsupported(Unsupported::EvictableContainerPath));
        }
        self.build_container_index()?;
        sf.container()?.path_of(obj.path_id(), sf.meter())
    }

    /// Whether `sf` is a dependency's file the LRU may evict and the
    /// Environment open again.
    fn may_reopen(&self, sf: &SerializedFile) -> bool {
        !never_evicts(&self.inner.options) && sf.meter().reservation() == Reservation::Dependency
    }

    /// `_build_container_index` (`environment.py:208-215`): once until the
    /// next load's registration, every registered `SerializedFile`'s preload
    /// table, dependencies' included, in registry order. UnityPy sets its
    /// flag before the loop and each registration clears it, so a pass that
    /// fails leaves the index built unless the pass itself registered
    /// something; here a pass marks built the registry generation it started
    /// at when it ends or fails with an error the input decides, and any
    /// load's registration since starts a new generation. A refusal (a
    /// budget, meter or I/O failure, [`crate::once::CellError::is_transient`])
    /// is never kept (plan 4.1): the pass is not marked built, and the next
    /// ask walks again (review round 5: a pool refused once left the preload
    /// unwalked for the generation). What a walk the file's own meter refused
    /// was granted counts as that open's work, so a refusal the next ask
    /// would repeat stops at [`crate::LimitKind::DecodeWork`], which is kept
    /// ([`Self::walk_counted`]). A slot's table is marked walked only when its
    /// walk ends (UnityPy clears `_preload_table` then). A pass whose own
    /// dereferences make a new key fails as UnityPy's loop over
    /// `cabs.values()` does (`RuntimeError: dictionary changed size during
    /// iteration`), at the step after; a key a nested container's entries
    /// make under [`LoadMode::Lazy`] is not one (an eager load made it at
    /// load), nor is one another thread's load makes. Under `Lazy` the pass
    /// first reaches every registered entry and every dropped one, an
    /// evicted dependency's opened again (the registry's module docs; plan
    /// section 9, Q13), so it walks what an eager load registered; a slot
    /// dropped after a pass started is dropped by a later load, whose
    /// registration starts a new generation in the step that lists the
    /// slot whether or not it changes a key (review round 16: one ranked
    /// between the key's position and its value changed none, started
    /// none, and the entries its slot's reach registered were never
    /// walked), or by the reach of such a load's slot, so a pass finished
    /// at the current generation leaves none unreached but for a kept
    /// refusal's. Two passes may run at once: each walks every table not yet marked, and each `path_dict`
    /// entry keeps the least stamp ([`crate::container`]), so both leave
    /// what one after the other would.
    ///
    /// [`LoadMode::Lazy`]: super::LoadMode::Lazy
    fn build_container_index(&self) -> Result<(), Error> {
        let reg = &self.inner.registry;
        // Every slot dropped before this call is reached first, as by every
        // other reader (plan section 3.16, R3); a refusal is the walk's to
        // meet, or the next call's.
        reg.settle().ok();
        let generation = reg.generation();
        if reg.built(generation) {
            return Ok(());
        }
        let result = self.container_pass(generation);
        if result.as_ref().err().is_none_or(|e| !e.is_transient()) {
            reg.publish_built(generation);
        }
        result
    }

    /// [`Self::preload`] of `sf`'s table, as a lazy computation of `sf`'s
    /// open (`crate::once`'s rule on work): when `sf`'s own meter refuses
    /// it, what the meter granted it on this thread counts as the open's
    /// work, and once that work has passed the allowance the refusal is
    /// [`crate::LimitKind::DecodeWork`], which the input decides, so the pass
    /// keeps it.
    fn walk_counted(
        &self,
        sf: &Arc<SerializedFile>,
        container: &Container,
        at: (u64, &Rank),
        siblings: &mut Siblings,
    ) -> Result<(), Error> {
        let meter = sf.meter();
        let scope = GrantScope::enter(meter);
        let walked = self.preload(sf, container, at, siblings);
        let own = scope.finish(meter);
        match walked {
            Err(e) if e.is_meter_refusal() => {
                meter.add_work(own);
                Err(meter.work_refused().map_or(e, Error::Limit))
            }
            other => other,
        }
    }

    fn container_pass(&self, generation: u64) -> Result<(), Error> {
        let reg = &self.inner.registry;
        let changed = || Error::Malformed {
            what: "container",
            detail: "dictionary changed size during iteration".into(),
        };
        // Under `Lazy`, every registered entry is reached before the walk
        // (the registry's module docs), so a nested container's entries are
        // walked at the positions and with the values an eager load gives
        // them, whichever container the walk would meet first (review round
        // 4: a later container's entry replaced the value of a key the walk
        // had passed, and the walk had read the earlier one).
        if self.inner.options.load == LoadMode::Lazy {
            let mut after: Option<Rank> = None;
            while let Some((rank, slot)) = reg.slot_after(after.as_ref()) {
                reg.reach(&slot, None)?;
                after = Some(rank);
            }
            // Every slot a registration dropped, those these reaches dropped
            // included, and those of an evicted dependency, opened again
            // (plan section 9, Q13); a refusal there is the pass's.
            reg.settle_for_pass()?;
        }
        let mut siblings = Siblings::default();
        let mut after: Option<Rank> = None;
        let mut grew = false;
        while let Some((rank, slot)) = reg.slot_after(after.as_ref()) {
            if grew {
                return Err(changed());
            }
            // The dependency's file is held while its table is walked, so
            // an eviction the walk makes cannot drop the CAB's bundle.
            let Some(held) = reg.resolve_held(&slot)? else {
                after = Some(rank);
                continue;
            };
            let Cab::Serialized(sf) = &held.cab else {
                after = Some(rank);
                continue;
            };
            if !slot.walked() {
                let before = load_keys_here();
                let mut walk = || {
                    let container = sf.container()?;
                    self.walk_counted(sf, container, (generation, &rank), &mut siblings)
                };
                // A dependency's table is walked on its meter: a walk the
                // pool refuses evicts the others and walks again
                // (`registry::with_room`; review round 7: under either mode
                // a pool with no room for the walk failed every pass). A
                // walk is idempotent: each `path_dict` entry keeps the
                // least stamp.
                match &slot.kind {
                    SlotKind::Dependency { dep } => dep.with_room(walk)?,
                    SlotKind::Entry { .. } | SlotKind::Ready(_) => walk()?,
                }
                slot.set_walked();
                grew = load_keys_here() != before;
            }
            after = Some(rank);
            drop(held);
        }
        if grew {
            return Err(changed());
        }
        Ok(())
    }

    /// `ContainerHelper.parse_preload_table`
    /// (`helpers/ContainerHelper.py:31-49`): every row's preload pointers
    /// dereferenced, and each target's path id given the row's path in its
    /// file's `path_dict` unless it has one (`setdefault`);
    /// `FileNotFoundError` and `KeyError` skipped, any other failure raised.
    ///
    /// UnityPy's loop is quadratic (every row walks its whole span). Here a
    /// table index is dereferenced at the first row whose span reaches it,
    /// and again, from the walk's next search for an index on, once a load
    /// has changed a key its external's lookup read since its last
    /// dereference, the walk's own or another thread's ([`Pending`], and the
    /// lists of [`Targets`]): UnityPy's dereference that gives the file the
    /// last one gave sets nothing (`setdefault`), so each file a dereference
    /// reaches gets the path of the first row that reaches it there, as in
    /// UnityPy (review round 13: each index was dereferenced once, so the
    /// file a load in between put under its external's key got no later
    /// row's path). The walk takes what changed before each search, so a
    /// load another thread makes gives what UnityPy's walk gives with each
    /// key the load changes changed at the point of the walk at which it
    /// was published, a point between two searches or dereferences (a load
    /// publishes its keys one at a time; `super::watch`'s module docs;
    /// review round 14: another thread's change put back no index, and set
    /// a flag the walk's own later change of the key then found gone), but
    /// for a key the load adds, which does not fail the pass (below).
    /// Each external's answer is kept while it holds ([`Targets`]), so an
    /// external is looked up once, and once more per change to a key its
    /// lookup read, under every option set and whatever other keys other
    /// threads change meanwhile (`EnvStats::preload_lookups` counts them);
    /// each bundle's names indexed once per pass ([`Siblings`]), and each
    /// row's path copied once, charged to this file's meter. So a walk no
    /// load changes is linear in the rows, the table and the externals
    /// (plan decision 11, counting no work), and what changes make it do
    /// again (an index put back, an external looked up again) is counted as
    /// work on this file's meter, whose allowance bounds a table whose
    /// answers loads change over and over ([`crate::LimitKind::DecodeWork`]).
    ///
    /// A dereference of an external answered before answers as UnityPy's
    /// would: `find_file` registers what it loads under the path it found,
    /// whose basename is the name asked when the walk found that name, and a
    /// longer one when it matched a file name by suffix (`ab.assets` finds
    /// `xab.assets`, `environment.py:345-354`), so every dereference of that
    /// external loads it again and the last copy keeps its key. The index
    /// that loaded such a copy is dereferenced again only once a change to a
    /// key its lookup read makes the name it asked findable; until then,
    /// where rows' spans overlap, the external is loaded once per index, not
    /// once per row and index (`PARITY.md` section 19.3; plan section 9,
    /// Q11).
    fn preload(
        &self,
        sf: &Arc<SerializedFile>,
        container: &Container,
        at: (u64, &Rank),
        siblings: &mut Siblings,
    ) -> Result<(), Error> {
        if matches!(container.preload_table(), Ok(None)) {
            return Ok(());
        }
        let malformed = |detail: &'static str| Error::Malformed {
            what: "container",
            detail: detail.into(),
        };
        let meter = sf.meter();
        let mut pending: Option<Pending> = None;
        let mut targets = Targets {
            known: HashMap::new(),
            answers: MeteredVec::new(meter),
            next: None,
            walk: self
                .inner
                .registry
                .watches()
                .start(meter, [&self.inner.registry, &self.inner.files]),
            charge: Charge::zero(meter),
        };
        for (r, row) in container.rows().iter().enumerate() {
            // `start = info.preloadIndex`, `size = info.preloadSize`: a
            // member the AssetInfo lacks raises; one that is no `int` raises
            // here too (PARITY.md section 19.3).
            let (Some(start), Some(size)) = (row.preload_index, row.preload_size) else {
                return Err(malformed("an AssetInfo index is not an int"));
            };
            if start < 0 || size <= 0 {
                continue;
            }
            // `len(self._preload_table)`: a table that is not a list raises.
            let table = container.preload_table()?.unwrap_or_default();
            if start + size > i128::try_from(table.len()).unwrap_or(i128::MAX) {
                continue;
            }
            let (Ok(s), Ok(e)) = (usize::try_from(start), usize::try_from(start + size)) else {
                continue;
            };
            let pending = match &mut pending {
                Some(u) => u,
                None => pending.insert(Pending::full(table.len(), meter)?),
            };
            let mut path: Option<Option<RawStr>> = None;
            let mut from = s;
            loop {
                #[cfg(test)]
                self.inner.registry.hook(super::registry::Hook::Search {
                    row: r,
                    index: from,
                });
                // What a load changed since the last search, any thread's
                // (the last dereference's own included, this index's
                // answer too), is dereferenced again from here on in this
                // row, or from the next row on: UnityPy's walk, which
                // dereferences every element of every span, would find the
                // change at each of those.
                targets.reopen(pending, meter)?;
                let Some(j) = pending.first_from(from).filter(|&j| j < e) else {
                    break;
                };
                pending.remove(j);
                from = j.saturating_add(1);
                #[cfg(test)]
                self.inner
                    .registry
                    .hook(super::registry::Hook::Step { row: r, index: j });
                // An element that is no pointer raises (PARITY.md 19.3).
                let Some(p) = table.get(j).copied().flatten() else {
                    return Err(malformed("a preload table element is not a pointer"));
                };
                if !p.is_set() {
                    continue;
                }
                self.inner.preload_derefs.fetch_add(1, Ordering::Relaxed);
                let (target, answer) = self.preload_target(sf, p, &mut targets, siblings)?;
                if let Some(id) = answer {
                    targets.link(j, id, table.len(), meter)?;
                }
                if let Some(target) = target
                    && !self.may_reopen(target.file())
                {
                    let tf = target.file();
                    let path = match &path {
                        Some(p) => p.clone(),
                        None => path
                            .insert(
                                row.path
                                    .as_ref()
                                    .map(|p| RawStr::charged(p.as_bytes(), meter))
                                    .transpose()?,
                            )
                            .clone(),
                    };
                    let stamp = (
                        at.0,
                        at.1.clone(),
                        u32::try_from(r).unwrap_or(u32::MAX),
                        u32::try_from(j).unwrap_or(u32::MAX),
                    );
                    tf.container()?
                        .set_default(p.path_id, path.as_ref(), &stamp, tf.meter())?;
                }
            }
        }
        Ok(())
    }

    /// `pptr.deref()` for a preload element: the object, or `None` where the
    /// preload skips (`FileNotFoundError`, `KeyError`); and the number of
    /// the flag whose setting changes the answer (`None`: an answer for the
    /// walk, the own file's, a sibling's or a file id past the end). An
    /// external's answer is kept in `targets` while it holds (charged to its
    /// charge); a lookup of an external the walk looked up before is
    /// counted as work on `sf`'s meter first (`STEP_WORK` and the bytes of
    /// the external's path, which the lookup cleans, simplifies and hashes).
    fn preload_target(
        &self,
        sf: &Arc<SerializedFile>,
        p: PPtr,
        targets: &mut Targets,
        siblings: &mut Siblings,
    ) -> Result<(Option<ObjectRef>, Option<usize>), Error> {
        let (found, answer) = if p.file_id == 0 {
            (Some(Found::File(File::Serialized(sf.clone()))), None)
        } else if let Some(k) = targets.kept(p.file_id) {
            (k.found.clone(), k.flag.as_ref().map(|f| f.id()))
        } else {
            if targets.known.contains_key(&p.file_id) {
                let bytes = Self::external_path(sf, p.file_id).map_or(0, <[u8]>::len);
                sf.meter().count_work(
                    STEP_WORK.saturating_add(u64::try_from(bytes).unwrap_or(u64::MAX)),
                )?;
            }
            self.inner.preload_lookups.fetch_add(1, Ordering::Relaxed);
            // Watched before the lookup reads, so no change it misses is
            // missed by the flag; its answer's list made first, so the flag's
            // number is its place there.
            let keys = Self::lookup_keys(sf, p.file_id, &targets.walk)?;
            targets.answers.reserve(1)?;
            let flag = targets.walk.watch(&keys)?;
            let id = flag.id();
            targets.answers.push(Answer { head: NONE })?;
            #[cfg(test)]
            self.inner.registry.hook(super::registry::Hook::Watched);
            // What to record: kept until a change, kept for the walk, or
            // not kept (the resolver's load).
            let (found, kept, flag) = match self.external(sf, p.file_id, Some(siblings), None) {
                Ok((t, Reached::Sibling)) => (Some(t), true, None),
                Ok((t, Reached::Lookup)) => (Some(t), true, Some(flag)),
                Ok((t, Reached::Load)) => (Some(t), false, Some(flag)),
                Err(DerefError::FileIdPastEnd { .. }) => (None, true, None),
                Err(DerefError::NotFound { .. }) => (None, true, Some(flag)),
                Err(e) => return Err(deref_error(e)),
            };
            let answer = flag.is_some().then_some(id);
            targets.keep(p.file_id, found.clone(), kept, flag)?;
            (found, answer)
        };
        let Some(found) = found else {
            return Ok((None, answer));
        };
        match Self::object_of(&found, p.path_id) {
            Ok(o) => Ok((Some(o), answer)),
            Err(DerefError::MissingObject { .. }) => Ok((None, answer)),
            Err(e) => Err(deref_error(e)),
        }
    }

    /// External `file_id`'s stored path, if `sf` has that external.
    fn external_path(sf: &SerializedFile, file_id: i32) -> Option<&[u8]> {
        let externals = sf.externals();
        pptr::external_index(file_id, externals.len())
            .ok()
            .and_then(|i| externals.get(i))
            .map(|e| e.path.as_bytes())
    }

    /// The keys external `file_id`'s lookup read past `sf`'s bundle names
    /// ([`Self::external`]), each with the lock it is published under: the
    /// registry key `find_file` asks (`simplify_name` twice), and for a
    /// top-level file the lowered `files` key its sibling scan matches.
    fn lookup_keys(
        sf: &SerializedFile,
        file_id: i32,
        walk: &Walk,
    ) -> Result<Vec<(usize, u64)>, Error> {
        let externals = sf.externals();
        let Some(external) = pptr::external_index(file_id, externals.len())
            .ok()
            .and_then(|i| externals.get(i))
        else {
            return Ok(Vec::new());
        };
        let meter = sf.meter();
        let clean = clean_external_path(&external.path, meter)?;
        let simple = simplify_name(&simplify_name(&clean, meter)?, meter)?;
        let mut keys = vec![(REGISTRY, walk.hash(simple.as_bytes()))];
        if sf.parent().is_none() {
            keys.push((FILES, walk.hash(clean.as_bytes())));
        }
        Ok(keys)
    }
}

#[cfg(test)]
mod drop_tests;