unityrs-testkit 1.1.0

Synthetic Unity files built in code, for unityrs's own tests and the CLI's test hooks; not a stable API. Never game data.
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
//! The NGUI scenes, one function each, and [`scenes`], which lists them by
//! name for `tools/parity/ngui_scenes.py`.
//!
//! Each scene is a set of bundles with the `deps` a job spec would give them
//! (`docs/BATCH_SPEC.md` 1.2), the inputs to cut, each over its own view
//! (4.2), and the cutter's options (1.5's `atlases`, and `textures`'s key
//! when the scene exports textures). Sheets are [`super::sheet`]s, so a
//! test computes every crop's pixels from [`super::pixel`] by hand.

use super::{
    BuildError, Object, PropName, V, atlas, file, format, material, mono_behaviour, mono_script,
    sheet, sprite, texture, vector,
};
use crate::env::{bundle, res_bytes};

mod failures;
mod outputs;
mod recognition;
mod shapes;
mod sheets;
mod winners;

pub use failures::failure_scenes;

pub use outputs::{
    duplicates, floats, json_bytes, json_bytes_with_textures, non_ascii, sheet_texture_key_taken,
    texture_key_is_json_key, two_atlases_one_sheet, two_atlases_without_textures, two_sheet_groups,
};
pub use recognition::{failed_reads, layouts};
pub use shapes::{
    bad_script_pointer, cleaned_names, list_pairs, long_keys, platform_blobs, raw_names,
    wide_texture_fields,
};
pub use sheets::{
    crop_bounds, failed_sheet_rows, fallback_rows, repeated_rows, sheet_skips, streamed_sheets,
    webfile_sibling,
};
pub use winners::{textured_winners, winners};

/// One bundle of a scene: its spec `id`, its bytes and its `deps`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SceneBundle {
    /// The spec `id`.
    pub id: String,
    /// The bundle's bytes.
    pub data: Vec<u8>,
    /// The spec `deps`.
    pub deps: Vec<String>,
}

/// The cutter's options (`docs/BATCH_SPEC.md` 1.5), the sheet scope
/// `own_files_by_name` being version 1's only one.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CutOptions {
    /// `atlases.sprite_key`.
    pub sprite_key: String,
    /// `atlases.json_key`.
    pub json_key: String,
    /// `textures.key`, when the scene exports textures.
    pub texture_key: Option<String>,
    /// `sprites.key`, when the scene exports Sprites.
    pub sprites_key: Option<String>,
    /// `atlases.script_classes`.
    pub script_classes: Vec<String>,
    /// `atlases.skip_name_suffixes`.
    pub skip_name_suffixes: Vec<String>,
}

impl Default for CutOptions {
    fn default() -> Self {
        Self {
            sprite_key: "{atlas}/{sprite}.png".to_owned(),
            json_key: "{atlas}.json".to_owned(),
            texture_key: None,
            sprites_key: None,
            script_classes: vec!["NGUIAtlas".to_owned()],
            skip_name_suffixes: Vec::new(),
        }
    }
}

impl CutOptions {
    /// The defaults, with textures exported under `key`.
    #[must_use]
    pub fn textures(key: &str) -> Self {
        Self {
            texture_key: Some(key.to_owned()),
            ..Self::default()
        }
    }
}

/// A scene: bundles, the inputs cut (each over its view) and the options.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AtlasScene {
    /// The bundles, inputs and dependencies alike.
    pub bundles: Vec<SceneBundle>,
    /// The ids of the inputs, in the order a test cuts them.
    pub inputs: Vec<String>,
    /// The cutter's options.
    pub options: CutOptions,
}

/// A scene's result.
pub type Built = Result<AtlasScene, BuildError>;

/// A scene's builder.
pub(super) type Make = fn() -> Built;

pub(super) fn one(id: &str, cabs: &[(&str, Vec<u8>)], deps: &[&str]) -> SceneBundle {
    SceneBundle {
        id: id.to_owned(),
        data: bundle(cabs),
        deps: deps.iter().map(|d| (*d).to_owned()).collect(),
    }
}

