Exemple #1
0
        public void RomComparer_CompareTwoNonRoms_ThrowsArgumentException()
        {
            var x = new object();
            var y = Array.CreateInstance(typeof(int), 1);

            Assert.Throws <ArgumentException>(() => RomComparer.GetDefaultComparerForMode(RomComparer.DefaultCompareMode).Compare(x, y));
        }
Exemple #2
0
        public void RomComparer_CompareTwoNullObjects_ReturnsEqual()
        {
            object x = null;
            object y = null;

            Assert.Equal(0, RomComparer.GetDefaultComparerForMode(RomComparer.DefaultCompareMode).Compare(x, y));
        }
Exemple #3
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));
            }
        }
        /// <summary>
        /// Compares the instance to another <see cref="IProgramDescription"/>.
        /// </summary>
        /// <param name="other">The other <see cref="IProgramDescription"/> to compare against.</param>
        /// <returns>If this instance and <paramref name="other"/> are considered equivalent, returns zero. A non-zero value indicates inequality.</returns>
        /// <remarks>Consider implementing the IComparable / IComparer / IEquatable / IEqualityComparer interfaces on ProgramDescription instead?
        /// Deficiency noted here: https://github.com/intvsteve/VINTage/issues/239 </remarks>
        private int CompareToIProgramDescription(IProgramDescription other)
        {
            var result = 1;

            if (other != null)
            {
                // Always use strict comparer.
                result = RomComparer.GetDefaultComparerForMode(RomComparison.Strict).Compare(Rom, ProgramInformation, other.Rom, other.ProgramInformation);
            }
            return(result);
        }
Exemple #5
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));
            }
        }
Exemple #6
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));
            }
        }
Exemple #7
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);
        }
Exemple #8
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));
            }
        }
Exemple #9
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));
            }
        }
Exemple #10
0
        public void RomComparer_GetDefaultComparerForMode_BehavesAsExpected(RomComparison comparisonMode, RomComparer expectedComparer, bool shouldThrowArgumentException)
        {
            if (shouldThrowArgumentException)
            {
                Assert.Throws <ArgumentException>(() => RomComparer.GetDefaultComparerForMode(comparisonMode));
            }
            else
            {
                var comparer = RomComparer.GetDefaultComparerForMode(comparisonMode);

                Assert.NotNull(comparer);
                Assert.True(object.ReferenceEquals(expectedComparer, comparer));
            }
        }
Exemple #11
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);
     }
 }
        /// <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);
        }
Exemple #13
0
        public void RomComparer_FirstNullRomSecondNotRom_ThrowsArgumentException()
        {
            var y = new object();

            Assert.Throws <ArgumentException>(() => RomComparer.GetDefaultComparerForMode(RomComparer.DefaultCompareMode).Compare(null, y));
        }
Exemple #14
0
 /// <summary>
 /// Compares two ROMs using the specified compare implementation.
 /// </summary>
 /// <param name="rom">The 'reference' ROM to compare against.</param>
 /// <param name="otherRom">The ROM to compare to <paramref name="rom"/>.</param>
 /// <param name="comparer">The comparison implementation to use.</param>
 /// <returns><c>true</c> if <paramref name="rom"/> and <paramref name="otherRom"/> are considered equal using <paramref name="comparer"/>.</returns>
 public static bool IsEquivalentTo(this IRom rom, IRom otherRom, RomComparer comparer)
 {
     return(rom.IsEquivalentTo(otherRom, null, comparer));
 }
Exemple #15
0
        /// <summary>
        /// Compares two ROMs using the specified compare implementation.
        /// </summary>
        /// <param name="rom">The 'reference' ROM to compare against.</param>
        /// <param name="otherRom">The ROM to compare to <paramref name="rom"/>.</param>
        /// <param name="otherProgramInfo">Pre-fetched program information, which may make certain comparisons slightly faster.</param>
        /// <param name="comparer">The comparison implementation to use.</param>
        /// <returns><c>true</c> if <paramref name="rom"/> and <paramref name="otherRom"/> are considered equal using <paramref name="comparer"/>.</returns>
        public static bool IsEquivalentTo(this IRom rom, IRom otherRom, IProgramInformation otherProgramInfo, RomComparer comparer)
        {
            var result     = comparer.Compare(rom, null, otherRom, otherProgramInfo);
            var equivalent = result == 0;

            return(equivalent);
        }