using System.IO.Compression; using System.Numerics; using System.Text; using EvilEngine.Adapters.Source.Maps; namespace EvilEngine.Adapters.Tests.Support; internal sealed class BspWriter { private readonly List<(string Id, ushort Version, byte[] Data, bool Compressed)> _gameLumps = []; private readonly Dictionary _lumps = []; public int Version { get; init; } = 20; public static byte[] ValveLzma(byte[] stream, int size) { var data = new byte[17 + stream.Length - 5]; "LZMA"u8.CopyTo(data); BitConverter.GetBytes(size).CopyTo(data, 4); BitConverter.GetBytes(stream.Length - 5).CopyTo(data, 8); stream.AsSpan(0, 5).CopyTo(data.AsSpan(12)); stream.AsSpan(5).CopyTo(data.AsSpan(17)); return data; } public static byte[] Zip(params (string Name, byte[] Data, int Method)[] entries) { using var zip = new MemoryStream(); using var writer = new BinaryWriter(zip); var directory = new List<(string Name, int Method, int Packed, int Size, int Offset)>(); foreach (var (name, data, method) in entries) { var packed = method switch { 0 => data, 8 => Deflate(data), _ => data }; var size = method == 14 ? BitConverter.ToInt32(packed, 0) : data.Length; if (method == 14) packed = packed[4..]; directory.Add((name, method, packed.Length, size, (int)zip.Position)); writer.Write(0x04034b50u); writer.Write((ushort)20); writer.Write((ushort)0); writer.Write((ushort)method); writer.Write(0u); writer.Write(0u); writer.Write(packed.Length); writer.Write(size); writer.Write((ushort)name.Length); writer.Write((ushort)0); writer.Write(Encoding.ASCII.GetBytes(name)); writer.Write(packed); } var start = (int)zip.Position; foreach (var (name, method, packed, size, offset) in directory) { writer.Write(0x02014b50u); writer.Write((ushort)20); writer.Write((ushort)20); writer.Write((ushort)0); writer.Write((ushort)method); writer.Write(0u); writer.Write(0u); writer.Write(packed); writer.Write(size); writer.Write((ushort)name.Length); writer.Write((ushort)0); writer.Write((ushort)0); writer.Write((ushort)0); writer.Write((ushort)0); writer.Write(0u); writer.Write(offset); writer.Write(Encoding.ASCII.GetBytes(name)); } var end = (int)zip.Position; writer.Write(0x06054b50u); writer.Write((ushort)0); writer.Write((ushort)0); writer.Write((ushort)directory.Count); writer.Write((ushort)directory.Count); writer.Write(end - start); writer.Write(start); writer.Write((ushort)0); writer.Flush(); return zip.ToArray(); } public static byte[] ZipLzma(byte[] stream, int size) { var data = new byte[4 + 4 + stream.Length]; BitConverter.GetBytes(size).CopyTo(data, 0); data[4] = 9; data[5] = 20; BitConverter.GetBytes((ushort)5).CopyTo(data, 6); stream.CopyTo(data, 8); return data; } public BspWriter Lump(int index, byte[] data, int version = 0) { _lumps[index] = (data, version); return this; } public BspWriter Records(int index, IEnumerable records, Action write, int version = 0) { using var stream = new MemoryStream(); using var writer = new BinaryWriter(stream); foreach (var record in records) write(writer, record); writer.Flush(); return Lump(index, stream.ToArray(), version); } public BspWriter GameLump(string id, ushort version, byte[] data, bool compressed = false) { _gameLumps.Add((id, version, data, compressed)); return this; } public BspWriter Entities(params (string Key, string Value)[][] entities) { var text = new StringBuilder(); foreach (var entity in entities) { text.Append("{\n"); foreach (var (key, value) in entity) text.Append('"').Append(key).Append("\" \"").Append(value).Append("\"\n"); text.Append("}\n"); } return Lump(BspLump.Entities, [.. Encoding.UTF8.GetBytes(text.ToString()), 0]); } public byte[] Write() { using var stream = new MemoryStream(); using var writer = new BinaryWriter(stream); writer.Write("VBSP"u8); writer.Write(Version); writer.Write(new byte[BspLump.Count * 16 + 4]); var entries = new (int Offset, int Length, int Version)[BspLump.Count]; for (var i = 0; i < BspLump.Count; i++) { if (i == BspLump.GameLump) { var start = (int)stream.Position; writer.Write(_gameLumps.Count); var table = (int)stream.Position; writer.Write(new byte[_gameLumps.Count * 16]); var placed = new List<(int Offset, int Length)>(); foreach (var lump in _gameLumps) { placed.Add(((int)stream.Position, lump.Data.Length)); writer.Write(lump.Data); } var end = stream.Position; stream.Position = table; for (var g = 0; g < _gameLumps.Count; g++) { var id = Encoding.ASCII.GetBytes(_gameLumps[g].Id); writer.Write((uint)((id[0] << 24) | (id[1] << 16) | (id[2] << 8) | id[3])); writer.Write((ushort)(_gameLumps[g].Compressed ? 1 : 0)); writer.Write(_gameLumps[g].Version); writer.Write(placed[g].Offset); writer.Write(placed[g].Length); } stream.Position = end; entries[i] = (start, (int)end - start, 0); continue; } if (!_lumps.TryGetValue(i, out var data)) continue; while (stream.Position % 4 != 0) writer.Write((byte)0); entries[i] = ((int)stream.Position, data.Data.Length, data.Version); writer.Write(data.Data); } stream.Position = 8; foreach (var (offset, length, version) in entries) { writer.Write(length == 0 ? 0 : offset); writer.Write(length); writer.Write(version); writer.Write(0); } writer.Write(7); writer.Flush(); return stream.ToArray(); } public static void Vector(BinaryWriter writer, Vector3 vector) { writer.Write(vector.X); writer.Write(vector.Y); writer.Write(vector.Z); } private static byte[] Deflate(byte[] data) { using var output = new MemoryStream(); using (var deflate = new DeflateStream(output, CompressionLevel.Optimal, true)) { deflate.Write(data); } return output.ToArray(); } }