0.2.0-dev Download ZIP
223 lines ·7.5 KB Raw
| 1 | using System.Buffers.Binary; |
| 2 | using System.Text; |
| 3 | using EvilEngine.Mounts; |
| 4 | |
| 5 | namespace EvilEngine.Adapters.Unity; |
| 6 | |
| 7 | public sealed record SerializedObject(long PathId, long Offset, uint Size, int ClassId); |
| 8 | |
| 9 | public sealed class SerializedFile |
| 10 | { |
| 11 | public const int MonoBehaviourClass = 114; |
| 12 | |
| 13 | public const int PlayerSettingsClass = 129; |
| 14 | |
| 15 | private const int SmallHeader = 20; |
| 16 | |
| 17 | private const int LargeHeader = 48; |
| 18 | |
| 19 | public static readonly IReadOnlyList<int> Formats = [21, 22]; |
| 20 | |
| 21 | private SerializedFile(int format, bool bigEndian, long dataOffset, long fileSize) |
| 22 | { |
| 23 | Format = format; |
| 24 | BigEndian = bigEndian; |
| 25 | DataOffset = dataOffset; |
| 26 | FileSize = fileSize; |
| 27 | } |
| 28 | |
| 29 | public int Format { get; } |
| 30 | |
| 31 | public bool BigEndian { get; } |
| 32 | |
| 33 | public long DataOffset { get; } |
| 34 | |
| 35 | public long FileSize { get; } |
| 36 | |
| 37 | public string UnityVersion { get; private set; } = ""; |
| 38 | |
| 39 | public int Platform { get; private set; } |
| 40 | |
| 41 | public bool TypeTrees { get; private set; } |
| 42 | |
| 43 | public IReadOnlyList<int> Classes { get; private set; } = []; |
| 44 | |
| 45 | public IReadOnlyList<SerializedObject> Objects { get; private set; } = []; |
| 46 | |
| 47 | public IReadOnlyList<string> Externals { get; private set; } = []; |
| 48 | |
| 49 | public static SerializedFile Read(IRandomAccess file) |
| 50 | { |
| 51 | if (file.Length < SmallHeader) |
| 52 | throw new InvalidDataException($"{file.Length} bytes is too short for a serialized file's header."); |
| 53 | Span<byte> header = stackalloc byte[LargeHeader]; |
| 54 | header = header[..(int)Math.Min(LargeHeader, file.Length)]; |
| 55 | file.ReadExactly(0, header); |
| 56 | var format = BinaryPrimitives.ReadInt32BigEndian(header[8..]); |
| 57 | if (!Formats.Contains(format)) |
| 58 | throw new InvalidDataException($"Format {format} isn't one the unity reader knows."); |
| 59 | long metadataSize = BinaryPrimitives.ReadUInt32BigEndian(header); |
| 60 | long fileSize = BinaryPrimitives.ReadUInt32BigEndian(header[4..]); |
| 61 | long dataOffset = BinaryPrimitives.ReadUInt32BigEndian(header[12..]); |
| 62 | var bigEndian = header[16] != 0; |
| 63 | var metadataAt = SmallHeader; |
| 64 | if (format >= 22) |
| 65 | { |
| 66 | if (header.Length < LargeHeader) |
| 67 | throw new InvalidDataException($"{file.Length} bytes is too short for a format {format} header."); |
| 68 | metadataSize = BinaryPrimitives.ReadUInt32BigEndian(header[20..]); |
| 69 | fileSize = BinaryPrimitives.ReadInt64BigEndian(header[24..]); |
| 70 | dataOffset = BinaryPrimitives.ReadInt64BigEndian(header[32..]); |
| 71 | metadataAt = LargeHeader; |
| 72 | } |
| 73 | |
| 74 | if (fileSize != file.Length) |
| 75 | throw new InvalidDataException($"The header says {fileSize} bytes, but the file is {file.Length}."); |
| 76 | if (metadataSize <= 0 || metadataAt + metadataSize > fileSize || dataOffset > fileSize || |
| 77 | dataOffset < metadataAt + metadataSize) |
| 78 | throw new InvalidDataException( |
| 79 | $"The header's metadata ({metadataSize} bytes) and data ({dataOffset}) don't fit {fileSize} bytes."); |
| 80 | var metadata = new byte[metadataSize]; |
| 81 | file.ReadExactly(metadataAt, metadata); |
| 82 | var result = new SerializedFile(format, bigEndian, dataOffset, fileSize); |
| 83 | result.ReadMetadata(new Reader(metadata, bigEndian)); |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | public byte[] ReadObject(IRandomAccess file, SerializedObject item) |
| 88 | { |
| 89 | var at = DataOffset + item.Offset; |
| 90 | if (at < DataOffset || at + item.Size > FileSize) |
| 91 | throw new InvalidDataException($"Object {item.PathId} runs past the end of the file."); |
| 92 | var bytes = new byte[item.Size]; |
| 93 | file.ReadExactly(at, bytes); |
| 94 | return bytes; |
| 95 | } |
| 96 | |
| 97 | private void ReadMetadata(Reader reader) |
| 98 | { |
| 99 | UnityVersion = reader.CString(); |
| 100 | Platform = reader.Int32(); |
| 101 | TypeTrees = reader.Byte() != 0; |
| 102 | var classes = new int[reader.Count(23)]; |
| 103 | for (var index = 0; index < classes.Length; index++) |
| 104 | { |
| 105 | var classId = reader.Int32(); |
| 106 | reader.Skip(3); |
| 107 | if (classId == MonoBehaviourClass) |
| 108 | reader.Skip(16); |
| 109 | reader.Skip(16); |
| 110 | if (TypeTrees) |
| 111 | { |
| 112 | var nodes = reader.Count(32); |
| 113 | var strings = reader.Int32(); |
| 114 | if (strings < 0) |
| 115 | throw new InvalidDataException($"A type tree has {strings} bytes of strings."); |
| 116 | reader.Skip(nodes * 32L + strings); |
| 117 | reader.Skip(reader.Count(4) * 4L); |
| 118 | } |
| 119 | |
| 120 | classes[index] = classId; |
| 121 | } |
| 122 | |
| 123 | var objects = new SerializedObject[reader.Count(20)]; |
| 124 | for (var index = 0; index < objects.Length; index++) |
| 125 | { |
| 126 | reader.Align(); |
| 127 | var pathId = reader.Int64(); |
| 128 | var offset = Format >= 22 ? reader.Int64() : reader.UInt32(); |
| 129 | var size = reader.UInt32(); |
| 130 | var type = reader.Int32(); |
| 131 | if (type < 0 || type >= classes.Length) |
| 132 | throw new InvalidDataException($"Object {pathId} names type {type} of {classes.Length}."); |
| 133 | objects[index] = new SerializedObject(pathId, offset, size, classes[type]); |
| 134 | } |
| 135 | |
| 136 | var scripts = reader.Count(12); |
| 137 | for (var index = 0; index < scripts; index++) |
| 138 | { |
| 139 | reader.Align(); |
| 140 | reader.Skip(12); |
| 141 | } |
| 142 | |
| 143 | var externals = new string[reader.Count(22)]; |
| 144 | for (var index = 0; index < externals.Length; index++) |
| 145 | { |
| 146 | reader.CString(); |
| 147 | reader.Skip(20); |
| 148 | externals[index] = reader.CString(); |
| 149 | } |
| 150 | |
| 151 | Classes = classes; |
| 152 | Objects = objects; |
| 153 | Externals = externals; |
| 154 | } |
| 155 | |
| 156 | private sealed class Reader(byte[] data, bool bigEndian) |
| 157 | { |
| 158 | private int _at; |
| 159 | |
| 160 | public byte Byte() |
| 161 | { |
| 162 | Need(1); |
| 163 | return data[_at++]; |
| 164 | } |
| 165 | |
| 166 | public int Int32() |
| 167 | { |
| 168 | Need(4); |
| 169 | var span = data.AsSpan(_at, 4); |
| 170 | _at += 4; |
| 171 | return bigEndian ? BinaryPrimitives.ReadInt32BigEndian(span) : BinaryPrimitives.ReadInt32LittleEndian(span); |
| 172 | } |
| 173 | |
| 174 | public uint UInt32() |
| 175 | { |
| 176 | return unchecked((uint)Int32()); |
| 177 | } |
| 178 | |
| 179 | public long Int64() |
| 180 | { |
| 181 | Need(8); |
| 182 | var span = data.AsSpan(_at, 8); |
| 183 | _at += 8; |
| 184 | return bigEndian ? BinaryPrimitives.ReadInt64BigEndian(span) : BinaryPrimitives.ReadInt64LittleEndian(span); |
| 185 | } |
| 186 | |
| 187 | public int Count(int smallest) |
| 188 | { |
| 189 | var count = Int32(); |
| 190 | if (count < 0 || (long)count * smallest > data.Length - _at) |
| 191 | throw new InvalidDataException($"A count of {count} doesn't fit the metadata's {data.Length} bytes."); |
| 192 | return count; |
| 193 | } |
| 194 | |
| 195 | public string CString() |
| 196 | { |
| 197 | var end = Array.IndexOf(data, (byte)0, _at); |
| 198 | if (end < 0) |
| 199 | throw new InvalidDataException("A string runs past the end of the metadata."); |
| 200 | var text = Encoding.UTF8.GetString(data, _at, end - _at); |
| 201 | _at = end + 1; |
| 202 | return text; |
| 203 | } |
| 204 | |
| 205 | public void Skip(long bytes) |
| 206 | { |
| 207 | if (bytes < 0 || bytes > data.Length - _at) |
| 208 | throw new InvalidDataException($"Skipping {bytes} bytes runs past the end of the metadata."); |
| 209 | _at += (int)bytes; |
| 210 | } |
| 211 | |
| 212 | public void Align() |
| 213 | { |
| 214 | Skip((4 - _at % 4) % 4); |
| 215 | } |
| 216 | |
| 217 | private void Need(int bytes) |
| 218 | { |
| 219 | if (bytes > data.Length - _at) |
| 220 | throw new InvalidDataException("The metadata ends early."); |
| 221 | } |
| 222 | } |
| 223 | } |