-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSP.cs
581 lines (491 loc) · 23.5 KB
/
BSP.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using ValveKeyValue;
namespace BSPPackStandalone
{
// this is the class that stores data about the bsp.
// You can find information about the file format here
// https://developer.valvesoftware.com/wiki/Source_BSP_File_Format#BSP_file_header
class CompressedBSPException : Exception { }
class BSP
{
private KeyValuePair<int, int>[] offsets; // offset/length
private static readonly char[] SpecialCaracters = { '*', '#', '@', '>', '<', '^', '(', ')', '}', '$', '!', '?', ' ' };
public List<Dictionary<string, string>> entityList { get; private set; }
public List<List<Tuple<string, string>>> entityListArrayForm { get; private set; }
public List<int>[] modelSkinList { get; private set; }
public List<string> ModelList { get; private set; }
public List<string> EntModelList { get; private set; }
public List<string> ParticleList { get; private set; }
public List<string> TextureList { get; private set; }
public List<string> EntTextureList { get; private set; }
public List<string> EntSoundList { get; private set; }
public List<string> MiscList { get; private set; }
// key/values as internalPath/externalPath
public KeyValuePair<string, string> particleManifest { get; set; }
public KeyValuePair<string, string> soundscript { get; set; }
public KeyValuePair<string, string> soundscape { get; set; }
public KeyValuePair<string, string> detail { get; set; }
public KeyValuePair<string, string> nav { get; set; }
public List<KeyValuePair<string, string>> res { get; } = new List<KeyValuePair<string, string>>();
public KeyValuePair<string, string> kv { get; set; }
public KeyValuePair<string, string> txt { get; set; }
public KeyValuePair<string, string> jpg { get; set; }
public KeyValuePair<string, string> radartxt { get; set; }
public List<KeyValuePair<string, string>> radardds { get; set; }
public KeyValuePair<string, string> RadarTablet { get; set; }
public List<KeyValuePair<string, string>> languages { get; set; }
public List<KeyValuePair<string, string>> VehicleScriptList { get; set; }
public List<KeyValuePair<string, string>> EffectScriptList { get; set; }
public List<string> vscriptList { get; set; }
public List<KeyValuePair<string, string>> PanoramaMapBackgrounds { get; set; }
public KeyValuePair<string, string> PanoramaMapIcon { get; set; }
public FileInfo file { get; private set; }
private bool isL4D2 = false;
private int bspVersion;
public BSP(FileInfo file)
{
this.file = file;
offsets = new KeyValuePair<int, int>[64];
using (var bsp = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var reader = new BinaryReader(bsp))
{
bsp.Seek(4, SeekOrigin.Begin); // skip header
this.bspVersion = reader.ReadInt32();
if (reader.ReadInt32() == 0 && this.bspVersion == 21)
isL4D2 = true;
bsp.Seek(-4, SeekOrigin.Current);
for (int i = 0; i < offsets.GetLength(0); i++)
{
if (isL4D2)
{
bsp.Seek(4, SeekOrigin.Current);
offsets[i] = new KeyValuePair<int, int>(reader.ReadInt32(), reader.ReadInt32());
bsp.Seek(4, SeekOrigin.Current);
}
else
{
offsets[i] = new KeyValuePair<int, int>(reader.ReadInt32(), reader.ReadInt32());
bsp.Seek(8, SeekOrigin.Current);
}
}
bsp.Seek(offsets[0].Key, SeekOrigin.Begin);
if (reader.ReadChars(4).SequenceEqual("LZMA".ToCharArray()))
{
throw new FormatException("BSP is compressed. Trying to decompress...");
}
buildEntityList(bsp, reader);
buildEntModelList();
buildModelList(bsp, reader);
buildParticleList();
buildEntTextureList();
buildTextureList(bsp, reader);
buildEntSoundList();
buildMiscList();
}
}
}
public void buildEntityList(FileStream bsp, BinaryReader reader)
{
entityList = new List<Dictionary<string, string>>();
entityListArrayForm = new List<List<Tuple<string, string>>>();
bsp.Seek(offsets[0].Key, SeekOrigin.Begin);
byte[] ent = reader.ReadBytes(offsets[0].Value);
List<byte> ents = new List<byte>();
const int LCURLY = 123;
const int RCURLY = 125;
const int NEWLINE = 10;
for (int i = 0; i < ent.Length; i++)
{
if (ent[i] == LCURLY && i + 1 < ent.Length)
{
// if curly isnt followed by newline assume its part of filename
if (ent[i + 1] != NEWLINE)
ents.Add(ent[i]);
}
if (ent[i] != LCURLY && ent[i] != RCURLY)
ents.Add(ent[i]);
else if (ent[i] == RCURLY)
{
// if curly isnt followed by newline assume its part of filename
if (i + 1 < ent.Length && ent[i + 1] != NEWLINE)
{
ents.Add(ent[i]);
continue;
}
string rawent = Encoding.ASCII.GetString(ents.ToArray());
Dictionary<string, string> entity = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
var entityArrayFormat = new List<Tuple<string, string>>();
// split on \n, ignore \n inside of quotes
foreach (string s in Regex.Split(rawent, "(?=(?:(?:[^\"]*\"){2})*[^\"]*$)\\n"))
{
if (s.Count() != 0)
{
// split on non escaped quotes
string[] c = Regex.Split(s, "(?<!\\\\)[\"\"]");
if (!entity.ContainsKey(c[1]))
entity.Add(c[1], c[3]);
entityArrayFormat.Add(Tuple.Create(c[1], c[3]));
}
}
entityList.Add(entity);
entityListArrayForm.Add(entityArrayFormat);
ents = new List<byte>();
}
}
}
public void buildTextureList(FileStream bsp, BinaryReader reader)
{
// builds the list of textures applied to brushes
string mapname = bsp.Name.Split('\\').Last().Split('.')[0];
TextureList = new List<string>();
bsp.Seek(offsets[43].Key, SeekOrigin.Begin);
TextureList = new List<string>(Encoding.ASCII.GetString(reader.ReadBytes(offsets[43].Value)).Split('\0'));
for (int i = 0; i < TextureList.Count; i++)
{
if (TextureList[i].StartsWith("/")) // materials in root level material directory start with /
//For some reason some texture names are converted to upper case in the bsp.
//Since linux pathnames are case sensitive the only solution I found so far is to normalize them to lower case
TextureList[i] = "materials" + TextureList[i].ToLower() + ".vmt";
else
TextureList[i] = "materials/" + TextureList[i].ToLower() + ".vmt";
}
// find skybox materials
Dictionary<string, string> worldspawn = entityList.FirstOrDefault(item => item["classname"] == "worldspawn", new Dictionary<string, string>());
if (worldspawn.ContainsKey("skyname"))
foreach (string s in new string[] { "", "bk", "dn", "ft", "lf", "rt", "up" })
{
TextureList.Add("materials/skybox/" + worldspawn["skyname"] + s + ".vmt");
TextureList.Add("materials/skybox/" + worldspawn["skyname"] + "_hdr" + s + ".vmt");
}
// find detail materials
if (worldspawn.ContainsKey("detailmaterial"))
TextureList.Add("materials/" + worldspawn["detailmaterial"] + ".vmt");
// find menu photos
TextureList.Add("materials/vgui/maps/menu_photos_" + mapname + ".vmt");
}
public void buildEntTextureList()
{
// builds the list of textures referenced in entities
List<string> materials = new List<string>();
HashSet<string> skybox_swappers = new HashSet<string>();
foreach (Dictionary<string, string> ent in entityList)
{
foreach (KeyValuePair<string, string> prop in ent)
{
if (Keys.vmfMaterialKeys.Contains(prop.Key.ToLower()))
{
materials.Add(prop.Value);
if (prop.Key.ToLower().StartsWith("team_icon"))
materials.Add(prop.Value + "_locked");
}
}
if(ent["classname"].Contains("skybox_swapper") && ent.ContainsKey("SkyboxName") )
{
if(ent.ContainsKey("targetname"))
{
skybox_swappers.Add(ent["targetname"].ToLower());
}
foreach (string s in new string[] { "", "bk", "dn", "ft", "lf", "rt", "up" })
{
materials.Add("skybox/" + ent["SkyboxName"] + s + ".vmt");
materials.Add("skybox/" + ent["SkyboxName"] + "_hdr" + s + ".vmt");
}
}
// special condition for sprites
if (ent["classname"].Contains("sprite") && ent.ContainsKey("model"))
materials.Add(ent["model"]);
// special condition for item_teamflag
if (ent["classname"].Contains("item_teamflag"))
{
if (ent.ContainsKey("flag_trail"))
{
materials.Add("effects/" + ent["flag_trail"]);
materials.Add("effects/" + ent["flag_trail"] + "_red");
materials.Add("effects/" + ent["flag_trail"] + "_blu");
}
if (ent.ContainsKey("flag_icon"))
{
materials.Add("vgui/" + ent["flag_icon"]);
materials.Add("vgui/" + ent["flag_icon"] + "_red");
materials.Add("vgui/" + ent["flag_icon"] + "_blu");
}
}
// special condition for env_funnel. Hardcoded to use sprites/flare6.vmt
if (ent["classname"].Contains("env_funnel"))
materials.Add("sprites/flare6.vmt");
// special condition for env_embers. Hardcoded to use particle/fire.vmt
if (ent["classname"].Contains("env_embers"))
materials.Add("particle/fire.vmt");
//special condition for func_dustcloud and func_dustmotes. Hardcoded to use particle/sparkles.vmt
if (ent["classname"].StartsWith("func_dust"))
materials.Add("particle/sparkles.vmt");
// special condition for vgui_slideshow_display. directory paramater references all textures in a folder (does not include subfolders)
if (ent["classname"].Contains("vgui_slideshow_display"))
{
if (ent.ContainsKey("directory"))
{
var directory = Path.Combine(Config.GameFolder, "materials", "vgui", ent["directory"]);
if (Directory.Exists(directory))
{
foreach (var file in Directory.GetFiles(directory))
{
if (file.EndsWith(".vmt"))
materials.Add($"/vgui/{ent["directory"]}/{Path.GetFileName(file)}");
}
}
}
}
}
// pack I/O referenced materials
// need to use array form of entity because multiple outputs with same command can't be stored in dict
foreach (var ent in entityListArrayForm)
{
foreach (var prop in ent)
{
var io = ParseIO(prop.Item2);
if (io == null)
continue;
var (target, command, parameter) = io;
switch (command) {
case "SetCountdownImage":
materials.Add($"vgui/{parameter}");
break;
case "Command":
// format of Command is <command> <parameter>
if(!parameter.Contains(' '))
{
continue;
}
(command, parameter) = parameter.Split(' ') switch { var param => (param[0], param[1])};
if (command == "r_screenoverlay")
materials.Add(parameter);
break;
case "AddOutput":
if(!parameter.Contains(' '))
{
continue;
}
string k, v;
(k,v) = parameter.Split(' ') switch { var a => (a[0], a[1])};
// support packing mats when using addoutput to change skybox_swappers
if(skybox_swappers.Contains(target.ToLower()) && k.ToLower() == "skyboxname")
{
foreach (string s in new string[] { "", "bk", "dn", "ft", "lf", "rt", "up" })
{
materials.Add("skybox/" + v + s + ".vmt");
materials.Add("skybox/" + v + "_hdr" + s + ".vmt");
}
}
break;
}
}
}
// format and add materials
EntTextureList = new List<string>();
foreach (string material in materials)
{
string materialpath = material;
if (!material.EndsWith(".vmt") && !materialpath.EndsWith(".spr"))
materialpath += ".vmt";
EntTextureList.Add("materials/" + materialpath);
}
}
public void buildModelList(FileStream bsp, BinaryReader reader)
{
// builds the list of models that are from prop_static
ModelList = new List<string>();
// getting information on the gamelump
int propStaticId = 0;
bsp.Seek(offsets[35].Key, SeekOrigin.Begin);
KeyValuePair<int, int>[] GameLumpOffsets = new KeyValuePair<int, int>[reader.ReadInt32()]; // offset/length
for (int i = 0; i < GameLumpOffsets.Length; i++)
{
if (reader.ReadInt32() == 1936749168)
propStaticId = i;
bsp.Seek(4, SeekOrigin.Current); //skip flags and version
GameLumpOffsets[i] = new KeyValuePair<int, int>(reader.ReadInt32(), reader.ReadInt32());
}
// reading model names from game lump
bsp.Seek(GameLumpOffsets[propStaticId].Key, SeekOrigin.Begin);
int modelCount = reader.ReadInt32();
for (int i = 0; i < modelCount; i++)
{
string model = Encoding.ASCII.GetString(reader.ReadBytes(128)).Trim('\0');
if (model.Length != 0)
ModelList.Add(model);
}
// from now on we have models, now we want to know what skins they use
// skipping leaf lump
int leafCount = reader.ReadInt32();
// bsp v25 uses ints instead of shorts for leaf lump
if (this.bspVersion == 25)
bsp.Seek(leafCount * sizeof(int), SeekOrigin.Current);
else
bsp.Seek(leafCount * sizeof(short), SeekOrigin.Current);
// reading staticprop lump
int propCount = reader.ReadInt32();
//dont bother if there's no props, avoid a dividebyzero exception.
if (propCount <= 0)
return;
long propOffset = bsp.Position;
int byteLength = GameLumpOffsets[propStaticId].Key + GameLumpOffsets[propStaticId].Value - (int)propOffset;
int propLength = byteLength / propCount;
modelSkinList = new List<int>[modelCount]; // stores the ids of used skins
for (int i = 0; i < modelCount; i++)
modelSkinList[i] = new List<int>();
for (int i = 0; i < propCount; i++)
{
bsp.Seek(i * propLength + propOffset + 24, SeekOrigin.Begin); // 24 skips origin and angles
int modelId = reader.ReadUInt16();
bsp.Seek(6, SeekOrigin.Current);
int skin = reader.ReadInt32();
if (modelSkinList[modelId].IndexOf(skin) == -1)
modelSkinList[modelId].Add(skin);
}
}
public void buildEntModelList()
{
// builds the list of models referenced in entities
EntModelList = new List<string>();
foreach (Dictionary<string, string> ent in entityList)
{
foreach (KeyValuePair<string, string> prop in ent)
{
if (ent["classname"].StartsWith("func"))
{
if (prop.Key == "gibmodel")
EntModelList.Add(prop.Value);
}
else if (!ent["classname"].StartsWith("trigger") &&
!ent["classname"].Contains("sprite"))
{
if (Keys.vmfModelKeys.Contains(prop.Key))
EntModelList.Add(prop.Value);
// item_sodacan is hardcoded to models/can.mdl
// env_beverage spawns item_sodacans
else if (prop.Value == "item_sodacan" || prop.Value == "env_beverage")
EntModelList.Add("models/can.mdl");
// tf_projectile_throwable is hardcoded to models/props_gameplay/small_loaf.mdl
else if (prop.Value == "tf_projectile_throwable")
EntModelList.Add("models/props_gameplay/small_loaf.mdl");
}
}
}
// pack I/O referenced models
// need to use array form of entity because multiple outputs with same command can't be stored in dict
foreach (var ent in entityListArrayForm)
{
foreach (var prop in ent)
{
var io = ParseIO(prop.Item2);
if (io == null)
continue;
var (target, command, parameter) = io;
if (command == "SetModel")
EntModelList.Add(parameter);
}
}
}
public void buildEntSoundList()
{
// builds the list of sounds referenced in entities
EntSoundList = new List<string>();
foreach (Dictionary<string, string> ent in entityList)
foreach (KeyValuePair<string, string> prop in ent)
{
if (Keys.vmfSoundKeys.Contains(prop.Key.ToLower()))
EntSoundList.Add("sound/" + prop.Value.Trim(SpecialCaracters));
}
// pack I/O referenced sounds
// need to use array form of entity because multiple outputs with same command can't be stored in dict
foreach (var ent in entityListArrayForm)
{
foreach (var prop in ent)
{
var io = ParseIO(prop.Item2);
if (io == null)
continue;
var (target, command, parameter) = io;
if (command == "PlayVO")
{
//Parameter value following PlayVO is always either a sound path or an empty string
if (!string.IsNullOrWhiteSpace(parameter))
EntSoundList.Add($"sound/{parameter}");
}
else if (command == "Command")
{
// format of Command is <command> <parameter>
if(!parameter.Contains(' '))
continue;
(command, parameter) = parameter.Split(' ') switch { var param => (param[0], param[1])};
if (command == "play" || command == "playgamesound" )
EntSoundList.Add($"sound/{parameter}");
}
}
}
}
// color correction, etc.
public void buildMiscList()
{
MiscList = new List<string>();
// find color correction files
foreach (Dictionary<string, string> cc in entityList.Where(item => item["classname"].StartsWith("color_correction")))
if (cc.ContainsKey("filename"))
TextureList.Add(cc["filename"]);
// pack I/O referenced TF2 upgrade files
// need to use array form of entity because multiple outputs with same command can't be stored in dict
foreach (var ent in entityListArrayForm)
{
foreach (var prop in ent)
{
var io = ParseIO(prop.Item2);
if (io == null) continue;
var (target, command, parameter) = io;
if (command.ToLower() != "setcustomupgradesfile") continue;
MiscList.Add(parameter);
}
}
}
public void buildParticleList()
{
ParticleList = new List<string>();
foreach (Dictionary<string, string> ent in entityList)
foreach (KeyValuePair<string, string> particle in ent)
if (particle.Key.ToLower() == "effect_name")
ParticleList.Add(particle.Value);
}
/// <summary>
/// Parses an IO string for the command and parameter. If the command is "AddOutput", it is parsed returns target, command, parameter
/// </summary>
/// <param name="property">Entity property</param>
/// <returns>Tuple containing (target, command, parameter)</returns>
private Tuple<string, string, string>? ParseIO(string property)
{
if (!property.Contains("\u001b")) return null;
var io = property.Split("\u001b");
if (io.Length != 5)
{
Console.WriteLine($"Failed to decode IO, ignoring: {property}");
return null;
}
var targetInput = io[1];
var parameter = io[2];
if (targetInput == "AddOutput" && parameter.Contains(':'))
{
var splitIo = parameter.Split(':');
if (splitIo.Length >= 3)
{
targetInput = splitIo[1];
parameter = splitIo[2];
}
}
return new Tuple<string, string, string>(io[0], targetInput, parameter);
}
}
}