pub(super) fn scene(bundles: Vec<SceneBundle>, inputs: &[&str], options: CutOptions) -> AtlasScene {
    AtlasScene {
        bundles,
        inputs: inputs.iter().map(|i| (*i).to_owned()).collect(),
        options,
    }
}

/// One input, `x.bundle`, of one SerializedFile `cab`, with default options.
pub(super) fn alone(cab: &str, objects: &[Object]) -> Built {
    let data = file(&[], objects, true)?;
    Ok(scene(
        vec![one("x.bundle", &[(cab, data)], &[])],
        &["x.bundle"],
        CutOptions::default(),
    ))
}

/// The atlas script every scene's atlases name, at path id 1.
pub(super) const SCRIPT: (i32, i64) = (0, 1);

pub(super) const NULL: (i32, i64) = (0, 0);

pub(super) fn rgba(path_id: i64, name: &str, base: u8, w: u8, h: u8) -> Object {
    let size = (i32::from(w), i32::from(h));
    texture(
        path_id,
        name.as_bytes(),
        format::RGBA32,
        size,
        sheet(base, w, h, false),
    )
}

pub(super) fn plain(name: &str, rect: (i32, i32, i32, i32)) -> V {
    sprite(name.as_bytes(), rect, [0; 8])
}

/// `v`, a struct, with `extra` fields after its own.
pub(super) fn with(v: V, extra: Vec<(&str, V)>) -> V {
    match v {
        V::Struct(ty, mut fields) => {
            fields.extend(extra.into_iter().map(|(n, f)| (n.to_owned(), f)));
            V::Struct(ty, fields)
        }
        other => other,
    }
}

/// An atlas with a null material, cutting the own texture of its name.
pub(super) fn by_name(path_id: i64, name: &str, sprites: Vec<V>) -> Object {
    atlas(path_id, name.as_bytes(), SCRIPT, NULL, sprites, Vec::new())
}

/// Step 1 by a plain-string `_MainTex`, in the atlas's own file. A texture
/// named as the atlas comes first in object order, and loses to the
/// material's. The sprites cover a crop inside the sheet, one past its right
/// and bottom edges, one past its top-left, and two empty rectangles; the
/// first has borders and paddings, which are not applied, and a `rotated`
/// field is written as read.
pub fn own_sheet_by_material_name() -> Built {
    let trimmed = with(
        sprite(b"a", (0, 0, 2, 2), [1, 2, 3, 4, 5, 6, 7, 8]),
        vec![("rotated", V::Bool(true))],
    );
    let mut sprites = vec![trimmed];
    for (name, rect) in [
        ("b", (1, 2, 3, 2)),
        ("edge", (3, 3, 2, 2)),
        ("neg", (-1, -1, 2, 2)),
        ("zero", (0, 0, 0, 2)),
        ("negw", (2, 2, -1, 1)),
    ] {
        sprites.push(with(plain(name, rect), vec![("rotated", V::Bool(false))]));
    }
    alone(
        "CAB-x1",
        &[
            mono_script(1, "NGUIAtlas"),
            rgba(2, "ui", 0x90, 2, 2),
            rgba(3, "sheet_a", 1, 4, 4),
            material(
                4,
                "ui_mat",
                PropName::Plain,
                &[("_BumpMap", NULL), ("_MainTex", (0, 3))],
            ),
            atlas(5, b"ui", SCRIPT, (0, 4), sprites, Vec::new()),
        ],
    )
}

/// Step 1 by a `FastPropertyName` `_MainTex` whose texture is in a
/// dependency: its name is read there, and matched among the input's own
/// textures, where a texture of that name is the sheet (and a texture named
/// as the atlas loses to it).
pub fn struct_maintex_in_dependency() -> Built {
    let dep = file(&[], &[rgba(10, "shared_sheet", 0x50, 1, 1)], true)?;
    let cab = file(
        &["archive:/CAB-d1/CAB-d1"],
        &[
            mono_script(1, "NGUIAtlas"),
            material(2, "m", PropName::Fast, &[("_MainTex", (1, 10))]),
            rgba(3, "shared_sheet", 0x30, 3, 3),
            atlas(
                4,
                b"far",
                SCRIPT,
                (0, 2),
                vec![plain("s", (0, 0, 3, 3))],
                Vec::new(),
            ),
            rgba(5, "far", 0x70, 1, 1),
        ],
        true,
    )?;
    Ok(scene(
        vec![
            one("d.bundle", &[("CAB-d1", dep)], &[]),
            one("x.bundle", &[("CAB-x2", cab)], &["d.bundle"]),
        ],
        &["x.bundle"],
        CutOptions::default(),
    ))
}

