Esempio n. 1
0
        public void BuildAndApplyPatches(object sender, DoWorkEventArgs args)
        {
            BackgroundWorker worker    = sender as BackgroundWorker;
            PatchIsoArgs     patchArgs = args.Argument as PatchIsoArgs;

            if (patchArgs == null)
            {
                throw new Exception("Incorrect args passed to BuildAndApplyPatches");
            }
            if (patchArgs.Patcher == null)
            {
                throw new ArgumentNullException("Patcher", "Patcher cannot be null");
            }

            using (Stream stream = File.Open(patchArgs.Filename, FileMode.Open, FileAccess.ReadWrite))
            {
                if (stream == null)
                {
                    throw new Exception("Could not open ISO file");
                }

                IList <ISerializableFile> files = new List <ISerializableFile>(Files.Count);
                Files.FindAll(f => f is ISerializableFile).ForEach(f => files.Add((ISerializableFile)f));

                List <ISerializableFile> dteFiles    = new List <ISerializableFile>();
                List <ISerializableFile> nonDteFiles = new List <ISerializableFile>();
                List <PatchedByteArray>  patches     = new List <PatchedByteArray>();

                foreach (ISerializableFile file in files)
                {
                    worker.ReportProgress(0,
                                          new ProgressForm.FileProgress {
                        File = file, State = ProgressForm.TaskState.Starting, Task = ProgressForm.Task.IsDteNeeded
                    });
                    if (file.IsDteNeeded())
                    {
                        dteFiles.Add(file);
                    }
                    else
                    {
                        nonDteFiles.Add(file);
                    }
                    worker.ReportProgress(0,
                                          new ProgressForm.FileProgress {
                        File = file, State = ProgressForm.TaskState.Done, Task = ProgressForm.Task.IsDteNeeded
                    });
                    if (worker.CancellationPending)
                    {
                        args.Cancel = true;
                        return;
                    }
                }
                if (dteFiles.Count > 0)
                {
                    Set <string> pairs = DTE.GetDteGroups(this.Filetype);
                    if (worker.CancellationPending)
                    {
                        args.Cancel = true;
                        return;
                    }
                    Set <KeyValuePair <string, byte> > currentPairs =
                        new Set <KeyValuePair <string, byte> >((x, y) => x.Key.Equals(y.Key) && (x.Value == y.Value) ? 0 : -1);
                    var filePreferredPairs =
                        new Dictionary <ISerializableFile, Set <KeyValuePair <string, byte> > >(dteFiles.Count);
                    dteFiles.Sort((x, y) => (y.ToCDByteArray().Length - y.Layout.Size).CompareTo(x.ToCDByteArray().Length - x.Layout.Size));
                    Stack <byte> dteBytes = DTE.GetAllowedDteBytes();
                    if (worker.CancellationPending)
                    {
                        args.Cancel = true;
                        return;
                    }

                    foreach (var dte in dteFiles)
                    {
                        worker.ReportProgress(0,
                                              new ProgressForm.FileProgress {
                            File = dte, State = ProgressForm.TaskState.Starting, Task = ProgressForm.Task.CalculateDte
                        });
                        filePreferredPairs[dte] = dte.GetPreferredDTEPairs(pairs, currentPairs, dteBytes);
                        currentPairs.AddRange(filePreferredPairs[dte]);
                        if (filePreferredPairs[dte] == null)
                        {
                            throw new DTE.DteException(dte);
                        }
                        worker.ReportProgress(0,
                                              new ProgressForm.FileProgress {
                            File = dte, State = ProgressForm.TaskState.Done, Task = ProgressForm.Task.CalculateDte
                        });
                        if (worker.CancellationPending)
                        {
                            args.Cancel = true;
                            return;
                        }
                    }

                    foreach (var file in dteFiles)
                    {
                        worker.ReportProgress(0,
                                              new ProgressForm.FileProgress {
                            File = file, State = ProgressForm.TaskState.Starting, Task = ProgressForm.Task.GeneratePatch
                        });
                        var currentFileEncoding = PatcherLib.Utilities.Utilities.DictionaryFromKVPs(filePreferredPairs[file]);
                        patches.AddRange(file.GetDtePatches(currentFileEncoding));
                        worker.ReportProgress(0,
                                              new ProgressForm.FileProgress {
                            File = file, State = ProgressForm.TaskState.Done, Task = ProgressForm.Task.GeneratePatch
                        });
                        if (worker.CancellationPending)
                        {
                            args.Cancel = true;
                            return;
                        }
                    }

                    patches.AddRange(DTE.GenerateDtePatches(this.Filetype, currentPairs));
                }

                foreach (var file in nonDteFiles)
                {
                    worker.ReportProgress(0,
                                          new ProgressForm.FileProgress {
                        File = file, State = ProgressForm.TaskState.Starting, Task = ProgressForm.Task.GeneratePatch
                    });
                    patches.AddRange(file.GetNonDtePatches());
                    worker.ReportProgress(0,
                                          new ProgressForm.FileProgress {
                        File = file, State = ProgressForm.TaskState.Done, Task = ProgressForm.Task.GeneratePatch
                    });
                    if (worker.CancellationPending)
                    {
                        args.Cancel = true;
                        return;
                    }
                }

                if (worker.CancellationPending)
                {
                    args.Cancel = true;
                    return;
                }

                worker.ReportProgress(0,
                                      new ProgressForm.FileProgress {
                    File = null, State = ProgressForm.TaskState.Starting, Task = ProgressForm.Task.ApplyingPatches
                });
                patchArgs.Patcher(stream, patches);
                worker.ReportProgress(0,
                                      new ProgressForm.FileProgress {
                    File = null, State = ProgressForm.TaskState.Done, Task = ProgressForm.Task.ApplyingPatches
                });
            }
        }
