Beispiel #1
0
        public void RomComparer_CompareNullToNullAsIRoms_ProducesCorrectResult(RomComparison comparisonMode)
        {
            IRom rom0 = null;
            IRom rom1 = null;

            using (var comparer = RomComparer.GetComparer(comparisonMode))
            {
                Assert.Equal(0, comparer.Compare(rom0, rom1));
            }
        }
Beispiel #2
0
        public void RomComparer_CompareNullToRomsAsObjects_ProducesCorrectResult(RomComparison comparisonMode)
        {
            var    romPath = RomComparerTestStorageAccess.Initialize(TestRomResources.TestRomPath).First();
            object rom0    = null;
            object rom1    = Rom.Create(romPath, null);

            using (var comparer = RomComparer.GetComparer(comparisonMode))
            {
                Assert.Equal(-1, comparer.Compare(rom0, rom1));
            }
        }
Beispiel #3
0
        public void RomComparer_CompareTwoDifferentRomsAsObjects_ProducesCorrectResult(RomComparison comparisonMode)
        {
            var    paths = RomComparerTestStorageAccess.Initialize(TestRomResources.TestRomPath, TestRomResources.TestBinPath);
            object rom0  = Rom.Create(paths[0], null);
            object rom1  = Rom.Create(paths[1], null);

            using (var comparer = RomComparer.GetComparer(comparisonMode))
            {
                Assert.NotEqual(0, comparer.Compare(rom0, rom1));
            }
        }
Beispiel #4
0
        /// <summary>
        /// Inspects two ROMs and determines if they are the same ROM, to the best it is capable of doing.
        /// </summary>
        /// <param name="rom">The reference ROM.</param>
        /// <param name="otherRom">The ROM to compare with.</param>
        /// <param name="programInfo">Program information used for comparison.</param>
        /// <param name="comparisonMode">Specifies the <see cref="RomComparison"/> mode to use to determine equivalence.</param>
        /// <returns>If the two ROMs are equivalent, returns <c>true</c>, otherwise returns <c>false</c>.</returns>
        /// <remarks>Because the same ROM may be packaged in up to three different file formats at this time (.bin + .cfg, .rom, and .luigi), it can be difficult to
        /// determine if two ROMs are, indeed, the same. The <paramref name="comparisonMode"/> defines how to compare the two ROMs</remarks>
        public static bool IsEquivalentTo(this IRom rom, IRom otherRom, IProgramInformation programInfo, RomComparison comparisonMode)
        {
            var equivalent = object.ReferenceEquals(rom, otherRom);

            if (!equivalent)
            {
                var comparer = RomComparer.GetComparer(comparisonMode);
                equivalent = rom.IsEquivalentTo(otherRom, programInfo, comparer);
            }
            return(equivalent);
        }
Beispiel #5
0
        public void RomComparer_FirstIsRomSecondNotRom_ThrowsArgumentException()
        {
            var romPath = RomComparerTestStorageAccess.Initialize(TestRomResources.TestBinPath).First();
            var rom     = Rom.Create(romPath, null);

            Assert.NotNull(rom);
            var y = new object();

            using (var comparer = RomComparer.GetComparer(RomComparison.StrictRomCrcOnly))
            {
                Assert.Throws <ArgumentException>(() => comparer.Compare(rom, y));
            }
        }
Beispiel #6
0
        public void RomComparer_GetComparerForMode_BehavesAsExpected(RomComparison comparisonMode, Type expectedComparisonType, bool shouldThrowArgumentException)
        {
            if (shouldThrowArgumentException)
            {
                Assert.Throws <ArgumentException>(() => RomComparer.GetComparer(comparisonMode));
            }
            else
            {
                var comparer = RomComparer.GetComparer(comparisonMode);

                Assert.NotNull(comparer);
                Assert.True(expectedComparisonType.IsInstanceOfType(comparer));
            }
        }
Beispiel #7
0
 private void RefreshProgramAvailabilityState(IProgramDescription description, IEnumerable <IPeripheral> peripherals, ProgramSupportFileState newState)
 {
     using (var comparer = RomComparer.GetComparer(RomComparer.DefaultCompareMode))
     {
         // Should we be using the comparer here?
         foreach (var program in Items.OfType <ProgramViewModel>().Where(p => (p.ProgramDescription.Crc == description.Crc) && (p.ProgramDescription.Rom.RomPath == description.Rom.RomPath)))
         {
             program.RefreshValidationState(peripherals);
         }
     }
     foreach (var directory in Items.OfType <FolderViewModel>())
     {
         directory.RefreshProgramAvailabilityState(description, peripherals, newState);
     }
 }
