use super::{Object, V, pair, pptr, s, st, vector};
fn head(script: (i32, i64), name: &[u8]) -> Vec<(String, V)> {
vec![
("m_GameObject".to_owned(), pptr("GameObject", (0, 0))),
("m_Enabled".to_owned(), V::Bool(true)),
("m_Script".to_owned(), pptr("MonoScript", script)),
("m_Name".to_owned(), V::Str(name.to_vec())),
]
}
#[must_use]
pub fn mono_behaviour(
path_id: i64,
name: &[u8],
script: (i32, i64),
fields: Vec<(&str, V)>,
) -> Object {
let mut all = head(script, name);
all.extend(fields.into_iter().map(|(n, v)| (n.to_owned(), v)));
Object::new(path_id, 114, V::Struct("MonoBehaviour".to_owned(), all))
}
#[must_use]
pub fn mono_script(path_id: i64, class_name: &str) -> Object {
let hash = V::Struct(
"Hash128".to_owned(),
(0..16u8)
.map(|i| (format!("bytes[{i}]"), V::UInt8(i)))
.collect(),
);
Object::new(
path_id,
115,
st(
"MonoScript",
vec![
("m_Name", s(class_name)),
("m_ExecutionOrder", V::Int(0)),
("m_PropertiesHash", hash),
("m_ClassName", s(class_name)),
("m_Namespace", s("")),
("m_AssemblyName", s("Assembly-CSharp.dll")),
],
),
)
}
#[must_use]
pub fn sprite(name: &[u8], rect: (i32, i32, i32, i32), trim: [i32; 8]) -> V {
let (x, y, width, height) = rect;
let names = [
"borderLeft",
"borderRight",
"borderTop",
"borderBottom",
"paddingLeft",
"paddingRight",
"paddingTop",
"paddingBottom",
];
let mut fields = vec![
("name".to_owned(), V::Str(name.to_vec())),
("x".to_owned(), V::Int(x)),
("y".to_owned(), V::Int(y)),
("width".to_owned(), V::Int(width)),
("height".to_owned(), V::Int(height)),
];
fields.extend(
names
.iter()
.zip(trim)
.map(|(n, v)| ((*n).to_owned(), V::Int(v))),
);
V::Struct("UISpriteData".to_owned(), fields)
}
#[must_use]
pub fn atlas(
path_id: i64,
name: &[u8],
script: (i32, i64),
material: (i32, i64),
sprites: Vec<V>,
extra: Vec<(&str, V)>,
) -> Object {
let shape = sprites
.first()
.cloned()
.unwrap_or_else(|| sprite(b"", (0, 0, 0, 0), [0; 8]));
let mut fields = vec![
("material", pptr("Material", material)),
("mSprites", vector(shape, sprites)),
("mPixelSize", V::Float(1.0)),
];
fields.extend(extra);
mono_behaviour(path_id, name, script, fields)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PropName {
Plain,
Fast,
}
fn prop(kind: PropName, name: &str) -> V {
match kind {
PropName::Plain => s(name),
PropName::Fast => st("FastPropertyName", vec![("name", s(name))]),
}
}
#[must_use]
pub fn material(path_id: i64, name: &str, kind: PropName, envs: &[(&str, (i32, i64))]) -> Object {
let vec2 = || st("Vector2f", vec![("x", V::Float(1.0)), ("y", V::Float(1.0))]);
let env = |texture: (i32, i64)| {
st(
"UnityTexEnv",
vec![
("m_Texture", pptr("Texture", texture)),
("m_Scale", vec2()),
("m_Offset", vec2()),
],
)
};
let map = |shape: V, items: Vec<V>| V::Vector("map".to_owned(), Box::new(shape), items);
let tex_envs = map(
pair(prop(kind, ""), env((0, 0))),
envs.iter()
.map(|(n, at)| pair(prop(kind, n), env(*at)))
.collect(),
);
let floats = map(
pair(prop(kind, ""), V::Float(0.0)),
vec![pair(prop(kind, "_Cutoff"), V::Float(0.5))],
);
let color = st(
"ColorRGBA",
vec![
("r", V::Float(1.0)),
("g", V::Float(1.0)),
("b", V::Float(1.0)),
("a", V::Float(1.0)),
],
);
let colors = map(
pair(prop(kind, ""), color.clone()),
vec![pair(prop(kind, "_Color"), color)],
);
let sheet = st(
"UnityPropertySheet",
vec![
("m_TexEnvs", tex_envs),
("m_Floats", floats),
("m_Colors", colors),
],
);
Object::new(
path_id,
21,
st(
"Material",
vec![
("m_Name", s(name)),
("m_Shader", pptr("Shader", (0, 0))),
("m_SavedProperties", sheet),
],
),
)
}
#[must_use]
pub fn material_tpk(path_id: i64, name: &str, envs: &[(&str, (i32, i64))]) -> Object {
let mut value = material(path_id, name, PropName::Plain, envs).value;
let strings = || vector(s(""), Vec::new());
let map = |shape: V| V::Vector("map".to_owned(), Box::new(shape), Vec::new());
if let V::Struct(_, fields) = &mut value {
let sheet = fields.pop();
fields.extend(
[
("m_ValidKeywords", strings()),
("m_InvalidKeywords", strings()),
("m_LightmapFlags", V::UInt(4)),
("m_EnableInstancingVariants", V::UInt8(0)),
("m_DoubleSidedGI", V::Bool(false)),
("m_CustomRenderQueue", V::Int(-1)),
("stringTagMap", map(pair(s(""), s("")))),
("disabledShaderPasses", strings()),
]
.map(|(n, v)| (n.to_owned(), v)),
);
if let Some((n, V::Struct(ty, mut sheet))) = sheet {
sheet.insert(1, ("m_Ints".to_owned(), map(pair(s(""), V::Int(0)))));
fields.push((n, V::Struct(ty, sheet)));
}
let stack = st(
"BuildTextureStackReference",
vec![("groupName", s("")), ("itemName", s(""))],
);
fields.push(("m_BuildTextureStacks".to_owned(), vector(stack, Vec::new())));
}
Object::new(path_id, 21, value)
}
pub mod format {
pub const ALPHA8: i32 = 1;
pub const RGB24: i32 = 3;
pub const RGBA32: i32 = 4;
pub const DXT1: i32 = 10;
pub const BC4: i32 = 26;
}
fn texture_fields(name: &[u8], fmt: i32, size: (i32, i32), complete: u32) -> Vec<(String, V)> {
let settings = st(
"GLTextureSettings",
vec![
("m_FilterMode", V::Int(1)),
("m_Aniso", V::Int(1)),
("m_MipBias", V::Float(0.0)),
("m_WrapU", V::Int(1)),
("m_WrapV", V::Int(1)),
("m_WrapW", V::Int(1)),
],
);
vec![
("m_Name".to_owned(), V::Str(name.to_vec())),
("m_ForcedFallbackFormat".to_owned(), V::Int(4)),
("m_DownscaleFallback".to_owned(), V::Bool(false)),
("m_Width".to_owned(), V::Int(size.0)),
("m_Height".to_owned(), V::Int(size.1)),
("m_CompleteImageSize".to_owned(), V::UInt(complete)),
("m_TextureFormat".to_owned(), V::Int(fmt)),
("m_MipCount".to_owned(), V::Int(1)),
("m_IsReadable".to_owned(), V::Bool(false)),
("m_ImageCount".to_owned(), V::Int(1)),
("m_TextureDimension".to_owned(), V::Int(2)),
("m_TextureSettings".to_owned(), settings),
("m_LightmapFormat".to_owned(), V::Int(0)),
("m_ColorSpace".to_owned(), V::Int(1)),
]
}
#[must_use]
pub fn texture(path_id: i64, name: &[u8], fmt: i32, size: (i32, i32), data: Vec<u8>) -> Object {
let complete = u32::try_from(data.len()).unwrap_or(u32::MAX);
let mut fields = texture_fields(name, fmt, size, complete);
fields.push(("image_data".to_owned(), V::Bytes(data)));
Object::new(path_id, 28, V::Struct("Texture2D".to_owned(), fields))
}
#[must_use]
pub fn texture_streamed(
path_id: i64,
name: &[u8],
fmt: i32,
size: (i32, i32),
path: &str,
offset: u64,
len: u32,
) -> Object {
let mut fields = texture_fields(name, fmt, size, len);
fields.push(("image_data".to_owned(), V::Bytes(Vec::new())));
let stream = st(
"StreamingInfo",
vec![
("offset", V::UInt64(offset)),
("size", V::UInt(len)),
("path", s(path)),
],
);
fields.push(("m_StreamData".to_owned(), stream));
Object::new(path_id, 28, V::Struct("Texture2D".to_owned(), fields))
}
#[must_use]
pub fn set_field(mut object: Object, name: &str, value: V, rename: Option<&str>) -> Object {
if let V::Struct(_, fields) = &mut object.value {
let to = rename.unwrap_or(name).to_owned();
match fields.iter_mut().find(|(n, _)| n == name) {
Some(f) => *f = (to, value),
None => fields.push((to, value)),
}
}
object
}