Exemple #1
0
        private DteResult DoDteForFiles(IList <ISerializableFile> dteFiles, BackgroundWorker worker, DoWorkEventArgs args,
                                        out IDictionary <ISerializableFile, Set <KeyValuePair <string, byte> > > preferredPairs,
                                        out Set <KeyValuePair <string, byte> > dtePairs)
        {
            var filePreferredPairs =
                new Dictionary <ISerializableFile, Set <KeyValuePair <string, byte> > >(dteFiles.Count);
            Set <KeyValuePair <string, byte> > currentPairs =
                new Set <KeyValuePair <string, byte> >((x, y) => x.Key.Equals(y.Key) && (x.Value == y.Value) ? 0 : -1);
            Stack <byte> dteBytes = DTE.GetAllowedDteBytes();
            var          pairs    = DTE.GetDteGroups(this.Filetype);

            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, worker);
                if (filePreferredPairs[dte] == null)
                {
                    dtePairs       = null;
                    preferredPairs = null;
                    return(new DteResult {
                        ResultCode = DteResult.Result.Fail, FailedFile = dte
                    });
                }
                currentPairs.AddRange(filePreferredPairs[dte]);
                worker.ReportProgress(0,
                                      new ProgressForm.FileProgress {
                    File = dte, State = ProgressForm.TaskState.Done, Task = ProgressForm.Task.CalculateDte
                });
                if (worker.CancellationPending)
                {
                    args.Cancel    = true;
                    dtePairs       = null;
                    preferredPairs = null;
                    return(new DteResult {
                        ResultCode = DteResult.Result.Cancelled
                    });
                }
            }

            preferredPairs = new ReadOnlyDictionary <ISerializableFile, Set <KeyValuePair <string, byte> > >(filePreferredPairs);
            dtePairs       = currentPairs;
            return(new DteResult {
                ResultCode = DteResult.Result.Success
            });
        }
Exemple #2
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
                });
            }
        }