0.2.0-dev Download ZIP
85 lines ·3.0 KB Raw
| 1 | using System.Buffers.Binary; |
| 2 | using System.Text; |
| 3 | |
| 4 | namespace EvilEngine.Adapters.Tests.Support; |
| 5 | |
| 6 | public sealed class PakWriter |
| 7 | { |
| 8 | private readonly List<Item> _items = []; |
| 9 | |
| 10 | public PakWriter Add(string name, byte[] data) |
| 11 | { |
| 12 | _items.Add(new Item(name, data, false, false, null)); |
| 13 | return this; |
| 14 | } |
| 15 | |
| 16 | public PakWriter Add(string name, string text) |
| 17 | { |
| 18 | return Add(name, Encoding.ASCII.GetBytes(text)); |
| 19 | } |
| 20 | |
| 21 | public PakWriter Dead(byte[] data) |
| 22 | { |
| 23 | _items.Add(new Item(null, data, false, false, null)); |
| 24 | return this; |
| 25 | } |
| 26 | |
| 27 | public PakWriter Appended(string name, string text) |
| 28 | { |
| 29 | _items.Add(new Item(name, Encoding.ASCII.GetBytes(text), true, false, null)); |
| 30 | return this; |
| 31 | } |
| 32 | |
| 33 | public PakWriter Listed(string name, int offset, int size) |
| 34 | { |
| 35 | _items.Add(new Item(name, [], false, true, (offset, size))); |
| 36 | return this; |
| 37 | } |
| 38 | |
| 39 | public byte[] Build(int? directoryOffset = null, int? directoryLength = null, int extraDirectoryBytes = 0) |
| 40 | { |
| 41 | using var output = new MemoryStream(); |
| 42 | output.Write(new byte[12]); |
| 43 | var records = new List<(string Name, int Offset, int Size)>(); |
| 44 | foreach (var item in _items.Where(item => !item.After && !item.RecordOnly)) |
| 45 | { |
| 46 | var at = (int)output.Position; |
| 47 | output.Write(item.Data); |
| 48 | if (item.Name is not null) |
| 49 | records.Add((item.Name, at, item.Data.Length)); |
| 50 | } |
| 51 | |
| 52 | var directory = (int)output.Position; |
| 53 | var after = _items.Where(item => item.After).ToArray(); |
| 54 | var count = records.Count + after.Length + _items.Count(item => item.RecordOnly); |
| 55 | var length = count * 64 + extraDirectoryBytes; |
| 56 | var data = directory + length; |
| 57 | foreach (var item in after) |
| 58 | { |
| 59 | records.Add((item.Name!, data, item.Data.Length)); |
| 60 | data += item.Data.Length; |
| 61 | } |
| 62 | |
| 63 | records.AddRange(_items.Where(item => item.RecordOnly) |
| 64 | .Select(item => (item.Name!, item.Record!.Value.Offset, item.Record.Value.Size))); |
| 65 | foreach (var (name, offset, size) in records) |
| 66 | { |
| 67 | var record = new byte[64]; |
| 68 | Encoding.ASCII.GetBytes(name).AsSpan(0, Math.Min(name.Length, 56)).CopyTo(record); |
| 69 | BinaryPrimitives.WriteInt32LittleEndian(record.AsSpan(56), offset); |
| 70 | BinaryPrimitives.WriteInt32LittleEndian(record.AsSpan(60), size); |
| 71 | output.Write(record); |
| 72 | } |
| 73 | |
| 74 | output.Write(new byte[extraDirectoryBytes]); |
| 75 | foreach (var item in after) |
| 76 | output.Write(item.Data); |
| 77 | var bytes = output.ToArray(); |
| 78 | "PACK"u8.CopyTo(bytes); |
| 79 | BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(4), directoryOffset ?? directory); |
| 80 | BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(8), directoryLength ?? length); |
| 81 | return bytes; |
| 82 | } |
| 83 | |
| 84 | private sealed record Item(string? Name, byte[] Data, bool After, bool RecordOnly, (int Offset, int Size)? Record); |
| 85 | } |