-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameResources.cs
509 lines (460 loc) · 15.1 KB
/
GameResources.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
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEngine.Profiling;
using System.Buffers;
using UnityEngine.Pool;
namespace Chisel.Import.Source.VPKTools
{
public class GameEntry
{
public string keyname;
public string extension;
public string name;
public string sourceFilename;
public VPKEntry entry;
}
public class GameResources
{
// TODO: make this work well with resource lookups in packages (lets avoid duplication of functionality)
readonly Dictionary<string, GameEntry> lookup = new();
public IEnumerable<string> GetEntryNames() { return lookup.Keys; }
public GameEntry GetEntry(string entryName, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
PackagePath.CleanFullPath(ref entryName);
if (string.IsNullOrEmpty(entryName)) return null;
if (lookup.TryGetValue(entryName, out var entry))
return entry;
if (searchPaths == null)
return null;
foreach(var searchPath in searchPaths)
{
if (lookup.TryGetValue(PackagePath.Combine(searchPath, entryName), out entry))
return entry;
}
return null;
}
public bool LoadFileAsStream(GameEntry entry, VPKParser.FileLoadDelegate streamLoadDelegate)
{
if (entry == null)
return false;
if (streamLoadDelegate == null) throw new NullReferenceException(nameof(streamLoadDelegate));
if (entry.sourceFilename.EndsWith($".{PackagePath.ExtensionVPK}"))
{
VPKResource resource = LoadResource(entry.sourceFilename);
if (resource == null)
return false;
return resource.LoadFileAsStream(entry.keyname, streamLoadDelegate);
}
using FileStream file = new(entry.sourceFilename, FileMode.Open);
return streamLoadDelegate.Invoke(file);
}
public string LoadText(string entryName, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
GameEntry entry = GetEntry(entryName, searchPaths);
if (entry == null)
return null;
return LoadText(entry);
}
public string LoadText(GameEntry entry)
{
if (entry == null) return null;
string sourceText = null;
if (!LoadFileAsStream(entry, delegate (Stream stream)
{
sourceText = TextParser.LoadStreamAsString(stream);
return true;
})) return null;
return sourceText;
}
public string ImportFile(string entryName, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
GameEntry entry = GetEntry(entryName, searchPaths);
if (entry == null)
return null;
string outputPath = null;
if (!LoadFileAsStream(entry, delegate (Stream stream)
{
outputPath = PackagePath.GetOutputPath(entry.keyname);
if (!File.Exists(outputPath))
{
PackagePath.EnsureDirectoriesExist(outputPath);
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
File.WriteAllBytes(outputPath, bytes);
var assetPath = PackagePath.GetAssetPath(entry.keyname);
AssetDatabase.ImportAsset(assetPath);
}
return true;
})) return null;
return outputPath;
}
public VTF LoadVTF(string entryName, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
PackagePath.EnsureExtension(ref entryName, PackagePath.ExtensionVTF);
if (searchPaths == null)
searchPaths = PackagePath.DefaultMaterialPaths;
GameEntry entry = GetEntry(entryName, searchPaths);
if (entry == null)
return null;
return LoadVTF(entry);
}
public VTF LoadVTF(GameEntry entry)
{
if (entry == null) return null;
VTF sourceTexture = null;
if (!LoadFileAsStream(entry, delegate (Stream stream)
{
sourceTexture = VTF.Read(stream);
return true;
})) return null;
return sourceTexture;
}
public Texture2D[] ImportVTF(string entryName, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
PackagePath.EnsureExtension(ref entryName, PackagePath.ExtensionVTF);
if (searchPaths == null)
searchPaths = PackagePath.DefaultMaterialPaths;
GameEntry entry = GetEntry(entryName, searchPaths);
if (entry == null)
return null;
if (entry.extension != PackagePath.ExtensionVTF)
return null;
var sourceTexture = LoadVTF(entry);
if (sourceTexture == null)
return null;
var outputPath = PackagePath.GetOutputPath(entry.keyname);
return Texture2DImporter.Import(sourceTexture, outputPath);
}
public VMT LoadVMT(string entryName, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
PackagePath.EnsureExtension(ref entryName, PackagePath.ExtensionVMT);
if (searchPaths == null)
searchPaths = PackagePath.DefaultMaterialPaths;
GameEntry entry = GetEntry(entryName, searchPaths);
if (entry == null)
return null;
return LoadVMT(entry);
}
public VMT LoadVMT(GameEntry entry)
{
if (entry == null) return null;
VMT sourceMaterial = null;
if (!LoadFileAsStream(entry, delegate (Stream stream)
{
sourceMaterial = VMT.Read(stream);
return true;
}))
{
return null;
}
return sourceMaterial;
}
public Material ImportVMT(string entryName, string[] searchPaths = null, bool isSprite = false)
{
if (string.IsNullOrEmpty(entryName)) return null;
PackagePath.EnsureExtension(ref entryName, PackagePath.ExtensionVMT);
if (searchPaths == null)
searchPaths = PackagePath.DefaultMaterialPaths;
GameEntry entry = GetEntry(entryName, searchPaths);
if (entry == null)
return null;
VMT sourceMaterial = LoadVMT(entry);
if (sourceMaterial == null)
return null;
var outputPath = PackagePath.GetOutputPath(entry.keyname);
return MaterialImporter.Import(this, sourceMaterial, outputPath, isSprite);
}
// any reference to a .spr file, is really a .vmt file
public VMT LoadSPR(string entryName, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
PackagePath.CleanFullPath(ref entryName);
if (entryName.EndsWith($".{PackagePath.ExtensionSPR}"))
entryName = Path.ChangeExtension(entryName, string.Empty);
return LoadVMT(entryName, searchPaths);
}
// any reference to a .spr file, is really a .vmt file
public Material ImportSPR(string entryName, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
PackagePath.CleanFullPath(ref entryName);
if (entryName.EndsWith($".{PackagePath.ExtensionSPR}"))
entryName = entryName.Remove(entryName.Length - (PackagePath.ExtensionSPR.Length + 1));
return ImportVMT(entryName, searchPaths, isSprite: true);
}
public VTX LoadVTX(string entryName, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
PackagePath.EnsureExtension(ref entryName, PackagePath.ExtensionVTX);
GameEntry entry = GetEntry(entryName, searchPaths);
if (entry == null)
return null;
return LoadVTX(entry);
}
public VTX LoadVTX(GameEntry entry)
{
if (entry == null) return null;
if (entry.extension != PackagePath.ExtensionVTX) return null;
VTX header = null;
if (!LoadFileAsStream(entry, delegate (Stream stream)
{
using var reader = new BinaryReader(stream);
header = VTX.Read(reader);
return true;
})) return null;
return header;
}
public VVD LoadVVD(string entryName, MDL mdlHeader, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
PackagePath.EnsureExtension(ref entryName, PackagePath.ExtensionVVD);
GameEntry entry = GetEntry(entryName, searchPaths);
if (entry == null)
return null;
return LoadVVD(entry, mdlHeader);
}
public VVD LoadVVD(GameEntry entry, MDL mdlHeader)
{
if (entry == null) return null;
if (entry.extension != PackagePath.ExtensionVVD) return null;
VVD header = null;
if (!LoadFileAsStream(entry, delegate (Stream stream)
{
using var reader = new BinaryReader(stream);
header = VVD.Load(reader, mdlHeader);
return true;
})) return null;
return header;
}
public MDL LoadMDL(string entryName, Lookup _lookup, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
PackagePath.EnsureExtension(ref entryName, PackagePath.ExtensionMDL);
GameEntry entry = GetEntry(entryName, searchPaths);
if (entry == null)
return null;
return LoadMDL(entry, _lookup);
}
public MDL LoadMDL(GameEntry entry, Lookup lookup)
{
if (entry == null) return null;
if (entry.extension != PackagePath.ExtensionMDL) return null;
MDL header = null;
try
{
if (!LoadFileAsStream(entry, delegate (Stream stream)
{
using var reader = new BinaryReader(stream);
header = MDL.Read(reader, this, lookup);
return true;
})) return null;
}
catch (Exception ex)
{
Debug.LogException(ex);
}
return header;
}
public SourceModel LoadSourceModel(string entryName, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
var lookup = new Lookup(); // TODO: get rid of this?
var mdlEntry = GetEntry(entryName, searchPaths);
var mdl = mdlEntry != null ? LoadMDL(mdlEntry, lookup) : null;
if (mdl == null)
{
Debug.LogError($"Failed to load mdlHeader {entryName}");
return null;
}
var vvdHeaderName = Path.ChangeExtension(entryName, PackagePath.ExtensionVVD);
var vvdEntry = GetEntry(vvdHeaderName);
var vvd = (vvdEntry != null) ? LoadVVD(vvdEntry, mdl) : null;
if (vvd == null)
{
Debug.LogError($"Failed to load vvdHeader {vvdHeaderName}");
return null;
}
var vtxHeaderName = Path.ChangeExtension(entryName, PackagePath.ExtensionVTX_DX90);
var vtxEntry = GetEntry(vtxHeaderName);
var vtx = (vtxEntry != null) ? LoadVTX(vtxEntry) : null;
if (vtx == null)
{
Debug.LogError($"Failed to load vtxHeader {vtxHeaderName}");
return null;
}
for (var i = 0; i < mdl.TextureDirs.Length; i++)
{
mdl.TextureDirs[i] =
PackagePath.CleanDirectory(mdl.TextureDirs[i]);
}
return new SourceModel
{
MDL = mdl,
VVD = vvd,
VTX = vtx,
MDLEntry = mdlEntry,
VVDEntry = vvdEntry,
VTXEntry = vtxEntry
};
}
public GameObject ImportModel(string entryName, string[] searchPaths = null)
{
if (string.IsNullOrEmpty(entryName)) return null;
var sourceModel = LoadSourceModel(entryName, searchPaths);
if (sourceModel == null)
return null;
var outputPath = PackagePath.GetOutputPath(sourceModel.MDLEntry.keyname);
return ModelImporter.Import(this, sourceModel, outputPath, skin: 0);
}
public T Import<T>(GameEntry entry) where T : UnityEngine.Object
{
if (entry == null) return null;
return Import<T>(entry.keyname);
}
public T Import<T>(string entryName, string[] searchPaths = null) where T : UnityEngine.Object
{
if (string.IsNullOrEmpty(entryName)) return null;
var extension = PackagePath.GetExtension(entryName);
switch (extension)
{
case PackagePath.ExtensionVMT: return ImportVMT(entryName, searchPaths) as T;
case PackagePath.ExtensionVTF: var textures = ImportVTF(entryName, searchPaths); return (textures == null || textures.Length == 0) ? null : textures[0] as T;
case PackagePath.ExtensionMDL: return ImportModel(entryName, searchPaths) as T;
default:
{
var outputPath = ImportFile(entryName, searchPaths);
if (outputPath == null) return null;
var assetPath = PackagePath.GetAssetPath(entryName);
return AssetDatabase.LoadAssetAtPath<T>(assetPath);
}
}
}
readonly Dictionary<string, VPKResource> resourceLookup = new();
public VPKResource LoadResource(string resourceFilename)
{
if (!resourceLookup.TryGetValue(resourceFilename, out var resource))
{
if (File.Exists(resourceFilename))
resource = new VPKResource(resourceFilename);
else
resource = null;
resourceLookup[resourceFilename] = resource;
}
if (resource == null) { Debug.LogError($"Could not find {resourceFilename}"); }
return resource;
}
public void InitializePaths(string[] inputPaths)
{
var rentedCharList = ArrayPool<char>.Shared.Rent(255);
var vpkFiles = HashSetPool<string>.Get();
try
{
foreach (var path in inputPaths)
{
Profiler.BeginSample("Directory.GetFiles");
var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
Profiler.EndSample();
if (files.Length == 0)
continue;
Profiler.BeginSample("EnsureCapacity");
lookup.EnsureCapacity(files.Length);
vpkFiles.Clear();
vpkFiles.EnsureCapacity(files.Length);
Profiler.EndSample();
foreach (var filename in files)
{
if (string.IsNullOrEmpty(filename))
continue;
if (filename.EndsWith($".{PackagePath.ExtensionVPK}"))
{
Profiler.BeginSample("vpkFiles.Add");
if (filename.EndsWith(PackagePath.ExtensionVPK_Dir))
vpkFiles.Add(filename);
Profiler.EndSample();
continue;
}
var relativefilename = filename.Substring(path.Length);
var keyname = relativefilename;
PackagePath.CleanFullPath(ref keyname);
if (lookup.ContainsKey(keyname))
continue;
PackagePath.DecomposePath(relativefilename, out string directory, out string name, out string extension);
Profiler.BeginSample("lookup");
var entry = new GameEntry()
{
keyname = keyname,
extension = extension,
name = name,
sourceFilename = relativefilename,
entry = new VPKEntry { }
};
lookup[keyname] = entry;
Profiler.EndSample();
}
Profiler.BeginSample("LoadVPKFiles");
foreach (var vpkFile in vpkFiles)
{
try
{
// TODO: do we really need this log?
Profiler.BeginSample("new VPKArchive");
var logFileName = $"{Application.dataPath}\\{Path.GetFileNameWithoutExtension(vpkFile)}_log.txt";
var archive = new VPKArchive(vpkFile, logFileName, 2);
Profiler.EndSample();
Profiler.BeginSample("archive.GetEntries");
var archiveEntries = archive.GetEntries();
Profiler.EndSample();
if (archiveEntries.Count == 0)
continue;
Profiler.BeginSample("EnsureCapacity");
lookup.EnsureCapacity(archiveEntries.Count);
Profiler.EndSample();
Profiler.BeginSample("archiveEntries Iteration");
foreach (var item in archiveEntries)
{
var keyname = item.Key;
PackagePath.CleanFullPath(ref keyname);
if (lookup.ContainsKey(keyname))
continue;
if (lookup.ContainsKey(keyname))
continue;
PackagePath.DecomposePath(keyname, out string directory, out string name, out string extension);
Profiler.BeginSample("lookup");
var entry = new GameEntry()
{
keyname = keyname,
extension = extension,
name = name,
sourceFilename = vpkFile,
entry = item.Value
};
lookup[keyname] = entry;
Profiler.EndSample();
}
Profiler.EndSample();
}
catch (System.Exception ex)
{
Debug.LogException(ex);
}
}
Profiler.EndSample();
}
}
finally
{
ArrayPool<char>.Shared.Return(rentedCharList);
HashSetPool<string>.Release(vpkFiles);
}
}
}
}