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
// Ported from UnityPy 1.25.2 (MIT): UnityPyBoost/TypeTreeHelper.cpp. See THIRD_PARTY_LICENSES.
//! The Python objects UnityPy's writer duck-types (W2): what a value is to
//! Python ([`Obj`]), the attributes of each type the writer models, and the
//! operations the writer applies to an object (`bool`, `len`, `o[i]`,
//! `getattr`, the numbers `struct.pack` takes).

use super::Writer;
use crate::value::{PrimArray, Value};

/// The Python object a value is to UnityPy's writer, which duck-types
/// (W2): a value of the tree, or one Python's operations make from one (an
/// element of a `str`, `bytes` or a run, a dict's key, an attribute).
#[derive(Clone, Copy, Debug)]
pub(super) enum Obj<'a> {
    V(&'a Value),
    Int(i128),
    Bool(bool),
    F32(u32),
    F64(u64),
    /// One character of a `str` (its surrogate-escaped bytes), or a key.
    Str(&'a [u8]),
    /// A bound method or a builtin: truthy, no attribute the writer reads.
    Method,
}

/// The Python type an object is, for `getattr`.
#[derive(Clone, Copy, PartialEq, Eq)]
enum PyType {
    Int,
    Bool,
    Float,
    Str,
    Bytes,
    List,
    Tuple,
    Dict,
    Method,
}

/// Python 3.11's non-dunder attributes of each type a value becomes, and
/// which are data (`int`, `float`) rather than methods (`dir(type)`, recorded
/// by `tools/fixtures/walk.py` into `py_attrs.json`).
const INT_ATTRS: &[(&str, &str)] = &[
    ("as_integer_ratio", "method"),
    ("bit_count", "method"),
    ("bit_length", "method"),
    ("conjugate", "method"),
    ("denominator", "int"),
    ("from_bytes", "method"),
    ("imag", "int"),
    ("numerator", "int"),
    ("real", "int"),
    ("to_bytes", "method"),
];
const FLOAT_ATTRS: &[(&str, &str)] = &[
    ("as_integer_ratio", "method"),
    ("conjugate", "method"),
    ("fromhex", "method"),
    ("hex", "method"),
    ("imag", "float"),
    ("is_integer", "method"),
    ("real", "float"),
];
const STR_ATTRS: &[(&str, &str)] = &[
    ("capitalize", "method"),
    ("casefold", "method"),
    ("center", "method"),
    ("count", "method"),
    ("encode", "method"),
    ("endswith", "method"),
    ("expandtabs", "method"),
    ("find", "method"),
    ("format", "method"),
    ("format_map", "method"),
    ("index", "method"),
    ("isalnum", "method"),
    ("isalpha", "method"),
    ("isascii", "method"),
    ("isdecimal", "method"),
    ("isdigit", "method"),
    ("isidentifier", "method"),
    ("islower", "method"),
    ("isnumeric", "method"),
    ("isprintable", "method"),
    ("isspace", "method"),
    ("istitle", "method"),
    ("isupper", "method"),
    ("join", "method"),
    ("ljust", "method"),
    ("lower", "method"),
    ("lstrip", "method"),
    ("maketrans", "method"),
    ("partition", "method"),
    ("removeprefix", "method"),
    ("removesuffix", "method"),
    ("replace", "method"),
    ("rfind", "method"),
    ("rindex", "method"),
    ("rjust", "method"),
    ("rpartition", "method"),
    ("rsplit", "method"),
    ("rstrip", "method"),
    ("split", "method"),
    ("splitlines", "method"),
    ("startswith", "method"),
    ("strip", "method"),
    ("swapcase", "method"),
    ("title", "method"),
    ("translate", "method"),
    ("upper", "method"),
    ("zfill", "method"),
];
const BYTES_ATTRS: &[(&str, &str)] = &[
    ("capitalize", "method"),
    ("center", "method"),
    ("count", "method"),
    ("decode", "method"),
    ("endswith", "method"),
    ("expandtabs", "method"),
    ("find", "method"),
    ("fromhex", "method"),
    ("hex", "method"),
    ("index", "method"),
    ("isalnum", "method"),
    ("isalpha", "method"),
    ("isascii", "method"),
    ("isdigit", "method"),
    ("islower", "method"),
    ("isspace", "method"),
    ("istitle", "method"),
    ("isupper", "method"),
    ("join", "method"),
    ("ljust", "method"),
    ("lower", "method"),
    ("lstrip", "method"),
    ("maketrans", "method"),
    ("partition", "method"),
    ("removeprefix", "method"),
    ("removesuffix", "method"),
    ("replace", "method"),
    ("rfind", "method"),
    ("rindex", "method"),
    ("rjust", "method"),
    ("rpartition", "method"),
    ("rsplit", "method"),
    ("rstrip", "method"),
    ("split", "method"),
    ("splitlines", "method"),
    ("startswith", "method"),
    ("strip", "method"),
    ("swapcase", "method"),
    ("title", "method"),
    ("translate", "method"),
    ("upper", "method"),
    ("zfill", "method"),
];
const LIST_ATTRS: &[(&str, &str)] = &[
    ("append", "method"),
    ("clear", "method"),
    ("copy", "method"),
    ("count", "method"),
    ("extend", "method"),
    ("index", "method"),
    ("insert", "method"),
    ("pop", "method"),
    ("remove", "method"),
    ("reverse", "method"),
    ("sort", "method"),
];
const TUPLE_ATTRS: &[(&str, &str)] = &[("count", "method"), ("index", "method")];

/// The writer's table of a Python type's non-dunder attributes and their
/// kinds (`"method"`, `"int"`, `"float"`), by the type's name; empty for a
/// type it does not model. Public for the fixture test that checks it
/// against Python 3.11's `dir()`.
#[doc(hidden)]
#[must_use]
pub fn python_attributes(type_name: &str) -> &'static [(&'static str, &'static str)] {
    match type_name {
        "int" | "bool" => INT_ATTRS,
        "float" => FLOAT_ATTRS,
        "str" => STR_ATTRS,
        "bytes" => BYTES_ATTRS,
        "list" => LIST_ATTRS,
        "tuple" => TUPLE_ATTRS,
        _ => &[],
    }
}

