0.2.0-dev Download ZIP
323 lines ·12.5 KB Raw
| 1 | using System.Buffers.Binary; |
| 2 | using EvilEngine.Mounts; |
| 3 | |
| 4 | namespace EvilEngine.Adapters.Unreal.Unreal3; |
| 5 | |
| 6 | public readonly record struct Ue3Name(int Index, int Number); |
| 7 | |
| 8 | public sealed record Ue3Import(Ue3Name ClassPackage, Ue3Name ClassName, int Outer, Ue3Name ObjectName); |
| 9 | |
| 10 | public sealed record Ue3Export( |
| 11 | int Class, |
| 12 | int Super, |
| 13 | int Outer, |
| 14 | Ue3Name ObjectName, |
| 15 | int Archetype, |
| 16 | ulong ObjectFlags, |
| 17 | int SerialSize, |
| 18 | long SerialOffset, |
| 19 | uint ExportFlags, |
| 20 | byte[] PackageGuid, |
| 21 | uint PackageFlags); |
| 22 | |
| 23 | public enum Ue3Tables |
| 24 | { |
| 25 | Clear, |
| 26 | Sealed |
| 27 | } |
| 28 | |
| 29 | public sealed class Ue3Package |
| 30 | { |
| 31 | public const int LongestName = 1024; |
| 32 | |
| 33 | private readonly IRandomAccess _access; |
| 34 | |
| 35 | private readonly Dictionary<int, byte[]> _chunks = []; |
| 36 | |
| 37 | private readonly byte[]? _header; |
| 38 | |
| 39 | private readonly string _location; |
| 40 | |
| 41 | private Ue3Package(IRandomAccess access, string location, Ue3Summary summary, IReadOnlyList<Ue3Chunk> chunks, |
| 42 | byte[]? header) |
| 43 | { |
| 44 | _access = access; |
| 45 | _location = location; |
| 46 | Summary = summary; |
| 47 | Chunks = chunks; |
| 48 | _header = header; |
| 49 | } |
| 50 | |
| 51 | public Ue3Summary Summary { get; } |
| 52 | |
| 53 | public IReadOnlyList<Ue3Chunk> Chunks { get; } |
| 54 | |
| 55 | public IReadOnlyList<string> Names { get; private set; } = []; |
| 56 | |
| 57 | public IReadOnlyList<Ue3Import> Imports { get; private set; } = []; |
| 58 | |
| 59 | public IReadOnlyList<Ue3Export> Exports { get; private set; } = []; |
| 60 | |
| 61 | public static Ue3Tables Tables(IRandomAccess access, Ue3Summary summary) |
| 62 | { |
| 63 | if (summary.Compressed && summary.Chunks.Count > 0) |
| 64 | return Ue3Tables.Clear; |
| 65 | if (summary.NameCount == 0) |
| 66 | return Ue3Tables.Clear; |
| 67 | var length = Math.Min(4 + LongestName * 2, access.Length - summary.NameOffset); |
| 68 | if (summary.NameOffset < 0 || length < 4) |
| 69 | return Ue3Tables.Sealed; |
| 70 | var start = new byte[length]; |
| 71 | access.ReadExactly(summary.NameOffset, start); |
| 72 | return IsName(start) ? Ue3Tables.Clear : Ue3Tables.Sealed; |
| 73 | } |
| 74 | |
| 75 | public static bool IsName(ReadOnlySpan<byte> entry) |
| 76 | { |
| 77 | var length = BinaryPrimitives.ReadInt32LittleEndian(entry); |
| 78 | if (length is >= 1 and <= LongestName) |
| 79 | return entry.Length >= 4 + length && entry[3 + length] == 0 && |
| 80 | !entry.Slice(4, length - 1).ContainsAnyInRange((byte)0, (byte)31); |
| 81 | if (length is <= -1 and >= -LongestName) |
| 82 | { |
| 83 | var bytes = -length * 2; |
| 84 | if (entry.Length < 4 + bytes || entry[2 + bytes] != 0 || entry[3 + bytes] != 0) |
| 85 | return false; |
| 86 | for (var i = 4; i < 2 + bytes; i += 2) |
| 87 | if (BinaryPrimitives.ReadUInt16LittleEndian(entry[i..]) < 32) |
| 88 | return false; |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | public static Ue3Package Open(IRandomAccess access, string location) |
| 96 | { |
| 97 | var summary = Ue3Summary.Read(access, location); |
| 98 | if (summary.Compressed && summary.CompressionFlags != Ue3Summary.Zlib) |
| 99 | throw new MountException( |
| 100 | $"'{location}' compresses with flags {summary.CompressionFlags}, which aren't read."); |
| 101 | if (summary.Hidden is null) |
| 102 | { |
| 103 | var plain = new Ue3Package(access, location, summary, summary.Chunks, null); |
| 104 | plain.ReadTables(); |
| 105 | return plain; |
| 106 | } |
| 107 | |
| 108 | var header = Hidden(access, summary, location); |
| 109 | var package = new Ue3Package(access, location, summary, HiddenChunks(header, summary, location), header); |
| 110 | package.ReadTables(); |
| 111 | return package; |
| 112 | } |
| 113 | |
| 114 | private static byte[] Hidden(IRandomAccess access, Ue3Summary summary, string location) |
| 115 | { |
| 116 | if (summary.NameOffset < 0 || summary.HiddenEnd < summary.NameOffset || |
| 117 | summary.HiddenChunkListAt < summary.NameOffset || |
| 118 | summary.HiddenChunkListAt + 4 > summary.HiddenEnd || |
| 119 | summary.NameOffset + (long)summary.HiddenLength > access.Length) |
| 120 | throw new MountException($"'{location}' places its hidden tables outside its header."); |
| 121 | var header = new byte[summary.HiddenLength]; |
| 122 | access.ReadExactly(summary.NameOffset, header); |
| 123 | if (summary.NameCount > 0 && !IsName(header)) |
| 124 | throw new MountException($"'{location}' keeps its tables encrypted, so they're sealed."); |
| 125 | return header; |
| 126 | } |
| 127 | |
| 128 | private static Ue3Chunk[] HiddenChunks(byte[] header, Ue3Summary summary, string location) |
| 129 | { |
| 130 | var reader = new UnrealReader(header, location) { Position = summary.HiddenChunkListAt - summary.NameOffset }; |
| 131 | var count = reader.I32(); |
| 132 | var chunks = new Ue3Chunk[Math.Max(count, 0)]; |
| 133 | var fits = count >= 0 && |
| 134 | 4 + (long)count * summary.Layout.ChunkSize == summary.HiddenEnd - summary.HiddenChunkListAt; |
| 135 | if (fits) |
| 136 | for (var i = 0; i < chunks.Length; i++) |
| 137 | chunks[i] = summary.Layout.WideOffsets |
| 138 | ? new Ue3Chunk(reader.I64(), reader.I32(), reader.I64(), reader.I32()) |
| 139 | : new Ue3Chunk(reader.I32(), reader.I32(), reader.I32(), reader.I32()); |
| 140 | if (!fits || (chunks.Length > 0 && chunks[0].CompressedOffset != summary.HeaderSize)) |
| 141 | throw new MountException($"'{location}' has a hidden chunk list that doesn't hold together."); |
| 142 | return chunks; |
| 143 | } |
| 144 | |
| 145 | public string Name(Ue3Name name) |
| 146 | { |
| 147 | if (name.Index < 0 || name.Index >= Names.Count) |
| 148 | throw new MountException($"'{_location}' names entry {name.Index} of its {Names.Count}."); |
| 149 | return name.Number == 0 ? Names[name.Index] : $"{Names[name.Index]}_{name.Number - 1}"; |
| 150 | } |
| 151 | |
| 152 | public string ClassOf(Ue3Export export) |
| 153 | { |
| 154 | return export.Class switch |
| 155 | { |
| 156 | < 0 when -export.Class - 1 < Imports.Count => Name(Imports[-export.Class - 1].ObjectName), |
| 157 | > 0 when export.Class - 1 < Exports.Count => Name(Exports[export.Class - 1].ObjectName), |
| 158 | 0 => "Class", |
| 159 | _ => throw new MountException($"'{_location}' gives an export class {export.Class}, outside its tables.") |
| 160 | }; |
| 161 | } |
| 162 | |
| 163 | public byte[] ReadExport(Ue3Export export) |
| 164 | { |
| 165 | return Read(export.SerialOffset, export.SerialSize); |
| 166 | } |
| 167 | |
| 168 | public byte[] Read(long offset, int length) |
| 169 | { |
| 170 | if (offset < 0 || length < 0) |
| 171 | throw new MountException($"'{_location}' reads {length} bytes at {offset}."); |
| 172 | var output = new byte[length]; |
| 173 | if (!Summary.Compressed) |
| 174 | { |
| 175 | if (offset + length > _access.Length) |
| 176 | throw new MountException($"'{_location}' reads {length} bytes at {offset}, past its end."); |
| 177 | _access.ReadExactly(offset, output); |
| 178 | return output; |
| 179 | } |
| 180 | |
| 181 | var written = 0; |
| 182 | while (written < length) |
| 183 | { |
| 184 | var at = offset + written; |
| 185 | var index = -1; |
| 186 | for (var i = 0; i < Chunks.Count; i++) |
| 187 | { |
| 188 | var chunk = Chunks[i]; |
| 189 | if (at >= chunk.UncompressedOffset && at < chunk.UncompressedOffset + chunk.UncompressedSize) |
| 190 | index = i; |
| 191 | } |
| 192 | |
| 193 | if (index < 0) |
| 194 | throw new MountException($"'{_location}' has no chunk holding byte {at}."); |
| 195 | var data = Chunk(index); |
| 196 | var from = (int)(at - Chunks[index].UncompressedOffset); |
| 197 | var count = Math.Min(length - written, data.Length - from); |
| 198 | data.AsSpan(from, count).CopyTo(output.AsSpan(written)); |
| 199 | written += count; |
| 200 | } |
| 201 | |
| 202 | return output; |
| 203 | } |
| 204 | |
| 205 | private byte[] Chunk(int index) |
| 206 | { |
| 207 | if (_chunks.TryGetValue(index, out var data)) |
| 208 | return data; |
| 209 | var chunk = Chunks[index]; |
| 210 | data = Ue3Blocks.Inflate(_access, chunk.CompressedOffset, chunk.UncompressedSize, _location); |
| 211 | _chunks[index] = data; |
| 212 | return data; |
| 213 | } |
| 214 | |
| 215 | private void ReadTables() |
| 216 | { |
| 217 | var end = new[] { Summary.DependsOffset, Summary.HeaderSize }.Where(offset => offset > Summary.NameOffset) |
| 218 | .Min(); |
| 219 | var tables = _header is null ? Read(Summary.NameOffset, end - Summary.NameOffset) : _header; |
| 220 | var reader = new UnrealReader(tables, _location); |
| 221 | var names = new string[Summary.NameCount]; |
| 222 | for (var i = 0; i < names.Length; i++) |
| 223 | { |
| 224 | names[i] = reader.FString(); |
| 225 | reader.U64(); |
| 226 | } |
| 227 | |
| 228 | Names = names; |
| 229 | reader.Position = Summary.ImportOffset - Summary.NameOffset; |
| 230 | var imports = new Ue3Import[Summary.ImportCount]; |
| 231 | for (var i = 0; i < imports.Length; i++) |
| 232 | imports[i] = new Ue3Import(Name(ref reader), Name(ref reader), reader.I32(), Name(ref reader)); |
| 233 | Imports = imports; |
| 234 | reader.Position = Summary.ExportOffset - Summary.NameOffset; |
| 235 | var exports = new Ue3Export[Summary.ExportCount]; |
| 236 | for (var i = 0; i < exports.Length; i++) |
| 237 | { |
| 238 | var (type, super, outer) = (reader.I32(), reader.I32(), reader.I32()); |
| 239 | var name = Name(ref reader); |
| 240 | var archetype = reader.I32(); |
| 241 | var flags = reader.U64(); |
| 242 | var size = reader.I32(); |
| 243 | var offset = Summary.Layout.WideOffsets ? reader.I64() : reader.I32(); |
| 244 | var exportFlags = reader.U32(); |
| 245 | reader.Bytes(reader.Count(4) * 4); |
| 246 | var guid = reader.Bytes(16).ToArray(); |
| 247 | exports[i] = new Ue3Export(type, super, outer, name, archetype, flags, size, offset, exportFlags, guid, |
| 248 | reader.U32()); |
| 249 | } |
| 250 | |
| 251 | Exports = exports; |
| 252 | foreach (var import in imports) |
| 253 | { |
| 254 | Name(import.ClassPackage); |
| 255 | Name(import.ClassName); |
| 256 | Name(import.ObjectName); |
| 257 | } |
| 258 | |
| 259 | foreach (var export in exports) |
| 260 | Name(export.ObjectName); |
| 261 | } |
| 262 | |
| 263 | private static Ue3Name Name(ref UnrealReader reader) |
| 264 | { |
| 265 | return new Ue3Name(reader.I32(), reader.I32()); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | public static class Ue3Blocks |
| 270 | { |
| 271 | public const int HeaderSize = 16; |
| 272 | |
| 273 | public static (int BlockSize, int Compressed, int Uncompressed) Header(ReadOnlySpan<byte> header, string location) |
| 274 | { |
| 275 | if (!Ue3Summary.Recognises(header)) |
| 276 | throw new MountException($"'{location}' has no compressed chunk where one should start."); |
| 277 | var blockSize = BinaryPrimitives.ReadInt32LittleEndian(header[4..]); |
| 278 | var compressed = BinaryPrimitives.ReadInt32LittleEndian(header[8..]); |
| 279 | var uncompressed = BinaryPrimitives.ReadInt32LittleEndian(header[12..]); |
| 280 | if (blockSize <= 0 || compressed < 0 || uncompressed < 0) |
| 281 | throw new MountException( |
| 282 | $"'{location}' has a compressed chunk with block size {blockSize}, {compressed} bytes holding {uncompressed}."); |
| 283 | return (blockSize, compressed, uncompressed); |
| 284 | } |
| 285 | |
| 286 | public static long StoredLength(int blockSize, int compressed, int uncompressed) |
| 287 | { |
| 288 | var blocks = uncompressed == 0 ? 0 : (uncompressed + (long)blockSize - 1) / blockSize; |
| 289 | return HeaderSize + blocks * 8 + compressed; |
| 290 | } |
| 291 | |
| 292 | public static byte[] Inflate(IRandomAccess access, long offset, int expected, string location) |
| 293 | { |
| 294 | Span<byte> header = stackalloc byte[HeaderSize]; |
| 295 | access.ReadExactly(offset, header); |
| 296 | var (blockSize, compressed, uncompressed) = Header(header, location); |
| 297 | if (uncompressed != expected) |
| 298 | throw new MountException( |
| 299 | $"'{location}' has a chunk at {offset} holding {uncompressed} bytes, not {expected}."); |
| 300 | var count = (int)((uncompressed + (long)blockSize - 1) / blockSize); |
| 301 | var table = new byte[count * 8]; |
| 302 | access.ReadExactly(offset + HeaderSize, table); |
| 303 | var stored = new byte[compressed]; |
| 304 | access.ReadExactly(offset + HeaderSize + table.Length, stored); |
| 305 | var output = new byte[uncompressed]; |
| 306 | int read = 0, written = 0; |
| 307 | for (var i = 0; i < count; i++) |
| 308 | { |
| 309 | var size = BinaryPrimitives.ReadInt32LittleEndian(table.AsSpan(i * 8)); |
| 310 | var length = BinaryPrimitives.ReadInt32LittleEndian(table.AsSpan(i * 8 + 4)); |
| 311 | if (size < 0 || length < 0 || read + size > stored.Length || written + length > output.Length) |
| 312 | throw new MountException($"'{location}' has a block at {offset} reaching past its chunk."); |
| 313 | Codecs.Decode("Zlib", stored.AsSpan(read, size), output.AsSpan(written, length), location); |
| 314 | read += size; |
| 315 | written += length; |
| 316 | } |
| 317 | |
| 318 | if (written != uncompressed) |
| 319 | throw new MountException( |
| 320 | $"'{location}' has a chunk at {offset} whose blocks hold {written} bytes, not {uncompressed}."); |
| 321 | return output; |
| 322 | } |
| 323 | } |