0.2.0-dev Download ZIP
122 lines ·4.4 KB Raw
| 1 | using System.Buffers.Binary; |
| 2 | using System.IO.Compression; |
| 3 | using System.Text; |
| 4 | |
| 5 | namespace EvilEngine.Adapters.Tests.Support; |
| 6 | |
| 7 | public enum DbpfPacking : ushort |
| 8 | { |
| 9 | Stored = 0x0000, |
| 10 | Zlib = 0x5A42, |
| 11 | Deleted = 0xFFE0, |
| 12 | Streamable = 0xFFFE, |
| 13 | Internal = 0xFFFF, |
| 14 | LegacyRefPack = 0x0001 |
| 15 | } |
| 16 | |
| 17 | public sealed class DbpfWriter |
| 18 | { |
| 19 | private readonly List<(uint Type, uint Group, ulong Instance, byte[] Data, DbpfPacking Packing)> _entries = []; |
| 20 | |
| 21 | public DbpfWriter Add(uint type, uint group, ulong instance, byte[] data, DbpfPacking packing = DbpfPacking.Stored) |
| 22 | { |
| 23 | _entries.Add((type, group, instance, data, packing)); |
| 24 | return this; |
| 25 | } |
| 26 | |
| 27 | public DbpfWriter Add(uint type, uint group, ulong instance, string text, DbpfPacking packing = DbpfPacking.Stored) |
| 28 | { |
| 29 | return Add(type, group, instance, Encoding.UTF8.GetBytes(text), packing); |
| 30 | } |
| 31 | |
| 32 | public DbpfWriter Delete(uint type, uint group, ulong instance) |
| 33 | { |
| 34 | return Add(type, group, instance, [], DbpfPacking.Deleted); |
| 35 | } |
| 36 | |
| 37 | public byte[] Build(uint minor = 1, uint? constantType = null, uint? constantGroup = null, |
| 38 | int indexOffsetField = 64, |
| 39 | long pastEnd = 0, bool extended = true) |
| 40 | { |
| 41 | using var output = new MemoryStream(); |
| 42 | output.Write(new byte[96]); |
| 43 | var placed = new List<(int Offset, int Size, int Memory, ushort Compression)>(); |
| 44 | foreach (var entry in _entries) |
| 45 | { |
| 46 | var packed = entry.Packing switch |
| 47 | { |
| 48 | DbpfPacking.Zlib => Zlib(entry.Data), |
| 49 | DbpfPacking.Internal or DbpfPacking.LegacyRefPack => EbWriter.RefPackLiterals(entry.Data), |
| 50 | DbpfPacking.Deleted => [], |
| 51 | _ => entry.Data |
| 52 | }; |
| 53 | var offset = entry.Packing == DbpfPacking.Deleted ? 0 : (int)output.Position; |
| 54 | output.Write(packed); |
| 55 | placed.Add((offset, packed.Length, entry.Data.Length, |
| 56 | entry.Packing == DbpfPacking.LegacyRefPack ? (ushort)0 : (ushort)entry.Packing)); |
| 57 | } |
| 58 | |
| 59 | var indexStart = (int)output.Position; |
| 60 | var flags = (constantType is null ? 0u : 1u) | (constantGroup is null ? 0u : 2u); |
| 61 | Write(output, flags); |
| 62 | if (constantType is { } type) |
| 63 | Write(output, type); |
| 64 | if (constantGroup is { } group) |
| 65 | Write(output, group); |
| 66 | for (var i = 0; i < _entries.Count; i++) |
| 67 | { |
| 68 | var (entryType, entryGroup, instance, _, _) = _entries[i]; |
| 69 | var (offset, size, memory, compression) = placed[i]; |
| 70 | if (constantType is null) |
| 71 | Write(output, entryType); |
| 72 | if (constantGroup is null) |
| 73 | Write(output, entryGroup); |
| 74 | Write(output, (uint)(instance >> 32)); |
| 75 | Write(output, (uint)instance); |
| 76 | Write(output, (uint)(offset + (i == _entries.Count - 1 ? pastEnd : 0))); |
| 77 | Write(output, (uint)size | (extended ? 0x8000_0000u : 0)); |
| 78 | Write(output, (uint)memory); |
| 79 | if (!extended) |
| 80 | continue; |
| 81 | Write(output, compression); |
| 82 | Write(output, 1); |
| 83 | } |
| 84 | |
| 85 | var package = output.ToArray(); |
| 86 | var span = package.AsSpan(); |
| 87 | "DBPF"u8.CopyTo(span); |
| 88 | BinaryPrimitives.WriteUInt32LittleEndian(span[4..], 2); |
| 89 | BinaryPrimitives.WriteUInt32LittleEndian(span[8..], minor); |
| 90 | BinaryPrimitives.WriteUInt32LittleEndian(span[36..], (uint)_entries.Count); |
| 91 | BinaryPrimitives.WriteUInt32LittleEndian(span[44..], (uint)(package.Length - indexStart)); |
| 92 | BinaryPrimitives.WriteUInt32LittleEndian(span[60..], 3); |
| 93 | if (_entries.Count > 0) |
| 94 | BinaryPrimitives.WriteUInt32LittleEndian(span[indexOffsetField..], (uint)indexStart); |
| 95 | return package; |
| 96 | } |
| 97 | |
| 98 | private static byte[] Zlib(byte[] data) |
| 99 | { |
| 100 | using var output = new MemoryStream(); |
| 101 | using (var zlib = new ZLibStream(output, CompressionLevel.Optimal, true)) |
| 102 | { |
| 103 | zlib.Write(data); |
| 104 | } |
| 105 | |
| 106 | return output.ToArray(); |
| 107 | } |
| 108 | |
| 109 | private static void Write(Stream output, uint value) |
| 110 | { |
| 111 | Span<byte> bytes = stackalloc byte[4]; |
| 112 | BinaryPrimitives.WriteUInt32LittleEndian(bytes, value); |
| 113 | output.Write(bytes); |
| 114 | } |
| 115 | |
| 116 | private static void Write(Stream output, ushort value) |
| 117 | { |
| 118 | Span<byte> bytes = stackalloc byte[2]; |
| 119 | BinaryPrimitives.WriteUInt16LittleEndian(bytes, value); |
| 120 | output.Write(bytes); |
| 121 | } |
| 122 | } |