/// A clean name longer than every attribute the writer models.
pub(super) const CLEAN_MAX: usize = 32;

/// UnityPyBoost's `clean_name` (`UnityPyBoost/TypeTreeHelper.cpp:28-65`;
/// the C++ node is the one UnityPy builds when the extension loads), into a
/// fixed buffer: `None` when the name cleans to more than [`CLEAN_MAX`]
/// bytes, which no modelled attribute is. It reads at most the name's first
/// `CLEAN_MAX + 8` bytes: a C string longer than that cleans to more than
/// [`CLEAN_MAX`] whatever follows (review round 6's sweep: a write that
/// failed at a child of a long name had searched all of it for a NUL).
pub(super) fn clean_name(name: &[u8], buf: &mut [u8; CLEAN_MAX]) -> Option<usize> {
    // `:1218-1220`: the name is a C string (`PyUnicode_AsUTF8`), so it ends
    // at its first NUL, and so does the cleaned one. The `(int&)` prefix,
    // a trailing `?` and the NUL are the most a clean drops.
    let window = name.get(..CLEAN_MAX + 8).unwrap_or(name);
    crate::walk::steps(window.len());
    let mut name = match window.iter().position(|b| *b == 0) {
        Some(end) => window.get(..end).unwrap_or_default(),
        None if window.len() < name.len() => return None,
        None => window,
    };
    // `:38-41`: the `(int&)` prefix.
    if let Some(rest) = name.strip_prefix(b"(int&)") {
        name = rest;
    }
    // `:44-47`: one trailing `?`.
    if let Some(rest) = name.strip_suffix(b"?") {
        name = rest;
    }
    // `:58-62`: a leading digit takes an `x` (`isdigit`, ASCII in the C
    // locale); such a name is never `pass` or `from`.
    let digit = name.first().is_some_and(u8::is_ascii_digit);
    let pass_from = matches!(name, b"pass" | b"from");
    let len = name.len() + usize::from(digit) + usize::from(pass_from);
    let out = buf.get_mut(..len)?;
    let (head, rest) = out.split_at_mut(usize::from(digit));
    head.fill(b'x');
    for (o, b) in rest.iter_mut().zip(name.iter().chain(b"_".iter())) {
        // `:50`: `[ \.:\-\[\]]` becomes `_`.
        *o = if matches!(b, b' ' | b'.' | b':' | b'-' | b'[' | b']') {
            b'_'
        } else {
            *b
        };
    }
    Some(len)
}

/// Whether UnityPyBoost's `clean_name` of `name` is `target` ([`clean_name`]),
/// the key UnityPy's object mode gives a field (`parse_class`). A `target`
/// longer than [`CLEAN_MAX`] bytes is never one.
#[must_use]
pub fn cleans_to(name: &[u8], target: &[u8]) -> bool {
    let mut buf = [0; CLEAN_MAX];
    clean_name(name, &mut buf).and_then(|n| buf.get(..n)) == Some(target)
}

