Example #1
0
 public static bool RequestBiosFile(string fileName, int fileSize, RomFormat format)
 {
     if (MesenMsgBox.Show("BiosNotFound", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, fileName, fileSize.ToString()) == DialogResult.OK)
     {
         using (OpenFileDialog ofd = new OpenFileDialog()) {
             ofd.SetFilter(ResourceHelper.GetMessage("FilterAll"));
             if (ofd.ShowDialog(Application.OpenForms[0]) == DialogResult.OK)
             {
                 byte[] fileData;
                 string hash = GetFileHash(ofd.FileName, out fileData);
                 if (!GetExpectedHashes(format).Contains(hash))
                 {
                     if (MesenMsgBox.Show("BiosMismatch", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, GetExpectedHashes(format)[0], hash) != DialogResult.OK)
                     {
                         //Files don't match and user cancelled the action
                         return(false);
                     }
                 }
                 File.WriteAllBytes(Path.Combine(ConfigManager.HomeFolder, fileName), fileData);
                 return(true);
             }
         }
     }
     return(false);
 }
Example #2
0
        /// <summary>
        /// Gets the file extension for the given ROM format.
        /// </summary>
        /// <returns>The file extension.</returns>
        /// <param name="romFormat">Rom format.</param>
        public static string FileExtension(this RomFormat romFormat)
        {
            var fileExtension = string.Empty;

            switch (romFormat)
            {
            case RomFormat.Bin:
                fileExtension = ".bin";
                break;

            case RomFormat.Intellicart:
                fileExtension = ".rom";
                break;

            case RomFormat.CuttleCart3:
            case RomFormat.CuttleCart3Advanced:
                fileExtension = ".cc3";
                break;

            case RomFormat.Luigi:
                fileExtension = ".luigi";
                break;
            }
            return(fileExtension);
        }
Example #3
0
        /// <summary>
        /// Determines if two ROM formats should be considered compatible.
        /// </summary>
        /// <param name="romFormat">One ROM format to check.</param>
        /// <param name="otherRomFormat">The other ROM format to check.</param>
        /// <returns><c>true</c> if the two ROM formats should be considered compatible.</returns>
        /// <remarks>There are technically three variations of the .ROM format, though the differences are minor. This method will consider all
        /// three of the .ROM formats as compatible.</remarks>
        public static bool IsCompatibleWithRomFormat(this RomFormat romFormat, RomFormat otherRomFormat)
        {
            var match = romFormat == otherRomFormat;

            if (!match)
            {
                switch (romFormat)
                {
                case RomFormat.CuttleCart3:
                case RomFormat.CuttleCart3Advanced:
                case RomFormat.Intellicart:
                    switch (otherRomFormat)
                    {
                    case RomFormat.CuttleCart3:
                    case RomFormat.CuttleCart3Advanced:
                    case RomFormat.Intellicart:
                        match = true;
                        break;

                    default:
                        break;
                    }
                    break;

                default:
                    break;
                }
            }
            return(match);
        }
Example #4
0
        /// <summary>
        /// Gets the application(s) to use to convert a ROM file from one format to another.
        /// </summary>
        /// <param name="rom">The ROM file to be converted.</param>
        /// <param name="targetFormat">The target format to convert the ROM to.</param>
        /// <returns>An enumerable containing the fully qualified paths to the converter programs needed to perform the conversion, as well as
        /// the output file format produced by the converter.</returns>
        /// <remarks>The converter programs are returned in the order they must execute.</remarks>
        public IEnumerable <Tuple <string, RomFormat> > GetConverterApps(IRom rom, RomFormat targetFormat)
        {
            var converterPrograms = GetConverterPrograms(rom.Format, targetFormat);
            var converterApps     = converterPrograms.Select(p => new Tuple <string, RomFormat>(GetProgramPath(p), p.OutputFormat()));

            return(converterApps);
        }
