Example #1
0
        /// <summary>
        /// List all duplicates found in a DAT based on a rom
        /// </summary>
        /// <param name="datdata">Dat to match against</param>
        /// <param name="remove">True to mark matched roms for removal from the input, false otherwise (default)</param>
        /// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
        /// <returns>List of matched DatItem objects</returns>
        public List <DatItem> GetDuplicates(DatFile datdata, bool remove = false, bool sorted = false)
        {
            List <DatItem> output = new List <DatItem>();

            // Check for an empty rom list first
            if (datdata.Count == 0)
            {
                return(output);
            }

            // We want to get the proper key for the DatItem
            string key = SortAndGetKey(datdata, sorted);

            // If the key doesn't exist, return the empty list
            if (!datdata.Contains(key))
            {
                return(output);
            }

            // Try to find duplicates
            List <DatItem> roms = datdata[key];
            List <DatItem> left = new List <DatItem>();

            for (int i = 0; i < roms.Count; i++)
            {
                DatItem datItem = roms[i];

                if (this.Equals(datItem))
                {
                    datItem.Remove = true;
                    output.Add(datItem);
                }
                else
                {
                    left.Add(datItem);
                }
            }

            // If we're in removal mode, add back all roms with the proper flags
            if (remove)
            {
                datdata.Remove(key);
                datdata.AddRange(key, output);
                datdata.AddRange(key, left);
            }

            return(output);
        }
        /// <summary>
        /// Filter a DatFile using the inputs
        /// </summary>
        /// <param name="datFile"></param>
        /// <returns>True if the DatFile was filtered, false on error</returns>
        public bool FilterDatFile(DatFile datFile)
        {
            try
            {
                // Loop over every key in the dictionary
                List <string> keys = datFile.Keys;
                foreach (string key in keys)
                {
                    // For every item in the current key
                    List <DatItem> items    = datFile[key];
                    List <DatItem> newitems = new List <DatItem>();
                    foreach (DatItem item in items)
                    {
                        // If the rom passes the filter, include it
                        if (ItemPasses(item))
                        {
                            // If we are in single game mode, rename all games
                            if (this.Single.Neutral)
                            {
                                item.MachineName = "!";
                            }

                            // If we are in NTFS trim mode, trim the game name
                            if (this.Trim.Neutral)
                            {
                                // Windows max name length is 260
                                int usableLength = 260 - item.MachineName.Length - this.Root.Neutral.Length;
                                if (item.Name.Length > usableLength)
                                {
                                    string ext = Path.GetExtension(item.Name);
                                    item.Name  = item.Name.Substring(0, usableLength - ext.Length);
                                    item.Name += ext;
                                }
                            }

                            // Lock the list and add the item back
                            lock (newitems)
                            {
                                newitems.Add(item);
                            }
                        }
                    }

                    datFile.Remove(key);
                    datFile.AddRange(key, newitems);
                }
            }
            catch (Exception ex)
            {
                Globals.Logger.Error(ex.ToString());
                return(false);
            }

            return(true);
        }