0.2.0-dev Download ZIP
163 lines ·6.3 KB Raw
| 1 | using System.Buffers.Binary; |
| 2 | using System.IO.Compression; |
| 3 | using System.Text; |
| 4 | using EvilEngine.Adapters.EaBig; |
| 5 | |
| 6 | namespace EvilEngine.Adapters.Tests.Support; |
| 7 | |
| 8 | public sealed class EbWriter |
| 9 | { |
| 10 | private readonly List<(string Path, byte[] Data, EbCompression Compression, uint ChunkMethod)> _entries = []; |
| 11 | |
| 12 | public EbWriter Add(string path, byte[] data, EbCompression compression = EbCompression.Stored, |
| 13 | uint chunkMethod = 2) |
| 14 | { |
| 15 | _entries.Add((path, data, compression, chunkMethod)); |
| 16 | return this; |
| 17 | } |
| 18 | |
| 19 | public EbWriter Add(string path, string text, EbCompression compression = EbCompression.Stored, |
| 20 | uint chunkMethod = 2) |
| 21 | { |
| 22 | return Add(path, Encoding.UTF8.GetBytes(text), compression, chunkMethod); |
| 23 | } |
| 24 | |
| 25 | public byte[] Build(int shift = 4) |
| 26 | { |
| 27 | var count = _entries.Count; |
| 28 | var directories = _entries.Select(entry => Directory(entry.Path)).Distinct().ToList(); |
| 29 | var nameRecord = 2 + _entries.Max(entry => Encoding.UTF8.GetByteCount(FileName(entry.Path))) + 1; |
| 30 | var directoryRecord = directories.Max(Encoding.UTF8.GetByteCount) + 1; |
| 31 | var compressionStart = EbArchive.HeaderSize + Align(16 * count, 16); |
| 32 | var indexSize = Align(compressionStart + count, 16); |
| 33 | var directoriesStart = indexSize + Align(nameRecord * count, 16); |
| 34 | var namesSize = directoriesStart - indexSize + directoryRecord * directories.Count; |
| 35 | var packed = _entries.Select(entry => Pack(entry.Data, entry.Compression, entry.ChunkMethod)).ToArray(); |
| 36 | |
| 37 | var unit = 1 << shift; |
| 38 | var offsets = new int[count]; |
| 39 | var cursor = Align(indexSize + namesSize, unit); |
| 40 | for (var i = 0; i < count; i++) |
| 41 | { |
| 42 | offsets[i] = cursor; |
| 43 | cursor = Align(cursor + packed[i].Length, unit); |
| 44 | } |
| 45 | |
| 46 | var archive = new byte[cursor]; |
| 47 | var span = archive.AsSpan(); |
| 48 | BinaryPrimitives.WriteUInt16BigEndian(span, EbArchive.Magic); |
| 49 | BinaryPrimitives.WriteUInt16BigEndian(span[2..], EbArchive.Version); |
| 50 | BinaryPrimitives.WriteUInt32BigEndian(span[4..], (uint)count); |
| 51 | BinaryPrimitives.WriteUInt16BigEndian(span[8..], 0x10); |
| 52 | span[10] = (byte)shift; |
| 53 | BinaryPrimitives.WriteUInt32BigEndian(span[12..], (uint)indexSize); |
| 54 | BinaryPrimitives.WriteUInt32BigEndian(span[16..], (uint)namesSize); |
| 55 | span[20] = (byte)nameRecord; |
| 56 | span[21] = (byte)directoryRecord; |
| 57 | BinaryPrimitives.WriteUInt16BigEndian(span[22..], (ushort)directories.Count); |
| 58 | for (var i = 0; i < count; i++) |
| 59 | { |
| 60 | var entry = span[(EbArchive.HeaderSize + i * 16)..]; |
| 61 | BinaryPrimitives.WriteUInt32BigEndian(entry, (uint)(offsets[i] >> shift)); |
| 62 | BinaryPrimitives.WriteUInt32BigEndian(entry[4..], (uint)packed[i].Length); |
| 63 | BinaryPrimitives.WriteUInt32BigEndian(entry[8..], (uint)_entries[i].Data.Length); |
| 64 | span[compressionStart + i] = (byte)_entries[i].Compression; |
| 65 | var name = span[(indexSize + i * nameRecord)..]; |
| 66 | BinaryPrimitives.WriteUInt16BigEndian(name, (ushort)directories.IndexOf(Directory(_entries[i].Path))); |
| 67 | Encoding.UTF8.GetBytes(FileName(_entries[i].Path), name[2..]); |
| 68 | packed[i].CopyTo(span[offsets[i]..]); |
| 69 | } |
| 70 | |
| 71 | for (var i = 0; i < directories.Count; i++) |
| 72 | Encoding.UTF8.GetBytes(directories[i], span[(directoriesStart + i * directoryRecord)..]); |
| 73 | return archive; |
| 74 | } |
| 75 | |
| 76 | public static byte[] RefPackLiterals(ReadOnlySpan<byte> data) |
| 77 | { |
| 78 | using var output = new MemoryStream(); |
| 79 | output.Write([0x10, 0xFB, (byte)(data.Length >> 16), (byte)(data.Length >> 8), (byte)data.Length]); |
| 80 | while (data.Length >= 4) |
| 81 | { |
| 82 | var run = Math.Min(112, data.Length & ~3); |
| 83 | output.WriteByte((byte)(0xE0 + run / 4 - 1)); |
| 84 | output.Write(data[..run]); |
| 85 | data = data[run..]; |
| 86 | } |
| 87 | |
| 88 | output.WriteByte((byte)(0xFC + data.Length)); |
| 89 | output.Write(data); |
| 90 | return output.ToArray(); |
| 91 | } |
| 92 | |
| 93 | private static byte[] Pack(byte[] data, EbCompression compression, uint chunkMethod) |
| 94 | { |
| 95 | return compression switch |
| 96 | { |
| 97 | EbCompression.RefPack => RefPackLiterals(data), |
| 98 | EbCompression.ChunkRef => ChunkRefOf(data, chunkMethod), |
| 99 | _ => data |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | private static byte[] ChunkRefOf(byte[] data, uint method) |
| 104 | { |
| 105 | const int chunkSize = 64; |
| 106 | const int alignment = 16; |
| 107 | var chunks = data.Chunk(chunkSize).ToArray(); |
| 108 | using var output = new MemoryStream(); |
| 109 | var header = new byte[ChunkRef.HeaderSize]; |
| 110 | ChunkRef.Magic.CopyTo(header); |
| 111 | BinaryPrimitives.WriteUInt32BigEndian(header.AsSpan(8), 2); |
| 112 | BinaryPrimitives.WriteUInt32BigEndian(header.AsSpan(12), (uint)data.Length); |
| 113 | BinaryPrimitives.WriteUInt32BigEndian(header.AsSpan(16), chunkSize); |
| 114 | BinaryPrimitives.WriteUInt32BigEndian(header.AsSpan(20), (uint)chunks.Length); |
| 115 | BinaryPrimitives.WriteUInt32BigEndian(header.AsSpan(24), alignment); |
| 116 | output.Write(header); |
| 117 | foreach (var chunk in chunks) |
| 118 | { |
| 119 | var packed = method switch |
| 120 | { |
| 121 | 2 => RefPackLiterals(chunk), |
| 122 | 3 => Deflate(chunk), |
| 123 | _ => chunk |
| 124 | }; |
| 125 | var payload = Align((int)output.Length + 8, alignment); |
| 126 | output.SetLength(payload); |
| 127 | output.Position = payload - 8; |
| 128 | var descriptor = new byte[8]; |
| 129 | BinaryPrimitives.WriteUInt32BigEndian(descriptor, (uint)packed.Length); |
| 130 | BinaryPrimitives.WriteUInt32BigEndian(descriptor.AsSpan(4), method); |
| 131 | output.Write(descriptor); |
| 132 | output.Write(packed); |
| 133 | } |
| 134 | |
| 135 | return output.ToArray(); |
| 136 | } |
| 137 | |
| 138 | private static byte[] Deflate(byte[] data) |
| 139 | { |
| 140 | using var output = new MemoryStream(); |
| 141 | using (var zlib = new ZLibStream(output, CompressionLevel.Optimal, true)) |
| 142 | { |
| 143 | zlib.Write(data); |
| 144 | } |
| 145 | |
| 146 | return output.ToArray(); |
| 147 | } |
| 148 | |
| 149 | private static string Directory(string path) |
| 150 | { |
| 151 | return path.Contains('/') ? path[..path.LastIndexOf('/')] : "."; |
| 152 | } |
| 153 | |
| 154 | private static string FileName(string path) |
| 155 | { |
| 156 | return path[(path.LastIndexOf('/') + 1)..]; |
| 157 | } |
| 158 | |
| 159 | private static int Align(int value, int alignment) |
| 160 | { |
| 161 | return (value + alignment - 1) & ~(alignment - 1); |
| 162 | } |
| 163 | } |