use super::super::{
BuildError, Object, PropName, V, atlas, file, material, material_tpk, mono_behaviour,
mono_script, pair, pptr, s, st, vector,
};
use super::{Built, CutOptions, NULL, SCRIPT, by_name, one, plain, rgba, scene};
fn element(fields: Vec<(&str, V)>) -> V {
st("UISpriteData", fields)
}
fn shaped(path_id: i64, name: &[u8], sprites: V) -> Object {
mono_behaviour(path_id, name, SCRIPT, vec![("mSprites", sprites)])
}
fn edited(o: Object, edit: impl FnOnce(&mut Vec<(String, V)>)) -> Object {
match o.value {
V::Struct(ty, mut fields) => {
edit(&mut fields);
Object::new(o.path_id, o.class_id, V::Struct(ty, fields))
}
other => Object::new(o.path_id, o.class_id, other),
}
}
fn one_of(item: V) -> V {
vector(item.clone(), vec![item])
}
fn odd_shapes() -> Vec<Object> {
let rect = |name: V, x: V, height: V| {
element(vec![
("name", name),
("x", x),
("y", V::Int(0)),
("width", V::Int(1)),
("height", height),
])
};
vec![
shaped(17, b"str_empty", s("")),
shaped(18, b"str_full", s("abc")),
shaped(
19,
b"no_width",
one_of(element(vec![
("name", s("w")),
("x", V::Int(0)),
("y", V::Int(0)),
("height", V::Int(1)),
])),
),
shaped(
20,
b"float_rect",
one_of(rect(s("f"), V::Float(0.0), V::Int(1))),
),
shaped(
21,
b"bool_rect",
one_of(rect(s("b"), V::Int(0), V::Bool(true))),
),
shaped(
22,
b"map_sprites",
V::Vector(
"map".to_owned(),
Box::new(pair(s(""), V::Int(0))),
vec![pair(s("a"), V::Int(1))],
),
),
shaped(
23,
b"int_name",
one_of(rect(V::Int(0), V::Int(0), V::Int(1))),
),
shaped(
24,
b"mixed",
one_of(element(vec![
("name", s("m")),
("x", V::UInt8(0)),
("y", V::UInt(0)),
("width", V::UInt64(1)),
("height", V::Int(1)),
])),
),
]
}
pub fn layouts() -> Built {
let legacy = st(
"Sprite",
vec![
("name", s("old")),
(
"outer",
st(
"Rect",
vec![
("x", V::Float(0.0)),
("y", V::Float(0.0)),
("width", V::Float(1.0)),
("height", V::Float(1.0)),
],
),
),
("rotated", V::Bool(false)),
],
);
let empty = || vector(V::Int(0), Vec::new());
let mut objects = vec![
mono_script(1, "NGUIAtlas"),
mono_script(2, "UIPanel"),
rgba(3, "t", 1, 1, 1),
mono_behaviour(10, b"panel", (0, 2), vec![("depth", V::Int(3))]),
mono_behaviour(11, b"t", NULL, vec![("mSprites", empty())]),
mono_behaviour(12, b"t", (0, 77), vec![("mSprites", empty())]),
mono_behaviour(13, b"old", SCRIPT, vec![("sprites", one_of(legacy))]),
shaped(14, b"flat", V::Int(0)),
by_name(15, "", vec![plain("e", (0, 0, 1, 1))]),
mono_behaviour(16, b"t", (0, 3), vec![("mSprites", empty())]),
];
objects.extend(odd_shapes());
objects.push(rgba(25, "mixed", 0x0C, 1, 1));
let first = file(&[], &objects, true)?;
let bare = file(
&["archive:/CAB-x11a/CAB-x11a"],
&[atlas(
1,
b"bare",
(1, 1),
NULL,
vec![plain("e", (0, 0, 1, 1))],
Vec::new(),
)],
false,
)?;
let stripped = file(
&[],
&[
mono_script(1, "NGUIAtlas"),
by_name(2, "stripped", vec![plain("e", (0, 0, 1, 1))]),
],
false,
)?;
let again = file(
&[],
&[
mono_script(1, "NGUIAtlas"),
by_name(2, "bare", vec![plain("e", (0, 0, 1, 1))]),
],
true,
)?;
Ok(scene(
vec![one(
"x.bundle",
&[
("CAB-x11a", first),
("CAB-x11b", bare),
("CAB-x11c", stripped),
("CAB-x11d", again),
],
&[],
)],
&["x.bundle"],
CutOptions::default(),
))
}
pub fn failed_reads() -> Built {
let ui_name = 8;
let head = 12 + 4 + 12 + 12;
let one_sprite = || vec![plain("s", (0, 0, 1, 1))];
let stub = Object::new(13, 28, st("Texture2D", vec![("m_Name", s("stub"))]));
let cb = rgba(14, "cb", 0x1C, 1, 1).cut_to(ui_name + 4);
let nolm = edited(rgba(15, "nolm", 0x1D, 1, 1), |f| {
f.retain(|(n, _)| n != "m_LightmapFormat");
});
let extra = edited(rgba(16, "extra", 0x1E, 1, 1), |f| {
f.push(("m_Extra".to_owned(), V::Int(7)));
});
let cab = file(
&[],
&[
mono_script(1, "NGUIAtlas"),
rgba(2, "ui", 0x13, 2, 2).cut_to(ui_name),
rgba(3, "ui", 0x14, 2, 2),
by_name(4, "ui", one_sprite()),
by_name(5, "short", one_sprite()).cut_to(head),
rgba(6, "short", 0x15, 1, 1),
material(7, "viamat", PropName::Plain, &[("_MainTex", (0, 2))]),
atlas(8, b"via", SCRIPT, (0, 7), one_sprite(), Vec::new()),
rgba(9, "via", 0x16, 1, 1),
by_name(10, "tail", one_sprite()).with_trailing(4),
rgba(11, "ttail", 0x17, 1, 1).with_trailing(4),
by_name(12, "ttail", one_sprite()),
stub,
cb,
nolm,
extra,
material(17, "trailmat", PropName::Plain, &[("_MainTex", (0, 19))]).with_trailing(4),
atlas(18, b"mt", SCRIPT, (0, 17), one_sprite(), Vec::new()),
rgba(19, "mtx", 0x19, 1, 1),
rgba(20, "mt", 0x1A, 1, 1),
mono_script(21, "NGUIAtlas").with_trailing(4),
atlas(22, b"ms", (0, 21), NULL, one_sprite(), Vec::new()),
edited(mono_script(23, "NGUIAtlas"), |f| {
for (n, v) in f.iter_mut() {
if n == "m_ClassName" {
*v = V::Int(5);
}
}
}),
atlas(24, b"mi", (0, 23), NULL, one_sprite(), Vec::new()),
edited(material(25, "tsmat", PropName::Plain, &[]), |f| {
let env = st("UnityTexEnv", vec![("m_Texture", pptr("Texture", (0, 27)))]);
let envs = st("TexEnvs", vec![("_MainTex", env)]);
f.pop();
f.push((
"m_SavedProperties".to_owned(),
st("UnityPropertySheet", vec![("m_TexEnvs", envs)]),
));
}),
atlas(26, b"ts", SCRIPT, (0, 25), one_sprite(), Vec::new()),
rgba(27, "tsx", 0x2E, 1, 1),
rgba(28, "ts", 0x2F, 1, 1),
nameless(rgba(29, "", 0x30, 1, 1)),
],
true,
)?;
let (typeless, typed) = typetree_reach()?;
let zero_texture = file(&[], &[rgba(1, "zero", 0x18, 1, 1).cut_to(0)], true)?;
let zero_head = file(&[], &[by_name(1, "zmb", one_sprite()).cut_to(0)], true)?;
Ok(scene(
vec![one(
"x.bundle",
&[
("CAB-x15", cab),
("CAB-x15b", zero_texture),
("CAB-x15c", zero_head),
("CAB-x15d", typeless),
("CAB-x15e", typed),
],
&[],
)],
&["x.bundle"],
CutOptions::textures("{name}.png"),
))
}
fn nameless(o: Object) -> Object {
match o.value {
V::Struct(_, mut fields) => {
fields.retain(|(n, _)| n != "m_Name");
Object::new(
o.path_id,
o.class_id,
V::Struct("NamelessTexture2D".to_owned(), fields),
)
}
other => Object::new(o.path_id, o.class_id, other),
}
}
fn typetree_reach() -> Result<(Vec<u8>, Vec<u8>), BuildError> {
let one_sprite = || vec![plain("s", (0, 0, 1, 1))];
let typeless = file(
&["archive:/CAB-x15e/CAB-x15e"],
&[
rgba(1, "tl", 0x2A, 1, 1),
material_tpk(2, "tlmat", &[("_MainTex", (1, 3))]),
],
false,
)?;
let typed = file(
&["archive:/CAB-x15d/CAB-x15d"],
&[
mono_script(1, "NGUIAtlas"),
rgba(2, "tl", 0x2B, 1, 1),
rgba(3, "tmx", 0x2C, 1, 1),
rgba(4, "tm", 0x2D, 1, 1),
by_name(5, "tl", one_sprite()),
atlas(6, b"tm", SCRIPT, (1, 2), one_sprite(), Vec::new()),
material(7, "tqmat", PropName::Plain, &[("_MainTex", (1, 1))]),
atlas(8, b"tq", SCRIPT, (0, 7), one_sprite(), Vec::new()),
rgba(9, "tq", 0x31, 1, 1),
],
true,
)?;
Ok((typeless, typed))
}