Ejemplo n.º 1
0
        /// <summary>
        /// Extract data from an unpatched ROM file, for inclusion in a patch file.
        /// </summary>
        private static bool TryGenerateBaseline(string patchPath, string romPath)
        {
            SRecordReader reader    = new SRecordReader(patchPath);
            Stream        romStream = File.OpenRead(romPath);
            Patcher       patcher   = new Patcher(reader, romStream);

            if (!patcher.TryReadPatches())
            {
                return(false);
            }

            // Handy for manual work, but with this suppressed, you can append-pipe the output to the patch file.
            // Console.WriteLine("Generating baseline SRecords for:");
            // patcher.PrintPatches();

            return(patcher.TryPrintBaselines());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Determine whether a patch is suitable for a ROM, and optionally apply the patch if so.
        /// </summary>
        private static bool TryApply(string patchPath, string romPath, bool apply, bool commit)
        {
            SRecordReader reader = new SRecordReader(patchPath);
            Stream        romStream;
            string        workingPath = romPath + ".temp";

            if (commit)
            {
                File.Copy(romPath, workingPath, true);
                romStream = File.Open(workingPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            }
            else
            {
                romStream = File.OpenRead(romPath);
            }

            Patcher patcher = new Patcher(reader, romStream);

            if (!patcher.TryReadPatches())
            {
                return(false);
            }

            Console.WriteLine("This patch file was intended for: {0}.", patcher.InitialCalibrationId);
            Console.WriteLine("This patch file converts ROM to:  {0}.", patcher.FinalCalibrationId);

            if (!apply)
            {
                Console.WriteLine("Preparing to remove patch.");
                patcher.TryReversePatches();
            }

            if (!patcher.TryVerifyExpectedData())
            {
                if (apply)
                {
                    Console.WriteLine("This patch file can NOT be applied to this ROM file.");
                }
                else
                {
                    Console.WriteLine("This patch file was NOT previously applied to this ROM file.");
                }

                return(false);
            }

            if (apply)
            {
                Console.WriteLine("This patch file can be applied to this ROM file.");
            }
            else
            {
                Console.WriteLine("This patch file was previously applied to this ROM file.");
            }

            if (!commit)
            {
                return(true);
            }

            if (apply)
            {
                Console.WriteLine("Applying patch.");
            }
            else
            {
                Console.WriteLine("Removing patch.");
            }

            if (patcher.TryApplyPatches())
            {
                reader.Dispose();
                romStream.Dispose();

                Console.WriteLine("Verifying patch.");
                using (Verifier verifier = new Verifier(patchPath, workingPath, apply))
                {
                    if (!verifier.TryVerify(patcher.Patches))
                    {
                        Console.WriteLine("Verification failed, ROM file not modified.");
                        return(false);
                    }
                }

                File.Copy(workingPath, romPath, true);
                Console.WriteLine("ROM file modified successfully.");
            }
            else
            {
                Console.WriteLine("The ROM file has not been modified.");
            }

            return(true);
        }