0.2.0-dev Download ZIP
314 lines ·12.6 KB Raw
| 1 | using System.Buffers.Binary; |
| 2 | using System.Runtime.InteropServices; |
| 3 | using System.Text; |
| 4 | using EvilEngine.Adapters.EaPegasus.Attrib; |
| 5 | |
| 6 | namespace EvilEngine.Adapters.Tests.Support; |
| 7 | |
| 8 | public sealed record Blob(byte[] Bytes, params (int At, string Text)[] Texts) |
| 9 | { |
| 10 | public static Blob Of(params byte[] bytes) |
| 11 | { |
| 12 | return new Blob(bytes); |
| 13 | } |
| 14 | |
| 15 | public static Blob Float(float value) |
| 16 | { |
| 17 | var bytes = new byte[4]; |
| 18 | BinaryPrimitives.WriteSingleBigEndian(bytes, value); |
| 19 | return new Blob(bytes); |
| 20 | } |
| 21 | |
| 22 | public static Blob Int32(int value) |
| 23 | { |
| 24 | var bytes = new byte[4]; |
| 25 | BinaryPrimitives.WriteInt32BigEndian(bytes, value); |
| 26 | return new Blob(bytes); |
| 27 | } |
| 28 | |
| 29 | public static Blob Text(string text) |
| 30 | { |
| 31 | return new Blob(new byte[4], (0, text)); |
| 32 | } |
| 33 | |
| 34 | public static Blob Array(ushort capacity, ushort stride, int alignment, params Blob[] items) |
| 35 | { |
| 36 | var bytes = new List<byte>(new byte[8]); |
| 37 | BinaryPrimitives.WriteUInt16BigEndian(CollectionsMarshal.AsSpan(bytes), capacity); |
| 38 | BinaryPrimitives.WriteUInt16BigEndian(CollectionsMarshal.AsSpan(bytes)[2..], (ushort)items.Length); |
| 39 | BinaryPrimitives.WriteUInt16BigEndian(CollectionsMarshal.AsSpan(bytes)[4..], stride); |
| 40 | BinaryPrimitives.WriteUInt16BigEndian(CollectionsMarshal.AsSpan(bytes)[6..], (ushort)alignment); |
| 41 | var texts = new List<(int, string)>(); |
| 42 | for (var i = 0; i < capacity; i++) |
| 43 | { |
| 44 | while (bytes.Count % alignment != 0) |
| 45 | bytes.Add(0); |
| 46 | var item = i < items.Length ? items[i] : new Blob(new byte[stride]); |
| 47 | texts.AddRange(item.Texts.Select(text => (text.At + bytes.Count, text.Text))); |
| 48 | bytes.AddRange(item.Bytes); |
| 49 | } |
| 50 | |
| 51 | return new Blob([.. bytes], [.. texts]); |
| 52 | } |
| 53 | |
| 54 | public static Blob Layout(int size, params (int Offset, Blob Value)[] values) |
| 55 | { |
| 56 | var bytes = new byte[size]; |
| 57 | var texts = new List<(int, string)>(); |
| 58 | foreach (var (offset, value) in values) |
| 59 | { |
| 60 | value.Bytes.CopyTo(bytes, offset); |
| 61 | texts.AddRange(value.Texts.Select(text => (text.At + offset, text.Text))); |
| 62 | } |
| 63 | |
| 64 | return new Blob(bytes, [.. texts]); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public sealed record FieldDef( |
| 69 | string Name, |
| 70 | string Type, |
| 71 | ushort Offset, |
| 72 | ushort Size, |
| 73 | ushort MaxCount, |
| 74 | AttribFieldFlags Flags, |
| 75 | byte Alignment); |
| 76 | |
| 77 | public sealed record EntryDef(string Field, int TypeIndex, byte Node, Blob Value); |
| 78 | |
| 79 | public sealed class VaultWriter |
| 80 | { |
| 81 | private readonly List<(long At, long Destination)> _binPointers = []; |
| 82 | private readonly List<byte> _dat = []; |
| 83 | private readonly long _datAt; |
| 84 | private readonly List<byte> _data = []; |
| 85 | private readonly long _dataAt; |
| 86 | private readonly List<(ulong Id, ulong Type, long At, int Size)> _exports = []; |
| 87 | private readonly string _name; |
| 88 | private readonly string[] _strings; |
| 89 | private readonly List<(long At, long Destination)> _vltPointers = []; |
| 90 | |
| 91 | public VaultWriter(string name, params string[] strings) |
| 92 | { |
| 93 | _name = name; |
| 94 | _strings = strings; |
| 95 | _datAt = 32 + DependencyChunk().Length + 8; |
| 96 | _dataAt = StringChunk().Length + 16; |
| 97 | } |
| 98 | |
| 99 | public List<(ushort Kind, ushort Index)> ExtraPointers { get; } = []; |
| 100 | |
| 101 | public VaultWriter Database(uint classCount, uint defaultDataSize, params (string Name, uint Size)[] types) |
| 102 | { |
| 103 | var names = Bin(types.SelectMany(type => Encoding.ASCII.GetBytes(type.Name + "\0")).ToArray(), 8); |
| 104 | var record = new byte[16 + types.Length * 4]; |
| 105 | BinaryPrimitives.WriteUInt32BigEndian(record, classCount); |
| 106 | BinaryPrimitives.WriteUInt32BigEndian(record.AsSpan(4), defaultDataSize); |
| 107 | BinaryPrimitives.WriteUInt32BigEndian(record.AsSpan(8), (uint)types.Length); |
| 108 | for (var i = 0; i < types.Length; i++) |
| 109 | BinaryPrimitives.WriteUInt32BigEndian(record.AsSpan(16 + i * 4), types[i].Size); |
| 110 | var at = Export(AttribHash.Of("Attrib::Database"), AttribDatabase.DatabaseExport, record); |
| 111 | _vltPointers.Add((at + 12, names)); |
| 112 | return this; |
| 113 | } |
| 114 | |
| 115 | public VaultWriter Class(string name, uint reserve, uint layoutSize, ushort required, FieldDef[] fields, |
| 116 | Blob? statics = null) |
| 117 | { |
| 118 | var definitions = new byte[fields.Length * 24]; |
| 119 | for (var i = 0; i < fields.Length; i++) |
| 120 | { |
| 121 | var field = fields[i]; |
| 122 | var span = definitions.AsSpan(i * 24); |
| 123 | BinaryPrimitives.WriteUInt64BigEndian(span, AttribHash.Of(field.Name)); |
| 124 | BinaryPrimitives.WriteUInt64BigEndian(span[8..], AttribHash.Of(field.Type)); |
| 125 | BinaryPrimitives.WriteUInt16BigEndian(span[16..], field.Offset); |
| 126 | BinaryPrimitives.WriteUInt16BigEndian(span[18..], field.Size); |
| 127 | BinaryPrimitives.WriteUInt16BigEndian(span[20..], field.MaxCount); |
| 128 | span[22] = (byte)field.Flags; |
| 129 | span[23] = field.Alignment; |
| 130 | } |
| 131 | |
| 132 | var definitionsAt = fields.Length > 0 ? Bin(definitions, 8) : 0; |
| 133 | var record = new byte[40]; |
| 134 | BinaryPrimitives.WriteUInt64BigEndian(record, AttribHash.Of(name)); |
| 135 | BinaryPrimitives.WriteUInt32BigEndian(record.AsSpan(8), reserve); |
| 136 | BinaryPrimitives.WriteUInt32BigEndian(record.AsSpan(12), (uint)fields.Length); |
| 137 | BinaryPrimitives.WriteUInt32BigEndian(record.AsSpan(20), (uint)(statics?.Bytes.Length ?? 0)); |
| 138 | BinaryPrimitives.WriteUInt32BigEndian(record.AsSpan(28), layoutSize); |
| 139 | BinaryPrimitives.WriteUInt16BigEndian(record.AsSpan(34), required); |
| 140 | var at = Export(AttribHash.Of(name), AttribDatabase.ClassExport, record); |
| 141 | if (fields.Length > 0) |
| 142 | _vltPointers.Add((at + 16, definitionsAt)); |
| 143 | if (statics is not null) |
| 144 | _vltPointers.Add((at + 24, Place(statics))); |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | public VaultWriter Collection(string @class, string name, string? parent, string[] types, Blob? layout, |
| 149 | params EntryDef[] entries) |
| 150 | { |
| 151 | var typesLength = types.Length % 2 == 0 ? types.Length : types.Length + 1; |
| 152 | var record = new byte[48 + typesLength * 8 + entries.Length * 16]; |
| 153 | BinaryPrimitives.WriteUInt64BigEndian(record, AttribHash.Of(name)); |
| 154 | BinaryPrimitives.WriteUInt64BigEndian(record.AsSpan(8), AttribHash.Of(@class)); |
| 155 | BinaryPrimitives.WriteUInt64BigEndian(record.AsSpan(16), parent is null ? 0 : AttribHash.Of(parent)); |
| 156 | BinaryPrimitives.WriteUInt32BigEndian(record.AsSpan(24), (uint)entries.Length); |
| 157 | BinaryPrimitives.WriteUInt32BigEndian(record.AsSpan(32), (uint)entries.Length); |
| 158 | BinaryPrimitives.WriteUInt16BigEndian(record.AsSpan(36), (ushort)types.Length); |
| 159 | BinaryPrimitives.WriteUInt16BigEndian(record.AsSpan(38), (ushort)typesLength); |
| 160 | for (var i = 0; i < types.Length; i++) |
| 161 | BinaryPrimitives.WriteUInt64BigEndian(record.AsSpan(48 + i * 8), AttribHash.Of(types[i])); |
| 162 | var entriesAt = 48 + typesLength * 8; |
| 163 | var pending = new List<(long Offset, long Destination)>(); |
| 164 | var inlineTexts = new List<(int Offset, string Text)>(); |
| 165 | for (var i = 0; i < entries.Length; i++) |
| 166 | { |
| 167 | var entry = entries[i]; |
| 168 | var span = record.AsSpan(entriesAt + i * 16); |
| 169 | BinaryPrimitives.WriteUInt64BigEndian(span, AttribHash.Of(entry.Field)); |
| 170 | BinaryPrimitives.WriteUInt16BigEndian(span[12..], (ushort)entry.TypeIndex); |
| 171 | span[14] = entry.Node; |
| 172 | if ((entry.Node & 0x40) != 0) |
| 173 | { |
| 174 | entry.Value.Bytes.CopyTo(span[8..]); |
| 175 | inlineTexts.AddRange(entry.Value.Texts.Select(text => (entriesAt + i * 16 + 8 + text.At, text.Text))); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | pending.Add((entriesAt + i * 16 + 8, Place(entry.Value))); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | var at = Export(AttribHash.Of($"{@class}/{name}"), AttribDatabase.CollectionExport, record); |
| 184 | foreach (var (offset, destination) in pending) |
| 185 | _vltPointers.Add((at + offset, destination)); |
| 186 | foreach (var (offset, text) in inlineTexts) |
| 187 | _vltPointers.Add((at + offset, BinText(text))); |
| 188 | if (layout is not null) |
| 189 | _vltPointers.Add((at + 40, Place(layout))); |
| 190 | return this; |
| 191 | } |
| 192 | |
| 193 | public (byte[] Vlt, byte[] Bin) Build() |
| 194 | { |
| 195 | var pointers = new List<byte>(); |
| 196 | |
| 197 | void Pointer(uint offset, ushort kind, ushort index, ulong destination) |
| 198 | { |
| 199 | var entry = new byte[16]; |
| 200 | BinaryPrimitives.WriteUInt32BigEndian(entry, offset); |
| 201 | BinaryPrimitives.WriteUInt16BigEndian(entry.AsSpan(4), kind); |
| 202 | BinaryPrimitives.WriteUInt16BigEndian(entry.AsSpan(6), index); |
| 203 | BinaryPrimitives.WriteUInt64BigEndian(entry.AsSpan(8), destination); |
| 204 | pointers.AddRange(entry); |
| 205 | } |
| 206 | |
| 207 | Pointer(0, 2, 1, 0); |
| 208 | foreach (var (at, destination) in _binPointers) |
| 209 | Pointer((uint)at, 3, 1, (ulong)destination); |
| 210 | Pointer(0, 2, 0, 0); |
| 211 | foreach (var (at, destination) in _vltPointers) |
| 212 | Pointer((uint)at, 3, 1, (ulong)destination); |
| 213 | foreach (var (kind, index) in ExtraPointers) |
| 214 | Pointer(0, kind, index, 0); |
| 215 | Pointer(0, 0, 0, 0); |
| 216 | pointers.AddRange(new byte[8]); |
| 217 | |
| 218 | var exports = new List<byte>(new byte[8]); |
| 219 | BinaryPrimitives.WriteUInt64BigEndian(CollectionsMarshal.AsSpan(exports), (ulong)_exports.Count); |
| 220 | foreach (var (id, type, at, size) in _exports) |
| 221 | { |
| 222 | var entry = new byte[24]; |
| 223 | BinaryPrimitives.WriteUInt64BigEndian(entry, id); |
| 224 | BinaryPrimitives.WriteUInt64BigEndian(entry.AsSpan(8), type); |
| 225 | BinaryPrimitives.WriteUInt32BigEndian(entry.AsSpan(16), (uint)size); |
| 226 | BinaryPrimitives.WriteUInt32BigEndian(entry.AsSpan(20), (uint)at); |
| 227 | exports.AddRange(entry); |
| 228 | } |
| 229 | |
| 230 | var version = new byte[8]; |
| 231 | BinaryPrimitives.WriteUInt64BigEndian(version, 0x390F9394B014A5F0); |
| 232 | var vlt = new List<byte>(); |
| 233 | vlt.AddRange(Chunk("Vers", version)); |
| 234 | vlt.AddRange(Chunk("StrN", new byte[8])); |
| 235 | vlt.AddRange(DependencyChunk()); |
| 236 | vlt.AddRange(Chunk("DatN", Pad([.. _dat], 16, 8))); |
| 237 | vlt.AddRange(Chunk("ExpN", Pad([.. exports], 16, 8))); |
| 238 | vlt.AddRange(Chunk("PtrN", [.. pointers])); |
| 239 | vlt.AddRange(Chunk("EndC", new byte[8])); |
| 240 | |
| 241 | var bin = new List<byte>(); |
| 242 | bin.AddRange(StringChunk()); |
| 243 | bin.AddRange(Chunk("EndC", new byte[8])); |
| 244 | bin.AddRange(_data); |
| 245 | return ([.. vlt], [.. bin]); |
| 246 | } |
| 247 | |
| 248 | private long Export(ulong id, ulong type, byte[] record) |
| 249 | { |
| 250 | while (_dat.Count % 8 != 0) |
| 251 | _dat.Add(0); |
| 252 | var at = _datAt + _dat.Count; |
| 253 | _dat.AddRange(record); |
| 254 | _exports.Add((id, type, at, record.Length)); |
| 255 | return at; |
| 256 | } |
| 257 | |
| 258 | private long Bin(byte[] bytes, int alignment) |
| 259 | { |
| 260 | while (_data.Count % alignment != 0) |
| 261 | _data.Add(0); |
| 262 | var at = _dataAt + _data.Count; |
| 263 | _data.AddRange(bytes); |
| 264 | return at; |
| 265 | } |
| 266 | |
| 267 | private long BinText(string text) |
| 268 | { |
| 269 | return Bin(Encoding.UTF8.GetBytes(text + "\0"), 1); |
| 270 | } |
| 271 | |
| 272 | private long Place(Blob blob) |
| 273 | { |
| 274 | var at = Bin(blob.Bytes, 16); |
| 275 | foreach (var (offset, text) in blob.Texts) |
| 276 | _binPointers.Add((at + offset, BinText(text))); |
| 277 | return at; |
| 278 | } |
| 279 | |
| 280 | private byte[] DependencyChunk() |
| 281 | { |
| 282 | string[] names = [$"{_name}.vlt", $"{_name}.bin"]; |
| 283 | var payload = new List<byte>(new byte[8 + 16 + 8]); |
| 284 | var span = CollectionsMarshal.AsSpan(payload); |
| 285 | BinaryPrimitives.WriteUInt64BigEndian(span, 2); |
| 286 | BinaryPrimitives.WriteUInt64BigEndian(span[8..], AttribHash.Of(names[0])); |
| 287 | BinaryPrimitives.WriteUInt64BigEndian(span[16..], AttribHash.Of(names[1])); |
| 288 | BinaryPrimitives.WriteUInt32BigEndian(span[28..], (uint)names[0].Length + 1); |
| 289 | payload.AddRange(Encoding.ASCII.GetBytes($"{names[0]}\0{names[1]}\0")); |
| 290 | return Chunk("DepN", Pad([.. payload], 16, 8)); |
| 291 | } |
| 292 | |
| 293 | private byte[] StringChunk() |
| 294 | { |
| 295 | return Chunk("StrE", Pad(_strings.SelectMany(text => Encoding.UTF8.GetBytes(text + "\0")).ToArray(), 16, 8)); |
| 296 | } |
| 297 | |
| 298 | private static byte[] Pad(byte[] payload, int alignment, int header) |
| 299 | { |
| 300 | var size = (payload.Length + header + alignment - 1) / alignment * alignment - header; |
| 301 | var padded = new byte[size]; |
| 302 | payload.CopyTo(padded, 0); |
| 303 | return padded; |
| 304 | } |
| 305 | |
| 306 | private static byte[] Chunk(string tag, byte[] payload) |
| 307 | { |
| 308 | var chunk = new byte[8 + payload.Length]; |
| 309 | Encoding.ASCII.GetBytes(tag).CopyTo(chunk, 0); |
| 310 | BinaryPrimitives.WriteUInt32BigEndian(chunk.AsSpan(4), (uint)chunk.Length); |
| 311 | payload.CopyTo(chunk, 8); |
| 312 | return chunk; |
| 313 | } |
| 314 | } |