-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
546 lines (444 loc) · 19.4 KB
/
Program.cs
File metadata and controls
546 lines (444 loc) · 19.4 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
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
using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using zlib;
namespace HatchPack {
class Program {
static List<string> compressMatches = new List<string> {
"*.txt", "*.json", "*.xml", "*.tmx", "*.glsl",
"*.wav",
"*.obj", "*.mtl",
"*.fbx", "*.dae", "*.hmdl",
"*.ttf"
};
static List<string> encryptMatches = new List<string> {
"*.ibc", "*.hcm",
"*.hscn"
};
static List<string> nameMatches = new List<string>();
static List<string> excludeMatches = new List<string>();
static bool Opt_Compress(List<string> args) {
if (args.Count == 0) {
return true;
}
compressMatches.Clear();
foreach (string arg in args) {
compressMatches.Add(arg);
}
return false;
}
static bool Opt_Encrypt(List<string> args) {
if (args.Count == 0) {
return true;
}
encryptMatches.Clear();
foreach (string arg in args) {
encryptMatches.Add(arg);
}
return false;
}
static bool Opt_Pack(List<string> args) {
if (args.Count == 0) {
return true;
}
nameMatches.Clear();
foreach (string arg in args) {
nameMatches.Add(arg);
}
return false;
}
static bool Opt_Exclude(List<string> args) {
if (args.Count == 0) {
return true;
}
excludeMatches.Clear();
foreach (string arg in args) {
excludeMatches.Add(arg);
}
return false;
}
static bool ParseOption(string option, List<string> args) {
switch (option) {
case "--compress":
case "-c":
Opt_Compress(args);
return true;
case "--encrypt":
case "-e":
Opt_Encrypt(args);
return true;
case "--pack":
Opt_Pack(args);
return true;
case "--exclude":
case "-x":
Opt_Exclude(args);
return true;
default:
Console.WriteLine("Unrecognized option " + option);
return false;
}
}
static bool ParseCommandLineArgs(List<string> args) {
for (int i = 0; i < args.Count;) {
if (args[i] == "--") {
args.RemoveAt(i);
return true;
}
else if (args[i].StartsWith("-") || args[i].StartsWith("--")) {
string option = args[i];
List<string> optionArgs = new List<string>();
args.RemoveAt(i);
while (i < args.Count) {
if (args[i].StartsWith("-")) {
break;
}
optionArgs.Add(args[i]);
args.RemoveAt(i);
}
if (!ParseOption(option, optionArgs)) {
return false;
}
}
else {
i++;
}
}
return true;
}
static UInt32 CRC32_EncryptString(string message) {
int i, j;
UInt32 bytee, crc, mask;
i = 0;
crc = 0xFFFFFFFF;
while (i < message.Length) {
bytee = message[i];
crc = crc ^ bytee;
for (j = 7; j >= 0; j--) {
mask = 0xFFFFFFFF * (crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i++;
}
return ~crc;
}
static UInt32 CRC32_EncryptData(byte[] data) {
int i, j;
UInt32 bytee, crc, mask;
i = 0;
crc = 0xFFFFFFFF;
while (i < data.Length) {
bytee = data[i];
crc = crc ^ bytee;
for (j = 7; j >= 0; j--) {
mask = 0xFFFFFFFF * (crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i++;
}
return ~crc;
}
static void CryptoXOR(ref byte[] data, string filename, bool dec) {
byte[] keyA = new byte[16];
byte[] keyB = new byte[16];
UInt64 fileSize = (UInt64)data.Length;
UInt32 filenameHash = CRC32_EncryptString(filename);
UInt32 sizeHash = CRC32_EncryptData(BitConverter.GetBytes(fileSize));
byte[] filenameHashBytes = BitConverter.GetBytes(filenameHash);
byte[] sizeHashBytes = BitConverter.GetBytes(sizeHash);
// Populate Key A
for (var i = 0; i < 16; i++) {
keyA[i] = filenameHashBytes[i & 3];
}
// Populate Key B
for (var i = 0; i < 16; i++) {
keyB[i] = sizeHashBytes[i & 3];
}
bool swapNibbles = false;
int indexKeyA = 0;
int indexKeyB = 8;
int xorValue = (int)((fileSize / 4) & 0x7F);
for (uint x = 0; x < fileSize; x++) {
int temp = data[x];
if (dec)
temp ^= xorValue ^ keyB[indexKeyB++];
else
temp ^= keyA[indexKeyA++];
if (swapNibbles)
temp = (((temp & 0x0F) << 4) | ((temp & 0xF0) >> 4));
if (!dec)
temp ^= xorValue ^ keyB[indexKeyB++];
else
temp ^= keyA[indexKeyA++];
data[x] = (byte)temp;
if (indexKeyA <= 15) {
if (indexKeyB > 12) {
indexKeyB = 0;
swapNibbles = !swapNibbles;
}
}
else if (indexKeyB <= 8) {
indexKeyA = 0;
swapNibbles = !swapNibbles;
}
else {
xorValue = (xorValue + 2) & 0x7F;
if (swapNibbles) {
swapNibbles = false;
indexKeyA = xorValue % 7;
indexKeyB = (xorValue % 12) + 2;
}
else {
swapNibbles = true;
indexKeyA = (xorValue % 12) + 3;
indexKeyB = xorValue % 7;
}
}
}
}
static string GetFilesizeString(UInt64 size) {
float sizeDecimal = size;
if (size >= 1024 * 1024 * 1024) {
sizeDecimal /= 1024 * 1024 * 1024;
return $"{sizeDecimal:F2} GiB";
}
else if (size >= 1024 * 1024) {
sizeDecimal /= 1024 * 1024;
return $"{sizeDecimal:F2} MiB";
}
else if (size >= 1024) {
sizeDecimal /= 1024;
return $"{sizeDecimal:F2} KiB";
}
return size + " bytes";
}
static List<string> GetFileList(string resourcesFolder) {
if (nameMatches.Count == 0 && excludeMatches.Count == 0) {
string[] filePaths = Directory.GetFiles(resourcesFolder, "*.*", SearchOption.AllDirectories);
return new List<string>(filePaths);
}
List<string> filesToPack = new List<string>();
foreach (string pattern in nameMatches) {
string[] filePaths = Directory.GetFiles(resourcesFolder, pattern, SearchOption.AllDirectories);
foreach (string file in filePaths) {
string realPath = file.Substring(resourcesFolder.Length).Replace('\\', '/');
if (excludeMatches.Any(match => realPath.WildcardMatch(match))) {
Console.WriteLine("Excluding file " + realPath);
continue;
}
filesToPack.Add(file);
}
}
return filesToPack;
}
static bool PackHatchFile(string outFilename, string resourcesFolder) {
List<string> filesToPack;
try {
filesToPack = GetFileList(resourcesFolder);
if (filesToPack.Count == 0) {
Console.WriteLine("No files to pack");
return false;
}
}
catch (System.IO.DirectoryNotFoundException) {
Console.WriteLine("Directory '" + Path.GetFullPath(resourcesFolder) + "' not found");
return false;
}
catch (System.IO.IOException) {
Console.WriteLine("Invalid path '" + Path.GetFullPath(resourcesFolder) + "'");
return false;
}
UInt64 offsetGLOB = 0;
using (FileStream stream = new FileStream(outFilename, FileMode.Create)) {
stream.Write(new byte[] { 0x48, 0x41, 0x54, 0x43, 0x48 }, 0, 5); // HATCH
stream.Write(new byte[] { 0x01, 0x00, 0x00 }, 0, 3); // 1.0.0
if (filesToPack.Count > 65535) {
Console.WriteLine("Too many files to pack (Count is " + filesToPack.Count + ", maximum is 65535)");
return false;
}
stream.WriteByte((byte)(filesToPack.Count & 0xFF));
stream.WriteByte((byte)(filesToPack.Count >> 8 & 0xFF));
UInt64 tocEnd = (UInt64)(stream.Position + 32 * filesToPack.Count);
UInt64 compressedTotal = 0;
UInt64 uncompressedTotal = 0;
foreach (string file in filesToPack) {
string realPath = file.Substring(resourcesFolder.Length).Replace('\\', '/');
UInt32 hash = CRC32_EncryptString(realPath);
UInt64 offset = tocEnd + offsetGLOB;
byte[] fileBytes = File.ReadAllBytes(file);
bool needsCompression = compressMatches.Any(match => realPath.WildcardMatch(match));
bool needsEncryption = encryptMatches.Any(match => realPath.WildcardMatch(match));
UInt64 size = (UInt64)fileBytes.Length;
UInt32 dataType = 0;
UInt64 compressedSize = size;
// Print what file is going to be packed before compressing or encrypting it
Console.Write(hash.ToString("X8") + ": " + realPath + " ");
if (needsCompression || needsEncryption) {
Console.Write("...");
}
if (needsCompression) {
using (MemoryStream outMemoryStream = new MemoryStream())
using (ZOutputStream compress = new ZOutputStream(outMemoryStream, zlibConst.Z_BEST_COMPRESSION)) {
compress.Write(fileBytes, 0, fileBytes.Length);
compress.finish();
fileBytes = outMemoryStream.ToArray();
compressedSize = (UInt64)fileBytes.Length;
}
}
if (needsEncryption) {
CryptoXOR(ref fileBytes, realPath, false);
dataType = 2;
}
// Erase the ellipses
if (needsCompression || needsEncryption) {
Console.Write("\b\b\b");
}
Console.Write("(Size: " + GetFilesizeString(size));
if (needsCompression) {
Console.Write(", Compressed: " + GetFilesizeString(compressedSize));
}
if (needsEncryption) {
Console.Write(", Encrypted");
}
Console.WriteLine(")");
stream.Write(BitConverter.GetBytes(hash), 0, 4);
stream.Write(BitConverter.GetBytes(offset), 0, 8);
stream.Write(BitConverter.GetBytes(size), 0, 8);
stream.Write(BitConverter.GetBytes(dataType), 0, 4);
stream.Write(BitConverter.GetBytes(compressedSize), 0, 8);
Int64 mark = stream.Position;
stream.Seek((Int64)offset, SeekOrigin.Begin);
stream.Write(fileBytes, 0, (int)compressedSize);
stream.Seek(mark, SeekOrigin.Begin);
offsetGLOB += compressedSize;
uncompressedTotal += size;
compressedTotal += compressedSize;
}
Console.WriteLine("Packed " + filesToPack.Count + " files");
if (compressedTotal < uncompressedTotal) {
Console.WriteLine("Compressed " + GetFilesizeString(uncompressedTotal) + " to " + GetFilesizeString(compressedTotal));
}
}
return true;
}
[DllImport("kernel32.dll")]
static extern uint GetConsoleProcessList(uint[] processList, uint processCount);
// Attempt to detect whether the program was launched from a terminal or a double click
// From https://devblogs.microsoft.com/oldnewthing/20160125-00/?p=92922
static bool WasLaunchedFromTerminal() {
uint[] processList = new uint[1];
return GetConsoleProcessList(processList, 1) > 1;
}
static bool OpenResourcesFolderFileDialog(out string resourcesFolder) {
resourcesFolder = "";
using (CommonOpenFileDialog commonOpenFileDialog = new CommonOpenFileDialog()) {
commonOpenFileDialog.IsFolderPicker = true;
if (Properties.Settings1.Default.LastOpen != "")
commonOpenFileDialog.InitialDirectory = Properties.Settings1.Default.LastOpen;
if (commonOpenFileDialog.ShowDialog() == CommonFileDialogResult.Ok &&
!string.IsNullOrWhiteSpace(commonOpenFileDialog.FileName) &&
commonOpenFileDialog.FileName.Contains("Resources")) {
resourcesFolder = commonOpenFileDialog.FileName;
Properties.Settings1.Default.LastOpen = resourcesFolder;
Properties.Settings1.Default.Save();
return true;
}
}
return false;
}
static bool OpenOutputFileDialog(string resourcesFolder, out string outFilename) {
outFilename = "";
using (SaveFileDialog saveFileDialog = new SaveFileDialog()) {
saveFileDialog.InitialDirectory = resourcesFolder;
if (Properties.Settings1.Default.LastSave != "")
saveFileDialog.InitialDirectory = Properties.Settings1.Default.LastSave;
saveFileDialog.FileName = "Data.hatch";
saveFileDialog.Filter = "Hatch Data Pack (*.hatch)|*.hatch";
saveFileDialog.FilterIndex = 2;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK) {
outFilename = saveFileDialog.FileName;
Properties.Settings1.Default.LastSave = Directory.GetParent(outFilename).FullName;
Properties.Settings1.Default.Save();
return true;
}
}
return false;
}
static void PrintUsage() {
Console.Write("usage: hatchpack ");
Console.Write("[--compress | -c file...] ");
Console.Write("[--encrypt | -e file...] ");
Console.Write("[--pack file...] ");
Console.Write("[--exclude | -x file...] ");
Console.Write("[-h | --help] ");
Console.Write("input_dir output_file");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Positional arguments:");
Console.WriteLine(" input_dir The path to a directory containing the resources to pack.");
Console.WriteLine(" output_file The path to the output file.");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" --compress, -c A list of files to compress. Supports wildcards.");
Console.WriteLine(" Default: " + string.Join(", ", compressMatches));
Console.WriteLine(" --encrypt, -e A list of files to encrypt. Supports wildcards.");
Console.WriteLine(" Default: " + string.Join(", ", encryptMatches));
Console.WriteLine(" --pack A list of files to pack. Supports wildcards.");
Console.WriteLine(" By default, all files in the input directory are packed.");
Console.WriteLine(" --exclude, -x A list of files to exclude from packing. Supports wildcards.");
Console.WriteLine(" -h, --help Show this message and exit.");
}
[STAThread]
static int Main(string[] args) {
if (args.Length == 0 || args.Any(match => match == "-h" || match == "--help")) {
PrintUsage();
return 1;
}
string outFilename = "", resourcesFolder = "";
List<string> cmdLineArgs = new List<string>(args);
if (!ParseCommandLineArgs(cmdLineArgs)) {
return 1;
}
if (cmdLineArgs.Count >= 1 && !cmdLineArgs[0].StartsWith("-"))
resourcesFolder = cmdLineArgs[0];
if (cmdLineArgs.Count >= 2 && !cmdLineArgs[1].StartsWith("-"))
outFilename = cmdLineArgs[1];
if (!WasLaunchedFromTerminal()) {
if (resourcesFolder == "") {
OpenResourcesFolderFileDialog(out resourcesFolder);
}
if (resourcesFolder != "" && outFilename == "") {
OpenOutputFileDialog(resourcesFolder, out outFilename);
}
}
if (resourcesFolder == "" || outFilename == "") {
Console.WriteLine("Missing argument");
PrintUsage();
return 1;
}
resourcesFolder = resourcesFolder.Replace('\\', '/');
if (resourcesFolder[resourcesFolder.Length - 1] != '/')
resourcesFolder += "/";
if (!PackHatchFile(outFilename, resourcesFolder)) {
return 1;
}
return 0;
}
}
public static class StringExtensions {
public static bool WildcardMatch(this string text, string pattern) {
Regex regex = new("^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$",
RegexOptions.IgnoreCase | RegexOptions.Singleline);
return regex.IsMatch(text);
}
}
}