Example #5
0
        public static Stream GetCorrectStream(Stream str, RomFormat format)
        {
            switch (format)
            {
            case RomFormat.V64: return(new Int16SwapStream(str));

            case RomFormat.N64: return(new Int32SwapStream(str));

            default: return(str);
            }
        }
Example #6
0
        public CartRom(Stream romDataStream, Boolean headerOnly, FormatVerifyFlag verifyFlag)
        {
            m_HeaderOnly = headerOnly;

            /* Check some properties about the source stream */
            if (romDataStream.Length < (HeaderSize + BootRom.Size))
            {
                throw new ArgumentOutOfRangeException("Rom is too small to be valid");
            }

            if (!romDataStream.CanRead)
            {
                throw new ArgumentException("The source rom stream is not readable");
            }

            /* Verify the rom format and make the nessarily action */
            RomFormat format = RomFormat.Unknown;

            switch (verifyFlag)
            {
            case FormatVerifyFlag.Skip: break;

            case FormatVerifyFlag.Header: format = Cartridge.DetectFormatHeader(romDataStream); break;

            case FormatVerifyFlag.Cic: format = Cartridge.DectectFormatCIC(romDataStream); break;

            case FormatVerifyFlag.Crc: format = Cartridge.DectectFormatCRC(romDataStream); break;
            }

            if (verifyFlag != FormatVerifyFlag.Skip && format == RomFormat.Unknown)
            {
                logger.Warn("treid to verify rom but failed, emulator may be unstable");
            }
            else
            {
                romDataStream = Cartridge.GetCorrectStream(romDataStream, format);
            }

            /* Create the header */
            CreateHeader(romDataStream);

            /* Create the bootcode information */
            m_BootRom = new BootRom(romDataStream);

            if (!headerOnly)
            {
                m_RomStream            = romDataStream;
                romDataStream.Position = 0;
            }
        }
Example #7
0
        private static List <string> GetExpectedHashes(RomFormat format)
        {
            switch (format)
            {
            case RomFormat.Fds: return(new List <string> {
                    "99C18490ED9002D9C6D999B9D8D15BE5C051BDFA7CC7E73318053C9A994B0178", "A0A9D57CBACE21BF9C85C2B85E86656317F0768D7772ACC90C7411AB1DBFF2BF"
                });

            case RomFormat.StudyBox: return(new List <string> {
                    "365F84C86F7F7C3AAA2042D78494D41448E998EC5A89AC1B5FECB452951D514C"
                });
            }
            throw new Exception("Unexpected format type");
        }
Example #8
0
        /// <summary>
        /// Checks if the given ROM's format is compatible with the given format.
        /// </summary>
        /// <param name="rom">The ROM whose format's compatibility is to be checked.</param>
        /// <param name="romFormat">The other ROM format.</param>
        /// <param name="considerOriginalFormat">If <c>true</c> and <paramref name="rom"/> is of a format that may have a different 'format of origin' such as LUIGI, the also consider that format.</param>
        /// <returns><c>true</c> if <paramref name="rom"/>'s format is compatible with <paramref name="romFormat"/>, or, if <paramref name="considerOriginalFormat"/> is <c>true</c>,
        /// the original ROM format is compatible with <paramref name="romFormat"/>.</returns>
        public static bool MatchingRomFormat(this IRom rom, RomFormat romFormat, bool considerOriginalFormat)
        {
            var match = false;

            if (rom != null)
            {
                match = rom.Format.IsCompatibleWithRomFormat(romFormat);
                if (!match && (rom.Format == RomFormat.Luigi) && considerOriginalFormat)
                {
                    var luigiHeader = rom.GetLuigiHeader();
                    match = (luigiHeader != null) && romFormat.IsCompatibleWithRomFormat(luigiHeader.OriginalRomFormat);
                }
            }
            return(match);
        }
Example #9
0
        private static Byte[] CopyTestDatFromRom(Stream romStream, RomFormat format)
        {
            romStream.Position = 0;
            Byte[] buff = new Byte[RomChecksum.InputSize];

            switch (format)
            {
            case RomFormat.Z64:
            case RomFormat.V64: romStream = new Int32SwapStream(romStream); break;

            default: break;
            }

            romStream.Read(buff, 0, buff.Length);
            return(buff);
        }
