-
Notifications
You must be signed in to change notification settings - Fork 4
/
MapPacker.cs
316 lines (249 loc) · 8.14 KB
/
MapPacker.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
using ValveResourceFormat;
using SteamDatabase.ValvePak;
using System.IO.Enumeration;
if (args.Length == 0)
{
Console.WriteLine("Please specify a '.vpk' map file or folder.");
Console.ReadKey(true);
return;
}
if (Directory.Exists(args[0]))
{
PackVpkFromDirectory(args[0]);
return;
}
else if (!Path.GetExtension(args[0]).Contains(".vpk"))
{
Console.WriteLine("Invalid file specified.");
Console.ReadKey(true);
return;
}
var vpkFile = args[0];
var mod = Directory.GetParent(vpkFile);
var game = null as DirectoryInfo;
while (mod != null && mod.Name != "game")
{
if (mod.Name == "maps")
{
mod = mod.Parent;
game = mod;
while (game != null && game.Name != "game")
{
game = game.Parent;
}
break;
}
mod = mod.Parent;
}
if (mod is null || game is null)
{
Console.WriteLine("Map file is not inside the 'game/<mod_or_addon>/maps' folder. Please drag it from within that folder.");
Console.ReadKey();
return;
}
Console.WriteLine("Do you want to attempt to auto-pack the map vpk? (y/n)\n" +
"You can skip this if you want to add extra files like sounds manually.\n");
ConsoleKeyInfo key = Console.ReadKey(true);
bool doVpkPack = key.KeyChar == 'y';
// Backup the original vpk
if (doVpkPack)
{
File.Copy(vpkFile, vpkFile + "_unpacked", true);
}
var mapname = Path.GetFileNameWithoutExtension(vpkFile);
// Create a brand new folder to extract the map into.
// Since source2 doesn't like loose map files inside the maps folder, lets do this outside of the addon/mod
var mapPackFolder = Path.Combine(game.FullName, "csgo_addons", "mappacker", $"{mapname}")!;
if (Directory.Exists(mapPackFolder))
{
Directory.Delete(mapPackFolder, true);
}
var outPackage = new Package();
outPackage.Read(vpkFile);
var inPackageSet = new HashSet<string>();
ExtractPackage(outPackage, false);
if (!doVpkPack)
outPackage.Dispose();
var vmap_path = Path.Combine(mapPackFolder, "maps", $"{mapname}.vmap_c");
var vmap_c = new Resource();
vmap_c.Read(vmap_path);
var new_files_added = 0;
CopyExternalReferences(vmap_c);
vmap_c.Dispose();
// Radar
AddExtraFile($"/resource/overviews/{mapname}.txt");
AddExtraFile($"/panorama/images/overheadmaps/{mapname}_radar_tga.vtex_c");
// Loading screen images
for (int i = 1; i <= 10; i++)
AddExtraFile($"/panorama/images/map_icons/screenshots/1080p/{mapname}_{i}_png.vtex_c");
if (!doVpkPack)
{
// Can't repack the map, so just move the map folder next to the original vpk
Console.WriteLine($"\nDone! Isolated all found assets into game/csgo_addons/mappacker/{mapname}\n" +
"You may drag that folder onto this tool to pack.");
}
else
{
outPackage.Write(vpkFile + "_packed");
// Release the .vpk before overwriting it
outPackage.Dispose();
File.Move(vpkFile + "_packed", vpkFile, true);
Console.WriteLine($"\nDone! Packed {new_files_added} new files into {mapname}.");
Directory.Delete(mapPackFolder, true);
}
Console.ReadKey();
void PackVpkFromDirectory(string dirPath)
{
string vpkPath = dirPath + ".vpk";
Console.WriteLine($"Packing {dirPath} into a vpk...");
if (File.Exists(vpkPath))
File.Delete(vpkPath);
Package vpk = new Package();
var mapFiles = new FileSystemEnumerable<string>(
dirPath,
(ref FileSystemEntry entry) => entry.ToSpecifiedFullPath(),
new EnumerationOptions
{
RecurseSubdirectories = true,
}
);
uint fileCount = 0;
int vpkSize = 0;
foreach (var file in mapFiles)
{
if (!File.Exists(file))
continue;
var name = file[(dirPath.Length + 1)..];
var data = File.ReadAllBytes(file);
vpk.AddFile(name, data);
fileCount++;
vpkSize += data.Length;
}
vpk.Write(vpkPath);
vpk.Dispose();
Console.WriteLine($"Wrote {Path.GetFileName(vpkPath)} with {fileCount} files totalling {vpkSize} bytes.");
Console.ReadKey();
}
void ExtractPackage(Package package, bool vmapOnly)
{
foreach (var entries in package.Entries.Values)
{
foreach (var entry in entries)
{
var filePath = entry.GetFullPath();
inPackageSet.Add(filePath);
if (vmapOnly && !filePath.Contains("vmap_c"))
continue;
var extractFilePath = Path.Combine(mapPackFolder, filePath);
Directory.CreateDirectory(Path.GetDirectoryName(extractFilePath)!);
package.ReadEntry(entry, out var data);
File.WriteAllBytes(extractFilePath, data);
}
}
Console.WriteLine($"Extracted {package.FileName}");
}
void AddExtraFile(string refFileName)
{
var fullFilePath = mod.FullName + refFileName;
if (!File.Exists(fullFilePath))
{
Console.WriteLine($"Could not find '{refFileName}' in the mod folder.");
return;
}
Console.WriteLine($"Adding '{refFileName}'");
if (doVpkPack)
{
var data = File.ReadAllBytes(fullFilePath);
outPackage.AddFile(refFileName, data);
}
else
{
var newFileFullPath = mapPackFolder + refFileName;
Directory.CreateDirectory(Path.GetDirectoryName(newFileFullPath)!);
File.Copy(fullFilePath, newFileFullPath, true);
}
}
void AddExtraMap(string mapName)
{
var mapPath = Path.Combine("maps", mapName + ".vpk");
var mapFullPath = Path.Combine(mod.FullName, mapPath);
if (!File.Exists(mapFullPath))
{
Console.WriteLine($"Could not find {mapPath} in the mod folder.");
return;
}
Console.WriteLine($"Adding '{mapName}'");
var mapPackage = new Package();
mapPackage.Read(mapFullPath);
ExtractPackage(mapPackage, true);
mapPackage.Dispose();
var vmap_path = Path.Combine(mapPackFolder, "maps", $"{mapName}.vmap_c");
var vmap_c = new Resource();
vmap_c.Read(vmap_path);
CopyExternalReferences(vmap_c);
vmap_c.Dispose();
File.Delete(vmap_path);
if (doVpkPack)
{
var data = File.ReadAllBytes(mapFullPath);
outPackage.AddFile(mapPath, data);
}
else
{
var newMapFullPath = Path.Combine(mapPackFolder, "maps", mapName + ".vpk");
File.Copy(mapFullPath, newMapFullPath, true);
}
new_files_added++;
}
void CopyExternalReferences(Resource resource, int depth = 0)
{
if (resource.ExternalReferences is null)
return;
foreach (var resource_reference in resource.ExternalReferences.ResourceRefInfoList)
{
var refFileName = resource_reference.Name;
// lua vscripts are authored directly in the game folder, they don't get compiled by the engine
if (!refFileName.EndsWith(".lua"))
refFileName += "_c";
// Extra maps like skyboxes are handled separately, their vmap should not be packed
if (refFileName.EndsWith(".vmap_c"))
{
AddExtraMap(Path.GetFileNameWithoutExtension(refFileName));
continue;
}
if (refFileName.StartsWith('_') || inPackageSet.Contains(refFileName))
continue;
var fullRefFilePath = Path.Combine(mod.FullName, refFileName);
if (!File.Exists(fullRefFilePath))
{
Console.WriteLine($"Could not find '{refFileName}' in the mod folder.");
continue;
}
for (var i = 0; i < depth; i++)
Console.Write(" ");
Console.WriteLine($"Adding '{refFileName}'");
using var child_resource = new Resource();
if (!refFileName.EndsWith(".lua"))
{
try
{
child_resource.Read(fullRefFilePath);
} catch { }
CopyExternalReferences(child_resource, depth + 1);
}
// Copy the file to the map folder
var newFullFilePath = Path.Combine(mapPackFolder, refFileName);
if (File.Exists(newFullFilePath))
{
continue;
}
if (doVpkPack)
{
var data = File.ReadAllBytes(fullRefFilePath);
outPackage.AddFile(refFileName, data);
}
Directory.CreateDirectory(Path.GetDirectoryName(newFullFilePath)!);
File.Copy(fullRefFilePath, newFullFilePath);
new_files_added++;
}
}