use crate::blob::{NodeSpec, Strings, blob_tree, no_nul_blob, old_tree};
use crate::writer::ByteWriter;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TypeSpec {
pub class_id: i32,
pub stripped: bool,
pub script_type_index: i16,
pub script_id: [u8; 16],
pub old_type_hash: [u8; 16],
pub nodes: Vec<NodeSpec>,
pub ref_names: Option<(Vec<u8>, Vec<u8>, Vec<u8>)>,
pub dependencies: Vec<i32>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ObjectSpec {
pub path_id: i64,
pub type_id: i32,
pub class_id: u16,
pub data: Vec<u8>,
pub byte_size: Option<u32>,
pub script_type_index: Option<i16>,
pub is_destroyed: Option<u16>,
pub is_stripped: Option<i8>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ExternalSpec {
pub temp_empty: Vec<u8>,
pub guid: [u8; 16],
pub kind: i32,
pub path: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SerializedSpec {
pub format: u32,
pub big_endian: bool,
pub unity_version: Vec<u8>,
pub platform: i32,
pub type_trees: bool,
pub types: Vec<TypeSpec>,
pub objects: Vec<ObjectSpec>,
pub scripts: Vec<(i32, i64)>,
pub externals: Vec<ExternalSpec>,
pub ref_types: Vec<TypeSpec>,
pub user_information: Vec<u8>,
pub big_id_enabled: i32,
pub metadata_size: Option<u32>,
pub file_size: Option<i64>,
pub data_offset: Option<i64>,
}
impl Default for SerializedSpec {
fn default() -> Self {
Self {
format: 22,
big_endian: false,
unity_version: b"2022.3.0f1".to_vec(),
platform: 13,
type_trees: true,
types: Vec::new(),
objects: Vec::new(),
scripts: Vec::new(),
externals: Vec::new(),
ref_types: Vec::new(),
user_information: Vec::new(),
big_id_enabled: 0,
metadata_size: None,
file_size: None,
data_offset: None,
}
}
}
fn header_len(format: u32) -> usize {
match format {
0..=8 => 16,
9..=21 => 20,
_ => 48,
}
}
#[must_use]
pub fn build(spec: &SerializedSpec) -> Vec<u8> {
let v = spec.format;
let header = header_len(v);
let mut data = ByteWriter::le();
let mut starts = Vec::with_capacity(spec.objects.len());
for o in &spec.objects {
data.align(8);
starts.push(data.len());
data.bytes(&o.data);
}
let data = data.into_inner();
let meta_start = if v >= 9 { header } else { header + data.len() };
let mut w = if spec.big_endian {
ByteWriter::be()
} else {
ByteWriter::le()
};
w.bytes(&vec![0; meta_start]);
if v < 9 {
w.bool(spec.big_endian);
}
metadata(spec, &starts, &mut w);
let mut file = w.into_inner();
let meta_end = file.len();
let metadata_size = meta_end - meta_start;
let data_offset = if v >= 9 {
let at = meta_end.div_ceil(16) * 16;
file.resize(at, 0);
file.extend_from_slice(&data);
at
} else {
if let Some(region) = file.get_mut(header..header + data.len()) {
region.copy_from_slice(&data);
}
header
};
let file_size = spec
.file_size
.unwrap_or_else(|| i64::try_from(file.len()).unwrap_or(i64::MAX));
let metadata_size = spec
.metadata_size
.unwrap_or_else(|| u32::try_from(metadata_size).unwrap_or(u32::MAX));
let data_offset = spec
.data_offset
.unwrap_or_else(|| i64::try_from(data_offset).unwrap_or(i64::MAX));
let mut h = ByteWriter::be();
if v >= 22 {
h.u32(0).u32(0).u32(v).u32(0);
} else {
h.u32(metadata_size)
.u32(truncate_u32(file_size))
.u32(v)
.u32(truncate_u32(data_offset));
}
if v >= 9 {
h.bool(spec.big_endian).bytes(&[0; 3]);
}
if v >= 22 {
h.u32(metadata_size).i64(file_size).i64(data_offset).i64(0);
}
let h = h.into_inner();
if let Some(region) = file.get_mut(..h.len()) {
region.copy_from_slice(&h);
}
file
}
fn truncate_u32(v: i64) -> u32 {
let b = v.to_le_bytes();
u32::from_le_bytes([b[0], b[1], b[2], b[3]])
}
fn metadata(spec: &SerializedSpec, starts: &[usize], w: &mut ByteWriter) {
let v = spec.format;
if v >= 7 {
w.cstr(&spec.unity_version);
}
if v >= 8 {
w.i32(spec.platform);
}
let trees = v < 13 || spec.type_trees;
if v >= 13 {
w.bool(spec.type_trees);
}
w.i32(len_i32(spec.types.len()));
for t in &spec.types {
type_record(t, v, spec.big_endian, trees, false, w);
}
let big_id = (7..14).contains(&v) && spec.big_id_enabled != 0;
if (7..14).contains(&v) {
w.i32(spec.big_id_enabled);
}
w.i32(len_i32(spec.objects.len()));
for (o, start) in spec.objects.iter().zip(starts) {
if big_id {
w.i64(o.path_id);
} else if v < 14 {
w.i32(truncate_i32(o.path_id));
} else {
w.align(4);
w.i64(o.path_id);
}
let start = u32::try_from(*start).unwrap_or(u32::MAX);
if v >= 22 {
w.i64(i64::from(start));
} else {
w.u32(start);
}
let size = u32::try_from(o.data.len()).unwrap_or(u32::MAX);
w.u32(o.byte_size.unwrap_or(size));
w.i32(o.type_id);
if v < 16 {
w.u16(o.class_id);
}
if v < 11 {
w.u16(o.is_destroyed.unwrap_or(0));
}
if (11..17).contains(&v) {
w.i16(o.script_type_index.unwrap_or(-1));
}
if v == 15 || v == 16 {
w.i8(o.is_stripped.unwrap_or(0));
}
}
if v >= 11 {
w.i32(len_i32(spec.scripts.len()));
for (file_index, identifier) in &spec.scripts {
w.i32(*file_index);
if v < 14 {
w.i32(truncate_i32(*identifier));
} else {
w.align(4);
w.i64(*identifier);
}
}
}
w.i32(len_i32(spec.externals.len()));
for e in &spec.externals {
if v >= 6 {
w.cstr(&e.temp_empty);
}
if v >= 5 {
w.bytes(&e.guid);
w.i32(e.kind);
}
w.cstr(&e.path);
}
if v >= 20 {
w.i32(len_i32(spec.ref_types.len()));
for t in &spec.ref_types {
type_record(t, v, spec.big_endian, trees, true, w);
}
}
if v >= 5 {
w.cstr(&spec.user_information);
}
}
fn type_record(
t: &TypeSpec,
v: u32,
big_endian: bool,
trees: bool,
is_ref_type: bool,
w: &mut ByteWriter,
) {
w.i32(t.class_id);
if v >= 16 {
w.bool(t.stripped);
}
let script_type_index = if v >= 17 { t.script_type_index } else { -1 };
if v >= 17 {
w.i16(t.script_type_index);
}
if v >= 13 {
if (is_ref_type && script_type_index >= 0)
|| (v < 16 && t.class_id < 0)
|| (v >= 16 && t.class_id == 114)
{
w.bytes(&t.script_id);
}
w.bytes(&t.old_type_hash);
}
if trees {
if v >= 12 || v == 10 {
w.bytes(&blob_tree(&t.nodes, v, big_endian, Strings::Local, &[]));
} else {
w.bytes(&old_tree(&t.nodes, v, big_endian));
}
if v >= 21 {
if is_ref_type {
let empty = (Vec::new(), Vec::new(), Vec::new());
let (class, ns, asm) = t.ref_names.as_ref().unwrap_or(&empty);
w.cstr(class).cstr(ns).cstr(asm);
} else {
w.i32(len_i32(t.dependencies.len()));
for d in &t.dependencies {
w.i32(*d);
}
}
}
}
}
#[must_use]
pub fn no_nul_strings(rows: usize, complete: bool) -> Vec<u8> {
const HEADER: usize = 48;
let mut w = ByteWriter::le();
w.bytes(&[0; HEADER]);
w.cstr(b"2018.4.0f1").i32(13).bool(true).i32(1);
w.i32(1).bool(false).i16(-1).bytes(&[0; 16]);
w.bytes(&no_nul_blob(rows, 65_536));
if complete {
w.i32(0).i32(0).i32(0).i32(0).i32(0).cstr(b"");
}
let mut file = w.into_inner();
let metadata = u32::try_from(file.len() - HEADER).unwrap_or(u32::MAX);
let end = i64::try_from(file.len()).unwrap_or(i64::MAX);
let mut h = ByteWriter::be();
h.u32(0).u32(0).u32(22).u32(0).bool(false).bytes(&[0; 3]);
h.u32(metadata).i64(end).i64(end).i64(0);
file[..HEADER].copy_from_slice(h.as_slice());
file
}
fn len_i32(n: usize) -> i32 {
i32::try_from(n).unwrap_or(i32::MAX)
}
fn truncate_i32(v: i64) -> i32 {
let b = v.to_le_bytes();
i32::from_le_bytes([b[0], b[1], b[2], b[3]])
}
#[must_use]
pub fn one_object(class_id: i32, nodes: Vec<NodeSpec>, data: Vec<u8>) -> SerializedSpec {
SerializedSpec {
types: vec![TypeSpec {
class_id,
nodes,
..TypeSpec::default()
}],
objects: vec![ObjectSpec {
path_id: 1,
type_id: 0,
class_id: u16::try_from(class_id).unwrap_or(0),
data,
..ObjectSpec::default()
}],
..SerializedSpec::default()
}
}
#[cfg(test)]
mod tests {
use super::{ExternalSpec, ObjectSpec, SerializedSpec, TypeSpec, build, one_object};
use crate::blob::NodeSpec;
#[test]
fn overrides_default_to_what_unity_writes() {
let o = ObjectSpec::default();
assert_eq!(
(o.script_type_index, o.is_destroyed, o.is_stripped),
(None, None, None)
);
let s = SerializedSpec::default();
assert_eq!(s.big_id_enabled, 0);
assert_eq!(
(s.metadata_size, s.file_size, s.data_offset),
(None, None, None)
);
assert_eq!(NodeSpec::new(0, "Base", "Base").index, None);
}
fn be_u32(b: &[u8], at: usize) -> u32 {
u32::from_be_bytes(b[at..at + 4].try_into().unwrap())
}
#[test]
fn a_format_22_header_is_big_endian_with_its_64_bit_fields() {
let spec = one_object(1, vec![NodeSpec::new(0, "Base", "Base")], vec![7; 4]);
let f = build(&spec);
assert_eq!(be_u32(&f, 8), 22);
assert_eq!(f[16], 0, "little-endian metadata");
let file_size = i64::from_be_bytes(f[24..32].try_into().unwrap());
let data_offset = i64::from_be_bytes(f[32..40].try_into().unwrap());
assert_eq!(usize::try_from(file_size).unwrap(), f.len());
assert_eq!(data_offset % 16, 0);
assert_eq!(&f[usize::try_from(data_offset).unwrap()..], &[7; 4]);
assert_eq!(&f[48..59], b"2022.3.0f1\0");
}
#[test]
fn below_9_the_metadata_is_at_the_end_after_the_endian_byte() {
let spec = SerializedSpec {
format: 8,
big_endian: true,
externals: vec![ExternalSpec {
path: b"x".to_vec(),
..ExternalSpec::default()
}],
objects: vec![ObjectSpec {
path_id: 3,
data: vec![1, 2, 3],
..ObjectSpec::default()
}],
types: vec![TypeSpec {
class_id: 1,
nodes: vec![NodeSpec::new(0, "Base", "Base")],
..TypeSpec::default()
}],
..SerializedSpec::default()
};
let f = build(&spec);
let metadata_size = usize::try_from(be_u32(&f, 0)).unwrap();
let file_size = usize::try_from(be_u32(&f, 4)).unwrap();
assert_eq!(file_size, f.len());
assert_eq!(f[file_size - metadata_size], 1, "the endian byte");
assert_eq!(be_u32(&f, 12), 16, "data right after the header");
assert_eq!(&f[16..19], &[1, 2, 3]);
assert_eq!(&f[file_size - metadata_size + 1..][..11], b"2022.3.0f1\0");
}
#[test]
fn the_overrides_replace_the_header_fields() {
let spec = SerializedSpec {
format: 17,
metadata_size: Some(5),
file_size: Some(-1),
data_offset: Some(0x1_0000_0002),
..SerializedSpec::default()
};
let f = build(&spec);
assert_eq!(be_u32(&f, 0), 5);
assert_eq!(be_u32(&f, 4), u32::MAX);
assert_eq!(be_u32(&f, 12), 2);
}
}