Example #10
0
        /// <summary>
        /// Retrieve the command line arguments to use for the bin2rom utility based on the target ROM format.
        /// </summary>
        /// <param name="targetFormat">Target ROM format.</param>
        /// <returns>The command line arguments to use.</returns>
        public static string GetCommandLineArgForBin2Rom(this RomFormat targetFormat)
        {
            var arg = string.Empty;

            switch (targetFormat)
            {
            case RomFormat.Intellicart:
                break;

            case RomFormat.CuttleCart3:
                arg = "--cc3";
                break;

            case RomFormat.CuttleCart3Advanced:
                arg = "--adv";
                break;
            }
            return(arg);
        }
Example #11
0
        private static uint GetCrcs(RomFormat format, string romPath, string cfgPath, out uint cfgCrc)
        {
            cfgCrc = 0;
            uint romCrc = 0;

            if (StreamUtilities.FileExists(romPath))
            {
                byte replacementByte = AutoBaudBytes[format];

#if IGNORE_METADATA_FOR_CRC
                var metadataRange  = new List <Range <int> >();
                var metadataOffset = GetMetadataOffset();
                metadataRange.Add(new Range <int>(metadataOffset, int.MaxValue));
                romCrc = Crc32.OfFile(romPath, format != RomFormat.Intellicart, replacementByte, metadataRange);
#else
                romCrc = Crc32.OfFile(romPath, format != RomFormat.Intellicart, replacementByte);
#endif // IGNORE_METADATA_FOR_CRC
            }
            return(romCrc);
        }
Example #12
0
        private void UpdateVectorAddresses()
        {
            RomFormat format = InteropEmu.GetRomInfo().Format;

            if (format == RomFormat.Nsf)
            {
                NsfHeader header = InteropEmu.NsfGetHeader();
                mnuGoToInitHandler.ShortcutKeyDisplayString = "$" + header.InitAddress.ToString("X4");
                mnuGoToPlayHandler.ShortcutKeyDisplayString = "$" + header.PlayAddress.ToString("X4");
            }
            else
            {
                mnuGoToNmiHandler.ShortcutKeyDisplayString   = "$" + GetHandlerTarget(0xFFFA).ToString("X4");
                mnuGoToResetHandler.ShortcutKeyDisplayString = "$" + GetHandlerTarget(0xFFFC).ToString("X4");
                mnuGoToIrqHandler.ShortcutKeyDisplayString   = "$" + GetHandlerTarget(0xFFFE).ToString("X4");

                if (format == RomFormat.Fds)
                {
                    mnuFdsIrqHandler.ShortcutKeyDisplayString   = "$" + GetHandlerTarget(0xDFFE).ToString("X4");
                    mnuFdsNmiHandler1.ShortcutKeyDisplayString  = "$" + GetHandlerTarget(0xDFF6).ToString("X4");
                    mnuFdsNmiHandler2.ShortcutKeyDisplayString  = "$" + GetHandlerTarget(0xDFF8).ToString("X4");
                    mnuFdsNmiHandler3.ShortcutKeyDisplayString  = "$" + GetHandlerTarget(0xDFFA).ToString("X4");
                    mnuFdsResetHandler.ShortcutKeyDisplayString = "$" + GetHandlerTarget(0xDFFC).ToString("X4");
                }
            }

            mnuGoToInitHandler.Tag  = mnuGoToInitHandler.Visible = format == RomFormat.Nsf;
            mnuGoToPlayHandler.Tag  = mnuGoToPlayHandler.Visible = format == RomFormat.Nsf;
            mnuGoToIrqHandler.Tag   = mnuGoToIrqHandler.Visible = format != RomFormat.Nsf;
            mnuGoToNmiHandler.Tag   = mnuGoToNmiHandler.Visible = format != RomFormat.Nsf;
            mnuGoToResetHandler.Tag = mnuGoToResetHandler.Visible = format != RomFormat.Nsf;

            sepFds.Tag             = sepFds.Visible = format == RomFormat.Fds;
            mnuFdsIrqHandler.Tag   = mnuFdsIrqHandler.Visible = format == RomFormat.Fds;
            mnuFdsNmiHandler1.Tag  = mnuFdsNmiHandler1.Visible = format == RomFormat.Fds;
            mnuFdsNmiHandler2.Tag  = mnuFdsNmiHandler2.Visible = format == RomFormat.Fds;
            mnuFdsNmiHandler3.Tag  = mnuFdsNmiHandler3.Visible = format == RomFormat.Fds;
            mnuFdsResetHandler.Tag = mnuFdsResetHandler.Visible = format == RomFormat.Fds;
        }
