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) { var dtePatches = DoDteCrap(dteFiles, worker, args); if (dtePatches == null) { args.Cancel = true; return; } patches.AddRange(dtePatches); } 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 }); } }
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 }); } }
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) { IDictionary <byte, string> dteMap; var dtePatches = DoDteCrap(dteFiles, worker, args, out dteMap); if (dtePatches == null) { args.Cancel = true; return; } patches.AddRange(dtePatches); patches.AddRange(GetCharMapPatches(stream, dteFiles[0].Context, CharMap, dteMap)); } else { patches.AddRange(RemoveCharMapPatches(stream, files[0].Context)); } 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; } if (customGlyphs != null) { if (this.Filetype == Context.US_PSX) { const int battleBinOffset = 0xE7614; const int fontBinOffset = 0; const int worldBinOffset = 0x5B8f8; const int battleBinWidthOffset = 0xFF0FC; const int worldBinWidthOffset = 0x733E0; foreach (Glyph g in customGlyphs) { var glyphBytes = g.ToByteArray(); byte[] widthBytes = new byte[] { g.Width }; patches.Add(new PatchedByteArray(PsxIso.Sectors.BATTLE_BIN, battleBinOffset + g.Index * 14 * 10 / 4, glyphBytes)); patches.Add(new PatchedByteArray(PsxIso.Sectors.EVENT_FONT_BIN, fontBinOffset + g.Index * 14 * 10 / 4, glyphBytes)); patches.Add(new PatchedByteArray(PsxIso.Sectors.WORLD_WORLD_BIN, worldBinOffset + g.Index * 14 * 10 / 4, glyphBytes)); patches.Add(new PatchedByteArray(PsxIso.Sectors.BATTLE_BIN, battleBinWidthOffset + g.Index, widthBytes)); patches.Add(new PatchedByteArray(PsxIso.Sectors.WORLD_WORLD_BIN, worldBinWidthOffset + g.Index, widthBytes)); } } else { const int fontOffset1 = 0x27B80C; const int fontOffset2 = 0x2F73B8; const int widthOffset1 = 0x293F40; const int widthOffset2 = 0x30FAC0; foreach (Glyph g in customGlyphs) { var glyphBytes = g.ToByteArray(); byte[] widthBytes = new byte[] { g.Width }; patches.Add(new PatchedByteArray(PspIso.Sectors.PSP_GAME_SYSDIR_BOOT_BIN, fontOffset1 + g.Index * 14 * 10 / 4, glyphBytes)); patches.Add(new PatchedByteArray(PspIso.Sectors.PSP_GAME_SYSDIR_BOOT_BIN, fontOffset2 + g.Index * 14 * 10 / 4, glyphBytes)); patches.Add(new PatchedByteArray(PspIso.Sectors.PSP_GAME_SYSDIR_BOOT_BIN, widthOffset1 + g.Index, widthBytes)); patches.Add(new PatchedByteArray(PspIso.Sectors.PSP_GAME_SYSDIR_BOOT_BIN, widthOffset2 + g.Index, widthBytes)); patches.Add(new PatchedByteArray(PspIso.Sectors.PSP_GAME_SYSDIR_EBOOT_BIN, fontOffset1 + g.Index * 14 * 10 / 4, glyphBytes)); patches.Add(new PatchedByteArray(PspIso.Sectors.PSP_GAME_SYSDIR_EBOOT_BIN, fontOffset2 + g.Index * 14 * 10 / 4, glyphBytes)); patches.Add(new PatchedByteArray(PspIso.Sectors.PSP_GAME_SYSDIR_EBOOT_BIN, widthOffset1 + g.Index, widthBytes)); patches.Add(new PatchedByteArray(PspIso.Sectors.PSP_GAME_SYSDIR_EBOOT_BIN, widthOffset2 + g.Index, widthBytes)); } } } 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 }); } }