Пример #1
0
        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();
            }
        }
Пример #2
0
 private void AddPatch(Stream ms, string patchname = "")
 {
     using (var br = new BinaryStream(ms, Encoding.ASCII, leaveOpen: true))
     {
         if (br.ReadUInt32() != 0x5031484b)
         {
             br.Close();
             ms.Close();
             throw new InvalidDataException("Invalid KH1Patch file!");
         }
         patchms.Add(ms);
         uint oaAuther    = br.ReadUInt32(),
              obFileCount = br.ReadUInt32(),
              num         = br.ReadUInt32();
         patchname = Path.GetFileName(patchname);
         try
         {
             string author = br.ReadCString();
             Console.ForegroundColor = ConsoleColor.Cyan;
             Console.WriteLine("Loading patch {0} version {1} by {2}", patchname, num, author);
             Console.ResetColor();
             br.Seek(oaAuther, SeekOrigin.Begin);
             uint os1 = br.ReadUInt32(),
                  os2 = br.ReadUInt32(),
                  os3 = br.ReadUInt32();
             br.Seek(oaAuther + os1, SeekOrigin.Begin);
             num = br.ReadUInt32();
             if (num > 0)
             {
                 br.Seek(num * 4, SeekOrigin.Current);
                 Console.WriteLine("Changelog:");
                 Console.ForegroundColor = ConsoleColor.Green;
                 while (num > 0)
                 {
                     --num;
                     Console.WriteLine(" * {0}", br.ReadCString());
                 }
             }
             br.Seek(oaAuther + os2, SeekOrigin.Begin);
             num = br.ReadUInt32();
             if (num > 0)
             {
                 br.Seek(num * 4, SeekOrigin.Current);
                 Console.ResetColor();
                 Console.WriteLine("Credits:");
                 Console.ForegroundColor = ConsoleColor.Green;
                 while (num > 0)
                 {
                     --num;
                     Console.WriteLine(" * {0}", br.ReadCString());
                 }
                 Console.ResetColor();
             }
             br.Seek(oaAuther + os3, SeekOrigin.Begin);
             author = br.ReadCString();
             if (author.Length != 0)
             {
                 Console.ResetColor();
                 Console.WriteLine("Other information:\r\n");
                 Console.ForegroundColor = ConsoleColor.Green;
                 Console.WriteLine("{0}", author);
             }
             Console.ResetColor();
         }
         catch (Exception e)
         {
             Console.ForegroundColor = ConsoleColor.Red;
             Console.WriteLine("Error reading kh1patch header: {0}: {1}\r\nAttempting to continue files...",
                               e.GetType(), e.Message);
             Console.ResetColor();
         }
         Console.WriteLine("");
         br.Seek(obFileCount, SeekOrigin.Begin);
         num = br.ReadUInt32();
         while (num > 0)
         {
             --num;
             var nPatch = new Patch();
             nPatch.Hash             = br.ReadUInt32();
             oaAuther                = br.ReadUInt32();
             nPatch.CompressedSize   = br.ReadUInt32();
             nPatch.UncompressedSize = br.ReadUInt32();
             nPatch.Parent           = br.ReadUInt32();
             nPatch.Relink           = br.ReadUInt32();
             nPatch.Compressed       = br.ReadUInt32() != 0;
             nPatch.IsNew            = br.ReadUInt32() == 1; //Custom
             if (!nPatch.IsRelink)
             {
                 if (nPatch.CompressedSize != 0)
                 {
                     nPatch.Stream = new Substream(ms, oaAuther, nPatch.CompressedSize);
                 }
                 else
                 {
                     throw new InvalidDataException("File length is 0, but not relinking.");
                 }
             }
             // Use the last file patch
             if (patches.ContainsKey(nPatch.Hash))
             {
                 Console.ForegroundColor = ConsoleColor.Red;
                 Console.WriteLine("The file {0} has been included multiple times. Using the one from {1}.",
                                   HashList.NameFromHash(nPatch.Hash), patchname);
                 patches[nPatch.Hash].Dispose();
                 patches.Remove(nPatch.Hash);
                 Console.ResetColor();
             }
             patches.Add(nPatch.Hash, nPatch);
             //Global checks
             if (!KINGDOMChanged && nPatch.IsInKINGDOM || nPatch.IsInKINGDOM)
             {
                 KINGDOMChanged = true;
             }
             else if (!ISOChanged && nPatch.IsinISO)
             {
                 ISOChanged = true;
             }
             if (nPatch.IsNew)
             {
                 AddToNewFiles(nPatch);
             }
             br.Seek(60, SeekOrigin.Current);
         }
     }
 }