Skip to content
0.2.0-dev Download ZIP
218 lines ·7.3 KB Raw
1using System.IO.Compression;
2using System.Numerics;
3using System.Text;
4using EvilEngine.Adapters.Source.Maps;
5
6namespace EvilEngine.Adapters.Tests.Support;
7
8internal sealed class BspWriter
9{
10 private readonly List<(string Id, ushort Version, byte[] Data, bool Compressed)> _gameLumps = [];
11 private readonly Dictionary<int, (byte[] Data, int Version)> _lumps = [];
12
13 public int Version { get; init; } = 20;
14
15 public static byte[] ValveLzma(byte[] stream, int size)
16 {
17 var data = new byte[17 + stream.Length - 5];
18 "LZMA"u8.CopyTo(data);
19 BitConverter.GetBytes(size).CopyTo(data, 4);
20 BitConverter.GetBytes(stream.Length - 5).CopyTo(data, 8);
21 stream.AsSpan(0, 5).CopyTo(data.AsSpan(12));
22 stream.AsSpan(5).CopyTo(data.AsSpan(17));
23 return data;
24 }
25
26 public static byte[] Zip(params (string Name, byte[] Data, int Method)[] entries)
27 {
28 using var zip = new MemoryStream();
29 using var writer = new BinaryWriter(zip);
30 var directory = new List<(string Name, int Method, int Packed, int Size, int Offset)>();
31 foreach (var (name, data, method) in entries)
32 {
33 var packed = method switch
34 {
35 0 => data,
36 8 => Deflate(data),
37 _ => data
38 };
39 var size = method == 14 ? BitConverter.ToInt32(packed, 0) : data.Length;
40 if (method == 14)
41 packed = packed[4..];
42 directory.Add((name, method, packed.Length, size, (int)zip.Position));
43 writer.Write(0x04034b50u);
44 writer.Write((ushort)20);
45 writer.Write((ushort)0);
46 writer.Write((ushort)method);
47 writer.Write(0u);
48 writer.Write(0u);
49 writer.Write(packed.Length);
50 writer.Write(size);
51 writer.Write((ushort)name.Length);
52 writer.Write((ushort)0);
53 writer.Write(Encoding.ASCII.GetBytes(name));
54 writer.Write(packed);
55 }
56
57 var start = (int)zip.Position;
58 foreach (var (name, method, packed, size, offset) in directory)
59 {
60 writer.Write(0x02014b50u);
61 writer.Write((ushort)20);
62 writer.Write((ushort)20);
63 writer.Write((ushort)0);
64 writer.Write((ushort)method);
65 writer.Write(0u);
66 writer.Write(0u);
67 writer.Write(packed);
68 writer.Write(size);
69 writer.Write((ushort)name.Length);
70 writer.Write((ushort)0);
71 writer.Write((ushort)0);
72 writer.Write((ushort)0);
73 writer.Write((ushort)0);
74 writer.Write(0u);
75 writer.Write(offset);
76 writer.Write(Encoding.ASCII.GetBytes(name));
77 }
78
79 var end = (int)zip.Position;
80 writer.Write(0x06054b50u);
81 writer.Write((ushort)0);
82 writer.Write((ushort)0);
83 writer.Write((ushort)directory.Count);
84 writer.Write((ushort)directory.Count);
85 writer.Write(end - start);
86 writer.Write(start);
87 writer.Write((ushort)0);
88 writer.Flush();
89 return zip.ToArray();
90 }
91
92 public static byte[] ZipLzma(byte[] stream, int size)
93 {
94 var data = new byte[4 + 4 + stream.Length];
95 BitConverter.GetBytes(size).CopyTo(data, 0);
96 data[4] = 9;
97 data[5] = 20;
98 BitConverter.GetBytes((ushort)5).CopyTo(data, 6);
99 stream.CopyTo(data, 8);
100 return data;
101 }
102
103 public BspWriter Lump(int index, byte[] data, int version = 0)
104 {
105 _lumps[index] = (data, version);
106 return this;
107 }
108
109 public BspWriter Records<T>(int index, IEnumerable<T> records, Action<BinaryWriter, T> write, int version = 0)
110 {
111 using var stream = new MemoryStream();
112 using var writer = new BinaryWriter(stream);
113 foreach (var record in records)
114 write(writer, record);
115 writer.Flush();
116 return Lump(index, stream.ToArray(), version);
117 }
118
119 public BspWriter GameLump(string id, ushort version, byte[] data, bool compressed = false)
120 {
121 _gameLumps.Add((id, version, data, compressed));
122 return this;
123 }
124
125 public BspWriter Entities(params (string Key, string Value)[][] entities)
126 {
127 var text = new StringBuilder();
128 foreach (var entity in entities)
129 {
130 text.Append("{\n");
131 foreach (var (key, value) in entity)
132 text.Append('"').Append(key).Append("\" \"").Append(value).Append("\"\n");
133 text.Append("}\n");
134 }
135
136 return Lump(BspLump.Entities, [.. Encoding.UTF8.GetBytes(text.ToString()), 0]);
137 }
138
139 public byte[] Write()
140 {
141 using var stream = new MemoryStream();
142 using var writer = new BinaryWriter(stream);
143 writer.Write("VBSP"u8);
144 writer.Write(Version);
145 writer.Write(new byte[BspLump.Count * 16 + 4]);
146 var entries = new (int Offset, int Length, int Version)[BspLump.Count];
147 for (var i = 0; i < BspLump.Count; i++)
148 {
149 if (i == BspLump.GameLump)
150 {
151 var start = (int)stream.Position;
152 writer.Write(_gameLumps.Count);
153 var table = (int)stream.Position;
154 writer.Write(new byte[_gameLumps.Count * 16]);
155 var placed = new List<(int Offset, int Length)>();
156 foreach (var lump in _gameLumps)
157 {
158 placed.Add(((int)stream.Position, lump.Data.Length));
159 writer.Write(lump.Data);
160 }
161
162 var end = stream.Position;
163 stream.Position = table;
164 for (var g = 0; g < _gameLumps.Count; g++)
165 {
166 var id = Encoding.ASCII.GetBytes(_gameLumps[g].Id);
167 writer.Write((uint)((id[0] << 24) | (id[1] << 16) | (id[2] << 8) | id[3]));
168 writer.Write((ushort)(_gameLumps[g].Compressed ? 1 : 0));
169 writer.Write(_gameLumps[g].Version);
170 writer.Write(placed[g].Offset);
171 writer.Write(placed[g].Length);
172 }
173
174 stream.Position = end;
175 entries[i] = (start, (int)end - start, 0);
176 continue;
177 }
178
179 if (!_lumps.TryGetValue(i, out var data))
180 continue;
181 while (stream.Position % 4 != 0)
182 writer.Write((byte)0);
183 entries[i] = ((int)stream.Position, data.Data.Length, data.Version);
184 writer.Write(data.Data);
185 }
186
187 stream.Position = 8;
188 foreach (var (offset, length, version) in entries)
189 {
190 writer.Write(length == 0 ? 0 : offset);
191 writer.Write(length);
192 writer.Write(version);
193 writer.Write(0);
194 }
195
196 writer.Write(7);
197 writer.Flush();
198 return stream.ToArray();
199 }
200
201 public static void Vector(BinaryWriter writer, Vector3 vector)
202 {
203 writer.Write(vector.X);
204 writer.Write(vector.Y);
205 writer.Write(vector.Z);
206 }
207
208 private static byte[] Deflate(byte[] data)
209 {
210 using var output = new MemoryStream();
211 using (var deflate = new DeflateStream(output, CompressionLevel.Optimal, true))
212 {
213 deflate.Write(data);
214 }
215
216 return output.ToArray();
217 }
218}