Exemplo n.º 1
0
 /// <summary>
 /// 输出批处理的日志
 /// </summary>
 /// <param name="worker"></param>
 /// <param name="loginfo"></param>
 public static void outputWorkerLog(MyBackgroundWorker worker, LogType lType, string loginfo)
 {
     worker.logType    = lType;
     worker.StatusText = loginfo;
     worker.ReportProgress(20);
     System.Threading.Thread.Sleep(100);
 }
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string             strID = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.ToString("hhmmss") + DateTime.Now.Millisecond.ToString();
        MyBackgroundWorker tmpBg = (sender as MyBackgroundWorker);

        tmpBg.Name = "bg_" + strID;
        ProgressBar pb = new ProgressBar();

        pb.Minimum = 0;
        pb.Maximum = 100;
        pb.Name    = "pb_" + strID;
        pb.Width   = txtNumber.Width;
        this.BeginInvoke((MethodInvoker) delegate
        {
            flowLayoutPanel1.Controls.Add(pb);
        });
        dct.Add(tmpBg.Name, pb.Name);
        int input = (int)e.Argument;

        for (int i = 1; i <= input; i++)
        {
            Console.WriteLine(tmpBg.Name + ": " + (i * 100 / input));           //for testing (this line slows the program down a lot)
            tmpBg.ReportProgress(i * 100 / input);
            if (tmpBg.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
        }
    }
Exemplo n.º 3
0
    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();
    }