using System.Buffers.Binary; using System.Text; namespace EvilEngine.Adapters.Tests.Support; public sealed class SerializedFileWriter(string unity = "2019.4.29f1", int platform = 24) { private readonly List _externals = []; private readonly List<(int ClassId, byte[] Data)> _objects = []; public SerializedFileWriter Object(int classId, byte[] data) { _objects.Add((classId, data)); return this; } public SerializedFileWriter External(string path) { _externals.Add(path); return this; } public static byte[] PlayerSettings(string company, string product, string version, int beforeCategory = 260) { var bytes = new MemoryStream(); bytes.Write(new byte[36]); AlignedString(bytes, company); AlignedString(bytes, product); bytes.Write(new byte[beforeCategory]); AlignedString(bytes, "public.app-category.games"); bytes.Write(new byte[120]); AlignedString(bytes, version); bytes.Write(new byte[64]); return bytes.ToArray(); } public static byte[] PlayerSettings2022(string company, string product, string version, int logos = 1, int mipmapLimitGroups = 0, int stackTraceTypes = 6) { var bytes = new MemoryStream(); bytes.Write(new byte[36]); AlignedString(bytes, company); AlignedString(bytes, product); bytes.Write(new byte[104]); Int32(bytes, logos); bytes.Write(new byte[logos * 16]); bytes.Write(new byte[124]); Int32(bytes, mipmapLimitGroups); for (var group = 0; group < mipmapLimitGroups; group++) { AlignedString(bytes, $"Group {group}"); Int32(bytes, group); } Int32(bytes, stackTraceTypes); bytes.Write(new byte[stackTraceTypes * 4]); bytes.Write(new byte[80]); AlignedString(bytes, "public.app-category.games"); bytes.Write(new byte[144]); AlignedString(bytes, "1.0"); AlignedString(bytes, "1.0"); AlignedString(bytes, version); bytes.Write(new byte[64]); return bytes.ToArray(); } public static byte[] BuildSettings(params string[] scenes) { var bytes = new MemoryStream(); Int32(bytes, scenes.Length); foreach (var scene in scenes) AlignedString(bytes, scene); bytes.Write(new byte[32]); return bytes.ToArray(); } public static byte[] MonoScript(string name, string className, string ns = "", string assembly = "Assembly-CSharp") { var bytes = new MemoryStream(); AlignedString(bytes, name); bytes.Write(new byte[4 + 16]); AlignedString(bytes, className); AlignedString(bytes, ns); AlignedString(bytes, assembly); return bytes.ToArray(); } public static byte[] MonoBehaviour(int scriptFile, long scriptPathId, string name, params int[] fields) { var bytes = new MemoryStream(); Int32(bytes, 0); bytes.Write(BitConverter.GetBytes(7L)); bytes.Write([1, 0, 0, 0]); Int32(bytes, scriptFile); bytes.Write(BitConverter.GetBytes(scriptPathId)); AlignedString(bytes, name); foreach (var field in fields) Int32(bytes, field); return bytes.ToArray(); } public byte[] Build(int format = 21, bool bigEndian = false, bool typeTrees = false) { var classes = _objects.Select(item => item.ClassId).Distinct().ToList(); var metadata = new Metadata(bigEndian); metadata.CString(unity); metadata.Int32(platform); metadata.Byte(typeTrees ? (byte)1 : (byte)0); metadata.Int32(classes.Count); foreach (var classId in classes) { metadata.Int32(classId); metadata.Byte(0); metadata.Int16(-1); if (classId == 114) metadata.Bytes(new byte[16]); metadata.Bytes(new byte[16]); if (!typeTrees) continue; metadata.Int32(1); metadata.Int32(4); metadata.Bytes(new byte[32]); metadata.Bytes("Base"u8.ToArray()); metadata.Int32(0); } var headerSize = format >= 22 ? 48 : 20; var placed = new List(); long data = 0; foreach (var (_, bytes) in _objects) { placed.Add(data); data = (data + bytes.Length + 7) & ~7L; } metadata.Int32(_objects.Count); for (var index = 0; index < _objects.Count; index++) { metadata.Align(); metadata.Int64(index + 1); if (format >= 22) metadata.Int64(placed[index]); else metadata.Int32((int)placed[index]); metadata.Int32(_objects[index].Data.Length); metadata.Int32(classes.IndexOf(_objects[index].ClassId)); } metadata.Int32(0); metadata.Int32(_externals.Count); foreach (var external in _externals) { metadata.CString(""); metadata.Bytes(new byte[16]); metadata.Int32(0); metadata.CString(external); } metadata.Int32(0); metadata.CString(""); var body = metadata.ToArray(); var dataOffset = (headerSize + body.Length + 15) & ~15L; var fileSize = dataOffset + data; var file = new byte[fileSize]; var header = file.AsSpan(); BinaryPrimitives.WriteInt32BigEndian(header[8..], format); header[16] = bigEndian ? (byte)1 : (byte)0; if (format >= 22) { BinaryPrimitives.WriteUInt32BigEndian(header[20..], (uint)body.Length); BinaryPrimitives.WriteInt64BigEndian(header[24..], fileSize); BinaryPrimitives.WriteInt64BigEndian(header[32..], dataOffset); } else { BinaryPrimitives.WriteUInt32BigEndian(header, (uint)body.Length); BinaryPrimitives.WriteUInt32BigEndian(header[4..], (uint)fileSize); BinaryPrimitives.WriteUInt32BigEndian(header[12..], (uint)dataOffset); } body.CopyTo(file, headerSize); for (var index = 0; index < _objects.Count; index++) _objects[index].Data.CopyTo(file, dataOffset + placed[index]); return file; } private static void Int32(MemoryStream bytes, int value) { Span span = stackalloc byte[4]; BinaryPrimitives.WriteInt32LittleEndian(span, value); bytes.Write(span); } private static void AlignedString(MemoryStream bytes, string text) { var encoded = Encoding.UTF8.GetBytes(text); Span length = stackalloc byte[4]; BinaryPrimitives.WriteInt32LittleEndian(length, encoded.Length); bytes.Write(length); bytes.Write(encoded); while (bytes.Length % 4 != 0) bytes.WriteByte(0); } private sealed class Metadata(bool bigEndian) { private readonly MemoryStream _bytes = new(); public void Byte(byte value) { _bytes.WriteByte(value); } public void Bytes(byte[] value) { _bytes.Write(value); } public void Int16(short value) { Span span = stackalloc byte[2]; if (bigEndian) BinaryPrimitives.WriteInt16BigEndian(span, value); else BinaryPrimitives.WriteInt16LittleEndian(span, value); _bytes.Write(span); } public void Int32(int value) { Span span = stackalloc byte[4]; if (bigEndian) BinaryPrimitives.WriteInt32BigEndian(span, value); else BinaryPrimitives.WriteInt32LittleEndian(span, value); _bytes.Write(span); } public void Int64(long value) { Span span = stackalloc byte[8]; if (bigEndian) BinaryPrimitives.WriteInt64BigEndian(span, value); else BinaryPrimitives.WriteInt64LittleEndian(span, value); _bytes.Write(span); } public void CString(string value) { _bytes.Write(Encoding.UTF8.GetBytes(value + "\0")); } public void Align() { while (_bytes.Length % 4 != 0) _bytes.WriteByte(0); } public byte[] ToArray() { return _bytes.ToArray(); } } }