forked from AntonioDePau/KHPCPatchManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKHPCPatchManager.cs
658 lines (594 loc) · 25.2 KB
/
KHPCPatchManager.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Ionic.Zlib;
using Ionic.Zip;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
public class MyBackgroundWorker : BackgroundWorker{
public string PKG;
}
public class ListBoxItem{
public string ShortName {get; set;}
public string Path {get; set;}
public ListBoxItem(string a, string b){
ShortName = a;
Path = b;
}
}
namespace OpenKh.Egs{
public class ZipManager{
public static List<ZipFile> ZipFiles {get{return KHPCPatchManager.ZipFiles;}}
private static bool ZipDirectoryExists(string dir){
return ZipFiles.Find(x => x.SelectEntries(Path.Combine(dir, "*")).Count > 0) != null;
}
public static bool ZipFileExists(string file){
return ZipFiles.Find(x => x.ContainsEntry(file)) != null;
}
public static bool DirectoryExists(string dir){
return ZipDirectoryExists(dir) || Directory.Exists(dir);
}
public static bool FileExists(string file){
return ZipFileExists(file) || File.Exists(file);
}
public static IEnumerable<string> GetFiles(string folder){
if(ZipDirectoryExists(folder)){
List<string> foundFiles = new List<string>();
ZipFiles.ForEach(x => {
ICollection<ZipEntry> entries = x.SelectEntries(Path.Combine(folder, "*"));
foreach(var entry in entries){
string filename = entry.FileName.Replace(folder.Replace(@"\", "/") + "/", "");
if(!entry.IsDirectory && !foundFiles.Contains(filename)){
foundFiles.Add(filename);
}
}
});
return foundFiles;
}else if(Directory.Exists(folder)){
return Directory.EnumerateFiles(folder, "*.*", SearchOption.AllDirectories)
.Select(x => x.Replace($"{folder}\\", "")
.Replace(@"\", "/"));
}
return Enumerable.Empty<string>();
}
public static byte[] FileReadAllBytes(string file){
if(ZipFileExists(file)){
ZipEntry entry = null;
foreach(var zipFile in ZipFiles){
var entries = zipFile.SelectEntries(file).Where(y => !y.IsDirectory);
if(entries.FirstOrDefault() != null){
entry = entries.FirstOrDefault();
using (var stream = entry.OpenReader()){
var bytes = new byte[entry.UncompressedSize];
stream.Read(bytes, 0, (int)entry.UncompressedSize);
return bytes;
}
}
};
}else if(File.Exists(file)){
return File.ReadAllBytes(file);
}
return new byte[0];
}
public static string[] FileReadAllLines(string file){
if(ZipFileExists(file)){
byte[] bytes = FileReadAllBytes(file);
string text = System.Text.Encoding.ASCII.GetString(bytes);
return text.Split(
new string[] { Environment.NewLine },
StringSplitOptions.None
);
}else if(File.Exists(file)){
return File.ReadAllLines(file);
}
return new string[0];
}
public static Stream FileReadStream(string file){
if(ZipFileExists(file)){
byte[] bytes = FileReadAllBytes(file);
return new MemoryStream(bytes);
}else if(File.Exists(file)){
var stream = File.OpenRead(file);
return stream;
}
return new MemoryStream();
}
}
}
public class KHPCPatchManager{
static Assembly ExecutingAssembly = Assembly.GetExecutingAssembly();
static string[] EmbeddedLibraries = ExecutingAssembly.GetManifestResourceNames().Where(x => x.EndsWith(".dll")).ToArray();
static bool GUI_Displayed = false;
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args){
var assemblyName = new AssemblyName(args.Name).Name + ".dll";
var resourceName = EmbeddedLibraries.FirstOrDefault(x => x.EndsWith(assemblyName));
if(resourceName == null){
return null;
}
using (var stream = ExecutingAssembly.GetManifestResourceStream(resourceName)){
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
return Assembly.Load(bytes);
}
}
static KHPCPatchManager(){
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
static Dictionary<string,string[]> khFiles = new Dictionary<string,string[]>(){
{"KH1", new string[]{
"kh1_first",
"kh1_second",
"kh1_third",
"kh1_fourth",
"kh1_fifth"
}},
{"KH2", new string[]{
"kh2_first",
"kh2_second",
"kh2_third",
"kh2_fourth",
"kh2_fifth",
"kh2_sixth"
}},
{"BBS", new string[]{
"bbs_first",
"bbs_second",
"bbs_third",
"bbs_fourth"
}},
{"DDD", new string[]{
"kh3d_first",
"kh3d_second",
"kh3d_third",
"kh3d_fourth"
}},
{"COM", new string[]{
"Recom"
}}
};
static List<string> patchType = new List<string>();
static string version = "";
static string multiplePatchTypesSelected = "You have selected different types of patches (meant for different games)!";
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
[STAThread]
static void Main(string[] args){
if (Environment.OSVersion.Version.Major >= 6)
SetProcessDPIAware();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(ExecutingAssembly.Location);
version = "v" + fvi.ProductVersion;
if(!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "/resources")){
UpdateResources();
}
if(!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "/resources/custom_filenames.txt")){
File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "/resources/custom_filenames.txt", "");
}
Console.WriteLine($"KHPCPatchManager {version}");
bool extract_raw = false;
bool nobackup = false;
bool extractPatch = false;
string hedFile = null, pkgFile = null, pkgFolder = null;
List<string> originFolder = new List<string>();
List<string> patchFolders = new List<string>();
bool help = false;
try{
for(int i=0;i<args.Length;i++){
if(Path.GetExtension(args[i]) == ".hed"){
hedFile = args[i];
}else if(Path.GetExtension(args[i]) == ".pkg"){
pkgFile = args[i];
}else if(Directory.Exists(args[i])){
pkgFolder = args[i];
patchFolders.Add(args[i]);
}else if(Path.GetExtension(args[i]) == ".kh1pcpatch"){
patchType.Add("KH1");
originFolder.Add(args[i]);
}else if(Path.GetExtension(args[i]) == ".kh2pcpatch"){
patchType.Add("KH2");
originFolder.Add(args[i]);
}else if(Path.GetExtension(args[i]) == ".compcpatch"){
patchType.Add("COM");
originFolder.Add(args[i]);
}else if(Path.GetExtension(args[i]) == ".bbspcpatch"){
patchType.Add("BBS");
originFolder.Add(args[i]);
}else if(Path.GetExtension(args[i]) == ".dddpcpatch"){
patchType.Add("DDD");
originFolder.Add(args[i]);
}else if(args[i] == "-extract"){
extractPatch = true;
}else if(args[i] == "-nobackup"){
nobackup = true;
}else if(args[i] == "-raw"){
extract_raw = true;
}else{
if(args[i] == "help" || args[i] == "-help" || args[i] == "--help" || args[i] == "-h" || args[i] == "--h" || args[i] == "?") help = true;
}
}
if(hedFile != null && !extract_raw){
Console.WriteLine("Extracting pkg...");
OpenKh.Egs.EgsTools.Extract(hedFile, hedFile + "_out");
Console.WriteLine("Done!");
}else if(hedFile != null && extract_raw){
Console.WriteLine("Extracting raw pkg...");
OpenKh.Egs.EgsTools.ExtractRAW(hedFile, hedFile + "_out");
Console.WriteLine("Done!");
}else if(pkgFile != null && pkgFolder != null){
Console.WriteLine("Patching pkg...");
OpenKh.Egs.EgsTools.Patch(pkgFile, pkgFolder, pkgFolder + "_out");
Console.WriteLine("Done!");
}else if(pkgFile == null && pkgFolder != null){
Console.WriteLine("Creating patch...");
using(var zip = new ZipFile()){
for(int i=0;i<patchFolders.Count;i++){
Console.WriteLine("Adding: {0}", patchFolders[i]);
zip.AddDirectory(patchFolders[i], "");
if (Directory.Exists(patchFolders[i] + @"\kh1_first") || Directory.Exists(patchFolders[i] + @"\kh1_second") || Directory.Exists(patchFolders[i] + @"\kh1_third") || Directory.Exists(patchFolders[i] + @"\kh1_fourth") || Directory.Exists(patchFolders[i] + @"\kh1_fifth")){
zip.Save("MyPatch.kh1pcpatch");
}else if (Directory.Exists(patchFolders[i] + @"\kh2_first") || Directory.Exists(patchFolders[i] + @"\kh2_second") || Directory.Exists(patchFolders[i] + @"\kh2_third") || Directory.Exists(patchFolders[i] + @"\kh2_fourth") || Directory.Exists(patchFolders[i] + @"\kh2_fifth") || Directory.Exists(patchFolders[i] + @"\kh2_sixth")){
zip.Save("MyPatch.kh2pcpatch");
}else if (Directory.Exists(patchFolders[i] + @"\Recom")){
zip.Save("MyPatch.compcpatch");
}else if (Directory.Exists(patchFolders[i] + @"\bbs_first") || Directory.Exists(patchFolders[i] + @"\bbs_second") || Directory.Exists(patchFolders[i] + @"\bbs_third") || Directory.Exists(patchFolders[i] + @"\bbs_fourth")){
zip.Save("MyPatch.bbspcpatch");
}else if (Directory.Exists(patchFolders[i] + @"\kh3d_first") || Directory.Exists(patchFolders[i] + @"\kh3d_second") || Directory.Exists(patchFolders[i] + @"\kh3d_third") || Directory.Exists(patchFolders[i] + @"\kh3d_fourth")){
zip.Save("MyPatch.dddpcpatch");
}
}
}
Console.WriteLine("Done!");
}else if(originFolder.Count > 0){
if(patchType.Distinct().ToList().Count == 1){
ApplyPatch(originFolder, patchType[0], null, !nobackup, extractPatch);
}else{
Console.WriteLine(multiplePatchTypesSelected);
}
}else if(help){
Console.WriteLine("\nHow to use KHPCPatchManager in CLI:");
Console.WriteLine("- Feed a .hed file to unpack the associated .pkg file:\n khpcpatchmanager <hed_file>\n");
Console.WriteLine("- Feed a .pkg file and its unpacked folder to patch it:\n khpcpatchmanager <pkg_file> <unpacked_pkg_folder>\n");
Console.WriteLine("- Feed a folder(s) (extracted .pkg format) to create a kh1pcpatch, kh2pcpatch, bbspcpatch, compcpatch or a dddpcpatch:\n khpcpatchmanager <unpacked_pkg_folder>\n");
Console.WriteLine("- Feed a kh1pcpatch, kh2pcpatch, bbspcpatch, compcpatch or a dddpcpatch to patch your .pkgs:\n khpcpatchmanager <.[kh1/com/kh2/bbs/ddd]pcpatch file>\n");
}else{
InitUI();
}
}catch(Exception e){
Console.WriteLine($"Error: {e}");
}
if(!GUI_Displayed) Console.ReadLine();
}
static void UpdateResources(){
string resourceName = ExecutingAssembly.GetManifestResourceNames().Single(str => str.EndsWith("resources.zip"));
using (Stream stream = ExecutingAssembly.GetManifestResourceStream(resourceName)){
ZipFile zip = ZipFile.Read(stream);
Directory.CreateDirectory("resources");
zip.ExtractSelectedEntries("*.txt", "resources", "", ExtractExistingFileAction.OverwriteSilently);
}
}
static int filesExtracted = 0;
static string currentExtraction = "";
static int totalFiles = 0;
static void ExtractionProgress(object sender, ExtractProgressEventArgs e){
if (e.EventType != ZipProgressEventType.Extracting_BeforeExtractEntry) return;
filesExtracted++;
//int percent = Convert.ToInt32(100 * e.BytesTransferred / e.TotalBytesToTransfer);
int percent = 100 * filesExtracted / totalFiles;
if(GUI_Displayed) status.Text = "Extracting " + currentExtraction + $": {percent}%";
}
public static List<ZipFile> ZipFiles;
static void ApplyPatch(List<string> patchFile, string patchType, string epicFolder = null, bool backupPKG = true, bool extractPatch = false){
Console.WriteLine("Applying " + patchType + " patch...");
if(epicFolder == null){
epicFolder = @"C:\Program Files\Epic Games\KH_1.5_2.5\Image\en\";
if(patchType == "DDD") epicFolder = null;
}
while(!Directory.Exists(epicFolder)){
if (patchType == "KH1" || patchType == "KH2" || patchType == "BBS"|| patchType == "COM") {
Console.WriteLine("If you want to patch KH1, KH2, Recom or BBS, please drag your \"en\" folder (the one that contains kh1_first, kh1_second, etc.) located under \"Kingdom Hearts HD 1 5 and 2 5 ReMIX/Image/\" here, and press Enter:");
epicFolder = Console.ReadLine().Trim('"');
}
else if (patchType == "DDD"){
Console.WriteLine("If you want to patch Dream Drop Distance, please drag your \"en\" folder (the one that contains kh3d_first, kh3d_second, etc.) located under \"Kingdom Hearts HD 2 8 Final Chapter Prologue/Image/\" here, and press Enter:");
epicFolder = Console.ReadLine().Trim('"');
}
}
string timestamp = DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss_ms");
string tempFolder = "";
if(extractPatch){
Console.WriteLine("Extracting patch...");
if(GUI_Displayed) status.Text = $"Extracting patch: 0%";
tempFolder = patchFile[0] + "_" + timestamp;
Directory.CreateDirectory(tempFolder);
}
MyBackgroundWorker backgroundWorker1 = new MyBackgroundWorker();
backgroundWorker1.ProgressChanged += (s,e) => {
Console.WriteLine((string)e.UserState);
if(GUI_Displayed) status.Text = (string)e.UserState;
};
backgroundWorker1.DoWork += (s,e) => {
string epicBackup = Path.Combine(epicFolder, "backup");
Directory.CreateDirectory(epicBackup);
ZipFiles = new List<ZipFile>();
for(int i=0;i<patchFile.Count;i++){
using(ZipFile zip = ZipFile.Read(patchFile[i])){
if(extractPatch){
totalFiles = zip.Count;
filesExtracted = 0;
currentExtraction = patchFile[i];
zip.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(ExtractionProgress);
zip.ExtractAll(tempFolder, ExtractExistingFileAction.OverwriteSilently);
}else{
ZipFiles.Insert(0, zip);
}
}
}
backgroundWorker1.ReportProgress(0, "Applying patch...");
bool foundFolder = false;
for(int i=0;i<khFiles[patchType].Length;i++){
backgroundWorker1.ReportProgress(0, $"Searching {khFiles[patchType][i]}...");
string epicFile = Path.Combine(epicFolder, khFiles[patchType][i] + ".pkg");
string epicHedFile = Path.Combine(epicFolder, khFiles[patchType][i] + ".hed");
string patchFolder = Path.Combine(tempFolder, khFiles[patchType][i]);
string epicPkgBackupFile = Path.Combine(epicBackup, khFiles[patchType][i] + (!backupPKG ? "_" + timestamp : "") + ".pkg");
string epicHedBackupFile = Path.Combine(epicBackup, khFiles[patchType][i] + (!backupPKG ? "_" + timestamp : "") + ".hed");
try{
if(((!extractPatch && OpenKh.Egs.ZipManager.DirectoryExists(khFiles[patchType][i])) || (extractPatch && Directory.Exists(patchFolder))) && File.Exists(epicFile)){
foundFolder = true;
if(File.Exists(epicPkgBackupFile)) File.Delete(epicPkgBackupFile);
File.Move(epicFile, epicPkgBackupFile);
if(File.Exists(epicHedBackupFile)) File.Delete(epicHedBackupFile);
File.Move(epicHedFile, epicHedBackupFile);
backgroundWorker1.ReportProgress(0, $"Patching {khFiles[patchType][i]}...");
backgroundWorker1.PKG = khFiles[patchType][i];
OpenKh.Egs.EgsTools.Patch(epicPkgBackupFile, (!extractPatch ? khFiles[patchType][i] : patchFolder), epicFolder, backgroundWorker1);
if(!backupPKG){
if(File.Exists(epicPkgBackupFile)) File.Delete(epicPkgBackupFile);
File.Move(Path.Combine(epicFolder, khFiles[patchType][i] + "_" + timestamp + ".pkg"), Path.Combine(epicFolder, khFiles[patchType][i] + ".pkg"));
if(File.Exists(epicHedBackupFile)) File.Delete(epicHedBackupFile);
File.Move(Path.Combine(epicFolder, khFiles[patchType][i] + "_" + timestamp + ".hed"), Path.Combine(epicFolder, khFiles[patchType][i] + ".hed"));
}
}
}catch(Exception ex){
Console.WriteLine(ex.ToString());
}
}
if(extractPatch && Directory.Exists(tempFolder)) Directory.Delete(tempFolder, true);
if(!foundFolder){
string error = "Could not find any folder to patch!\nMake sure you are using the correct path for the \"en\" folder!";
Console.WriteLine(error);
if(GUI_Displayed) status.Text = "";
if(GUI_Displayed) MessageBox.Show(error);
}else{
if(GUI_Displayed) status.Text = "";
if(GUI_Displayed) MessageBox.Show("Patch applied!");
Console.WriteLine("Done!");
}
};
backgroundWorker1.RunWorkerCompleted += (s,e) => {
if(e.Error != null)
{
if(GUI_Displayed) MessageBox.Show("There was an error! " + e.Error.ToString());
Console.WriteLine("There was an error! " + e.Error.ToString());
}
if(GUI_Displayed) selPatchButton.Enabled = true;
if(GUI_Displayed) applyPatchButton.Enabled = true;
if(GUI_Displayed) backupOption.Enabled = true;
};
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();
}
static StatusBar status = new StatusBar();
static Button selPatchButton = new Button();
static Button applyPatchButton = new Button();
static MenuItem backupOption = new MenuItem();
static void InitUI(){
UpdateResources();
GUI_Displayed = true;
var handle = GetConsoleWindow();
string defaultEpicFolder = @"C:\Program Files\Epic Games\KH_1.5_2.5\Image\en\";
string epicFolder = defaultEpicFolder;
string[] patchFiles = new string[]{};
Form f = new Form();
f.Icon = Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetExecutingAssembly().Location);
f.Size = new System.Drawing.Size(350, 300);
f.Text = $"KHPCPatchManager {version}";
f.MinimumSize = new System.Drawing.Size(350, 300);
status.Text = "";
f.Controls.Add(status);
Label patch = new Label();
patch.Text = "Patch: ";
patch.AutoSize = true;
f.Controls.Add(patch);
f.Menu = new MainMenu();
MenuItem item = new MenuItem("Options");
f.Menu.MenuItems.Add(item);
MenuItem backupOption = new MenuItem();
backupOption.Text = "Backup PKG";
backupOption.Checked = true;
backupOption.Click += (s,e) => backupOption.Checked = !backupOption.Checked;
item.MenuItems.AddRange(new MenuItem[]{backupOption});
MenuItem extractOption = new MenuItem();
extractOption.Text = "Extract patch before applying";
extractOption.Checked = false;
extractOption.Click += (s,e) => extractOption.Checked = !extractOption.Checked;
item.MenuItems.AddRange(new MenuItem[]{extractOption});
item = new MenuItem("?");
f.Menu.MenuItems.Add(item);
MenuItem helpOption = new MenuItem();
helpOption.Text = "About";
helpOption.Click += (s,e) => {
Form f2 = new Form();
f2.Text = "About - " + f.Text;
f2.Size = new System.Drawing.Size(450, 370);
f2.MinimumSize = new System.Drawing.Size(450, 370);
f2.Icon = Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetExecutingAssembly().Location);
Color c = f2.BackColor;
string rgb = c.R.ToString() + ", " + c.G.ToString() + ", " + c.B.ToString();
WebBrowser wb = new WebBrowser();
wb.Dock = DockStyle.Fill;
wb.AutoSize = true;
wb.Size = new Size(f2.Width, f2.Height);
wb.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
wb.DocumentText = "<html style='font-family:calibri;overflow:hidden;width:97%;background-color: rgb(" + rgb + @")'><div style='width:100%;text-align:center;'>
Tool made by <b>AntonioDePau</b><br>
Thanks to:<br>
<ul style='text-align:left'>
<li><a href='https://github.com/Noxalus/OpenKh/tree/feature/egs-hed-packer'>Noxalus</a></li>
<li><a href='https://twitter.com/xeeynamo'>Xeeynamo</a> and the whole <a href='https://github.com/Xeeynamo/OpenKh'>OpenKH</a> team</li>
<li>DemonBoy (aka: DA) for making custom HD assets for custom MDLX files possible</li>
<li><a href='https://twitter.com/tieulink'>TieuLink</a> for extensive testing and help in debugging</li>
</ul>
Source code: <a href='https://github.com/AntonioDePau/KHPCPatchManager'>GitHub</a><br>
Report bugs: <a href='https://github.com/AntonioDePau/KHPCPatchManager/issues'>GitHub</a><br>
<br>
<b>Note:</b> <i>For some issues, you may want to contact the patch's author instead of me!</i>
</div>
</html>";
wb.Navigating += (s,e) => {
e.Cancel = true;
Process.Start(e.Url.ToString());
};
f2.Controls.Add(wb);
f2.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
f2.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
f2.ResumeLayout(false);
f2.ShowDialog();
};
item.MenuItems.AddRange(new MenuItem[]{helpOption});
selPatchButton.Text = "Select patch";
f.Controls.Add(selPatchButton);
selPatchButton.Location = new Point(
f.ClientSize.Width / 2 - selPatchButton.Size.Width / 2, 25);
selPatchButton.Anchor = AnchorStyles.Top;
selPatchButton.Click += (s,e) => {
using(OpenFileDialog openFileDialog = new OpenFileDialog()){
openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
openFileDialog.Filter = "KH pcpatch files (*.*pcpatch)|*.*pcpatch|All files (*.*)|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.Multiselect = true;
if(openFileDialog.ShowDialog() == DialogResult.OK){
//Get the path of specified file
//MessageBox.Show(openFileDialog.FileName);
patchFiles = openFileDialog.FileNames;
for(int i=0;i<patchFiles.Length;i++){
string ext = Path.GetExtension(patchFiles[i]).Replace("pcpatch", "").Replace(".","");
patchType.Add(ext.ToUpper());
}
if(patchType.Distinct().ToList().Count == 1){
if(patchFiles.Length>1){
patchFiles = ReorderPatches(patchFiles);
}
patch.Text = "Patch" + (patchFiles.Length>1?"es: " + patchFiles.Aggregate((x, y) => Path.GetFileNameWithoutExtension(x) + ", " + Path.GetFileNameWithoutExtension(y)):": " + Path.GetFileNameWithoutExtension(patchFiles[0]));
applyPatchButton.Enabled = true;
}else{
MessageBox.Show(multiplePatchTypesSelected + ":\n" + patchType.Aggregate((x, y) => x + ", " + y));
applyPatchButton.Enabled = false;
}
}
}
};
applyPatchButton.Text = "Apply patch";
f.Controls.Add(applyPatchButton);
applyPatchButton.Location = new Point(
f.ClientSize.Width / 2 - applyPatchButton.Size.Width / 2, 50);
applyPatchButton.Anchor = AnchorStyles.Top;
applyPatchButton.Enabled = false;
applyPatchButton.Click += (s,e) => {
if(!Directory.Exists(epicFolder) || patchType[0] == "DDD"){
using(FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog()){
folderBrowserDialog.Description = "Could not find the installation path for Kingdom Hearts on this PC!\nPlease browse for the \"Epic Games\\KH_1.5_2.5\" (or \"2.8\" for DDD) folder.";
if(folderBrowserDialog.ShowDialog() == DialogResult.OK){
string temp = Path.Combine(folderBrowserDialog.SelectedPath, "Image\\en");
if(Directory.Exists(temp)){
epicFolder = temp;
selPatchButton.Enabled = false;
applyPatchButton.Enabled = false;
backupOption.Enabled = false;
extractOption.Enabled = false;
ApplyPatch(patchFiles.ToList(), patchType[0], epicFolder, backupOption.Checked, extractOption.Checked);
}else{
MessageBox.Show("Could not find \"\\Image\\en\" in the provided folder!\nPlease try again by selecting the correct folder.");
}
}
}
}else{
selPatchButton.Enabled = false;
applyPatchButton.Enabled = false;
backupOption.Enabled = false;
ApplyPatch(patchFiles.ToList(), patchType[0], epicFolder, backupOption.Checked, extractOption.Checked);
}
};
f.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
f.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
f.ResumeLayout(false);
ShowWindow(handle, SW_HIDE);
f.ShowDialog();
}
static string[] ReorderPatches(string[] patchFiles){
string[] ordered = patchFiles;
Form f = new Form();
f.Icon = Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetExecutingAssembly().Location);
f.Size = new System.Drawing.Size(350, 300);
f.Text = $"Patch order";
f.MinimumSize = new System.Drawing.Size(350, 300);
Label label = new Label();
label.Text = "Click on a patch and drag it to change its position in the list:";
label.AutoSize = true;
f.Controls.Add(label);
ListBox lb = new ListBox();
lb.AllowDrop = true;
lb.AutoSize = true;
f.Controls.Add(lb);
BindingList<ListBoxItem> ListBoxItems = new BindingList<ListBoxItem>();
for(int i=0;i<patchFiles.Length;i++){
ListBoxItems.Add(new ListBoxItem(Path.GetFileNameWithoutExtension(patchFiles[i]), patchFiles[i]));
}
lb.DataSource = ListBoxItems;
lb.DisplayMember = "ShortName";
lb.ValueMember = "Path";
lb.MouseDown += (s,e) => {
if(lb.SelectedItem == null) return;
lb.DoDragDrop(lb.SelectedItem, DragDropEffects.Move);
};
lb.DragOver += (s,e) => {
e.Effect = DragDropEffects.Move;
};
lb.DragDrop += (s,e) => {
Point point = lb.PointToClient(new Point(e.X, e.Y));
int index = lb.IndexFromPoint(point);
if (index < 0) index = lb.Items.Count - 1;
ListBoxItem data = (ListBoxItem)lb.SelectedItem;
ListBoxItems.Remove(data);
ListBoxItems.Insert(index, data);
};
lb.Location = new Point(0, 15);
Button confirm = new Button();
confirm.Text = "Confirm";
confirm.Location = new Point(
f.ClientSize.Width / 2 - confirm.Size.Width / 2, lb.Height + 50);
confirm.Anchor = AnchorStyles.Top;
f.Controls.Add(confirm);
confirm.Click += (s,e) => {
f.Close();
for(int i=0;i<lb.Items.Count;i++){
ordered[i] = ((ListBoxItem)(lb.Items[i])).Path;
}
};
f.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
f.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
f.ResumeLayout(false);
f.ShowDialog();
return ordered;
}
}