/// The characters of a surrogate-escaped `str`: each valid UTF-8 character,
/// and each byte that is not part of one (one escape character each).
pub(super) fn chars(s: &[u8]) -> impl Iterator<Item = &[u8]> {
    s.utf8_chunks().flat_map(|c| {
        let valid = c.valid();
        let v = valid.char_indices().map(move |(i, ch)| {
            valid
                .as_bytes()
                .get(i..i + ch.len_utf8())
                .unwrap_or_default()
        });
        let invalid = c.invalid();
        v.chain((0..invalid.len()).map(move |i| invalid.get(i..=i).unwrap_or_default()))
    })
}

pub(super) fn narrow(x: f64) -> Option<u32> {
    // `PyFloat_Pack4`: `(float)x`, an overflow refused; a NaN's quiet bit
    // set and payload kept, as x86-64's `cvtsd2ss`.
    if x.is_nan() {
        let b = x.to_bits();
        let sign = u32::try_from(b >> 63).unwrap_or(0) << 31;
        let mant = u32::try_from((b >> 29) & 0x003f_ffff).unwrap_or(0);
        return Some(sign | 0x7fc0_0000 | mant);
    }
    #[allow(clippy::cast_possible_truncation)]
    let y = x as f32;
    if y.is_infinite() && !x.is_infinite() {
        return None;
    }
    Some(y.to_bits())
}

/// `(double)f` on x86-64: exact, a NaN's quiet bit set.
fn widen(bits: u32) -> f64 {
    let f = f32::from_bits(bits);
    if f.is_nan() {
        let sign = u64::from(bits >> 31) << 63;
        let mant = u64::from((bits & 0x007f_ffff) | 0x0040_0000) << 29;
        return f64::from_bits(sign | 0x7ff0_0000_0000_0000 | mant);
    }
    f64::from(f)
}

impl Writer<'_, '_> {
    fn py_type(o: Obj<'_>) -> PyType {
        match o {
            Obj::V(Value::Bool(_)) | Obj::Bool(_) => PyType::Bool,
            Obj::V(Value::Int(_) | Value::UInt(_)) | Obj::Int(_) => PyType::Int,
            Obj::V(Value::F32(_) | Value::F64(_)) | Obj::F32(_) | Obj::F64(_) => PyType::Float,
            Obj::V(Value::Str(_)) | Obj::Str(_) => PyType::Str,
            Obj::V(Value::Bytes(_)) => PyType::Bytes,
            Obj::V(Value::Prims(_) | Value::List(_)) => PyType::List,
            Obj::V(Value::Tuple(_)) => PyType::Tuple,
            Obj::V(Value::Dict(_)) => PyType::Dict,
            Obj::Method => PyType::Method,
        }
    }

    /// `bool(o)`.
    pub(super) fn truthy(o: Obj<'_>) -> bool {
        match o {
            Obj::V(v) => match v {
                Value::F32(b) => widen(*b) != 0.0,
                Value::F64(b) => f64::from_bits(*b) != 0.0,
                Value::Bool(b) => *b,
                Value::Int(i) => *i != 0,
                Value::UInt(_) => true,
                Value::Str(s) => !s.is_empty(),
                Value::Bytes(b) => !b.is_empty(),
                Value::Prims(p) => !p.is_empty(),
                Value::List(l) => !l.is_empty(),
                Value::Tuple(t) => !t.is_empty(),
                Value::Dict(m) => !m.is_empty(),
            },
            Obj::Int(i) => i != 0,
            Obj::Bool(b) => b,
            Obj::F32(b) => widen(b) != 0.0,
            Obj::F64(b) => f64::from_bits(b) != 0.0,
            Obj::Str(s) => !s.is_empty(),
            Obj::Method => true,
        }
    }

    /// The integer `struct.pack` takes (`int`, `bool`).
    pub(super) fn as_int(o: Obj<'_>) -> Option<i128> {
        match o {
            Obj::V(Value::Bool(b)) => Some(i128::from(*b)),
            Obj::Bool(b) => Some(i128::from(b)),
            Obj::V(Value::Int(i)) => Some(i128::from(*i)),
            Obj::V(Value::UInt(u)) => Some(i128::from(*u)),
            Obj::Int(i) => Some(i),
            _ => None,
        }
    }

    /// The `float` `struct.pack('d')` takes: `float(o)` of an `int`, `bool`
    /// or `float`.
    #[allow(clippy::cast_precision_loss)]
    pub(super) fn as_double(o: Obj<'_>) -> Option<f64> {
        match o {
            Obj::V(Value::F32(b)) => Some(widen(*b)),
            Obj::F32(b) => Some(widen(b)),
            Obj::V(Value::F64(b)) => Some(f64::from_bits(*b)),
            Obj::F64(b) => Some(f64::from_bits(b)),
            _ => Self::as_int(o).map(|i| i as f64),
        }
    }