Example #13
0
 /// <summary>
 /// Add a memo for a ROM.
 /// </summary>
 /// <param name="romPath">Absolute path of the ROM whose memo is to be added.</param>
 /// <param name="format">ROM format.</param>
 protected static void AddMemo(string romPath, RomFormat format)
 {
     Memos.AddMemo(romPath, format);
 }
        /// <summary>
        /// Determines if a given <see cref="IProgramDescription"/> is considered a match based on given criteria.
        /// </summary>
        /// <param name="programDescription">An instance</param>
        /// <param name="programIdentifier">Provides the unique identifier that must match the value that can be determined from <paramref name="programDescription"/>.</param>
        /// <param name="romFormat">If this value is not <see cref="RomFormat.None"/>, then <paramref name="programDescription"/> must have the same ROM format to be a match.</param>
        /// <param name="cfgCrcMustMatch">If <paramref name="romFormat"/> matches and is <see cref="RomFormat.Bin"/>, and this value is <c>true</c>, the CRC of the CFG file must also match.</param>
        /// <returns><c>true</c> if all of the specified criteria match the corresponding data in <paramref name="programDescription"/>.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="programDescription"/> is <c>null</c>.</exception>
        /// <exception cref="System.ArgumentException">Thrown if <paramref name="programIdentifier"/> is invalid.</exception>
        public static bool IsMatchingProgramDescription(this IProgramDescription programDescription, ProgramIdentifier programIdentifier, RomFormat romFormat, bool cfgCrcMustMatch)
        {
            var match = IsMatchingProgramDescription(programDescription, programIdentifier, romFormat, cfgCrcMustMatch, null);

            return(match);
        }
        /// <summary>
        /// Determines if a given <see cref="IProgramDescription"/> is considered a match based on given criteria.
        /// </summary>
        /// <param name="programDescription">An instance</param>
        /// <param name="programIdentifier">Provides the unique identifier that must match the value that can be determined from <paramref name="programDescription"/>.</param>
        /// <param name="romFormat">If this value is not <see cref="RomFormat.None"/>, then <paramref name="programDescription"/> must have the same ROM format to be a match.</param>
        /// <param name="cfgCrcMustMatch">If <paramref name="romFormat"/> matches and is <see cref="RomFormat.Bin"/>, and this value is <c>true</c>, the CRC of the CFG file must also match.</param>
        /// <param name="code">If specified (not <c>null</c> or empty), and it can be determined that <paramref name="programDescription"/> has a value, value is used in determining match.</param>
        /// <returns><c>true</c> if all of the specified criteria match the corresponding data in <paramref name="programDescription"/>.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="programDescription"/> is <c>null</c>.</exception>
        /// <exception cref="System.ArgumentException">Thrown if <paramref name="programIdentifier"/> is invalid.</exception>
        public static bool IsMatchingProgramDescription(this IProgramDescription programDescription, ProgramIdentifier programIdentifier, RomFormat romFormat, bool cfgCrcMustMatch, string code)
        {
            if (programDescription == null)
            {
                throw new ArgumentNullException("programDescription");
            }
            if (programIdentifier == ProgramIdentifier.Invalid)
            {
                throw new ArgumentException("programIdentifier");
            }

            var crcMatch        = programDescription.Crc == programIdentifier.DataCrc;
            var cfgCrcsMatch    = !cfgCrcMustMatch;
            var romFormatsMatch = romFormat == RomFormat.None; // don't care
            var codesMatch      = string.IsNullOrEmpty(code);

            if (programDescription.Rom != null)
            {
                if (!romFormatsMatch)
                {
                    romFormatsMatch = programDescription.Rom.MatchingRomFormat(romFormat, considerOriginalFormat: true);
                }
                crcMatch = programDescription.Rom.MatchesProgramIdentifier(programIdentifier, cfgCrcMustMatch);
                if (cfgCrcMustMatch)
                {
                    cfgCrcsMatch = crcMatch;
                }
            }

            // This may be nearly worthless -- how many XML ProgramInformation implementations are hooked to ProgramDescriptions -- don't they all end up being UserSpecifiedProgramInformation?
            if (!codesMatch && programDescription.ProgramInformation is IntvFunhouseXmlProgramInformation)
            {
                codesMatch = ((IntvFunhouseXmlProgramInformation)programDescription.ProgramInformation).Code == code;
            }
            var match = crcMatch && cfgCrcsMatch && romFormatsMatch && codesMatch;

            return(match);
        }