Esempio n. 2
0
        private IList <PatchedByteArray> DoDteCrap(IList <ISerializableFile> dteFiles, BackgroundWorker worker, DoWorkEventArgs args)
        {
            List <PatchedByteArray> patches = new List <PatchedByteArray>();

            if (worker.CancellationPending)
            {
                args.Cancel = true;
                return(null);
            }

            dteFiles.Sort((x, y) => (y.ToCDByteArray().Length - y.Layout.Size).CompareTo(x.ToCDByteArray().Length - x.Layout.Size));
            if (worker.CancellationPending)
            {
                args.Cancel = true;
                return(null);
            }

            IDictionary <ISerializableFile, Set <KeyValuePair <string, byte> > > filePreferredPairs = null;
            Set <KeyValuePair <string, byte> > currentPairs = null;

            DteResult result = DteResult.Empty;

            if (dteFiles.Count > 0)
            {
                int tries = dteFiles.Count;
                //DteResult result = DoDteForFiles( dteFiles, worker, args, out filePreferredPairs, out currentPairs );
                do
                {
                    result = DoDteForFiles(dteFiles, worker, args, out filePreferredPairs, out currentPairs);
                    switch (result.ResultCode)
                    {
                    case DteResult.Result.Cancelled:
                        args.Cancel = true;
                        return(null);

                    case DteResult.Result.Fail:
                        var failedFile = result.FailedFile;
                        if (dteFiles[0] == failedFile)
                        {
                            // Failed on the first file... this is hopeless
                            throw new FFTPatcher.TextEditor.DTE.DteException(failedFile);
                        }

                        // Bump the failed file to the top of the list
                        dteFiles.Remove(failedFile);
                        dteFiles.Insert(0, failedFile);
                        break;

                    case DteResult.Result.Success:
                        // do nothing
                        break;
                    }
                } while (result.ResultCode != DteResult.Result.Success && --tries >= 0);
            }

            switch (result.ResultCode)
            {
            case DteResult.Result.Fail:
                throw new FFTPatcher.TextEditor.DTE.DteException(dteFiles[0]);

            case DteResult.Result.Cancelled:
                args.Cancel = true;
                return(null);
            }

            foreach (var file in dteFiles)
            {
                worker.ReportProgress(0,
                                      new ProgressForm.FileProgress {
                    File = file, State = ProgressForm.TaskState.Starting, Task = ProgressForm.Task.GeneratePatch
                });
                var currentFileEncoding = PatcherLib.Utilities.Utilities.DictionaryFromKVPs(filePreferredPairs[file]);
                patches.AddRange(file.GetDtePatches(currentFileEncoding));
                worker.ReportProgress(0,
                                      new ProgressForm.FileProgress {
                    File = file, State = ProgressForm.TaskState.Done, Task = ProgressForm.Task.GeneratePatch
                });
                if (worker.CancellationPending)
                {
                    args.Cancel = true;
                    return(null);
                }
            }

            patches.AddRange(DTE.GenerateDtePatches(this.Filetype, currentPairs));
            return(patches.AsReadOnly());
        }