    /// The bytes of a `str` (`value.encode("utf8", "surrogateescape")`).
    pub(super) fn as_str(o: Obj<'_>) -> Option<&[u8]> {
        match o {
            Obj::V(Value::Str(s)) => Some(s.as_bytes()),
            Obj::Str(s) => Some(s),
            _ => None,
        }
    }

    /// `len(o)`.
    pub(super) fn len(o: Obj<'_>) -> Option<usize> {
        match o {
            Obj::V(Value::Str(s)) => Some(chars(s.as_bytes()).count()),
            Obj::Str(s) => Some(chars(s).count()),
            Obj::V(Value::Bytes(b)) => Some(b.len()),
            Obj::V(Value::Prims(p)) => Some(p.len()),
            Obj::V(Value::List(l)) => Some(l.len()),
            Obj::V(Value::Tuple(t)) => Some(t.len()),
            Obj::V(Value::Dict(m)) => Some(m.len()),
            _ => None,
        }
    }

    pub(super) fn prim_item(p: &PrimArray, i: usize) -> Option<Obj<'_>> {
        Some(match p {
            PrimArray::Bool(v) => Obj::Bool(*v.get(i)?),
            PrimArray::U8(v) => Obj::Int(i128::from(*v.get(i)?)),
            PrimArray::I8(v) => Obj::Int(i128::from(*v.get(i)?)),
            PrimArray::U16(v) => Obj::Int(i128::from(*v.get(i)?)),
            PrimArray::I16(v) => Obj::Int(i128::from(*v.get(i)?)),
            PrimArray::U32(v) => Obj::Int(i128::from(*v.get(i)?)),
            PrimArray::I32(v) => Obj::Int(i128::from(*v.get(i)?)),
            PrimArray::U64(v) => Obj::Int(i128::from(*v.get(i)?)),
            PrimArray::I64(v) => Obj::Int(i128::from(*v.get(i)?)),
            PrimArray::F32(v) => Obj::F32(*v.get(i)?),
            PrimArray::F64(v) => Obj::F64(*v.get(i)?),
        })
    }

    /// `o[i]` for a sequence (a dict's keys are strings, so `d[0]` fails).
    pub(super) fn item(o: Obj<'_>, i: usize) -> Option<Obj<'_>> {
        match o {
            Obj::V(Value::Str(s)) => chars(s.as_bytes()).nth(i).map(Obj::Str),
            Obj::Str(s) => chars(s).nth(i).map(Obj::Str),
            Obj::V(Value::Bytes(b)) => b.get(i).map(|x| Obj::Int(i128::from(*x))),
            Obj::V(Value::Prims(p)) => Self::prim_item(p, i),
            Obj::V(Value::List(l)) => l.get(i).map(Obj::V),
            Obj::V(Value::Tuple(t)) => t.get(i).map(Obj::V),
            _ => None,
        }
    }

    /// `getattr(o, name)` for a non-dict value (W2), `name` cleaned.
    pub(super) fn getattr<'o>(o: Obj<'o>, name: &[u8]) -> Result<Option<Obj<'o>>, ()> {
        if name.len() >= 4 && name.starts_with(b"__") && name.ends_with(b"__") {
            // Every Python object has dunder attributes; the writer does not
            // model them (plan 3.11, W2: the names its matrix uses).
            return Err(());
        }
        let ty = Self::py_type(o);
        let table = match ty {
            PyType::Int | PyType::Bool => INT_ATTRS,
            PyType::Float => FLOAT_ATTRS,
            PyType::Str => STR_ATTRS,
            PyType::Bytes => BYTES_ATTRS,
            PyType::List => LIST_ATTRS,
            PyType::Tuple => TUPLE_ATTRS,
            PyType::Dict | PyType::Method => &[],
        };
        let Some((attr, kind)) = table.iter().find(|(a, _)| a.as_bytes() == name) else {
            return Ok(None);
        };
        Ok(Some(match (*kind, *attr) {
            ("method", _) => Obj::Method,
            ("int", "real" | "numerator") => Obj::Int(Self::as_int(o).unwrap_or(0)),
            ("int", "denominator") => Obj::Int(1),
            ("int", _) => Obj::Int(0),
            ("float", "real") => match o {
                Obj::V(Value::F32(b)) => Obj::F32(*b),
                Obj::F32(b) => Obj::F32(b),
                Obj::V(Value::F64(b)) => Obj::F64(*b),
                Obj::F64(b) => Obj::F64(b),
                _ => Obj::F64(0),
            },
            _ => Obj::F64(0),
        }))
    }
}