Repository navigation
Expand file tree
/
Copy pathPakFile.cs
More file actions
518 lines (440 loc) · 18.9 KB
/
Copy pathPakFile.cs
File metadata and controls
518 lines (440 loc) · 18.9 KB
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
using System.Text.RegularExpressions;
namespace bspPack;
class PakFile
{
// This class is the class responsible for building the list of files to include
// The list can be saved to a text file for use with bspzip
// the dictionary is formated as <internalPath, externalPath>
// matching the bspzip specification https://developer.valvesoftware.com/wiki/BSPZIP
private readonly IDictionary<string, string> Files;
private bool AddFile(string internalPath, string externalPath)
{
if (externalPath.Length > 256)
Message.Error($"File length over 256 characters, file may not pack properly:\n{externalPath}");
return AddFile(new KeyValuePair<string, string>(internalPath, externalPath));
}
// onFailure is for utility files such as nav, radar, etc which get excluded. if they are excluded, the Delegate is run. This is used for removing the files from the BSP class, so they dont appear in the summary at the end
private bool AddFile(KeyValuePair<string, string> paths, Action<BSP>? onExcluded = null, BSP? bsp = null)
{
var externalPath = paths.Value.Replace('\\', '/');
// exclude files that are excluded
if (externalPath != "" && File.Exists(externalPath)
&& !excludedFiles.Any(file => file.Equals(externalPath, StringComparison.OrdinalIgnoreCase))
&& !excludedDirs.Any(dir => externalPath.StartsWith(dir, StringComparison.OrdinalIgnoreCase))
&& !excludedVpkFiles.Any(vpkFile => vpkFile.Equals(paths.Key, StringComparison.OrdinalIgnoreCase)))
{
Files.Add(paths);
return true;
}
if (onExcluded != null && bsp != null)
{
onExcluded(bsp);
}
return false;
}
/// <summary>
/// Adds a generic file dependency and tries to determine file type by extension
/// </summary>
/// <param name="internalPath"></param>
/// <param name="externalPath"></param>
private void AddGenericFile(string internalPath, string externalPath)
{
FileInfo fileInfo = new(externalPath);
// try to determine file type by extension
switch (fileInfo.Extension)
{
case ".vmt":
AddTexture(internalPath);
break;
case ".pcf":
AddParticle(internalPath);
break;
case ".mdl":
AddModel(internalPath);
break;
case ".wav":
case ".mp3":
AddSound(internalPath);
break;
case ".res":
AddInternalFile(internalPath, externalPath);
foreach (string material in AssetUtils.FindResMaterials(externalPath))
AddTexture(material);
break;
case ".nut":
AddVScript(internalPath);
break;
default:
AddInternalFile(internalPath, externalPath);
break;
}
}
private bool AddFile(string externalPath)
{
if (!File.Exists(externalPath))
return false;
// try to get the source directory the file is located in
FileInfo fileInfo = new FileInfo(externalPath);
// default base directory is the game folder
string baseDir = Config.GameFolder;
var potentialSubDir = new List<string>(sourceDirs); // clone to prevent accidental modification
potentialSubDir.Remove(baseDir);
foreach (var folder in potentialSubDir)
{
if (fileInfo.Directory != null
&& fileInfo.Directory.FullName.Contains(folder, StringComparison.OrdinalIgnoreCase))
{
baseDir = folder;
break;
}
}
// check needed for when file does not exist in any sub directory or the base directory
if (fileInfo.Directory != null && !fileInfo.Directory.FullName.ToLower().Contains(baseDir.ToLower()))
{
return false;
}
string internalPath = Regex.Replace(externalPath, Regex.Escape(baseDir + Path.DirectorySeparatorChar), "", RegexOptions.IgnoreCase);
AddGenericFile(internalPath, externalPath);
return true;
}
private readonly List<string> excludedFiles;
private readonly List<string> excludedDirs;
private readonly List<string> excludedVpkFiles;
private readonly List<string> sourceDirs;
private readonly string fileName;
public int MdlCount { get; private set; }
public int VmtCount { get; private set; }
public int PcfCount { get; private set; }
public int SoundCount { get; private set; }
public int VehicleScriptCount { get; private set; }
public int EffectScriptCount { get; private set; }
public int VscriptCount { get; private set; }
public int PanoramaMapBackgroundCount { get; private set; }
private bool noSwvtx;
public PakFile(BSP bsp, List<string> sourceDirectories, List<string> includeFiles, List<string> excludedFiles, List<string> excludedDirs, List<string> excludedVpkFiles, string outputFile, bool noswvtx)
{
MdlCount = VmtCount = PcfCount = SoundCount = VehicleScriptCount = EffectScriptCount = PanoramaMapBackgroundCount = 0;
sourceDirs = sourceDirectories;
fileName = outputFile;
noSwvtx = noswvtx;
this.excludedFiles = excludedFiles;
this.excludedDirs = excludedDirs;
this.excludedVpkFiles = excludedVpkFiles;
Files = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
if (bsp.Nav.Key != default)
AddFile(bsp.Nav, b => b.Nav = default, bsp);
if (bsp.Detail.Key != default)
AddFile(bsp.Detail, b => b.Detail = default, bsp);
if (bsp.Kv.Key != default)
AddFile(bsp.Kv, b => b.Kv = default, bsp);
if (bsp.Txt.Key != default)
AddFile(bsp.Txt, b => b.Txt = default, bsp);
if (bsp.Jpg.Key != default)
AddFile(bsp.Jpg, b => b.Jpg = default, bsp);
if (bsp.Radartxt.Key != default)
AddFile(bsp.Radartxt, b => b.Radartxt = default, bsp);
if (bsp.RadarTablet.Key != default)
AddFile(bsp.RadarTablet, b => b.RadarTablet = default, bsp);
if (bsp.PanoramaMapIcon.Key != default)
{
AddFile(bsp.PanoramaMapIcon, b => b.PanoramaMapIcon = default, bsp);
}
if (bsp.ParticleManifest.Key != default)
{
if (AddFile(bsp.ParticleManifest, b => b.ParticleManifest = default, bsp))
{
foreach (string particle in AssetUtils.FindManifestPcfs(bsp.ParticleManifest.Value))
AddParticle(particle);
}
}
if (bsp.Soundscape.Key != default)
{
if (AddFile(bsp.Soundscape, b => b.Soundscape = default, bsp))
{
foreach (string sound in AssetUtils.FindSoundscapeSounds(bsp.Soundscape.Value))
AddSound(sound);
}
}
if (bsp.Soundscript.Key != default)
{
if (AddFile(bsp.Soundscript, b => b.Soundscript = default, bsp))
{
foreach (string sound in AssetUtils.FindSoundscapeSounds(bsp.Soundscript.Value))
AddSound(sound);
}
}
foreach (KeyValuePair<string, string> vehicleScript in bsp.VehicleScriptList)
if (AddInternalFile(vehicleScript.Key, vehicleScript.Value))
VehicleScriptCount++;
foreach (KeyValuePair<string, string> effectScript in bsp.EffectScriptList)
if (AddInternalFile(effectScript.Key, effectScript.Value))
EffectScriptCount++;
foreach (KeyValuePair<string, string> dds in bsp.Radardds)
AddInternalFile(dds.Key, dds.Value);
foreach (KeyValuePair<string, string> lang in bsp.Languages)
AddInternalFile(lang.Key, lang.Value);
foreach (string model in bsp.EntModelList)
AddModel(model);
for (int i = 0; i < bsp.ModelList.Count; i++)
AddModel(bsp.ModelList[i], bsp.ModelSkinList[i]);
foreach (string vmt in bsp.TextureList)
AddTexture(vmt);
foreach (string vmt in bsp.EntTextureList)
AddTexture(vmt);
foreach (string misc in bsp.MiscList)
AddInternalFile(misc, FindExternalFile(misc));
foreach (string sound in bsp.EntSoundList)
AddSound(sound);
foreach (string vscript in bsp.VscriptList)
AddVScript(vscript);
foreach (KeyValuePair<string, string> teamSelectionBackground in bsp.PanoramaMapBackgrounds)
if (AddInternalFile(teamSelectionBackground.Key, teamSelectionBackground.Value))
PanoramaMapBackgroundCount++;
foreach (var res in bsp.Res)
{
if (AddFile(res, null, bsp))
{
foreach (string material in AssetUtils.FindResMaterials(res.Value))
AddTexture(material);
}
}
// add all manually included files
foreach (var file in includeFiles)
{
if (!AddFile(file))
Message.Warning($"WARNING: Failed to resolve internal path for {file}, skipping\n");
}
}
public PakFile(List<string> sourceDirectories, List<string> includeFiles, List<string> excludedFiles, List<string> excludedDirs, List<string> excludedVpkFiles, string outputFile, bool noswvtx)
{
MdlCount = VmtCount = PcfCount = SoundCount = VehicleScriptCount = EffectScriptCount = PanoramaMapBackgroundCount = 0;
sourceDirs = sourceDirectories;
fileName = outputFile;
noSwvtx = noswvtx;
this.excludedFiles = excludedFiles;
this.excludedDirs = excludedDirs;
this.excludedVpkFiles = excludedVpkFiles;
Files = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
foreach (var file in includeFiles)
{
Message.Warning("ADDING FILES");
if (!AddFile(file))
Message.Warning($"WARNING: Failed to resolve internal path for {file}, skipping\n");
}
}
public void OutputToFile()
{
var outputLines = new List<string>();
foreach (KeyValuePair<string, string> entry in Files)
{
outputLines.Add(entry.Key);
outputLines.Add(entry.Value);
}
if (!Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BSPZipFiles")))
Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BSPZipFiles"));
if (File.Exists(fileName))
File.Delete(fileName);
File.WriteAllLines(fileName, outputLines);
}
public Dictionary<string, string> GetResponseFile()
{
var output = new Dictionary<string, string>();
foreach (var entry in Files)
{
output.Add(entry.Key, entry.Value.Replace(entry.Key, ""));
}
return output;
}
public bool AddInternalFile(string internalPath, string externalPath)
{
internalPath = internalPath.Replace("\\", "/");
// sometimes internal paths can be relative, ex. "materials/vgui/../hud/logos/spray.vmt" should be stored as "materials/hud/logos/spray.vmt".
internalPath = Regex.Replace(internalPath, @"\/.*\/\.\.", "");
if (!Files.ContainsKey(internalPath))
{
return AddFile(internalPath, externalPath);
}
return false;
}
public void AddModel(string internalPath, List<int>? skins = null)
{
// adds mdl files and finds its dependencies
string externalPath = FindExternalFile(internalPath);
if (AddInternalFile(internalPath, externalPath))
{
MdlCount++;
List<string> vtxMaterialNames = [];
foreach (string reference in AssetUtils.FindMdlRefs(internalPath))
{
string ext_path = FindExternalFile(reference);
//don't pack .sw.vtx files if param is set
if (reference.EndsWith(".sw.vtx") && noSwvtx)
continue;
AddInternalFile(reference, ext_path);
if (reference.EndsWith(".phy"))
foreach (string gib in AssetUtils.FindPhyGibs(ext_path))
AddModel(gib);
if (reference.EndsWith(".vtx"))
{
try
{
vtxMaterialNames.AddRange(AssetUtils.FindVtxMaterials(ext_path));
}
catch (Exception)
{
Message.Warning($"WARNING: Failed to find vtx materials for file {ext_path}");
}
}
}
Tuple<List<string>, List<string>> mdlMatsAndModels;
try
{
mdlMatsAndModels = AssetUtils.FindMdlMaterialsAndModels(externalPath, skins, vtxMaterialNames);
}
catch (Exception)
{
Message.Warning($"WARNING: Failed to read file {externalPath}");
return;
}
foreach (string mat in mdlMatsAndModels.Item1)
AddTexture(mat);
foreach (var model in mdlMatsAndModels.Item2)
AddModel(model, null);
}
}
public void AddTexture(string internalPath)
{
// adds vmt files and finds its dependencies
string externalPath = FindExternalFile(internalPath);
if (AddInternalFile(internalPath, externalPath))
{
VmtCount++;
foreach (string vtf in AssetUtils.FindVmtTextures(externalPath))
AddInternalFile(vtf, FindExternalFile(vtf));
foreach (string vmt in AssetUtils.FindVmtMaterials(externalPath))
AddTexture(vmt);
}
}
public void AddParticle(string internalPath)
{
// adds pcf files and finds its dependencies
string externalPath = FindExternalFile(internalPath);
if (externalPath == String.Empty)
{
Message.Warning($"WARNING: Failed to find particle manifest file {externalPath}");
return;
}
PCF? pcf = ParticleUtils.ReadParticle(externalPath);
if (AddInternalFile(internalPath, externalPath) && pcf != null)
{
PcfCount++;
foreach (string mat in pcf.MaterialNames)
AddTexture(mat);
foreach (string model in pcf.ModelNames)
AddModel(model);
}
else
{
Message.Warning($"WARNING: Failed to find particle manifest file {externalPath}");
return;
}
}
public void AddSound(string internalPath)
{
// adds vmt files and finds its dependencies
string externalPath = FindExternalFile(internalPath);
if (AddInternalFile(internalPath, externalPath))
{
SoundCount++;
}
}
/// <summary>
/// Adds VScript file and finds it's dependencies
/// </summary>
/// <param name="internalPath"></param>
public void AddVScript(string internalPath)
{
string externalPath = FindExternalFile(internalPath);
// referenced scripts don't always have extension, try adding .nut
if (externalPath == string.Empty)
{
var newInternalPath = $"{internalPath}.nut";
externalPath = FindExternalFile(newInternalPath);
// if we find the file with the .nut extension, update the internal path to include it
if (externalPath != string.Empty)
internalPath = newInternalPath;
}
if (!AddInternalFile(internalPath, externalPath))
{
Message.Warning($"WARNING: Failed to find VScript file {internalPath}\n");
return;
}
VscriptCount++;
var (vscripts, models, sounds, includedFiles, includedDirectories) = AssetUtils.FindVScriptDependencies(externalPath);
foreach (string vscript in vscripts)
AddVScript(vscript);
foreach (string model in models)
AddModel(model);
foreach (string sound in sounds)
AddSound(sound);
foreach (string internalDirectoryPath in includedDirectories)
{
var externalDirectoriesPaths = FindExternalDirectories(internalDirectoryPath);
if (externalDirectoriesPaths.Count == 0)
{
Message.Warning($"WARNING: Failed to resolve external path for VScript hint {internalDirectoryPath}, skipping\n");
continue;
}
foreach (var externalDirectoryPath in externalDirectoriesPaths)
{
var files = Directory.GetFiles(externalDirectoryPath, "*", SearchOption.AllDirectories);
foreach (var file in files)
{
AddFile(file);
}
}
}
foreach (string internalFilePath in includedFiles)
{
var externalFilePath = FindExternalFile(internalFilePath);
if (!File.Exists(externalFilePath))
{
Message.Warning($"WARNING: Failed to resolve external path for VScript hint {internalFilePath}, skipping\n");
continue;
}
AddGenericFile(internalFilePath, externalPath);
}
}
private string FindExternalFile(string internalPath)
{
// Attempts to find the file from the internalPath
// returns the externalPath or an empty string
var sanitizedPath = SanitizePath(internalPath);
foreach (string source in sourceDirs)
if (File.Exists(Path.Combine(source, sanitizedPath)))
return Path.Combine(source, sanitizedPath.Replace("\\", "/"));
return "";
}
private List<string> FindExternalDirectories(string internalPath)
{
// Attempts to find the directory from the internalPath
// returns the externalPath or null
// Note: unlike with files, the user can have several folders
// with matching internal names but with different contents
// spread across several custom folders
// If the user desires a folder override rather than a merge
// they can use -excludedir to exclude the unwanted folder
var sanitizedPath = SanitizePath(internalPath);
var externalDirs = new List<string>();
foreach (string source in sourceDirs)
if (Directory.Exists(Path.Combine(source, sanitizedPath)))
externalDirs.Add(Path.Combine(source, sanitizedPath.Replace("\\", "/")));
return externalDirs;
}
private static readonly string invalidChars = Regex.Escape(new string(Path.GetInvalidPathChars()));
private static readonly string invalidRegString = $@"([{invalidChars}]*\.+$)|([{invalidChars}]+)";
private string SanitizePath(string path)
{
return Regex.Replace(path, invalidRegString, "");
}
}