Beispiel #8
0
        /// <summary>
        /// Identifies ROMs in a list of files and returns them.
        /// </summary>
        /// <param name="romFiles">A list of ROM files.</param>
        /// <param name="existingRoms">Existing, identified ROMs.</param>
        /// <param name="duplicateRoms">Receives ROMs that were not added because duplicates are already in the list.</param>
        /// <param name="acceptCancellation">A function that allows the operation to be cancelled.</param>
        /// <param name="progressFunc">A function to call to report progress of the search.</param>
        /// <param name="updateNumDiscovered">A function to call to indicate the number of valid ROMs discovered.</param>
        /// <param name="filter">Optional custom filter function. If filter is non-<c>null</c>, it must return <c>true</c> for a ROM to be added.</param>
        /// <returns>A list of program descriptions for the ROMs.</returns>
        public static IList <ProgramDescription> GatherRomsFromFileList(IEnumerable <IRom> romFiles, IEnumerable <ProgramDescription> existingRoms, IList <string> duplicateRoms, Func <bool> acceptCancellation, Action <string> progressFunc, Action <int> updateNumDiscovered, Func <IProgramInformation, bool> filter)
        {
            var addedItems = new List <ProgramDescription>();

#if DEBUGGING
            using (RomComparer strict = RomComparer.GetComparer(RomComparison.Strict),
                   strictCrcOnly = RomComparer.GetComparer(RomComparison.StrictRomCrcOnly),
                   canonical = RomComparer.GetComparer(RomComparison.CanonicalStrict),
                   canonicalCrcOnly = RomComparer.GetComparer(RomComparison.CanonicalRomCrcOnly))
#else
            using (RomComparer comparer = RomComparer.GetComparer(RomComparer.DefaultCompareMode))
#endif // DEBUGGING
            {
                foreach (var romFile in romFiles)
                {
                    if (acceptCancellation())
                    {
                        break;
                    }
                    if (progressFunc != null)
                    {
                        progressFunc(romFile.RomPath);
                    }
                    bool alreadyAdded = (romFile.Crc != 0) && (addedItems.FirstOrDefault(p => p.Rom.IsEquivalentTo(romFile, comparer)) != null);
                    if ((romFile.Crc != 0) && !alreadyAdded)
                    {
                        var programInfo = romFile.GetProgramInformation();
                        if ((filter == null) || filter(programInfo))
                        {
                            var haveIt = existingRoms.Any(d => d.Rom.IsEquivalentTo(romFile, programInfo, comparer));
                            if (!haveIt)
                            {
                                IRom localRomCopy = null;
                                if (romFile.RomPath.IsPathOnRemovableDevice())
                                {
                                    localRomCopy = romFile.CopyToLocalRomsDirectory();
                                }
                                var programDescription = new ProgramDescription(romFile.Crc, romFile, programInfo);
                                if (localRomCopy != null)
                                {
                                    programDescription.Files.AddSupportFile(ProgramFileKind.Rom, localRomCopy.RomPath);
                                }
                                if ((romFile.Format == RomFormat.Bin) && (string.IsNullOrEmpty(romFile.ConfigPath) || !File.Exists(romFile.ConfigPath) || (localRomCopy != null)))
                                {
                                    // Logic for .cfg file:
                                    // OnRemovableDevice: NO                          | YES
                                    // Has .cfg NO        Create (no local copy made) | Create (local copy made, original is null, copy is from stock - if original changes, update copy of both ROM and .cfg)
                                    //          YES       Use (no local copy made)    | Create (update if original changes)
                                    if (localRomCopy != null)
                                    {
                                        var cfgFilePath = localRomCopy.ConfigPath;
                                        if (string.IsNullOrEmpty(cfgFilePath) || !File.Exists(cfgFilePath))
                                        {
                                            cfgFilePath = localRomCopy.GenerateStockCfgFile(programInfo);
                                        }
                                        if (!string.IsNullOrEmpty(cfgFilePath) && File.Exists(cfgFilePath))
                                        {
                                            programDescription.Files.AddSupportFile(ProgramFileKind.CfgFile, cfgFilePath);
                                        }
                                    }
                                    else
                                    {
                                        var cfgFilePath = romFile.GenerateStockCfgFile(programInfo);
                                        if (!string.IsNullOrEmpty(cfgFilePath))
                                        {
                                            romFile.UpdateCfgFile(cfgFilePath);
                                        }
                                    }
                                }
                                addedItems.Add(programDescription);
                                if (updateNumDiscovered != null)
                                {
                                    updateNumDiscovered(addedItems.Count);
                                }
                            }
                            else if (duplicateRoms != null)
                            {
                                duplicateRoms.Add(romFile.RomPath);
                            }
                        }
                    }
                    else if (alreadyAdded && (duplicateRoms != null))
                    {
                        duplicateRoms.Add(romFile.RomPath);
                    }
                    else if ((romFile != null) && ((romFile.Crc == 0) || (romFile.Crc == INTV.Core.Utility.Crc32.InitialValue) || !romFile.IsValid))
                    {
                        // TODO: Report rejected ROMs?
                    }
                }
            }
            return(addedItems);
        }