private static void ExtractISO(Stream isofile, string tfolder = "export/") { if (oldui) { using (var iso = new ISOFileReader(isofile)) { var idxs = new List<IDXFile>(); var idxnames = new List<string>(); int i = 0; foreach (FileDescriptor file in iso) { ++i; string filename = file.FullName; if (filename.EndsWith(".IDX")) { idxs.Add(new IDXFile(iso.GetFileStream(file))); idxnames.Add(Path.GetFileNameWithoutExtension(filename)); //continue; //Write the IDX too } else if (filename.EndsWith(".IMG") && idxnames.Contains(Path.GetFileNameWithoutExtension(filename))) { continue; } Console.WriteLine("[ISO: {0,3}]\tExtracting {1}", i, filename); filename = Path.GetFullPath(tfolder + "ISO/" + filename); try { Directory.CreateDirectory(Path.GetDirectoryName(filename)); } catch (IOException e) { WriteError("Failed creating directory: {0}", e.Message); continue; } using (var output = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { iso.CopyFile(file, output); } } for (i = 0; i < idxs.Count; ++i) { try { FileDescriptor file = iso.FindFile(idxnames[i] + ".IMG"); using (Substream img = iso.GetFileStream(file)) { ConsoleProgress consoleProgress = new ConsoleProgress(1L, null, ConsoleColor.Green, false); ExtractIDX(idxs[i], img, consoleProgress,true,tfolder + "" + idxnames[i] + "/", idxnames[i]); } } catch (FileNotFoundException) { WriteError("ERROR: Failed to find matching IMG for IDX"); } } } } else { ConsoleProgress consoleProgress = new ConsoleProgress(1L, null, ConsoleColor.Green); Dictionary<string, IDXFile> dictionary = new Dictionary<string, IDXFile>(); using (var iso = new ISOFileReader(isofile)) { var idxs = new List<IDXFile>(); var idxnames = new List<string>(); int i = 0; foreach (FileDescriptor file in iso) { ++i; string filename = file.FullName; if (filename.EndsWith(".IDX")) { idxs.Add(new IDXFile(iso.GetFileStream(file))); idxnames.Add(Path.GetFileNameWithoutExtension(filename)); //continue; //Write the IDX too } else if (filename.EndsWith(".IMG") && idxnames.Contains(Path.GetFileNameWithoutExtension(filename))) { continue; } if (UISwitch) { consoleProgress.Text = string.Format("Extracting [{0}] {1}", "ISO", filename); } else { decimal nmbpercent = (((decimal)consoleProgress.Current / (decimal)consoleProgress.Total) * 100); consoleProgress.Text = string.Format(" [{0}% Done]", (int)nmbpercent); } long total; consoleProgress.Total = (total = consoleProgress.Total) + 1L; consoleProgress.Update(total); filename = Path.GetFullPath(tfolder + "ISO/" + filename); try { Directory.CreateDirectory(Path.GetDirectoryName(filename)); } catch (IOException e) { consoleProgress.Color = ConsoleColor.DarkRed; consoleProgress.ReDraw(); WriteError("Failed creating directory: {0}", e.Message); continue; } using (var output = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { iso.CopyFile(file, output); } } consoleProgress.Total += dictionary.Sum((KeyValuePair<string, IDXFile> kvp) => (long)((ulong)kvp.Value.Count)); for (i = 0; i < idxs.Count; ++i) { try { FileDescriptor file = iso.FindFile(idxnames[i] + ".IMG"); using (Substream img = iso.GetFileStream(file)) { ExtractIDX(idxs[i], img, consoleProgress , true, tfolder + "" + idxnames[i] + "/", idxnames[i]); } } catch (FileNotFoundException) { consoleProgress.Color = ConsoleColor.DarkRed; consoleProgress.ReDraw(); WriteError("ERROR: Failed to find matching IMG for IDX"); } } consoleProgress.Text = "Done extracting."; consoleProgress.Finish(); } } }
/// <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(); } } }