Example #16
0
        private static IEnumerable <ProgramFile> GetConverterPrograms(RomFormat sourceFormat, RomFormat targetFormat)
        {
            var convertPrograms = new List <ProgramFile>();

            switch (sourceFormat)
            {
            case RomFormat.Bin:
                switch (targetFormat)
                {
                case RomFormat.Bin:
                    break;

                case RomFormat.Intellicart:
                case RomFormat.CuttleCart3:
                case RomFormat.CuttleCart3Advanced:
                    convertPrograms.Add(ProgramFile.Bin2Rom);
                    break;

                case RomFormat.Luigi:
                    convertPrograms.Add(ProgramFile.Bin2Luigi);
                    break;
                }
                break;

            case RomFormat.Intellicart:
            case RomFormat.CuttleCart3:
            case RomFormat.CuttleCart3Advanced:
                switch (targetFormat)
                {
                case RomFormat.Bin:
                    convertPrograms.Add(ProgramFile.Rom2Bin);
                    break;

                case RomFormat.Intellicart:
                case RomFormat.CuttleCart3:
                case RomFormat.CuttleCart3Advanced:
                    break;

                case RomFormat.Luigi:
                    convertPrograms.Add(ProgramFile.Rom2Luigi);
                    break;
                }
                break;

            case RomFormat.Luigi:
                switch (targetFormat)
                {
                case RomFormat.Bin:
                    convertPrograms.Add(ProgramFile.Luigi2Bin);
                    break;

                case RomFormat.Intellicart:
                case RomFormat.CuttleCart3:
                case RomFormat.CuttleCart3Advanced:
                    convertPrograms.Add(ProgramFile.Luigi2Bin);
                    convertPrograms.Add(ProgramFile.Bin2Rom);
                    break;

                case RomFormat.Luigi:
                    break;
                }
                break;
            }

            return(convertPrograms);
        }
Example #17
0
        public void RomFormat_CheckRomFormatCompatibility_ExpectedCompatibilityDetermined(RomFormat firstRomFormat, RomFormat secondRomFormat, bool expectedCompatibility)
        {
            var actualCompatibility = firstRomFormat.IsCompatibleWithRomFormat(secondRomFormat);

            Assert.Equal(expectedCompatibility, actualCompatibility);
        }
Example #18
0
        public void RomFormatRom_LoadAndValidateRom_RomFormatIdentifiedCorrectly(string testRomPath, RomFormat expectedRomFormat)
        {
            var romPath = RomFormatRomTestStorageAccess.Initialize(testRomPath).First();
            var rom     = Rom.Create(romPath, null);

            Assert.NotNull(rom);
            Assert.Equal(expectedRomFormat, rom.Format);
            Assert.True(rom.Validate());
        }
