private static void ReplaceISO(Stream isofile) { var iso = new ISOFileReader (isofile); IDXFile KH2idx; IDXFile OVLidx; var KH2IDXName = iso.FindFile("KH2.IDX"); var OVLIDXName = iso.FindFile("OVL.IDX"); var KH2idxStream = iso.GetFileStream (KH2IDXName); var OVLidxStream = iso.GetFileStream (OVLIDXName); KH2idx = new IDXFile (KH2idxStream, leaveOpen: true); OVLidx = new IDXFile (OVLidxStream, leaveOpen: true); /*I need to add all sub hashes found in KH2.IMG: * case 0x0499386d: //000hb.idx case 0x0b2025ed: //000mu.idx case 0x2b87c9dc: //000di.idx case 0x2bb6ecb2: //000ca.idx case 0x35f6154a: //000al.idx case 0x3604eeef: //000tt.idx case 0x43887a92: //000po.idx case 0x4edb9e9e: //000gumi.idx case 0x608e02b4: //000es.idx case 0x60dd6d06: //000lk.idx case 0x79a2a329: //000eh.idx case 0x84eaa276: //000tr.idx case 0x904a97e0: //000wm.idx case 0xb0be1463: //000wi.idx case 0xd233219f: //000lm.idx case 0xe4633b6f: //000nm.idx case 0xeb89495d: //000bb.idx case 0xf87401c0: //000dc.idx case 0xff7a1379: //000he.idx */ Console.WriteLine ("I will now list all the hashes I got in memory."); foreach (IDXFile.IDXEntry idx in OVLidx) { Console.WriteLine ("{0}", String.Format("{0:X8}", idx.Hash)); } foreach (IDXFile.IDXEntry idx in KH2idx) { Console.WriteLine ("{0}", String.Format("{0:X8}", idx.Hash)); } foreach (var PatchEntry in Patches.patches) { Console.WriteLine ("Checking for hash {0} in ISO...", String.Format("{0:X8}", PatchEntry.Value.Hash)); try { /*KH2idx.FindEntryByHash(PatchEntry.Value.Hash); Console.Write("Hash found on KH2 IDX! Replacing..."); KH2idx.file.ReadUInt32(); KH2idx.file.ReadUInt32();//We don't care about compression flags/hashes do we? var IMGOffset = KH2idx.file.ReadUInt32() * 2048; //Such harmonious way to write new file size, isn't it? ;o WARNING: IT CRASHES KH2idxStream.Seek((int)KH2idxStream.Position, SeekOrigin.Begin); KH2idxStream.Write(PatchEntry.Value.CompressedSize); var KH2IMGName = iso.FindFile("KH2.IMG"); var KH2IMGStream = iso.GetFileStream (KH2IMGName); KH2IMGStream.Seek(IMGOffset, SeekOrigin.Begin); MemoryStream ms = new MemoryStream(); PatchEntry.Value.Stream.baseStream.CopyTo(ms); ms.ToArray(); KH2IMGStream.Write(ms.ToArray(), (int)KH2IMGStream.Position, ms.ToArray().Length);*/ Console.WriteLine ("Done!"); } catch { try { /* OVLidx.FindEntryByHash(PatchEntry.Value.Hash); Console.Write("Hash found on OVL IDX! Replacing..."); OVLidx.file.ReadUInt32(); OVLidx.file.ReadUInt32();//We don't care about compression flags/hashes do we? var IMGOffset = OVLidx.file.ReadUInt32() * 2048; OVLidxStream.Seek((int)OVLidxStream.Position, SeekOrigin.Begin); //Such harmonious way to write new file size, isn't it? ;o //OVLidxStream.Write(PatchEntry.Value.CompressedSize); var OVLIMGName = iso.FindFile("OVL.IMG"); var OVLIMGStream = iso.GetFileStream (OVLIMGName); OVLIMGStream.Seek(IMGOffset, SeekOrigin.Begin); MemoryStream ms = new MemoryStream(); PatchEntry.Value.Stream.baseStream.CopyTo(ms); byte[] PatchFile = ms.ToArray(); int FileLength = PatchFile.Length; int offset2 = (int)OVLIMGStream.Position; OVLIMGStream.Write(PatchFile, offset2, FileLength);*/ } catch { WriteError("No matching IDX entry were found in KH2 or OVL! Aborting replacing process..."); } } } }
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(); } } }