diff --git a/RombaSharp/Partials/RombaSharp.Helpers.cs b/RombaSharp/Partials/RombaSharp.Helpers.cs index 759a2aee..4713c07a 100644 --- a/RombaSharp/Partials/RombaSharp.Helpers.cs +++ b/RombaSharp/Partials/RombaSharp.Helpers.cs @@ -426,7 +426,7 @@ namespace RombaSharp string hash = sldr.GetString(0); if (datroot.ContainsKey(hash)) { - datroot[hash] = null; + datroot.Remove(hash); databaseDats.Add(hash); } else if (!databaseDats.Contains(hash)) diff --git a/SabreTools.Library/Dats/DatFile.cs b/SabreTools.Library/Dats/DatFile.cs index e04da511..544df832 100644 --- a/SabreTools.Library/Dats/DatFile.cs +++ b/SabreTools.Library/Dats/DatFile.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using SabreTools.Library.Data; @@ -294,6 +295,7 @@ namespace SabreTools.Library.Dats /// Passthrough to access the file dictionary /// /// Key in the dictionary to reference + /// We don't want to allow direct setting of values because it bypasses the statistics public List this[string key] { get @@ -316,26 +318,6 @@ namespace SabreTools.Library.Dats return _files[key]; } } - set - { - // If the dictionary is null, create it - if (_files == null) - { - _files = new SortedDictionary>(); - } - - lock (_files) - { - // If the key is missing from the dictionary, add it - if (!_files.ContainsKey(key)) - { - _files.Add(key, new List()); - } - - // Now set the value - _files[key] = value; - } - } } /// @@ -475,7 +457,7 @@ namespace SabreTools.Library.Dats /// /// Remove a key from the file dictionary /// - /// + /// Key in the dictionary to remove public void Remove(string key) { // If the dictionary is null, create it @@ -500,6 +482,45 @@ namespace SabreTools.Library.Dats } } + /// + /// Remove a value from the file dictionary + /// + /// Key in the dictionary to remove from + /// Value to remove from the dictionary + public void Remove(string key, DatItem value) + { + // If the dictionary is null, create it + if (_files == null) + { + _files = new SortedDictionary>(); + } + + lock (_files) + { + // While the key is in the dictionary and the item is there, remove it + while (_files.ContainsKey(key) && _files[key].Contains(value)) + { + // Remove the statistics first + RemoveItemStatistics(value); + + _files[key].Remove(value); + } + } + } + + /// + /// Remove a range of values from the file dictionary + /// + /// Key in the dictionary to remove from + /// Value to remove from the dictionary + public void RemoveRange(string key, List value) + { + foreach(DatItem item in value) + { + Remove(key, item); + } + } + /// /// Reset the file dictionary /// @@ -564,6 +585,44 @@ namespace SabreTools.Library.Dats #endregion + #region Dictionary Manipulation + + /// + /// Clones the files dictionary + /// + /// A new files dictionary instance + public SortedDictionary> CloneFiles() + { + // Create the placeholder dictionary to be used + SortedDictionary> sorted = new SortedDictionary>(); + + // Now perform a deep clone on the entire dictionary + List keys = Keys.ToList(); + foreach (string key in keys) + { + // Clone each list of DATs in the dictionary + List olditems = this[key]; + List newitems = new List(); + foreach (DatItem item in olditems) + { + newitems.Add((DatItem)item.Clone()); + } + + // If the key is missing from the new dictionary, add it + if (!sorted.ContainsKey(key)) + { + sorted.Add(key, new List()); + } + + // Now add the list of items + sorted[key].AddRange(newitems); + } + + return sorted; + } + + #endregion + #endregion // Instance Methods } } diff --git a/SabreTools.Library/Dats/DatItem.cs b/SabreTools.Library/Dats/DatItem.cs index 60b6d551..850ff727 100644 --- a/SabreTools.Library/Dats/DatItem.cs +++ b/SabreTools.Library/Dats/DatItem.cs @@ -327,7 +327,8 @@ namespace SabreTools.Library.Dats // If we're in removal mode, replace the list with the new one if (remove) { - datdata[key] = left; + datdata.Remove(key); + datdata.AddRange(key, left); } return output; diff --git a/SabreTools.Library/Dats/Partials/DatFile.Manipulate.cs b/SabreTools.Library/Dats/Partials/DatFile.Manipulate.cs index ab4e232c..c4516959 100644 --- a/SabreTools.Library/Dats/Partials/DatFile.Manipulate.cs +++ b/SabreTools.Library/Dats/Partials/DatFile.Manipulate.cs @@ -45,8 +45,9 @@ namespace SabreTools.Library.Dats // Set the sorted type _sortedBy = bucketBy; - // Create the temporary dictionary to sort into - SortedDictionary> sortable = new SortedDictionary>(); + // Clone the current dictionary into a new one for sorting then reset the internal one + SortedDictionary> sortable = this.CloneFiles(); + this.Reset(); Globals.Logger.User("Organizing roms by {0}" + (deduperoms != DedupeType.None ? " and merging" : ""), bucketBy); @@ -129,24 +130,17 @@ namespace SabreTools.Library.Dats newkey = ""; } - // Add the DatItem to the temp dictionary - lock (sortable) - { - if (!sortable.ContainsKey(newkey)) - { - sortable.Add(newkey, new List()); - } - sortable[newkey].Add(rom); - } + // Add the DatItem to the dictionary + Add(newkey, rom); } }); // Now go through and sort all of the individual lists - keys = sortable.Keys.ToList(); + keys = Keys.ToList(); Parallel.ForEach(keys, Globals.ParallelOptions, key => { // Get the possibly unsorted list - List sortedlist = sortable[key]; + List sortedlist = this[key]; // Sort the list of items to be consistent DatItem.Sort(ref sortedlist, false); @@ -157,15 +151,10 @@ namespace SabreTools.Library.Dats sortedlist = DatItem.Merge(sortedlist); } - // Add the list back to the temp dictionary - lock (sortable) - { - sortable[key] = sortedlist; - } + // Add the list back to the dictionary + Remove(key); + AddRange(key, sortedlist); }); - - // Now assign the dictionary back - _files = sortable; } #endregion @@ -545,7 +534,7 @@ namespace SabreTools.Library.Dats archive.Machine = currentMachine; if (this[game].Where(i => i.Name == archive.Name).Count() == 0 && !this[game].Contains(archive)) { - this[game].Add(archive); + Add(game, archive); } break; @@ -554,7 +543,7 @@ namespace SabreTools.Library.Dats biosSet.Machine = currentMachine; if (this[game].Where(i => i.Name == biosSet.Name).Count() == 0 && !this[game].Contains(biosSet)) { - this[game].Add(biosSet); + Add(game, biosSet); } break; @@ -563,7 +552,7 @@ namespace SabreTools.Library.Dats disk.Machine = currentMachine; if (this[game].Where(i => i.Name == disk.Name).Count() == 0 && !this[game].Contains(disk)) { - this[game].Add(disk); + Add(game, disk); } break; @@ -572,7 +561,7 @@ namespace SabreTools.Library.Dats release.Machine = currentMachine; if (this[game].Where(i => i.Name == release.Name).Count() == 0 && !this[game].Contains(release)) { - this[game].Add(release); + Add(game, release); } break; @@ -581,7 +570,7 @@ namespace SabreTools.Library.Dats rom.Machine = currentMachine; if (this[game].Where(i => i.Name == rom.Name).Count() == 0 && !this[game].Contains(rom)) { - this[game].Add(rom); + Add(game, rom); } break; @@ -590,7 +579,7 @@ namespace SabreTools.Library.Dats sample.Machine = currentMachine; if (this[game].Where(i => i.Name == sample.Name).Count() == 0 && !this[game].Contains(sample)) { - this[game].Add(sample); + Add(game, sample); } break; @@ -636,7 +625,7 @@ namespace SabreTools.Library.Dats archive.Machine = musheen; if (this[game].Where(i => i.Name == archive.Name).Count() == 0 && !this[game].Contains(archive)) { - this[game].Add(archive); + Add(game, archive); } break; @@ -645,7 +634,7 @@ namespace SabreTools.Library.Dats biosSet.Machine = musheen; if (this[game].Where(i => i.Name == biosSet.Name).Count() == 0 && !this[game].Contains(biosSet)) { - this[game].Add(biosSet); + Add(game, biosSet); } break; @@ -654,7 +643,7 @@ namespace SabreTools.Library.Dats disk.Machine = musheen; if (this[game].Where(i => i.Name == disk.Name).Count() == 0 && !this[game].Contains(disk)) { - this[game].Add(disk); + Add(game, disk); } break; @@ -663,7 +652,7 @@ namespace SabreTools.Library.Dats release.Machine = musheen; if (this[game].Where(i => i.Name == release.Name).Count() == 0 && !this[game].Contains(release)) { - this[game].Add(release); + Add(game, release); } break; @@ -672,7 +661,7 @@ namespace SabreTools.Library.Dats rom.Machine = musheen; if (this[game].Where(i => i.Name == rom.Name).Count() == 0 && !this[game].Contains(rom)) { - this[game].Add(rom); + Add(game, rom); } break; @@ -681,7 +670,7 @@ namespace SabreTools.Library.Dats sample.Machine = musheen; if (this[game].Where(i => i.Name == sample.Name).Count() == 0 && !this[game].Contains(sample)) { - this[game].Add(sample); + Add(game, sample); } break; @@ -737,7 +726,7 @@ namespace SabreTools.Library.Dats archive.Machine = currentMachine; if (this[game].Where(i => i.Name == archive.Name).Count() == 0 && !this[game].Contains(archive)) { - this[game].Add(archive); + Add(game, archive); } break; @@ -746,7 +735,7 @@ namespace SabreTools.Library.Dats biosSet.Machine = currentMachine; if (this[game].Where(i => i.Name == biosSet.Name).Count() == 0 && !this[game].Contains(biosSet)) { - this[game].Add(biosSet); + Add(game, biosSet); } break; @@ -755,7 +744,7 @@ namespace SabreTools.Library.Dats disk.Machine = currentMachine; if (this[game].Where(i => i.Name == disk.Name).Count() == 0 && !this[game].Contains(disk)) { - this[game].Add(disk); + Add(game, disk); } break; @@ -764,7 +753,7 @@ namespace SabreTools.Library.Dats release.Machine = currentMachine; if (this[game].Where(i => i.Name == release.Name).Count() == 0 && !this[game].Contains(release)) { - this[game].Add(release); + Add(game, release); } break; @@ -773,7 +762,7 @@ namespace SabreTools.Library.Dats rom.Machine = currentMachine; if (this[game].Where(i => i.Name == rom.Name).Count() == 0 && !this[game].Contains(rom)) { - this[game].Add(rom); + Add(game, rom); } break; @@ -782,7 +771,7 @@ namespace SabreTools.Library.Dats sample.Machine = currentMachine; if (this[game].Where(i => i.Name == sample.Name).Count() == 0 && !this[game].Contains(sample)) { - this[game].Add(sample); + Add(game, sample); } break; @@ -829,7 +818,7 @@ namespace SabreTools.Library.Dats if (item.Type == ItemType.Disk && (item.MergeTag == null || !this[parent].Select(i => i.Name).Contains(item.MergeTag))) { item.Machine = parentMachine; - this[parent].Add(item); + Add(parent, item); } // Otherwise, if the parent doesn't already contain the non-disk, add it @@ -842,7 +831,7 @@ namespace SabreTools.Library.Dats item.Machine = parentMachine; // Add the rom to the parent set - this[parent].Add(item); + Add(parent, item); } } @@ -907,56 +896,32 @@ namespace SabreTools.Library.Dats List parentItems = this[parent]; foreach (DatItem item in parentItems) { - // Figure out the type of the item and add it accordingly + // Figure out the type of the item and remove it accordingly switch (item.Type) { case ItemType.Archive: Archive archive = ((Archive)item).Clone() as Archive; - if (this[game].Contains(archive)) - { - this[game].Remove(archive); - } - + Remove(game, archive); break; case ItemType.BiosSet: BiosSet biosSet = ((BiosSet)item).Clone() as BiosSet; - if (this[game].Contains(biosSet)) - { - this[game].Remove(biosSet); - } - + Remove(game, biosSet); break; case ItemType.Disk: Disk disk = ((Disk)item).Clone() as Disk; - if (this[game].Contains(disk)) - { - this[game].Remove(disk); - } - + Remove(game, disk); break; case ItemType.Release: Release release = ((Release)item).Clone() as Release; - if (this[game].Contains(release)) - { - this[game].Remove(release); - } - + Remove(game, release); break; case ItemType.Rom: Rom rom = ((Rom)item).Clone() as Rom; - if (this[game].Contains(rom)) - { - this[game].Remove(rom); - } - + Remove(game, rom); break; case ItemType.Sample: Sample sample = ((Sample)item).Clone() as Sample; - if (this[game].Contains(sample)) - { - this[game].Remove(sample); - } - + Remove(game, sample); break; } } @@ -1006,51 +971,27 @@ namespace SabreTools.Library.Dats { case ItemType.Archive: Archive archive = ((Archive)item).Clone() as Archive; - while (this[game].Contains(archive)) - { - this[game].Remove(archive); - } - + Remove(game, archive); break; case ItemType.BiosSet: BiosSet biosSet = ((BiosSet)item).Clone() as BiosSet; - while (this[game].Contains(biosSet)) - { - this[game].Remove(biosSet); - } - + Remove(game, biosSet); break; case ItemType.Disk: Disk disk = ((Disk)item).Clone() as Disk; - while (this[game].Contains(disk)) - { - this[game].Remove(disk); - } - + Remove(game, disk); break; case ItemType.Release: Release release = ((Release)item).Clone() as Release; - while (this[game].Contains(release)) - { - this[game].Remove(release); - } - + Remove(game, release); break; case ItemType.Rom: Rom rom = ((Rom)item).Clone() as Rom; - while (this[game].Contains(rom)) - { - this[game].Remove(rom); - } - + Remove(game, rom); break; case ItemType.Sample: Sample sample = ((Sample)item).Clone() as Sample; - while (this[game].Contains(sample)) - { - this[game].Remove(sample); - } - + Remove(game, sample); break; } }