Example #19
0
        public void Convert(string outputFile, RomFormat format)
        {
            byte[] fileBytes = File.ReadAllBytes(input);

            switch (romFormat)
            {
            case RomFormat.N64:

                switch (format)
                {
                case RomFormat.Z64:
                    dWordSwap(fileBytes);
                    break;

                case RomFormat.V64:
                    wordSwap(dWordSwap(fileBytes));
                    break;

                default:
                    break;
                }

                break;

            case RomFormat.V64:

                switch (format)
                {
                case RomFormat.N64:
                    dWordSwap(wordSwap(fileBytes));
                    break;

                case RomFormat.Z64:
                    wordSwap(fileBytes);
                    break;

                default:
                    break;
                }

                break;

            case RomFormat.Z64:

                switch (format)
                {
                case RomFormat.N64:
                    dWordSwap(fileBytes);
                    break;

                case RomFormat.V64:
                    wordSwap(fileBytes);
                    break;

                default:
                    break;
                }

                break;
            }

            File.WriteAllBytes(outputFile, fileBytes);
        }
Example #20
0
        public void RomFormat_GetFileExtension_ReturnsExpectedFileExtension(RomFormat romFormat, string expectedFileExtension)
        {
            var actualFileExtension = romFormat.FileExtension();

            Assert.Equal(expectedFileExtension, actualFileExtension);
        }
Example #21
0
 public static Stream GetCorrectStream(Stream str, RomFormat format)
 {
     switch (format)
     {
         case RomFormat.V64: return new Int16SwapStream(str);
         case RomFormat.N64: return new Int32SwapStream(str);
         default: return str;
     }
 }
Example #22
0
 public void LuigiFormatRom_CheckFormatFromStream_RomFormatIdentifiedCorrectly(string testRomResource, RomFormat expectedRomFormat)
 {
     using (var romData = TestRomResources.OpenResourceStream(testRomResource))
     {
         Assert.NotNull(romData);
         Assert.Equal(expectedRomFormat, LuigiFormatRom.CheckFormat(romData));
     }
 }
Example #23
0
        private static Byte[] CopyTestDatFromRom(Stream romStream, RomFormat format)
        {
            romStream.Position = 0;
            Byte[] buff = new Byte[RomChecksum.InputSize];

            switch (format)
            {
                case RomFormat.Z64:
                case RomFormat.V64: romStream = new Int32SwapStream(romStream); break;
                default: break;
            }

            romStream.Read(buff, 0, buff.Length);
            return buff;
        }
 /// <inheritdoc />
 public IProgramRomInformationBuilder WithFormat(RomFormat romFormat)
 {
     _programRomInformation.Format = romFormat;
     return(this);
 }
        private IProgramDescription GetProgramDescription(ProgramIdentifier programIdentifier, RomFormat format, string code)
        {
            IProgramDescription programDescription = null;

            if (DefaultDescriptionsSource != null)
            {
                // Try the strictest match first.
                programDescription = DefaultDescriptionsSource.FirstOrDefault(d => d.IsMatchingProgramDescription(programIdentifier, format, true, code));
                if (programDescription == null)
                {
                    programDescription = DefaultDescriptionsSource.FirstOrDefault(d => d.IsMatchingProgramDescription(programIdentifier, format, true));
                }
                if (programDescription == null)
                {
                    programDescription = DefaultDescriptionsSource.FirstOrDefault(d => d.IsMatchingProgramDescription(programIdentifier, format, false));
                }
                if (programDescription == null)
                {
                    programDescription = DefaultDescriptionsSource.FirstOrDefault(d => d.IsMatchingProgramDescription(programIdentifier));
                }
                if (programDescription == null)
                {
                    programDescription = DefaultDescriptionsSource.FirstOrDefault(d => d.Crc == programIdentifier.DataCrc);
                }
            }
            return(programDescription);
        }