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
// Ported from UnityPy 1.25.2 (MIT): UnityPy/export/ShaderConverter.py. See THIRD_PARTY_LICENSES.
//! Parsed form conversion: export/ShaderConverter.py:92-540.
use super::{
    Error, Program,
    output::Output,
    view::{Seq, View},
};
use std::sync::Arc;
use unityrs_core::limits::{Meter, MeteredVec};

pub(super) fn shader(
    v: View<'_>,
    platforms: Seq<'_>,
    programs: &[Program<'_>],
    out: &mut Output<'_>,
    meter: &Arc<Meter>,
) -> Result<(), Error> {
    out.text("Shader \"")?;
    out.text(v.field("m_Name")?.text()?)?;
    out.text("\" {\n")?;
    properties(v.field("m_PropInfo")?, out)?;
    for sub in v.field("m_SubShaders")?.list()? {
        subshader(sub, platforms, programs, out, meter)?;
    }
    for (key, label) in [
        ("m_FallbackName", "Fall back"),
        ("m_CustomEditorName", "CustomEditor"),
    ] {
        let name = v.field(key)?.text()?;
        if !name.is_empty() {
            out.text(label)?;
            out.text(" \"")?;
            out.text(name)?;
            out.text("\"\n")?;
        }
    }
    out.text("}")
}
pub(super) fn subshader(
    v: View<'_>,
    platforms: Seq<'_>,
    programs: &[Program<'_>],
    out: &mut Output<'_>,
    meter: &Arc<Meter>,
) -> Result<(), Error> {
    out.text("SubShader {\n")?;
    let lod = v.field("m_LOD")?.int()?;
    if lod != 0 {
        out.text(" LOD ")?;
        out.int(i128::from(lod))?;
        out.text("\n")?;
    }
    tags(v.field("m_Tags")?, 1, out)?;
    for pass in v.field("m_Passes")?.list()? {
        pass_write(pass, platforms, programs, out, meter)?;
    }
    out.text("}\n")
}
pub(super) fn tags(v: View<'_>, indent: usize, out: &mut Output<'_>) -> Result<(), Error> {
    let tags = v.field("tags")?;
    if tags.empty() {
        return Ok(());
    }
    for _ in 0..indent {
        out.text(" ")?;
    }
    out.text("Tags { ")?;
    for pair in tags.list()? {
        let mut items = pair.list()?;
        let key = items.next().ok_or(Error::Field("tag key"))?.text()?;
        let value = items.next().ok_or(Error::Field("tag value"))?.text()?;
        out.text("\"")?;
        out.text(key)?;
        out.text("\" = \"")?;
        out.text(value)?;
        out.text("\" ")?;
    }
    out.text("}\n")
}
pub(super) fn properties(v: View<'_>, out: &mut Output<'_>) -> Result<(), Error> {
    // Python joins a first element already ending in a newline with '\n'.
    out.text("Properties {\n")?;
    for prop in v.field("m_Props")?.list()? {
        out.text("\n")?;
        property(prop, out)?;
    }
    out.text("\n}\n")
}
pub(super) fn property(prop: View<'_>, out: &mut Output<'_>) -> Result<(), Error> {
    for attr in prop.field("m_Attributes")?.list()? {
        out.text("[")?;
        out.text(attr.text()?)?;
        out.text("] ")?;
    }
    out.text(prop.field("m_Name")?.text()?)?;
    out.text(" (\"")?;
    out.text(prop.field("m_Description")?.text()?)?;
    out.text("\", ")?;
    let kind = prop.field("m_Type")?.int()?;
    match kind {
        0 => out.text("Color")?,
        1 => out.text("Vector")?,
        2 => out.text("Float")?,
        // Tier C: serialized Int (5) uses ShaderLab's real Integer declaration.
        // PARITY.md section 19.1 records the reference and binding exception.
        5 if out.int_properties => out.text("Integer")?,
        3 => {
            out.text("Range(")?;
            out.float(prop.field("m_DefValue_1_")?.num()?, true)?;
            out.text(", ")?;
            out.float(prop.field("m_DefValue_2_")?.num()?, true)?;
            out.text(")")?;
        }
        4 => {
            let dim = prop.field("m_DefTexture")?.field("m_TexDim")?.int()?;
            out.text(match dim {
                1 => "any",
                2 => "2D",
                3 => "3D",
                4 => "Cube",
                5 => "2DArray",
                6 => "CubeArray",
                _ => "",
            })?;
        }
        _ => {}
    }
    out.text(") = ")?;
    match kind {
        0 | 1 => {
            out.text("(")?;
            for (i, key) in [
                "m_DefValue_0_",
                "m_DefValue_1_",
                "m_DefValue_2_",
                "m_DefValue_3_",
            ]
            .iter()
            .enumerate()
            {
                if i > 0 {
                    out.text(",")?;
                }
                out.float(prop.field(key)?.num()?, true)?;
            }
            out.text(")")?;
        }
        2 | 3 => out.float(prop.field("m_DefValue_0_")?.num()?, false)?,
        5 if out.int_properties => out.integer_property(prop.field("m_DefValue_0_")?.num()?)?,
        4 => {
            out.text("\"")?;
            out.text(prop.field("m_DefTexture")?.field("m_DefaultName")?.text()?)?;
            out.text("\" { }")?;
        }
        n => return Err(Error::PropertyType(n)),
    }
    out.text("\n")?;
    Ok(())
}
#[allow(clippy::float_cmp)] // UnityPy compares exact serialized state constants.
pub(super) fn state(v: View<'_>, out: &mut Output<'_>) -> Result<(), Error> {
    let name = v.field("m_Name")?.text()?;
    if !name.is_empty() {
        out.text(" Name \"")?;
        out.text(name)?;
        out.text("\"\n")?;
    }
    let lod = v.field("m_LOD")?.int()?;
    if lod != 0 {
        out.text("  LOD ")?;
        out.int(i128::from(lod))?;
        out.text("\n")?;
    }
    tags(v.field("m_Tags")?, 2, out)?;
    let val = |key| v.field(key)?.field("val")?.num();
    if val("alphaToMask")? > 0. {
        out.text(" AlphaToMask On\n")?;
    }
    let clip = v.optional("zClip")?.ok_or(Error::Attribute("zClip"))?;
    if !clip.is_none && clip.field("val")?.num()? != 1. {
        out.text(" ZClip Off\n")?;
    }
    let z = val("zTest")?;
    if z != 4. {
        out.text(" ZTest ")?;
        out.text(match z {
            0. => "Off",
            1. => "Never",
            2. => "Less",
            3. => "Equal",
            5. => "Greater",
            6. => "NotEqual",
            7. => "GEqual",
            8. => "Always",
            _ => "",
        })?;
        out.text("\n")?;
    }
    if val("zWrite")? != 1. {
        out.text(" ZWrite Off\n")?;
    }
    let c = val("culling")?;
    if c != 2. {
        out.text(" Cull ")?;
        out.text(match c {
            0. => "Off",
            1. => "Front",
            _ => "",
        })?;
        out.text("\n")?;
    }
    let factor = val("offsetFactor")?;
    let units = val("offsetUnits")?;
    if factor != 0. || units != 0. {
        out.text(" Offset ")?;
        out.float(factor, false)?;
        out.text(", ")?;
        out.float(units, false)?;
        out.text("\n")?;
    }
    if v.field("lighting")?.boolean()? {
        out.text("  Lighting On\n")?;
    }
    out.text("  GpuProgramID ")?;
    out.int(i128::from(v.field("gpuProgramID")?.int()?))?;
    out.text("\n")
}
pub(super) fn pass_write(
    v: View<'_>,
    platforms: Seq<'_>,
    programs: &[Program<'_>],
    out: &mut Output<'_>,
    meter: &Arc<Meter>,
) -> Result<(), Error> {
    let kind = v.field("m_Type")?.int()?;
    out.text(match kind {
        0 => " Pass ",
        1 => " UsePass ",
        2 => " GrabPass ",
        _ => "",
    })?;
    if kind == 1 {
        out.text("\"")?;
        out.text(v.field("m_UseName")?.text()?)?;
        return out.text("\"\n");
    }
    out.text("{\n")?;
    if kind == 2 {
        let name = v.field("m_TextureName")?.text()?;
        if !name.is_empty() {
            out.text(" \"")?;
            out.text(name)?;
            out.text("\"\n")?;
        }
    } else {
        state(v.field("m_State")?, out)?;
        for (key, label) in super::layout::STAGES {
            let stage = v.field(key)?;
            let (subs, player) = super::layout::selected(stage)?;
            let mut has_variants = !subs.empty();
            if player {
                has_variants = false;
                for group in subs.list()? {
                    meter.count_decode(1)?;
                    if !group.empty() {
                        has_variants = true;
                        break;
                    }
                }
            }
            if has_variants {
                out.text("Program \"")?;
                out.text(label)?;
                out.text("\" {\n")?;
                subprograms(subs, player, platforms, programs, out, meter)?;
                out.text("}\n")?;
            }
        }
    }
    out.text("}\n")
}
pub(super) fn subprograms<'a>(
    v: View<'a>,
    player: bool,
    platforms: Seq<'_>,
    programs: &[Program<'_>],
    out: &mut Output<'_>,
    meter: &Arc<Meter>,
) -> Result<(), Error> {
    let mut sorted = MeteredVec::new(meter);
    let mut ordinal = 0usize;
    let mut add = |sub: View<'a>| -> Result<(), Error> {
        meter.count_decode(1)?;
        sorted.push((
            sub.field("m_BlobIndex")?.int()?,
            sub.field("m_GpuProgramType")?.int()?,
            ordinal,
            sub,
        ))?;
        ordinal += 1;
        Ok(())
    };
    for sub in v.list()? {
        meter.count_decode(1)?;
        if player {
            for sub in sub.list()? {
                add(sub)?;
            }
        } else {
            add(sub)?;
        }
    }
    // Include ordinal to reproduce both Python stable sorts without a sort allocation.
    super::sort::by_key(sorted.as_mut_slice(), |s| (s.0, s.1, s.2), meter)?;
    let mut at = 0;
    while let Some(first) = sorted.as_slice().get(at) {
        let mut end = at + 1;
        while let Some(next) = sorted.as_slice().get(end) {
            meter.count_decode(1)?;
            if (next.0, next.1) != (first.0, first.1) {
                break;
            }
            end += 1;
        }
        if let Some(i) = first_usable(platforms, programs.len(), first.1, meter)? {
            let platform = platforms.int(i)?;
            for sub in sorted.as_slice().get(at..end).ok_or(Error::Index)? {
                out.text("SubProgram \"")?;
                out.text(platform_name(platform))?;
                out.text(" ")?;
                if !player && end - at > 1 {
                    out.text("hw_tier")?;
                    let tier = sub.3.field("m_ShaderHardwareTier")?.int()?;
                    // Python two-character formatting includes the sign in the two-character width.
                    if (0..10).contains(&tier) {
                        out.text("0")?;
                    }
                    out.int(i128::from(tier))?;
                    out.text(" ")?;
                }
                out.text("\" {\n")?;
                programs.get(i).ok_or(Error::Index)?.write(sub.0, out)?;
                out.text("\n}\n")?;
            }
        }
        at = end;
    }
    Ok(())
}
// Share the renderer's short circuit with modern record collection.
pub(super) fn first_usable(
    platforms: Seq<'_>,
    count: usize,
    kind: i64,
    meter: &Meter,
) -> Result<Option<usize>, Error> {
    for i in 0..platforms.len().min(count) {
        meter.count_decode(1)?;
        if usable(platforms.int(i)?, kind)? {
            return Ok(Some(i));
        }
    }
    Ok(None)
}
// export/ShaderConverter.py:375-493. Metal intentionally accepts VS only.
pub(super) fn usable(platform: i64, kind: i64) -> Result<bool, Error> {
    Ok(match platform {
        0 => kind == 1,
        1 => (9..=12).contains(&kind),
        2 | 10 | 11 | 12 | 16 | 17 | 19 | 20 => (26..=30).contains(&kind),
        4 => (15..=22).contains(&kind),
        5 => kind == 5,
        8 => kind == 13 || kind == 14,
        9 => (2..=4).contains(&kind),
        14 => kind == 23,
        15 => (6..=8).contains(&kind),
        18 => kind == 25,
        _ => return Err(Error::NotImplemented),
    })
}
// export/ShaderConverter.py:496-540.
pub(super) fn platform_name(platform: i64) -> &'static str {
    match platform {
        0 => "openGL",
        1 => "d3d9",
        2 => "xbox360",
        3 => "ps3",
        4 => "d3d11",
        5 => "gles",
        6 => "glesdesktop",
        7 => "flash",
        8 => "d3d11_9x",
        9 => "gles3",
        10 => "psp2",
        11 => "ps4",
        12 => "xboxone",
        13 => "psm",
        14 => "metal",
        15 => "glcore",
        16 => "n3ds",
        17 => "wiiu",
        18 => "vulkan",
        19 => "switch",
        20 => "xboxone+_d3d12",
        _ => "unknown",
    }
}