/// The material and its `_MainTex` texture both in a dependency: the name
/// read there is none of the input's textures, so step 2 takes the texture
/// named as the atlas, an RGB24 sheet, cropped past its edge.
pub fn material_in_dependency() -> Built {
    let dep = file(
        &[],
        &[
            material(20, "m2", PropName::Plain, &[("_MainTex", (0, 21))]),
            rgba(21, "only_in_dep", 0x60, 1, 1),
        ],
        true,
    )?;
    let sheet_b = texture(22, b"atlas_b", format::RGB24, (2, 2), sheet(5, 2, 2, true));
    let cab = file(
        &["archive:/CAB-d2/CAB-d2"],
        &[
            mono_script(1, "NGUIAtlas"),
            atlas(
                2,
                b"atlas_b",
                SCRIPT,
                (1, 20),
                vec![plain("wide", (1, 0, 3, 3)), plain("in", (0, 1, 2, 1))],
                Vec::new(),
            ),
            sheet_b,
        ],
        true,
    )?;
    Ok(scene(
        vec![
            one("d.bundle", &[("CAB-d2", dep)], &[]),
            one("x.bundle", &[("CAB-x3", cab)], &["d.bundle"]),
        ],
        &["x.bundle"],
        CutOptions::default(),
    ))
}

/// A null material gives no name, so step 2 takes the texture named as the
/// atlas; `skip_name_suffixes` `["_ref"]` skips the reference atlas; a null
/// material with no texture of the atlas's name is `sheet_not_found`; and an
/// atlas with no `material` field at all gives no name either, so step 2
/// takes the texture of its name.
pub fn null_material_and_reference() -> Built {
    let one_sprite = || vec![plain("p", (0, 0, 2, 1))];
    let no_field = mono_behaviour(
        6,
        b"nomat",
        SCRIPT,
        vec![
            (
                "mSprites",
                vector(plain("q", (0, 0, 1, 1)), vec![plain("q", (0, 0, 1, 1))]),
            ),
            ("mPixelSize", V::Float(1.0)),
        ],
    );
    let mut built = alone(
        "CAB-x4",
        &[
            mono_script(1, "NGUIAtlas"),
            rgba(2, "plain", 7, 2, 1),
            by_name(3, "plain", one_sprite()),
            by_name(4, "plain_ref", one_sprite()),
            by_name(5, "lost", one_sprite()),
            no_field,
            rgba(7, "nomat", 0x0B, 1, 1),
        ],
    )?;
    built.options.skip_name_suffixes = vec!["_ref".to_owned()];
    Ok(built)
}

