/// <param name="idx">Stream of the idx inside the iso.</param> /// <param name="img">Stream of the img inside the iso.</param> /// <param name="imgd">File descriptor of the img file.</param> /// <param name="niso">New ISO.</param> /// <param name="IsOVL">Bool which define if the OVL should be patched</param> private static MemoryStream PatchIDX(Stream idx, Stream img, FileDescriptor imgd, ISOCopyWriter niso,ConsoleProgress progress, bool IsOVL = false) { using (idx) using (img) { niso.SeekEnd(); long imgOffset = niso.file.Position; MemoryStream idxms = PatchIDXInternal(idx, img, niso.file, imgOffset, progress, IsOVL ? 1u : 0u); imgd.ExtentLBA = (uint) imgOffset/2048; imgd.ExtentLength = (uint) (niso.file.Position - imgOffset); imgd.RecordingDate = DateTime.UtcNow; niso.PatchFile(imgd); niso.SeekEnd(); return idxms; } }
/// <param name="isofile">Original ISO</param> /// <param name="nisofile">New ISO file</param> private static void PatchISO(Stream isofile, Stream nisofile) { if (oldui) { using (var iso = new ISOFileReader(isofile)) using (var niso = new ISOCopyWriter(nisofile, iso)) { uint i = 0; Trivalent cKh2 = Patches.KH2Changed ? Trivalent.ChangesPending : Trivalent.NoChanges, cOvl = Patches.OVLChanged ? Trivalent.ChangesPending : Trivalent.NoChanges; bool cIso = Patches.ISOChanged; foreach (FileDescriptor file in iso) { Console.Write("[ISO: {0,4}]\t{1}", ++i, file.FullName); string name = file.FileName; if (name.EndsWith("KH2.IDX") || name.EndsWith("KH2.IMG")) { if (cKh2.HasFlag(Trivalent.ChangesPending)) { cKh2 = Trivalent.Changed; long lpos = niso.file.Position; if (oldui) { Console.WriteLine("\tRe-Building..."); } try { FileDescriptor img = iso.FindFile("KH2.IMG"), idx = iso.FindFile("KH2.IDX"); ConsoleProgress consoleProgress = new ConsoleProgress(1L, null, ConsoleColor.Green, false); using ( MemoryStream ms = PatchIDX(iso.GetFileStream(idx), iso.GetFileStream(img), img, niso, consoleProgress ) ) { idx.RecordingDate = DateTime.UtcNow; niso.AddFile2(idx, ms, name); } continue; } catch (Exception e) { WriteError(" Error creating IDX/IMG: {0}\n{1}", e.Message, e.StackTrace); niso.file.Position = lpos; } } else if (cKh2.HasFlag(Trivalent.Changed)) { Console.WriteLine("\tRe-Built"); continue; } } else if (name.EndsWith("OVL.IDX") || name.EndsWith("OVL.IMG")) { if (cOvl.HasFlag(Trivalent.ChangesPending)) { cOvl = Trivalent.Changed; long lpos = niso.file.Position; Console.WriteLine("\tRe-Building..."); try { FileDescriptor img = iso.FindFile("OVL.IMG"), idx = iso.FindFile("OVL.IDX"); ConsoleProgress consoleProgress = new ConsoleProgress(1L, null, ConsoleColor.Green, false); using ( MemoryStream ms = PatchIDX(iso.GetFileStream(idx), iso.GetFileStream(img), img, niso,consoleProgress, true)) { idx.RecordingDate = DateTime.UtcNow; niso.AddFile2(idx, ms, name); } continue; } catch (Exception e) { WriteError(" Error creating IDX/IMG: " + e.Message); niso.file.Position = lpos; } } else if (cOvl.HasFlag(Trivalent.Changed)) { Console.WriteLine("\tRe-Built"); continue; } } else if (cIso) { PatchManager.Patch patch; if (Patches.patches.TryGetValue(PatchManager.ToHash(name), out patch) && patch.IsinISO) { Console.WriteLine("\tPatching..."); file.RecordingDate = DateTime.UtcNow; niso.AddFile2(file, patch.Stream, name); continue; } } Console.WriteLine(""); niso.CopyFile(file); if (niso.SectorCount >= 0x230540) { WriteWarning( "Warning: This ISO has the size of a dual-layer ISO, but it isn't one. Some\nprograms may take a while to start while they search for the 2nd layer."); } } } } else { using (var iso = new ISOFileReader(isofile)) using (var niso = new ISOCopyWriter(nisofile, iso)) { ConsoleProgress consoleProgress = new ConsoleProgress((long)iso.Count<FileDescriptor>(), "Patching ISO...", ConsoleColor.Green); Trivalent cKh2 = Patches.KH2Changed ? Trivalent.ChangesPending : Trivalent.NoChanges, cOvl = Patches.OVLChanged ? Trivalent.ChangesPending : Trivalent.NoChanges; bool cIso = Patches.ISOChanged; foreach (FileDescriptor file in iso) { if (UISwitch) { consoleProgress.Text = string.Format("Adding [{0}] {1}", "ISO", file.FullName); } else { decimal nmbpercent = (((decimal)consoleProgress.Current / (decimal)consoleProgress.Total) * 100); consoleProgress.Text = string.Format(" [{0}% Done]", (int)nmbpercent); } consoleProgress.Increment(1L); string name = file.FileName; if (name.EndsWith("KH2.IDX") || name.EndsWith("KH2.IMG")) { if (cKh2.HasFlag(Trivalent.ChangesPending)) { cKh2 = Trivalent.Changed; long lpos = niso.file.Position; try { FileDescriptor img = iso.FindFile("KH2.IMG"), idx = iso.FindFile("KH2.IDX"); using ( MemoryStream ms = PatchIDX(iso.GetFileStream(idx), iso.GetFileStream(img), img, niso, consoleProgress, false) ) { idx.RecordingDate = DateTime.UtcNow; niso.AddFile2(idx, ms, name); } continue; } catch (Exception e) { WriteError(" Error creating IDX/IMG: {0}\n{1}", e.Message, e.StackTrace); niso.file.Position = lpos; } } else if (cKh2.HasFlag(Trivalent.Changed)) { continue; } } else if (name.EndsWith("OVL.IDX") || name.EndsWith("OVL.IMG")) { if (cOvl.HasFlag(Trivalent.ChangesPending)) { cOvl = Trivalent.Changed; long lpos = niso.file.Position; try { FileDescriptor img = iso.FindFile("OVL.IMG"), idx = iso.FindFile("OVL.IDX"); using ( MemoryStream ms = PatchIDX(iso.GetFileStream(idx), iso.GetFileStream(img), img, niso, consoleProgress, true )) { idx.RecordingDate = DateTime.UtcNow; niso.AddFile2(idx, ms, name); } continue; } catch (Exception e) { WriteError(" Error creating IDX/IMG: " + e.Message); niso.file.Position = lpos; } } else if (cOvl.HasFlag(Trivalent.Changed)) { continue; } } else if (cIso) { PatchManager.Patch patch; if (Patches.patches.TryGetValue(PatchManager.ToHash(name), out patch) && patch.IsinISO) { file.RecordingDate = DateTime.UtcNow; niso.AddFile2(file, patch.Stream, name); continue; } } niso.CopyFile(file); if (niso.SectorCount >= 0x230540) { WriteWarning( "Warning: This ISO has the size of a dual-layer ISO, but it isn't one. Some\nprograms may take a while to start while they search for the 2nd layer."); } } consoleProgress.Text = "Done patching."; consoleProgress.Finish(); } } }