Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WorkerForm.cs - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e12764960fb557b7c9c808436997a6527155b59 (plain)
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
using com.clusterrr.Famicom;
using com.clusterrr.FelLib;
using com.clusterrr.hakchi_gui.Properties;
using SevenZip;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace com.clusterrr.hakchi_gui
{
    public partial class WorkerForm : Form
    {
        public enum Tasks { DumpKernel, FlashKernel, Memboot, DownloadAllCovers, AddGames };
        public Tasks Task;
        //public string UBootDump;
        public string KernelDump;
        public string Mod = null;
        public Dictionary<string, bool> Config = null;
        public string[] HiddenGames;
        public NesMenuCollection Games;
        public SelectButtonsForm.NesButtons ResetCombination;
        public bool AutofireHack;
        public bool FcStart = true;
        public string ExtraCommandLineArguments = null;
        public string[] GamesToAdd;
        Thread thread = null;
        Fel fel = null;

        const UInt16 vid = 0x1F3A;
        const UInt16 pid = 0xEFE8;

        readonly string baseDirectory;
        readonly string fes1Path;
        readonly string ubootPath;
        readonly string tempDirectory;
        readonly string kernelDirectory;
        readonly string initramfs_cpio;
        readonly string initramfs_cpioPatched;
        readonly string ramfsDirectory;
        readonly string hakchiDirectory;
        readonly string modsDirectory;
        readonly string toolsDirectory;
        readonly string kernelPatched;
        readonly string ramdiskPatched;
        readonly string configPath;
        readonly string hiddenPath;
        readonly string tempGamesDirectory;
        readonly string cloverconDriverPath;
        readonly string argumentsFilePath;
        readonly string gamesDirectory;
        string[] correctKernels;
        const long maxRamfsSize = 40 * 1024 * 1024;
        DialogResult DeviceWaitResult = DialogResult.None;
        DialogResult MessageBoxResult = DialogResult.None;

        public WorkerForm()
        {
            InitializeComponent();
            DialogResult = DialogResult.None;
            baseDirectory = MainForm.BaseDirectory;
            fes1Path = Path.Combine(Path.Combine(baseDirectory, "data"), "fes1.bin");
            ubootPath = Path.Combine(Path.Combine(baseDirectory, "data"), "uboot.bin");
            gamesDirectory = MainForm.GamesDirectory;
            tempDirectory = Path.Combine(baseDirectory, "temp");
            kernelDirectory = Path.Combine(tempDirectory, "kernel");
            initramfs_cpio = Path.Combine(kernelDirectory, "initramfs.cpio");
            initramfs_cpioPatched = Path.Combine(kernelDirectory, "initramfs_mod.cpio");
            ramfsDirectory = Path.Combine(kernelDirectory, "initramfs");
            hakchiDirectory = Path.Combine(ramfsDirectory, "hakchi");
            modsDirectory = Path.Combine(baseDirectory, "mods");
            toolsDirectory = Path.Combine(baseDirectory, "tools");
            kernelPatched = Path.Combine(kernelDirectory, "patched_kernel.img");
            ramdiskPatched = Path.Combine(kernelDirectory, "kernel.img-ramdisk_mod.gz");
            configPath = Path.Combine(hakchiDirectory, "config");
            hiddenPath = Path.Combine(hakchiDirectory, "hidden_games");
            cloverconDriverPath = Path.Combine(hakchiDirectory, "clovercon.ko");
            argumentsFilePath = Path.Combine(hakchiDirectory, "extra_args");
            correctKernels = new string[] {
                "5cfdca351484e7025648abc3b20032ff", "07bfb800beba6ef619c29990d14b5158", // NES Mini
                "ac8144c3ea4ab32e017648ee80bdc230" // Famicom Mini
            };
            tempGamesDirectory = Path.Combine(ramfsDirectory, "games");
        }

        public DialogResult Start()
        {
            SetProgress(0, 1);
            if (Task == Tasks.DumpKernel || Task == Tasks.FlashKernel || Task == Tasks.Memboot)
            {
                if (!WaitingForm.WaitForDevice(vid, pid))
                {
                    DialogResult = DialogResult.Abort;
                    return DialogResult;
                }
            }
            thread = new Thread(StartThread);
            thread.Start();
            return ShowDialog();
        }

        void WaitForDeviceFromThread()
        {
            if (InvokeRequired)
            {
                Invoke(new Action(WaitForDeviceFromThread));
                return;
            }
            if (fel != null)
                fel.Close();
            DeviceWaitResult = WaitingForm.WaitForDevice(vid, pid) ? DialogResult.OK : DialogResult.Abort;
        }

        private delegate void MessageBoxFromThreadDelegate(IWin32Window owner, string text, string caption, MessageBoxButtons buttons,
            MessageBoxIcon icon, MessageBoxDefaultButton defaultButton);
        void MessageBoxFromThread(IWin32Window owner, string text, string caption, MessageBoxButtons buttons,
            MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            if (InvokeRequired)
            {
                Invoke(new MessageBoxFromThreadDelegate(MessageBoxFromThread),
                    new object[] { owner, text, caption, buttons, icon, defaultButton });
                return;
            }
            MessageBoxResult = MessageBox.Show(owner, text, caption, buttons, icon, defaultButton);
        }

        public void StartThread()
        {
            fel = new Fel();
            SetProgress(0, 1);
            try
            {
                if (Task == Tasks.DumpKernel || Task == Tasks.FlashKernel || Task == Tasks.Memboot)
                {
                    if (!File.Exists(fes1Path)) throw new FileNotFoundException(fes1Path + " not found");
                    if (!File.Exists(ubootPath)) throw new FileNotFoundException(ubootPath + " not found");
                    fel.Fes1Bin = File.ReadAllBytes(fes1Path);
                    fel.UBootBin = File.ReadAllBytes(ubootPath);
                    fel.Open(vid, pid);
                    SetStatus(Resources.UploadingFes1);
                    fel.InitDram(true);
                }
                switch (Task)
                {
                    case Tasks.DumpKernel:
                        DoKernelDump();
                        break;
                    case Tasks.FlashKernel:
                        FlashKernel();
                        break;
                    case Tasks.Memboot:
                        Memboot();
                        break;
                    case Tasks.DownloadAllCovers:
                        DownloadAllCovers();
                        break;
                    case Tasks.AddGames:
                        AddGames(gamesDirectory, GamesToAdd);
                        break;
                }
                Thread.Sleep(1000);
                DialogResult = DialogResult.OK;
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                ShowError(ex);
            }
            finally
            {
                thread = null;
                if (fel != null)
                {
                    fel.Close();
                    fel = null;
                }
            }
        }

        void SetStatus(string status)
        {
            if (Disposing) return;
            try
            {
                if (InvokeRequired)
                {
                    Invoke(new Action<string>(SetStatus), new object[] { status });
                    return;
                }
                labelStatus.Text = status;
            }
            catch { }
        }

        void SetProgress(int value, int max)
        {
            if (Disposing) return;
            try
            {
                if (InvokeRequired)
                {
                    Invoke(new Action<int, int>(SetProgress), new object[] { value, max });
                    return;
                }
                if (value > max) value = max;
                progressBar.Maximum = max;
                progressBar.Value = value;
            }
            catch { }
        }

        void ShowError(Exception ex, bool dontStop = false)
        {
            if (Disposing) return;
            try
            {
                if (InvokeRequired)
                {
                    Invoke(new Action<Exception, bool>(ShowError), new object[] { ex, dontStop });
                    return;
                }
                Debug.WriteLine(ex.Message + ex.StackTrace);
                if (ex is GameGenieFormatException || ex is GameGenieNotFoundException)
                    MessageBox.Show(this, ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                else if (ex is MadWizard.WinUSBNet.USBException)
                    MessageBox.Show(this, ex.Message + "\r\n" + Resources.PleaseTryAgainUSB, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show(this, ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (!dontStop)
                {
                    thread = null;
                    Close();
                }
            }
            catch { }
        }

        void ShowMessage(string text, string title)
        {
            if (Disposing) return;
            try
            {
                if (InvokeRequired)
                {
                    Invoke(new Action<string, string>(ShowMessage), new object[] { text, title });
                    return;
                }
                MessageBox.Show(this, text, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch { }
        }

        public void DoKernelDump()
        {
            int progress = 5;
            const int maxProgress = 80;
            SetProgress(progress, maxProgress);
            SetStatus(Resources.DumpingKernel);

            var kernel = fel.ReadFlash(Fel.kernel_base_f, Fel.sector_size * 0x20,
                delegate(Fel.CurrentAction action, string command)
                {
                    switch (action)
                    {
                        case Fel.CurrentAction.RunningCommand:
                            SetStatus(Resources.ExecutingCommand + " " + command);
                            break;
                        case Fel.CurrentAction.ReadingMemory:
                            SetStatus(Resources.DumpingKernel);
                            break;
                    }
                    progress++;
                    SetProgress(progress, maxProgress);
                }
            );

            var size = CalKernelSize(kernel);
            if (size == 0 || size > Fel.kernel_max_size)
                throw new Exception(Resources.InvalidKernelSize + " " + size);
            if (kernel.Length > size)
            {
                var sm_kernel = new byte[size];
                Array.Copy(kernel, 0, sm_kernel, 0, size);
                kernel = sm_kernel;
            }

            SetProgress(maxProgress, maxProgress);
            SetStatus(Resources.Done);

            var md5 = System.Security.Cryptography.MD5.Create();
            var hash = BitConverter.ToString(md5.ComputeHash(kernel)).Replace("-", "").ToLower();
            if (!correctKernels.Contains(hash))
            {
                if (MessageBox.Show(Resources.MD5Failed + " " + hash + "\r\n" + Resources.MD5Failed2 + "\r\n" + Resources.DoYouWantToContinue, Resources.Warning, MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
                    == DialogResult.No)
                {
                    DialogResult = DialogResult.Abort;
                    return;
                }
            }

            Directory.CreateDirectory(Path.GetDirectoryName(KernelDump));
            File.WriteAllBytes(KernelDump, kernel);
        }

        public void FlashKernel()
        {
            int progress = 5;
            int maxProgress = 120 + (string.IsNullOrEmpty(Mod) ? 0 : 5);
            SetProgress(progress, maxProgress);

            byte[] kernel;
            if (!string.IsNullOrEmpty(Mod))
            {
                kernel = CreatePatchedKernel();
                progress += 5;
                SetProgress(progress, maxProgress);
            }
            else
                kernel = File.ReadAllBytes(KernelDump);
            var size = CalKernelSize(kernel);
            if (size > kernel.Length || size > Fel.kernel_max_size)
                throw new Exception(Resources.InvalidKernelSize + " " + size);

            size = (size + Fel.sector_size - 1) / Fel.sector_size;
            size = size * Fel.sector_size;
            if (kernel.Length != size)
            {
                var newK = new byte[size];
                Array.Copy(kernel, newK, kernel.Length);
                kernel = newK;
            }

            fel.WriteFlash(Fel.kernel_base_f, kernel,
                delegate(Fel.CurrentAction action, string command)
                {
                    switch (action)
                    {
                        case Fel.CurrentAction.RunningCommand:
                            SetStatus(Resources.ExecutingCommand + " " + command);
                            break;
                        case Fel.CurrentAction.WritingMemory:
                            SetStatus(Resources.UploadingKernel);
                            break;
                    }
                    progress++;
                    SetProgress(progress, maxProgress);
                }
            );
            var r = fel.ReadFlash((UInt32)Fel.kernel_base_f, (UInt32)kernel.Length,
                delegate(Fel.CurrentAction action, string command)
                {
                    switch (action)
                    {
                        case Fel.CurrentAction.RunningCommand:
                            SetStatus(Resources.ExecutingCommand + " " + command);
                            break;
                        case Fel.CurrentAction.ReadingMemory:
                            SetStatus(Resources.Verifying);
                            break;
                    }
                    progress++;
                    SetProgress(progress, maxProgress);
                }
            );
            if (!kernel.SequenceEqual(r))
                throw new Exception(Resources.VerifyFailed);

            if (string.IsNullOrEmpty(Mod))
            {
                var shutdownCommand = string.Format("shutdown", Fel.kernel_base_m);
                SetStatus(Resources.ExecutingCommand + " " + shutdownCommand);
                fel.RunUbootCmd(shutdownCommand, true);
            }
            SetStatus(Resources.Done);
            SetProgress(maxProgress, maxProgress);
        }

        public void Memboot()
        {
            int progress = 5;
            SetProgress(progress, 300);
            int maxProgress = -1;
            var stats = new GamesTreeStats();

            do
            {
                if (stats.GamesProceed > 0)
                {
                    ShowMessage(Resources.ParticallyBody, Resources.ParticallyTitle);
                    DeviceWaitResult = DialogResult.None;
                    WaitForDeviceFromThread();
                    while (DeviceWaitResult == DialogResult.None)
                        Thread.Sleep(500);
                    if (DeviceWaitResult != DialogResult.OK)
                    {
                        DialogResult = DialogResult.Abort;
                        return;
                    }
                    fel = new Fel();
                    fel.Fes1Bin = File.ReadAllBytes(fes1Path);
                    fel.UBootBin = File.ReadAllBytes(ubootPath);
                    fel.Open(vid, pid);
                    SetStatus(Resources.UploadingFes1);
                    fel.InitDram(true);
                }

                byte[] kernel;
                if (!string.IsNullOrEmpty(Mod))
                    kernel = CreatePatchedKernel(stats);
                else
                    kernel = File.ReadAllBytes(KernelDump);
                var size = CalKernelSize(kernel);
                if (size > kernel.Length || size > Fel.kernel_max_size)
                    throw new Exception(Resources.InvalidKernelSize + " " + size);
                size = (size + Fel.sector_size - 1) / Fel.sector_size;
                size = size * Fel.sector_size;
                if (kernel.Length != size)
                {
                    var newK = new byte[size];
                    Array.Copy(kernel, newK, kernel.Length);
                    kernel = newK;
                }

                progress += 5;
                if (maxProgress < 0)
                {
                    if (stats.GamesProceed > 0)
                        maxProgress = (kernel.Length / 67000 + 15) * stats.GamesTotal / stats.GamesProceed + 75 * ((int)Math.Ceiling((float)stats.GamesTotal / (float)stats.GamesProceed) - 1);
                    else
                        maxProgress = (kernel.Length / 67000 + 15);
                }
                SetProgress(progress, maxProgress);

                SetStatus(Resources.UploadingKernel);
                fel.WriteMemory(Fel.flash_mem_base, kernel,
                    delegate(Fel.CurrentAction action, string command)
                    {
                        switch (action)
                        {
                            case Fel.CurrentAction.WritingMemory:
                                SetStatus(Resources.UploadingKernel);
                                break;
                        }
                        progress++;
                        SetProgress(progress, maxProgress);
                    }
                );

                var bootCommand = string.Format("boota {0:x}", Fel.kernel_base_m);
                SetStatus(Resources.ExecutingCommand + " " + bootCommand);
                fel.RunUbootCmd(bootCommand, true);
            } while (stats.GamesProceed < stats.GamesTotal);
            SetStatus(Resources.Done);
            SetProgress(maxProgress, maxProgress);
        }

        private byte[] CreatePatchedKernel(GamesTreeStats stats = null)
        {
            if (stats == null) stats = new GamesTreeStats();
            bool first = stats.GamesProceed == 0;
            bool partial = stats.GamesProceed > 0;
            SetStatus(Resources.BuildingCustom);
            if (first)
            {
                if (Directory.Exists(tempDirectory))
                    Directory.Delete(tempDirectory, true);
                Directory.CreateDirectory(tempDirectory);
                Directory.CreateDirectory(kernelDirectory);
                Directory.CreateDirectory(ramfsDirectory);
                if (!ExecuteTool("unpackbootimg.exe", string.Format("-i \"{0}\" -o \"{1}\"", KernelDump, kernelDirectory)))
                    throw new Exception("Can't unpack kernel image");
                if (!ExecuteTool("lzop.exe", string.Format("-d \"{0}\" -o \"{1}\"",
                    Path.Combine(kernelDirectory, "kernel.img-ramdisk.gz"), initramfs_cpio)))
                    throw new Exception("Can't unpack ramdisk");
                ExecuteTool("cpio.exe", string.Format("-imd --no-preserve-owner --quiet -I \"{0}\"",
                   @"..\initramfs.cpio"), ramfsDirectory);
                if (!File.Exists(Path.Combine(ramfsDirectory, "init"))) // cpio.exe fails on Windows XP for some reason. But working!
                    throw new Exception("Can't unpack ramdisk 2");
                if (Directory.Exists(hakchiDirectory)) Directory.Delete(hakchiDirectory, true);
                DirectoryCopy(Path.Combine(modsDirectory, Mod), ramfsDirectory, true);
                var ramfsFiles = Directory.GetFiles(ramfsDirectory, "*.*", SearchOption.AllDirectories);
                foreach (var file in ramfsFiles)
                {
                    var fInfo = new FileInfo(file);
                    if (fInfo.Length > 10 && fInfo.Length < 100 && ((fInfo.Attributes & FileAttributes.System) == 0) &&
                        (Encoding.ASCII.GetString(File.ReadAllBytes(file), 0, 10)) == "!<symlink>")
                        fInfo.Attributes |= FileAttributes.System;
                }

                if (HiddenGames != null && HiddenGames.Length > 0)
                {
                    StringBuilder h = new StringBuilder();
                    foreach (var game in HiddenGames)
                        h.Append(game + "\n");
                    File.WriteAllText(hiddenPath, h.ToString());
                }

                if (Config != null && Config.ContainsKey("hakchi_clovercon_hack")
                && Config["hakchi_clovercon_hack"] && File.Exists(cloverconDriverPath))
                {
                    byte[] drv = File.ReadAllBytes(cloverconDriverPath);
                    const string magicReset = "MAGIC_BUTTONS:";
                    for (int i = 0; i < drv.Length - magicReset.Length; i++)
                    {
                        if (Encoding.ASCII.GetString(drv, i, magicReset.Length) == magicReset)
                        {
                            int pos = i + magicReset.Length;
                            for (int b = 0; b < 8; b++)
                                drv[pos + b] = (byte)((((byte)ResetCombination & (1 << b)) != 0) ? '1' : '0');
                            break;
                        }
                    }
                    const string magicAutofire = "MAGIC_AUTOFIRE:";
                    for (int i = 0; i < drv.Length - magicAutofire.Length; i++)
                    {
                        if (Encoding.ASCII.GetString(drv, i, magicAutofire.Length) == magicAutofire)
                        {
                            int pos = i + magicAutofire.Length;
                            drv[pos] = (byte)(AutofireHack ? '1' : '0');
                            break;
                        }
                    }
                    const string magicFcStart = "MAGIC_FC_START:";
                    for (int i = 0; i < drv.Length - magicFcStart.Length; i++)
                    {
                        if (Encoding.ASCII.GetString(drv, i, magicFcStart.Length) == magicFcStart)
                        {
                            int pos = i + magicFcStart.Length;
                            drv[pos] = (byte)(FcStart ? '1' : '0');
                            break;
                        }
                    }
                    File.WriteAllBytes(cloverconDriverPath, drv);
                }
                if (!string.IsNullOrEmpty(ExtraCommandLineArguments))
                {
                    File.WriteAllText(argumentsFilePath, ExtraCommandLineArguments);
                }
            } // if first transfer
            else // else clean games directory and extra files
            {
                if (Directory.Exists(tempGamesDirectory))
                {
                    Debug.WriteLine("Clearing games directory");
                    Directory.Delete(tempGamesDirectory, true);
                    Directory.CreateDirectory(tempGamesDirectory);
                }
                var dirs = Directory.GetDirectories(hakchiDirectory);
                foreach (var dir in dirs)
                    Directory.Delete(dir, true);
                var files = from f in Directory.GetFiles(hakchiDirectory) where (Path.GetFileName(f) != "init" && Path.GetFileName(f) != "config") select f;
                foreach (var file in files)
                    File.Delete(file);
            }

            // Games!
            if (Games != null)
            {
                stats.Next();
                AddMenu(Games, stats);
                Debug.WriteLine(string.Format("Games copied: {0}/{1}, part size: {2}", stats.GamesProceed, stats.GamesTotal, stats.Size));
            }

            // Remove thumbnails
            if (Config != null && Config.ContainsKey("hakchi_remove_thumbnails") && Config["hakchi_remove_thumbnails"])
            {
                var thumbnails = Directory.GetFiles(tempGamesDirectory, "*_small.png", SearchOption.AllDirectories);
                foreach (var t in thumbnails)
                    File.WriteAllBytes(t, new byte[0]);
            }

            // Writing config files
            if (Config != null)
            {
                Config["hakchi_partial_first"] = first;
                Config["hakchi_partial_last"] = stats.GamesProceed >= stats.GamesTotal;
                var config = new StringBuilder();

                foreach (var key in Config.Keys)
                    config.AppendFormat("{0}={1}\n", key, Config[key] ? 'y' : 'n');
                File.WriteAllText(configPath, config.ToString());
            }

            // Building image
            if (first && Games != null && Games.Count > 0) // There is no reason to compress cryptsetup when we do not uploading games
                ExecuteTool("upx.exe", "--best sbin\\cryptsetup", ramfsDirectory);
            byte[] ramdisk;
            if (!ExecuteTool("mkbootfs.exe", string.Format("\"{0}\"", ramfsDirectory), out ramdisk))
                throw new Exception("Can't repack ramdisk");
            File.WriteAllBytes(initramfs_cpioPatched, ramdisk);
            var argCmdline = File.ReadAllText(Path.Combine(kernelDirectory, "kernel.img-cmdline")).Trim();
            var argBoard = File.ReadAllText(Path.Combine(kernelDirectory, "kernel.img-board")).Trim();
            var argBase = File.ReadAllText(Path.Combine(kernelDirectory, "kernel.img-base")).Trim();
            var argPagesize = File.ReadAllText(Path.Combine(kernelDirectory, "kernel.img-pagesize")).Trim();
            var argKerneloff = File.ReadAllText(Path.Combine(kernelDirectory, "kernel.img-kerneloff")).Trim();
            var argRamdiscoff = File.ReadAllText(Path.Combine(kernelDirectory, "kernel.img-ramdiskoff")).Trim();
            var argTagsoff = File.ReadAllText(Path.Combine(kernelDirectory, "kernel.img-tagsoff")).Trim();
            if (!ExecuteTool("lzop.exe", string.Format("--best -f -o \"{0}\" \"{1}\"",
                ramdiskPatched, initramfs_cpioPatched)))
                throw new Exception("Can't repack ramdisk 2");
            if (!ExecuteTool("mkbootimg.exe", string.Format("--kernel \"{0}\" --ramdisk \"{1}\" --cmdline \"{2}\" --board \"{3}\" --base \"{4}\" --pagesize \"{5}\" --kernel_offset \"{6}\" --ramdisk_offset \"{7}\" --tags_offset \"{8}\" -o \"{9}\"",
                Path.Combine(kernelDirectory, "kernel.img-zImage"), ramdiskPatched, argCmdline, argBoard, argBase, argPagesize, argKerneloff, argRamdiscoff, argTagsoff, kernelPatched)))
                throw new Exception("Can't rebuild kernel");

            var result = File.ReadAllBytes(kernelPatched);
#if !DEBUG
            Directory.Delete(tempDirectory, true);
#endif
            if (result.Length > Fel.kernel_max_size) throw new Exception("Kernel is too big");
            return result;
        }

        void DownloadAllCovers()
        {
            if (Games == null) return;
            int i = 0;
            foreach (NesGame game in Games)
            {
                SetStatus(Resources.GooglingFor + " " + game.Name + ImageGooglerForm.Suffix);
                string[] urls = null;
                for (int tries = 0; tries < 5; tries++)
                {
                    if (urls == null)
                    {
                        try
                        {
                            urls = ImageGooglerForm.GetImageUrls(game.Name + ImageGooglerForm.Suffix);
                            break;
                        }
                        catch (Exception ex)
                        {
                            SetStatus(Resources.Error + ": " + ex.Message);
                            Thread.Sleep(1500);
                            continue;
                        }
                    }
                }
                if (urls != null && urls.Length == 0)
                    SetStatus(Resources.NotFound + " " + game.Name);
                for (int tries = 0; urls != null && tries < 5 && tries < urls.Length; tries++)
                {
                    try
                    {
                        var cover = ImageGooglerForm.DownloadImage(urls[tries]);
                        game.SetImage(cover, ConfigIni.EightBitPngCompression);
                        break;
                    }
                    catch (Exception ex)
                    {
                        SetStatus(Resources.Error + ": " + ex.Message);
                        Thread.Sleep(1500);
                        continue;
                    }
                }
                SetProgress(++i, Games.Count);
                Thread.Sleep(500); // not so fast, Google don't like it
            }
        }

        private class GamesTreeStats
        {
            public List<NesMenuCollection> allMenus = new List<NesMenuCollection>();
            public int GamesTotal = 0;
            public int GamesStart = 0;
            public int GamesProceed = 0;
            public long Size = 0;

            public void Next()
            {
                allMenus.Clear();
                GamesStart = GamesProceed;
                GamesTotal = 0;
                GamesProceed = 0;
                Size = 0;
            }
        }

        private void AddMenu(NesMenuCollection menuCollection, GamesTreeStats stats = null)
        {
            if (stats == null)
                stats = new GamesTreeStats();
            if (!stats.allMenus.Contains(menuCollection))
                stats.allMenus.Add(menuCollection);
            int menuIndex = stats.allMenus.IndexOf(menuCollection);
            string targetDirectory;
            if (menuIndex == 0)
                targetDirectory = tempGamesDirectory;
            else
                targetDirectory = Path.Combine(tempGamesDirectory, string.Format("sub{0:D3}", menuIndex));
            foreach (var element in menuCollection)
            {
                if (element is NesGame)
                {
                    stats.GamesTotal++;
                    if (stats.Size >= maxRamfsSize) continue;
                    stats.GamesProceed++;
                    if (stats.GamesStart >= stats.GamesProceed) continue;
                    var game = element as NesGame;
                    var gameDir = Path.Combine(targetDirectory, game.Code);
                    Debug.Write(string.Format("Processing {0} ('{1}'), #{2}", game.Code, game.Name, stats.GamesProceed));
                    stats.Size += DirectoryCopy(game.GamePath, gameDir, true);
                    Debug.WriteLine(string.Format(", total size: {0}", stats.Size));
                    if (!string.IsNullOrEmpty(game.GameGenie))
                    {
                        var codes = game.GameGenie.Split(new char[] { ',', '\t', ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
                        var newNesFilePath = Path.Combine(gameDir, game.Code + ".nes");
                        try
                        {
                            var nesFile = new NesFile(newNesFilePath);
                            foreach (var code in codes)
                            {
                                try
                                {
                                    nesFile.PRG = GameGenie.Patch(nesFile.PRG, code.Trim());
                                }
                                catch (GameGenieFormatException)
                                {
                                    ShowError(new GameGenieFormatException(string.Format(Resources.GameGenieFormatError, code, game)), dontStop: true);
                                }
                                catch (GameGenieNotFoundException)
                                {
                                    ShowError(new GameGenieNotFoundException(string.Format(Resources.GameGenieNotFound, code, game.Name)), dontStop: true);
                                }
                            }
                            nesFile.Save(newNesFilePath);
                            var ggFilePath = Path.Combine(gameDir, NesGame.GameGenieFileName);
                            if (File.Exists(ggFilePath)) File.Delete(ggFilePath);
                        }
                        catch // in case of FDS game... just ignore
                        {
                        }
                    }
                }
                if (element is NesMenuFolder)
                {
                    var folder = element as NesMenuFolder;
                    if (!stats.allMenus.Contains(folder.Child))
                    {
                        stats.allMenus.Add(folder.Child);
                        AddMenu(folder.Child, stats);
                    }
                    if (stats.GamesStart == 0)
                    {
                        int childIndex = stats.allMenus.IndexOf(folder.Child);
                        var folderDir = Path.Combine(targetDirectory, folder.Code);
                        folder.Save(folderDir, childIndex);
                    }
                }
                if (element is NesDefaultGame)
                {
                    if (stats.GamesStart == 0)
                    {
                        var game = element as NesDefaultGame;
                        var gfilePath = Path.Combine(tempGamesDirectory, string.Format("gpath-{0}-{1}", game.Code, menuIndex));
                        Directory.CreateDirectory(Path.GetDirectoryName(gfilePath));
                        File.WriteAllText(gfilePath, menuIndex == 0 ? "." : string.Format("sub{0:D3}", menuIndex));
                    }
                }
            }
        }

        private bool ExecuteTool(string tool, string args, string directory = null, bool external = false)
        {
            byte[] output;
            return ExecuteTool(tool, args, out output, directory, external);
        }

        private bool ExecuteTool(string tool, string args, out byte[] output, string directory = null, bool external = false)
        {
            var process = new Process();
            var appDirectory = baseDirectory;
            var fileName = !external ? Path.Combine(toolsDirectory, tool) : tool;
            process.StartInfo.FileName = fileName;
            process.StartInfo.Arguments = args;
            if (string.IsNullOrEmpty(directory))
                directory = appDirectory;
            process.StartInfo.WorkingDirectory = directory;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(866);
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            Debug.WriteLine("Executing: " + fileName);
            Debug.WriteLine("Arguments: " + args);
            Debug.WriteLine("Directory: " + directory);
            process.Start();
            string outputStr = process.StandardOutput.ReadToEnd();
            string errorStr = process.StandardError.ReadToEnd();
            process.WaitForExit();
            output = Encoding.GetEncoding(866).GetBytes(outputStr);
            Debug.WriteLineIf(outputStr.Length > 0 && outputStr.Length < 300, "Output:\r\n" + outputStr);
            Debug.WriteLineIf(errorStr.Length > 0, "Errors:\r\n" + errorStr);
            Debug.WriteLine("Exit code: " + process.ExitCode);
            return process.ExitCode == 0;
        }

        static UInt32 CalKernelSize(byte[] header)
        {
            if (Encoding.ASCII.GetString(header, 0, 8) != "ANDROID!") throw new Exception(Resources.InvalidKernelHeader);
            UInt32 kernel_size = (UInt32)(header[8] | (header[9] * 0x100) | (header[10] * 0x10000) | (header[11] * 0x1000000));
            UInt32 kernel_addr = (UInt32)(header[12] | (header[13] * 0x100) | (header[14] * 0x10000) | (header[15] * 0x1000000));
            UInt32 ramdisk_size = (UInt32)(header[16] | (header[17] * 0x100) | (header[18] * 0x10000) | (header[19] * 0x1000000));
            UInt32 ramdisk_addr = (UInt32)(header[20] | (header[21] * 0x100) | (header[22] * 0x10000) | (header[23] * 0x1000000));
            UInt32 second_size = (UInt32)(header[24] | (header[25] * 0x100) | (header[26] * 0x10000) | (header[27] * 0x1000000));
            UInt32 second_addr = (UInt32)(header[28] | (header[29] * 0x100) | (header[30] * 0x10000) | (header[31] * 0x1000000));
            UInt32 tags_addr = (UInt32)(header[32] | (header[33] * 0x100) | (header[34] * 0x10000) | (header[35] * 0x1000000));
            UInt32 page_size = (UInt32)(header[36] | (header[37] * 0x100) | (header[38] * 0x10000) | (header[39] * 0x1000000));
            UInt32 dt_size = (UInt32)(header[40] | (header[41] * 0x100) | (header[42] * 0x10000) | (header[43] * 0x1000000));
            UInt32 pages = 1;
            pages += (kernel_size + page_size - 1) / page_size;
            pages += (ramdisk_size + page_size - 1) / page_size;
            pages += (second_size + page_size - 1) / page_size;
            pages += (dt_size + page_size - 1) / page_size;
            return pages * page_size;
        }

        private static long DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
        {
            long size = 0;
            // Get the subdirectories for the specified directory.
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: "
                    + sourceDirName);
            }

            DirectoryInfo[] dirs = dir.GetDirectories();
            // If the destination directory doesn't exist, create it.
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                size += file.CopyTo(temppath, true).Length;
            }

            // If copying subdirectories, copy them and their contents to new location.
            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    size += DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }
            }
            return size;
        }

        bool YesForAllPatches = false;
        public NesGame AddGames(string gamesDirectory, string[] files, Form parentForm = null)
        {
            NesGame nesGame = null;
            bool NoForAllUnsupportedMappers = false;
            YesForAllPatches = false;
            if (parentForm == null) parentForm = this;
            int count = 0;
            foreach (var file in files)
            {
                try
                {
                    var nesFileName = file;
                    var ext = Path.GetExtension(file).ToLower();
                    bool? needPatch = YesForAllPatches ? (bool?)true : null;
                    byte[] rawData = null;
                    if (ext == ".7z" || ext == ".zip" || ext == ".rar")
                    {
                        SevenZipExtractor.SetLibraryPath(Path.Combine(baseDirectory, IntPtr.Size == 8 ? @"tools\7z64.dll" : @"tools\7z.dll"));
                        var szExtractor = new SevenZipExtractor(file);
                        var filesInArchive = new List<string>();
                        foreach (var f in szExtractor.ArchiveFileNames)
                        {
                            var e = Path.GetExtension(f).ToLower();
                            if (e == ".nes" || e == ".fds")
                                filesInArchive.Add(f);
                        }
                        if (filesInArchive.Count == 1)
                        {
                            nesFileName = filesInArchive[0];
                        }
                        else
                        {
                            var fsForm = new SelectFileForm(filesInArchive.ToArray());
                            if (fsForm.ShowDialog() == DialogResult.OK)
                                nesFileName = (string)fsForm.listBoxFiles.SelectedItem;
                            else
                                continue;
                        }
                        var o = new MemoryStream();
                        szExtractor.ExtractFile(nesFileName, o);
                        rawData = new byte[szExtractor.ArchiveFileData[0].Size];
                        o.Seek(0, SeekOrigin.Begin);
                        o.Read(rawData, 0, rawData.Length);
                    }
                    try
                    {
                        nesGame = new NesGame(gamesDirectory, nesFileName, NoForAllUnsupportedMappers ? (bool?)false : null, ref needPatch, needPatchCallback, this, rawData);

                        // Trying to import Game Genie codes
                        var lGameGeniePath = Path.Combine(Path.GetDirectoryName(nesFileName), Path.GetFileNameWithoutExtension(nesFileName) + ".xml");
                        if (File.Exists(lGameGeniePath))
                        {
                            GameGenieDataBase lGameGenieDataBase = new GameGenieDataBase(nesGame);
                            lGameGenieDataBase.ImportCodes(lGameGeniePath, true);
                            lGameGenieDataBase.Save();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex is UnsupportedMapperException || ex is UnsupportedFourScreenException)
                        {
                            MessageBoxResult = DialogResult.None;
                            MessageBoxFromThread(this,
                                (ex is UnsupportedMapperException)
                                   ? string.Format(Resources.MapperNotSupported, Path.GetFileName(file), (ex as UnsupportedMapperException).ROM.Mapper)
                                   : string.Format(Resources.FourScreenNotSupported, Path.GetFileName(file)),
                                Resources.AreYouSure,
                                files.Length <= 1 ? MessageBoxButtons.YesNo : MessageBoxButtons.YesNoCancel,
                                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                            while (MessageBoxResult == DialogResult.None) Thread.Sleep(100);
                            if (MessageBoxResult == DialogResult.Yes)
                                nesGame = new NesGame(gamesDirectory, nesFileName, true, ref needPatch, needPatchCallback, this, rawData);
                            else if (MessageBoxResult == System.Windows.Forms.DialogResult.Cancel)
                            {
                                NoForAllUnsupportedMappers = true;
                            }
                        }
                        else throw ex;
                    }
                    ConfigIni.SelectedGames += ";" + nesGame.Code;
                }
                catch (Exception ex)
                {
                    if (ex is ThreadAbortException) return null;
                    Debug.WriteLine(ex.Message + ex.StackTrace);
                    MessageBoxFromThread(this, ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
                SetProgress(++count, files.Length);
            }
            return nesGame; // Last added game if any
        }

        private bool needPatchCallback(Form parentForm, string nesFileName)
        {
            MessageBoxResult = DialogResult.None;
            if (GamesToAdd == null || GamesToAdd.Length <= 1)
            {
                MessageBoxFromThread(parentForm,
                    string.Format(Resources.PatchQ, Path.GetFileName(nesFileName)),
                    Resources.PatchAvailable,
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button1);
                while (MessageBoxResult == DialogResult.None) Thread.Sleep(100);
                return MessageBoxResult == DialogResult.Yes;
            }
            else
            {
                MessageBoxFromThread(parentForm,
                    string.Format(Resources.PatchQ, Path.GetFileName(nesFileName)),
                    Resources.PatchAvailable,
                    MessageBoxButtons.AbortRetryIgnore,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2);
                while (MessageBoxResult == DialogResult.None) Thread.Sleep(100);
                if (MessageBoxResult == DialogResult.Abort)
                    YesForAllPatches = true;
                return MessageBoxResult != DialogResult.Ignore;
            }
        }

        private void WorkerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((thread != null) && (e.CloseReason == CloseReason.UserClosing))
            {
                if (MessageBox.Show(this, Resources.DoYouWantCancel, Resources.AreYouSure, MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
                    == System.Windows.Forms.DialogResult.No)
                    e.Cancel = true;
                else if (thread != null) thread.Abort();
            }
        }
    }
}