/// Every way step 1 gives no name, each atlas then cutting the own texture
/// of its name (step 2): no `_MainTex` pair (n1, whose `_BumpMap` names an
/// own texture, which a cutter taking the first pair would find), a
/// material in a CAB outside the view (n2), a missing object (n3), a null
/// `m_Texture` (n4), an `m_FileID` past the externals (n5), a `_MainTex`
/// texture, read in a dependency, whose name no own texture has (n6), a
/// pointer into the input's raw `.resS` row (n7), and a CAB outside the view
/// whose name is a directory at the root of a Linux host (n8), which
/// UnityPy's own lookup would try to open. And n9, whose material pointer's
/// `m_FileID` is -2: it counts from the end of the four externals, as
/// UnityPy's `PPtr.deref` indexes them, to `CAB-d5`, whose material names
/// `far`, so n9 cuts the own texture `far` (step 1), not its own name's.
pub fn missing_sheets() -> Built {
    let one_sprite = || vec![plain("p", (0, 0, 1, 1))];
    let dep = file(
        &[],
        &[
            rgba(1, "elsewhere", 0x21, 1, 1),
            material(2, "dmat", PropName::Plain, &[("_MainTex", (0, 3))]),
            rgba(3, "far", 0x22, 1, 1),
        ],
        true,
    )?;
    let mut objects = vec![
        mono_script(1, "NGUIAtlas"),
        material(2, "no_main", PropName::Plain, &[("_BumpMap", (0, 4))]),
        material(3, "null_tex", PropName::Plain, &[("_MainTex", NULL)]),
        rgba(4, "bump", 3, 1, 1),
        material(5, "named", PropName::Plain, &[("_MainTex", (2, 1))]),
    ];
    let materials = [
        (0, 2),
        (1, 99),
        (0, 98),
        (0, 3),
        (5, 2),
        (0, 5),
        (3, 1),
        (4, 1),
    ];
    for (n, at) in (1u8..).zip(materials) {
        let name = format!("n{n}");
        let path_id = 9 + i64::from(n);
        objects.push(atlas(
            path_id,
            name.as_bytes(),
            SCRIPT,
            at,
            one_sprite(),
            Vec::new(),
        ));
        objects.push(rgba(path_id + 10, &name, 0x30 + n, 1, 1));
    }
    objects.push(atlas(18, b"n9", SCRIPT, (-2, 2), one_sprite(), Vec::new()));
    objects.push(rgba(28, "n9", 0x39, 1, 1));
    objects.push(rgba(29, "far", 0x3A, 1, 1));
    let externals = [
        "archive:/CAB-gone/CAB-gone",
        "archive:/CAB-d5/CAB-d5",
        "archive:/CAB-x5/CAB-x5.resS",
        "archive:/etc/etc",
    ];
    let cab = file(&externals, &objects, true)?;
    Ok(scene(
        vec![
            one("d.bundle", &[("CAB-d5", dep)], &[]),
            one(
                "x.bundle",
                &[("CAB-x5", cab), ("CAB-x5.resS", res_bytes(16, 0))],
                &["d.bundle"],
            ),
        ],
        &["x.bundle"],
        CutOptions::default(),
    ))
}

/// Every scene, by name.
pub fn scenes() -> Result<Vec<(&'static str, AtlasScene)>, BuildError> {
    let all: [(&'static str, Make); 33] = [
        ("own_sheet_by_material_name", own_sheet_by_material_name),
        ("struct_maintex_in_dependency", struct_maintex_in_dependency),
        ("material_in_dependency", material_in_dependency),
        ("null_material_and_reference", null_material_and_reference),
        ("missing_sheets", missing_sheets),
        ("duplicates", duplicates),
        ("two_atlases_one_sheet", two_atlases_one_sheet),
        ("two_atlases_without_textures", two_atlases_without_textures),
        ("sheet_texture_key_taken", sheet_texture_key_taken),
        ("texture_key_is_json_key", texture_key_is_json_key),
        ("two_sheet_groups", two_sheet_groups),
        ("json_bytes", json_bytes),
        ("json_bytes_with_textures", json_bytes_with_textures),
        ("floats", floats),
        ("non_ascii", non_ascii),
        ("layouts", layouts),
        ("failed_reads", failed_reads),
        ("sheet_skips", sheet_skips),
        ("streamed_sheets", streamed_sheets),
        ("repeated_rows", repeated_rows),
        ("fallback_rows", fallback_rows),
        ("crop_bounds", crop_bounds),
        ("failed_sheet_rows", failed_sheet_rows),
        ("winners", winners),
        ("textured_winners", textured_winners),
        ("webfile_sibling", webfile_sibling),
        ("cleaned_names", cleaned_names),
        ("wide_texture_fields", wide_texture_fields),
        ("platform_blobs", platform_blobs),
        ("list_pairs", list_pairs),
        ("bad_script_pointer", bad_script_pointer),
        ("raw_names", raw_names),
        ("long_keys", long_keys),
    ];
    all.into_iter().map(|(name, f)| Ok((name, f()?))).collect()
}