/// <summary> /// <para>Clean up unmanaged resources</para> /// <para>Also try to delete ISO is not finalized</para> /// </summary> /// <param name="disposing">True is called from <c>Dispose</c>, False if from finalizer</param> protected virtual void Dispose(bool disposing) { if (bw != null) { bw.Flush(); bw.Close(); bw = null; } if (iso != null) { iso.Close(); if (!finalized) { try { // File.Delete(iso.Name); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ISO was never finalized, deleting incomplete file!"); Console.ResetColor(); } catch (Exception e) { Console.Write("Cannot delete {0}!\nDebug infos: {1}", iso.Name, e); } } iso = null; } if (origISO != null) { origISO = null; } }
/// <summary>Create a new <c>KH1ISOWriter</c></summary> /// <param name="filename">Path to the ISO file to create</param> /// <param name="orig">A <c>KH1ISOReader</c> instance, used to copy data from</param> /// <param name="upHeaders">Whether to update ISO 9660 headers (Recommended to do so)</param> public KH1ISOWriter(string filename, KH1ISOReader orig = null, bool upHeaders = true) { iso = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None); bw = new BinaryWriter(iso); origISO = orig; updateISOHeaders = upHeaders; }
private static void ExtractISO(string iso2, KH1ISOReader input, string tfolder = "export/") { var isofile = new FileStream(iso2, FileMode.Open, FileAccess.Read); using (var iso = new ISOFileReader(isofile)) { var idxnames = new List <string>(); int a = 0; foreach (FileDescriptor file in iso) { a++; string filename = file.FullName; if (filename.EndsWith(".IDX")) { //KH1ISOReader(); //idxs.Add(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}", a, 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); } } ExtractIDX(input); //, tfolder + "" + idxnames[i] + "/", idxnames[i] } }
private static void ExtractIDX(KH1ISOReader input, string tfolder = "export/") { for (int i = 0, idxC = input.idxEntries.Count; i < idxC; ++i) { IDXEntry entry = input.idxEntries[i]; bool compressed = entry.flags == 1; string name2; if (!HashList.pairs.TryGetValue(entry.hash, out name2)) { name2 = String.Format("@noname/{0:x8}.bin", entry.hash); } if (name2 == "system.cnf") { continue; } if (name2 == "SLPS_251.98") { continue; } if (name2 == "ioprp250.img") { continue; } if (name2 == "sio2man.irx") { continue; } if (name2 == "sio2d.irx") { continue; } if (name2 == "dbcman.irx") { continue; } if (name2 == "ds2o.irx") { continue; } if (name2 == "mcman.irx") { continue; } if (name2 == "mcserv.irx") { continue; } if (name2 == "libsd.irx") { continue; } if (name2 == "libssl.irx") { continue; } if (name2 == "dev9.irx") { continue; } if (name2 == "atad.irx") { continue; } if (name2 == "hdd.irx") { continue; } if (name2 == "pfs.irx") { continue; } if (name2 == "kingdom.idx") { continue; } if (name2 == "kingdom.img") { continue; } var idxs = new List <Tuple <IDXFile, string> >(); if (_advanced) { Console.WriteLine("-----------File {0,4}/{1}, using KINGDOM.IDX\n", i, input.idxEntries.Count - 1); Console.WriteLine("Hashed filename: {0}", entry.hash); Console.WriteLine("Compression flags: {0}", compressed); Console.WriteLine("Size (packed): {0}", entry.size); Console.WriteLine("Real name: {0}", name2); } else { Console.WriteLine("[KINGDOM: {0,4}/{1}]\tExtracting {2}", i, input.idxEntries.Count - 1, name2); } string filename = tfolder + "/KINGDOM/" + name2; Directory.CreateDirectory(Path.GetDirectoryName(filename)); bool adSize = _advanced; byte[] file = input.readFile(entry); if (compressed) { if (entry.size > int.MaxValue) { throw new NotSupportedException("File to big to decompress"); } try { byte[] decompressed = KH1Compressor.decompress(file, adSize); File.WriteAllBytes(filename, decompressed); } catch (Exception e) { WriteError(" ERROR: Failed to decompress: " + e.Message); } } else { File.WriteAllBytes(filename, file); } } }
private static void PatchISO(KH1ISOReader input, KH1ISOWriter output, PatchManager files, bool ocompress, string oextHead) { int number = 1; output.writeHeader(oextHead);//Let's copy the header //Console.WriteLine("Adding header using {0} source", ? "external" : "internal");//TODO: Delete this Console.WriteLine for (int i = 0, idxC = input.idxEntries.Count; i < idxC; ++i) { IDXEntry entry = input.idxEntries[i]; if (entry.hash == 0x0392ebe4) { continue; } //kingdom.img if (entry.hash == 0x0393eba4) //kingdom.idx { Console.WriteLine("[KINGDOM: {0}/{1}]\tKINGDOM.IDX", number, input.idxEntries.Count - 1); //-1 'cause of the file KINGDOM.IMG number++; output.writeDummyIDX(idxC); continue; } //Loading the patch UInt32 flags = 0; PatchManager.Patch patch; Stream s = null; uint h = 0; // Could make sure the parents match perfectly, but there's only 1 of every name anyway. // So I'll settle for just making sure the file isn't made for the ISO. if (Patches.patches.TryGetValue(entry.hash, out patch) && /*patch.Parent == parenthash*/ !patch.IsinISO) { s = patch.Stream; h = patch.Hash; } string name; if (!HashList.pairs.TryGetValue(entry.hash, out name)) { name = String.Format("@noname/{0:x8}.bin", entry.hash); } if (s == null) { flags = entry.flags;//FLAGS NEEDS TO BE OR 0 IF NOTHING, OR 1 IF COMPRESSED, OR THE NAME TO RELINK TO IF IT NEEDS TO if (h != 0) { if (patch.Compressed) { flags = 1; } else if (patch.IsRelink) { flags = patch.Relink; } } if (flags > 1) { Console.WriteLine("[KINGDOM: {0}/{1}]\t{2}\tRelinking...", number, input.idxEntries.Count - 1, name); //-1 'cause of the file KINGDOM.IMG number++; /*if (!HashList.pairs.TryGetValue(flags, out name)) * { * name = String.Format("@noname/{0:x8}.bin", flags); * } * Console.WriteLine("{0}", name);*///Err...what is this crappy code? No but srsly output.addRelink(entry.hash, flags); } else { Console.WriteLine("[KINGDOM: {0}/{1}]\t{2}", number, input.idxEntries.Count - 1, name); //-1 'cause of the file KINGDOM.IMG number++; output.copyFile(entry); } } else { Console.WriteLine("[KINGDOM: {0}/{1}]\t{2}\tPatching...", number, input.idxEntries.Count - 1, name); //-1 'cause of the file KINGDOM.IMG number++; output.importFile(s, entry.hash, flags); } } output.finalize(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("New ISO finished!"); Console.ResetColor(); }
private static void Main(string[] args) { bool obatch = false; try { // Some functions work with raw bits, so we HAVE to be using little endian. // .NET, however, supports running the same code on both types. if (BitConverter.IsLittleEndian != true) { throw new PlatformNotSupportedException( "Platform not supported, not using a little endian bitconverter"); } HashList.loadHashPairs(); Console.Title = KH1ISOReader.program.ProductName + " " + KH1ISOReader.program.FileVersion + " [" + KH1ISOReader.program.CompanyName + "]"; bool ocompress = true, oupdateHeads = true, extract = false; string iso = "", oextHead = "", NewIso; #region Arguments for (int i = 0, argc = args.Length; i < argc; ++i) { switch (args[i].ToLower()) { case "-batch": obatch = true; break; case "-nocompress": ocompress = false; break; case "-extractor": extract = true; break; case "-advancedinfo": _advanced = true; break; #if DEBUG case "-noupisohead": oupdateHeads = false; break; case "-externalhead": if (++i < argc && (oextHead = args[i]).Length != 0) { break; } oextHead = "KH1ISOMake-head.bin"; break; #endif case "-patchmaker": KH1_Patch_Maker.Program.Mainp(args); break; default: if (File.Exists(args[i])) { if (args[i].EndsWith(".iso", StringComparison.InvariantCultureIgnoreCase)) { iso = args[i]; } else if (args[i].EndsWith(".kh1patch", StringComparison.InvariantCultureIgnoreCase)) { Patches.AddPatch(args[i]); } } break; } } #endregion #region Description using (var files = new PatchManager()) { if (iso.Length == 0) { iso = "KHFM.ISO"; } Console.ForegroundColor = ConsoleColor.Gray; DateTime Builddate = RetrieveLinkerTimestamp(); Console.Write("{0}\nBuild Date: {2}\nVersion {1}", KH1ISOReader.program.ProductName, KH1ISOReader.program.FileVersion, Builddate); Console.ResetColor(); #if DEBUG Console.ForegroundColor = ConsoleColor.Red; Console.Write("\nPRIVATE RELEASE\n"); Console.ResetColor(); #else Console.Write("\nPUBLIC RELEASE\n"); #endif Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.Write( "\nProgrammed by {0}\nhttp://www.govanify.com\nhttp://www.twitter.com/GovanifY", KH1ISOReader.program.CompanyName); Console.ForegroundColor = ConsoleColor.Gray; if (extract) { Console.Write( "\n\nThis tool is able to extract the files of the game Kingdom Hearts 1(Final Mix).\nHe's using a list for extracting files with their real name which isn't completeBut this is the most complete one for now.\nHe can extract the files stored which got a reference into KINGDOM.IDX.\n\n"); } else { Console.Write( "\n\nThis tool is able to patch the game Kingdom Hearts 1(Final Mix).\nHe can modify iso files, like the elf and internal files,\nwich are stored inside the hidden file KINGDOM.IMG\nThis tool is recreating too new hashes into the idx files for avoid\na corrupted game. He can add some files too.\n\n"); } Console.ForegroundColor = ConsoleColor.Green; Console.Write("\nPress enter to run using the file:"); Console.ResetColor(); Console.Write(" {0}", iso); Console.ReadLine(); #endregion NewIso = Path.ChangeExtension(iso, "NEW.ISO"); using (var input = new KH1ISOReader(iso)) { if (extract) { ExtractISO(iso, input); } else { if (Patches.patches.Count == 0) { WriteWarning("No patches loaded!"); } else { try { using (var output = new KH1ISOWriter(NewIso, input, oupdateHeads)) { PatchISO(input, output, files, ocompress, oextHead); } } catch (Exception) { //Delete the new "incomplete" iso File.Delete(NewIso); throw; } } } } } } catch (FileNotFoundException e) { WriteWarning("Failed to open file: " + e.Message); } catch (Exception e) { WriteWarning( "An error has occured when trying to open your iso:\n{1}: {0}\n{2}", e.Message, e.GetType().FullName, e.StackTrace); } Patches.Dispose(); if (!obatch) { Console.Write("Press enter to exit..."); Console.ReadLine(); } }
private static void ExtractISO(string iso2, KH1ISOReader input, string tfolder = "export/") { var isofile = new FileStream(iso2, FileMode.Open, FileAccess.Read); using (var iso = new ISOFileReader(isofile)) { var idxnames = new List<string>(); int a = 0; foreach (FileDescriptor file in iso) { a++; string filename = file.FullName; if (filename.EndsWith(".IDX")) { //KH1ISOReader(); //idxs.Add(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}", a, 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); } } ExtractIDX(input); //, tfolder + "" + idxnames[i] + "/", idxnames[i] } }
private static void ExtractIDX(KH1ISOReader input, string tfolder = "export/") { for (int i = 0, idxC = input.idxEntries.Count; i < idxC; ++i) { IDXEntry entry = input.idxEntries[i]; bool compressed = entry.flags == 1; string name2; if (!HashList.pairs.TryGetValue(entry.hash, out name2)) { name2 = String.Format("@noname/{0:x8}.bin", entry.hash); } if (name2 == "system.cnf") { continue; } if (name2 == "SLPS_251.98") { continue; } if (name2 == "ioprp250.img") { continue; } if (name2 == "sio2man.irx") { continue; } if (name2 == "sio2d.irx") { continue; } if (name2 == "dbcman.irx") { continue; } if (name2 == "ds2o.irx") { continue; } if (name2 == "mcman.irx") { continue; } if (name2 == "mcserv.irx") { continue; } if (name2 == "libsd.irx") { continue; } if (name2 == "libssl.irx") { continue; } if (name2 == "dev9.irx") { continue; } if (name2 == "atad.irx") { continue; } if (name2 == "hdd.irx") { continue; } if (name2 == "pfs.irx") { continue; } if (name2 == "kingdom.idx") { continue; } if (name2 == "kingdom.img") { continue; } var idxs = new List<Tuple<IDXFile, string>>(); if (_advanced) { Console.WriteLine("-----------File {0,4}/{1}, using KINGDOM.IDX\n", i, input.idxEntries.Count - 1); Console.WriteLine("Hashed filename: {0}", entry.hash); Console.WriteLine("Compression flags: {0}", compressed); Console.WriteLine("Size (packed): {0}", entry.size); Console.WriteLine("Real name: {0}", name2); } else { Console.WriteLine("[KINGDOM: {0,4}/{1}]\tExtracting {2}", i, input.idxEntries.Count - 1, name2); } string filename = tfolder + "/KINGDOM/" + name2; Directory.CreateDirectory(Path.GetDirectoryName(filename)); bool adSize = _advanced; byte[] file = input.readFile(entry); if (compressed) { if (entry.size > int.MaxValue) { throw new NotSupportedException("File to big to decompress"); } try { byte[] decompressed = KH1Compressor.decompress(file, adSize); File.WriteAllBytes(filename, decompressed); } catch (Exception e) { WriteError(" ERROR: Failed to decompress: " + e.Message); } } else { File.WriteAllBytes(filename, file); } } }
private static void PatchISO(KH1ISOReader input, KH1ISOWriter output, PatchManager files, bool ocompress, string oextHead) { int number = 1; output.writeHeader(oextHead);//Let's copy the header //Console.WriteLine("Adding header using {0} source", ? "external" : "internal");//TODO: Delete this Console.WriteLine for (int i = 0, idxC = input.idxEntries.Count; i < idxC; ++i) { IDXEntry entry = input.idxEntries[i]; if (entry.hash == 0x0392ebe4) { continue; } //kingdom.img if (entry.hash == 0x0393eba4) //kingdom.idx { Console.WriteLine("[KINGDOM: {0}/{1}]\tKINGDOM.IDX", number, input.idxEntries.Count - 1); //-1 'cause of the file KINGDOM.IMG number++; output.writeDummyIDX(idxC); continue; } //Loading the patch UInt32 flags = 0; PatchManager.Patch patch; Stream s = null; uint h = 0; // Could make sure the parents match perfectly, but there's only 1 of every name anyway. // So I'll settle for just making sure the file isn't made for the ISO. if (Patches.patches.TryGetValue(entry.hash, out patch) && /*patch.Parent == parenthash*/!patch.IsinISO) {s = patch.Stream; h = patch.Hash; } string name; if (!HashList.pairs.TryGetValue(entry.hash, out name)) { name = String.Format("@noname/{0:x8}.bin", entry.hash); } if (s == null) { flags = entry.flags;//FLAGS NEEDS TO BE OR 0 IF NOTHING, OR 1 IF COMPRESSED, OR THE NAME TO RELINK TO IF IT NEEDS TO if (h != 0) { if (patch.Compressed) { flags = 1; } else if (patch.IsRelink) { flags = patch.Relink; } } if (flags > 1) { Console.WriteLine("[KINGDOM: {0}/{1}]\t{2}\tRelinking...", number, input.idxEntries.Count - 1, name); //-1 'cause of the file KINGDOM.IMG number++; /*if (!HashList.pairs.TryGetValue(flags, out name)) { name = String.Format("@noname/{0:x8}.bin", flags); } Console.WriteLine("{0}", name);*///Err...what is this crappy code? No but srsly output.addRelink(entry.hash, flags); } else { Console.WriteLine("[KINGDOM: {0}/{1}]\t{2}", number, input.idxEntries.Count - 1, name); //-1 'cause of the file KINGDOM.IMG number++; output.copyFile(entry); } } else { Console.WriteLine("[KINGDOM: {0}/{1}]\t{2}\tPatching...", number, input.idxEntries.Count - 1, name); //-1 'cause of the file KINGDOM.IMG number++; output.importFile(s, entry.hash, flags); } } output.finalize(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("New ISO finished!"); Console.ResetColor(); }