use super::scenes::{CutOptions, one, scene};
use super::{AtlasScene, BuildError, Object, V, file, format, pair, pptr, s, sheet, st, texture};
use crate::ngui::SceneBundle;
pub const RECT: u32 = 2;
pub const TIGHT: u32 = 0;
#[must_use]
pub fn packed(rotation: u32) -> u32 {
1 | RECT | rotation << 2
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Mesh {
pub vertices: Vec<[f32; 3]>,
pub submeshes: Vec<(u32, u32)>,
pub indices: Vec<u16>,
}
impl Mesh {
#[must_use]
pub fn triangle(side: f32) -> Self {
Self {
vertices: vec![[0.0, 0.0, 0.0], [side, 0.0, 0.0], [0.0, side, 0.0]],
submeshes: vec![(0, 3)],
indices: vec![0, 1, 2],
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct SpriteSpec {
pub name: Vec<u8>,
pub rect: [f32; 4],
pub settings: u32,
pub ppu: f32,
pub texture: (i32, i64),
pub alpha: (i32, i64),
pub mesh: Mesh,
pub atlas: (i32, i64),
pub key: ([u32; 4], i64),
pub tags: Vec<Vec<u8>>,
}
impl SpriteSpec {
#[must_use]
pub fn rect(name: &str, rect: [f32; 4], texture: (i32, i64)) -> Self {
Self {
name: name.as_bytes().to_vec(),
rect,
settings: RECT,
ppu: 1.0,
texture,
alpha: (0, 0),
mesh: Mesh::default(),
atlas: (0, 0),
key: ([1, 2, 3, 4], 7),
tags: Vec::new(),
}
}
#[must_use]
pub fn tight(name: &str, rect: [f32; 4], texture: (i32, i64), mesh: Mesh) -> Self {
Self {
settings: TIGHT,
ppu: 16.0,
mesh,
..Self::rect(name, rect, texture)
}
}
#[must_use]
pub fn settings(mut self, settings: u32) -> Self {
self.settings = settings;
self
}
#[must_use]
pub fn alpha(mut self, alpha: (i32, i64)) -> Self {
self.alpha = alpha;
self
}
#[must_use]
pub fn in_atlas(mut self, atlas: (i32, i64)) -> Self {
self.atlas = atlas;
self
}
#[must_use]
pub fn tagged(mut self, tags: &[&str]) -> Self {
self.tags = tags.iter().map(|t| t.as_bytes().to_vec()).collect();
self
}
#[must_use]
pub fn keyed(mut self, key: ([u32; 4], i64)) -> Self {
self.key = key;
self
}
#[must_use]
pub fn value(&self) -> V {
sprite_value(self)
}
#[must_use]
pub fn object(&self, path_id: i64) -> Object {
Object::new(path_id, 213, sprite_value(self))
}
}
fn floats(ty: &str, names: &[&str], values: &[f32]) -> V {
V::Struct(
ty.to_owned(),
names
.iter()
.zip(values)
.map(|(n, v)| ((*n).to_owned(), V::Float(*v)))
.collect(),
)
}
pub(super) fn floats_of(ty: &str, names: &[&str], values: &[f32]) -> V {
floats(ty, names, values)
}
pub(super) fn rectf(r: [f32; 4]) -> V {
floats("Rectf", &["x", "y", "width", "height"], &r)
}
fn bytes_vector(data: &[u8]) -> V {
super::vector(V::UInt8(0), data.iter().map(|b| V::UInt8(*b)).collect())
}
fn submeshes(mesh: &Mesh) -> V {
let count = u32::try_from(mesh.vertices.len()).unwrap_or(u32::MAX);
let aabb = || {
st(
"AABB",
vec![
("m_Center", floats("Vector3f", &["x", "y", "z"], &[0.0; 3])),
("m_Extent", floats("Vector3f", &["x", "y", "z"], &[0.0; 3])),
],
)
};
let sub = |first: u32, n: u32| {
st(
"SubMesh",
vec![
("firstByte", V::UInt(first)),
("indexCount", V::UInt(n)),
("topology", V::Int(0)),
("firstVertex", V::UInt(0)),
("vertexCount", V::UInt(count)),
("localAABB", aabb()),
],
)
};
super::vector(
sub(0, 0),
mesh.submeshes.iter().map(|(f, n)| sub(*f, *n)).collect(),
)
}
fn vertex_data(mesh: &Mesh) -> V {
let channel = |dimension: u8| {
st(
"ChannelInfo",
vec![
("stream", V::UInt8(0)),
("offset", V::UInt8(0)),
("format", V::UInt8(0)),
("dimension", V::UInt8(dimension)),
],
)
};
let channels = (0..14)
.map(|i| channel(if i == 0 { 3 } else { 0 }))
.collect();
let mut data: Vec<u8> = mesh
.vertices
.iter()
.flatten()
.flat_map(|f| f.to_le_bytes())
.collect();
data.resize(data.len().next_multiple_of(16), 0);
st(
"VertexData",
vec![
(
"m_VertexCount",
V::UInt(u32::try_from(mesh.vertices.len()).unwrap_or(u32::MAX)),
),
("m_Channels", super::vector(channel(0), channels)),
("m_DataSize", V::Bytes(data)),
],
)
}
fn sprite_value(spec: &SpriteSpec) -> V {
let indices: Vec<u8> = spec
.mesh
.indices
.iter()
.flat_map(|i| i.to_le_bytes())
.collect();
let rd = st(
"SpriteRenderData",
vec![
("texture", pptr("Texture2D", spec.texture)),
("m_SubMeshes", submeshes(&spec.mesh)),
("m_IndexBuffer", bytes_vector(&indices)),
("m_VertexData", vertex_data(&spec.mesh)),
("textureRect", rectf(spec.rect)),
(
"textureRectOffset",
floats("Vector2f", &["x", "y"], &[0.0; 2]),
),
("settingsRaw", V::UInt(spec.settings)),
("alphaTexture", pptr("Texture2D", spec.alpha)),
],
);
let (words, id) = spec.key;
st(
"Sprite",
vec![
("m_Name", V::Str(spec.name.clone())),
("m_Rect", rectf(spec.rect)),
("m_Offset", floats("Vector2f", &["x", "y"], &[0.0; 2])),
("m_PixelsToUnits", V::Float(spec.ppu)),
("m_Extrude", V::UInt(1)),
("m_RD", rd),
("m_SpriteAtlas", pptr("SpriteAtlas", spec.atlas)),
("m_RenderDataKey", key(words, id)),
(
"m_AtlasTags",
super::vector(s(""), spec.tags.iter().map(|t| V::Str(t.clone())).collect()),
),
],
)
}
pub(super) fn key(words: [u32; 4], id: i64) -> V {
let names = ["data[0]", "data[1]", "data[2]", "data[3]"];
let guid = st(
"GUID",
names
.iter()
.zip(words)
.map(|(n, w)| (*n, V::UInt(w)))
.collect(),
);
pair(guid, V::Int64(id))
}
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),
)
}
fn rgb(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::RGB24,
size,
sheet(base, w, h, true),
)
}
pub(super) fn sprites(key: &str) -> CutOptions {
CutOptions {
sprites_key: Some(key.to_owned()),
..CutOptions::default()
}
}
pub(super) fn alone(cab: &str, objects: &[Object], key: &str) -> Result<AtlasScene, BuildError> {
let data = file(&[], objects, true)?;
Ok(scene(
vec![one("x.bundle", &[(cab, data)], &[])],
&["x.bundle"],
sprites(key),
))
}
pub fn own_sheets() -> Result<AtlasScene, BuildError> {
let tri = Mesh::triangle(0.25);
alone(
"CAB-s1",
&[
rgba(1, "sheet", 1, 8, 8),
SpriteSpec::rect("a", [1.0, 1.0, 4.0, 3.0], (0, 1)).object(2),
rgb(3, "other", 0x40, 4, 4),
SpriteSpec::rect("b", [0.0, 0.0, 4.0, 4.0], (0, 3)).object(4),
SpriteSpec::rect("c", [2.0, 1.0, 5.0, 3.0], (0, 1))
.settings(packed(4))
.object(5),
SpriteSpec::tight("mask", [0.0, 0.0, 4.0, 4.0], (0, 1), tri.clone()).object(6),
SpriteSpec::tight("mask_rgb", [0.0, 0.0, 4.0, 4.0], (0, 3), tri).object(7),
SpriteSpec::rect("flip", [1.0, 2.0, 3.0, 2.0], (0, 1))
.settings(packed(1))
.object(8),
],
"{type}/{name}.png",
)
}
pub fn dependency_sheets() -> Result<AtlasScene, BuildError> {
let dep = file(
&[],
&[
rgba(10, "dep", 0x20, 4, 4),
rgb(11, "dep_alpha", 0x70, 2, 2),
],
true,
)?;
let cab = file(
&["archive:/CAB-d1/CAB-d1", "archive:/CAB-gone/CAB-gone"],
&[
SpriteSpec::rect("far", [0.0, 0.0, 4.0, 4.0], (1, 10)).object(1),
SpriteSpec::rect("far2", [1.0, 1.0, 2.0, 2.0], (1, 10)).object(2),
SpriteSpec::rect("gone", [0.0, 0.0, 1.0, 1.0], (2, 5)).object(3),
SpriteSpec::rect("null", [0.0, 0.0, 1.0, 1.0], (0, 0)).object(4),
SpriteSpec::rect("missing", [0.0, 0.0, 1.0, 1.0], (0, 99)).object(5),
SpriteSpec::rect("past", [0.0, 0.0, 1.0, 1.0], (3, 1)).object(6),
rgba(7, "own", 0x30, 2, 2),
SpriteSpec::rect("own", [0.0, 0.0, 2.0, 2.0], (0, 7))
.alpha((1, 11))
.object(8),
],
true,
)?;
Ok(scene(
vec![
one("d.bundle", &[("CAB-d1", dep)], &[]),
one("x.bundle", &[("CAB-s2", cab)], &["d.bundle"]),
],
&["x.bundle"],
sprites("{safe_input}/{name}_{path_id}.png"),
))
}
pub fn alpha_textures() -> Result<AtlasScene, BuildError> {
alone(
"CAB-s3",
&[
rgb(1, "rgb", 0x10, 8, 8),
rgba(2, "alpha", 0x90, 8, 8),
rgba(3, "small_alpha", 0x50, 8, 4),
SpriteSpec::rect("merged", [1.0, 1.0, 4.0, 4.0], (0, 1))
.alpha((0, 2))
.object(4),
SpriteSpec::rect("plain", [1.0, 1.0, 4.0, 4.0], (0, 1)).object(5),
SpriteSpec::rect("merged2", [0.0, 0.0, 8.0, 8.0], (0, 1))
.alpha((0, 2))
.object(6),
SpriteSpec::rect("mismatch", [0.0, 0.0, 2.0, 2.0], (0, 1))
.alpha((0, 3))
.object(7),
SpriteSpec::tight(
"alpha_mask",
[2.0, 2.0, 4.0, 4.0],
(0, 1),
Mesh::triangle(0.25),
)
.alpha((0, 2))
.object(8),
],
"{name}.png",
)
}
pub fn names_and_keys() -> Result<AtlasScene, BuildError> {
let one_px = |name: &str| SpriteSpec::rect(name, [0.0, 0.0, 2.0, 2.0], (0, 1));
let long = "n".repeat(260);
alone(
"CAB-s4",
&[
rgba(1, "t", 0x60, 4, 4),
one_px("dup").object(2),
one_px("dup").object(3),
one_px("a b").object(4),
one_px("a_b").object(5),
one_px(&long).object(6),
one_px("trailing").object(7).with_trailing(4),
one_px("cut").object(8).cut_to(24),
one_px("last").object(9),
],
"{safe_name}.png",
)
}
pub fn render_failures() -> Result<AtlasScene, BuildError> {
let on = |name: &str, rect: [f32; 4]| SpriteSpec::rect(name, rect, (0, 1));
let cap = 536_870_912.0;
let past = Mesh {
indices: vec![0, 1, 3],
..Mesh::triangle(0.25)
};
let no_vertices = Mesh {
submeshes: vec![(0, 0)],
..Mesh::default()
};
let mut objects = vec![rgba(1, "sheet", 0x11, 8, 8)];
let sprites = [
on("bomb", [0.0, 0.0, 20000.0, 20000.0]),
on("c_int", [3.0e9, 0.0, 1.0, 1.0]),
on("c_int_negative", [-3.0e9, 0.0, 1.0, 1.0]),
on("c_long", [1.0e19, 0.0, 0.0, 1.0]),
on("width_cap", [0.0, 0.0, cap, 0.0]),
on("below_cap", [0.0, 0.0, 536_870_880.0, 0.0]),
on("zero_width", [1.0, 1.0, 0.0, 3.0]),
on("nan", [f32::NAN, 0.0, 1.0, 1.0]),
on("infinity", [0.0, 0.0, f32::INFINITY, 1.0]),
on("negative_width", [2.0, 1.0, -1.0, 3.0]),
on("rotation_5", [1.0, 0.0, 5.0, 3.0]).settings(packed(5)),
SpriteSpec::tight("no_vertices", [0.0, 0.0, 4.0, 4.0], (0, 1), no_vertices),
SpriteSpec::tight("index_past", [0.0, 0.0, 4.0, 4.0], (0, 1), past),
on("fine", [0.0, 0.0, 2.0, 2.0]),
];
for (at, spec) in (2..).zip(sprites) {
objects.push(spec.object(at));
}
let size = (4, 4);
objects.extend([
texture(30, b"alpha8", format::ALPHA8, size, vec![0; 16]),
texture(31, b"empty", format::RGBA32, (0, 0), vec![0; 4]),
texture(34, b"empty_wide", format::RGBA32, (4, 0), vec![0; 4]),
texture(35, b"empty_alpha", format::RGBA32, (0, 4), vec![0; 4]),
texture(36, b"empty_rgb", format::RGB24, (8, 0), vec![0; 4]),
texture(32, b"no_data", format::RGBA32, size, Vec::new()),
texture(33, b"short", format::RGBA32, size, vec![0; 10]),
SpriteSpec::rect("bad_format", [0.0, 0.0, 1.0, 1.0], (0, 30)).object(40),
SpriteSpec::rect("empty_sheet", [0.0, 0.0, 1.0, 1.0], (0, 31)).object(41),
SpriteSpec::rect("no_data_1", [0.0, 0.0, 1.0, 1.0], (0, 32)).object(42),
SpriteSpec::rect("short_sheet", [0.0, 0.0, 1.0, 1.0], (0, 33)).object(43),
SpriteSpec::rect("no_data_2", [1.0, 1.0, 1.0, 1.0], (0, 32)).object(44),
SpriteSpec::rect("alpha_zero_beside", [0.0, 0.0, 1.0, 1.0], (0, 1))
.alpha((0, 35))
.object(45),
SpriteSpec::rect("alpha_zero_both", [0.0, 0.0, 1.0, 1.0], (0, 34))
.alpha((0, 35))
.object(46),
SpriteSpec::rect("alpha_zero_both_2", [0.0, 0.0, 0.0, 0.0], (0, 34))
.alpha((0, 35))
.object(47),
SpriteSpec::rect("alpha_zero_main", [0.0, 0.0, 1.0, 1.0], (0, 36))
.alpha((0, 1))
.object(48),
SpriteSpec::rect("texture_first", [0.0, 0.0, 1.0, 1.0], (0, 30))
.alpha((0, 99))
.object(49),
]);
alone("CAB-s5", &objects, "{name}.png")
}
fn winner_input(id: &str, base: u8) -> Result<SceneBundle, BuildError> {
let stem = id.trim_end_matches(".bundle");
let cab_name = format!("CAB-w{stem}");
let cab = file(
&[],
&[
rgba(1, "sheet", base, 2, 2),
SpriteSpec::rect("shared", [0.0, 0.0, 2.0, 2.0], (0, 1)).object(2),
SpriteSpec::rect(&format!("own_{stem}"), [0.0, 0.0, 1.0, 1.0], (0, 1)).object(3),
],
true,
)?;
Ok(one(id, &[(cab_name.as_str(), cab)], &[]))
}
pub fn sprite_winners() -> Result<AtlasScene, BuildError> {
let bundles = vec![
winner_input("a.bundle", 1)?,
winner_input("b.bundle", 1)?,
winner_input("c.bundle", 0x31)?,
];
let inputs = bundles.iter().map(|b| b.id.clone()).collect();
Ok(AtlasScene {
bundles,
inputs,
options: sprites("{name}.png"),
})
}
pub fn sprite_scenes() -> Result<Vec<(&'static str, AtlasScene)>, BuildError> {
let all: [(&'static str, super::scenes::Make); 18] = [
("own_sheets", own_sheets),
("dependency_sheets", dependency_sheets),
("alpha_textures", alpha_textures),
("names_and_keys", names_and_keys),
("render_failures", render_failures),
("sprite_winners", sprite_winners),
("atlas_lookup", super::sprite_atlases::atlas_lookup),
("atlas_dependency", super::sprite_atlases::atlas_dependency),
("nested_cabs", super::sprite_atlases::nested_cabs),
("sprite_fields", super::sprite_atlases::sprite_fields),
("sprite_classes", super::sprite_objects::sprite_classes),
("atlas_classes", super::sprite_objects::atlas_classes),
("shadowed_rows", super::sprite_objects::shadowed_rows),
("sprite_pointers", super::sprite_objects::sprite_pointers),
(
"sprite_attributes",
super::sprite_objects::sprite_attributes,
),
("int_tags", super::sprite_objects::int_tags),
("raised_tags", super::sprite_atlases::raised_tags),
(
"raised_tags_swapped",
super::sprite_atlases::raised_tags_swapped,
),
];
all.into_iter().map(|(name, f)| Ok((name, f()?))).collect()
}