[DatFile] Use wrapped functions; add Remove overloads

This commit is contained in:
Matt Nadareski
2017-09-25 12:21:52 -07:00
parent 9b4dc660a5
commit 231cc6141f
4 changed files with 126 additions and 125 deletions

View File

@@ -426,7 +426,7 @@ namespace RombaSharp
string hash = sldr.GetString(0); string hash = sldr.GetString(0);
if (datroot.ContainsKey(hash)) if (datroot.ContainsKey(hash))
{ {
datroot[hash] = null; datroot.Remove(hash);
databaseDats.Add(hash); databaseDats.Add(hash);
} }
else if (!databaseDats.Contains(hash)) else if (!databaseDats.Contains(hash))

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using SabreTools.Library.Data; using SabreTools.Library.Data;
@@ -294,6 +295,7 @@ namespace SabreTools.Library.Dats
/// Passthrough to access the file dictionary /// Passthrough to access the file dictionary
/// </summary> /// </summary>
/// <param name="key">Key in the dictionary to reference</param> /// <param name="key">Key in the dictionary to reference</param>
/// <remarks>We don't want to allow direct setting of values because it bypasses the statistics</remarks>
public List<DatItem> this[string key] public List<DatItem> this[string key]
{ {
get get
@@ -316,26 +318,6 @@ namespace SabreTools.Library.Dats
return _files[key]; return _files[key];
} }
} }
set
{
// If the dictionary is null, create it
if (_files == null)
{
_files = new SortedDictionary<string, List<DatItem>>();
}
lock (_files)
{
// If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key))
{
_files.Add(key, new List<DatItem>());
}
// Now set the value
_files[key] = value;
}
}
} }
/// <summary> /// <summary>
@@ -475,7 +457,7 @@ namespace SabreTools.Library.Dats
/// <summary> /// <summary>
/// Remove a key from the file dictionary /// Remove a key from the file dictionary
/// </summary> /// </summary>
/// <param name="key"></param> /// <param name="key">Key in the dictionary to remove</param>
public void Remove(string key) public void Remove(string key)
{ {
// If the dictionary is null, create it // If the dictionary is null, create it
@@ -500,6 +482,45 @@ namespace SabreTools.Library.Dats
} }
} }
/// <summary>
/// Remove a value from the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to remove from</param>
/// <param name="value">Value to remove from the dictionary</param>
public void Remove(string key, DatItem value)
{
// If the dictionary is null, create it
if (_files == null)
{
_files = new SortedDictionary<string, List<DatItem>>();
}
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);
}
}
}
/// <summary>
/// Remove a range of values from the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to remove from</param>
/// <param name="value">Value to remove from the dictionary</param>
public void RemoveRange(string key, List<DatItem> value)
{
foreach(DatItem item in value)
{
Remove(key, item);
}
}
/// <summary> /// <summary>
/// Reset the file dictionary /// Reset the file dictionary
/// </summary> /// </summary>
@@ -564,6 +585,44 @@ namespace SabreTools.Library.Dats
#endregion #endregion
#region Dictionary Manipulation
/// <summary>
/// Clones the files dictionary
/// </summary>
/// <returns>A new files dictionary instance</returns>
public SortedDictionary<string, List<DatItem>> CloneFiles()
{
// Create the placeholder dictionary to be used
SortedDictionary<string, List<DatItem>> sorted = new SortedDictionary<string, List<DatItem>>();
// Now perform a deep clone on the entire dictionary
List<string> keys = Keys.ToList();
foreach (string key in keys)
{
// Clone each list of DATs in the dictionary
List<DatItem> olditems = this[key];
List<DatItem> newitems = new List<DatItem>();
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<DatItem>());
}
// Now add the list of items
sorted[key].AddRange(newitems);
}
return sorted;
}
#endregion
#endregion // Instance Methods #endregion // Instance Methods
} }
} }

View File

@@ -327,7 +327,8 @@ namespace SabreTools.Library.Dats
// If we're in removal mode, replace the list with the new one // If we're in removal mode, replace the list with the new one
if (remove) if (remove)
{ {
datdata[key] = left; datdata.Remove(key);
datdata.AddRange(key, left);
} }
return output; return output;

View File

@@ -45,8 +45,9 @@ namespace SabreTools.Library.Dats
// Set the sorted type // Set the sorted type
_sortedBy = bucketBy; _sortedBy = bucketBy;
// Create the temporary dictionary to sort into // Clone the current dictionary into a new one for sorting then reset the internal one
SortedDictionary<string, List<DatItem>> sortable = new SortedDictionary<string, List<DatItem>>(); SortedDictionary<string, List<DatItem>> sortable = this.CloneFiles();
this.Reset();
Globals.Logger.User("Organizing roms by {0}" + (deduperoms != DedupeType.None ? " and merging" : ""), bucketBy); Globals.Logger.User("Organizing roms by {0}" + (deduperoms != DedupeType.None ? " and merging" : ""), bucketBy);
@@ -129,24 +130,17 @@ namespace SabreTools.Library.Dats
newkey = ""; newkey = "";
} }
// Add the DatItem to the temp dictionary // Add the DatItem to the dictionary
lock (sortable) Add(newkey, rom);
{
if (!sortable.ContainsKey(newkey))
{
sortable.Add(newkey, new List<DatItem>());
}
sortable[newkey].Add(rom);
}
} }
}); });
// Now go through and sort all of the individual lists // Now go through and sort all of the individual lists
keys = sortable.Keys.ToList(); keys = Keys.ToList();
Parallel.ForEach(keys, Globals.ParallelOptions, key => Parallel.ForEach(keys, Globals.ParallelOptions, key =>
{ {
// Get the possibly unsorted list // Get the possibly unsorted list
List<DatItem> sortedlist = sortable[key]; List<DatItem> sortedlist = this[key];
// Sort the list of items to be consistent // Sort the list of items to be consistent
DatItem.Sort(ref sortedlist, false); DatItem.Sort(ref sortedlist, false);
@@ -157,15 +151,10 @@ namespace SabreTools.Library.Dats
sortedlist = DatItem.Merge(sortedlist); sortedlist = DatItem.Merge(sortedlist);
} }
// Add the list back to the temp dictionary // Add the list back to the dictionary
lock (sortable) Remove(key);
{ AddRange(key, sortedlist);
sortable[key] = sortedlist;
}
}); });
// Now assign the dictionary back
_files = sortable;
} }
#endregion #endregion
@@ -545,7 +534,7 @@ namespace SabreTools.Library.Dats
archive.Machine = currentMachine; archive.Machine = currentMachine;
if (this[game].Where(i => i.Name == archive.Name).Count() == 0 && !this[game].Contains(archive)) if (this[game].Where(i => i.Name == archive.Name).Count() == 0 && !this[game].Contains(archive))
{ {
this[game].Add(archive); Add(game, archive);
} }
break; break;
@@ -554,7 +543,7 @@ namespace SabreTools.Library.Dats
biosSet.Machine = currentMachine; biosSet.Machine = currentMachine;
if (this[game].Where(i => i.Name == biosSet.Name).Count() == 0 && !this[game].Contains(biosSet)) if (this[game].Where(i => i.Name == biosSet.Name).Count() == 0 && !this[game].Contains(biosSet))
{ {
this[game].Add(biosSet); Add(game, biosSet);
} }
break; break;
@@ -563,7 +552,7 @@ namespace SabreTools.Library.Dats
disk.Machine = currentMachine; disk.Machine = currentMachine;
if (this[game].Where(i => i.Name == disk.Name).Count() == 0 && !this[game].Contains(disk)) if (this[game].Where(i => i.Name == disk.Name).Count() == 0 && !this[game].Contains(disk))
{ {
this[game].Add(disk); Add(game, disk);
} }
break; break;
@@ -572,7 +561,7 @@ namespace SabreTools.Library.Dats
release.Machine = currentMachine; release.Machine = currentMachine;
if (this[game].Where(i => i.Name == release.Name).Count() == 0 && !this[game].Contains(release)) if (this[game].Where(i => i.Name == release.Name).Count() == 0 && !this[game].Contains(release))
{ {
this[game].Add(release); Add(game, release);
} }
break; break;
@@ -581,7 +570,7 @@ namespace SabreTools.Library.Dats
rom.Machine = currentMachine; rom.Machine = currentMachine;
if (this[game].Where(i => i.Name == rom.Name).Count() == 0 && !this[game].Contains(rom)) if (this[game].Where(i => i.Name == rom.Name).Count() == 0 && !this[game].Contains(rom))
{ {
this[game].Add(rom); Add(game, rom);
} }
break; break;
@@ -590,7 +579,7 @@ namespace SabreTools.Library.Dats
sample.Machine = currentMachine; sample.Machine = currentMachine;
if (this[game].Where(i => i.Name == sample.Name).Count() == 0 && !this[game].Contains(sample)) if (this[game].Where(i => i.Name == sample.Name).Count() == 0 && !this[game].Contains(sample))
{ {
this[game].Add(sample); Add(game, sample);
} }
break; break;
@@ -636,7 +625,7 @@ namespace SabreTools.Library.Dats
archive.Machine = musheen; archive.Machine = musheen;
if (this[game].Where(i => i.Name == archive.Name).Count() == 0 && !this[game].Contains(archive)) if (this[game].Where(i => i.Name == archive.Name).Count() == 0 && !this[game].Contains(archive))
{ {
this[game].Add(archive); Add(game, archive);
} }
break; break;
@@ -645,7 +634,7 @@ namespace SabreTools.Library.Dats
biosSet.Machine = musheen; biosSet.Machine = musheen;
if (this[game].Where(i => i.Name == biosSet.Name).Count() == 0 && !this[game].Contains(biosSet)) if (this[game].Where(i => i.Name == biosSet.Name).Count() == 0 && !this[game].Contains(biosSet))
{ {
this[game].Add(biosSet); Add(game, biosSet);
} }
break; break;
@@ -654,7 +643,7 @@ namespace SabreTools.Library.Dats
disk.Machine = musheen; disk.Machine = musheen;
if (this[game].Where(i => i.Name == disk.Name).Count() == 0 && !this[game].Contains(disk)) if (this[game].Where(i => i.Name == disk.Name).Count() == 0 && !this[game].Contains(disk))
{ {
this[game].Add(disk); Add(game, disk);
} }
break; break;
@@ -663,7 +652,7 @@ namespace SabreTools.Library.Dats
release.Machine = musheen; release.Machine = musheen;
if (this[game].Where(i => i.Name == release.Name).Count() == 0 && !this[game].Contains(release)) if (this[game].Where(i => i.Name == release.Name).Count() == 0 && !this[game].Contains(release))
{ {
this[game].Add(release); Add(game, release);
} }
break; break;
@@ -672,7 +661,7 @@ namespace SabreTools.Library.Dats
rom.Machine = musheen; rom.Machine = musheen;
if (this[game].Where(i => i.Name == rom.Name).Count() == 0 && !this[game].Contains(rom)) if (this[game].Where(i => i.Name == rom.Name).Count() == 0 && !this[game].Contains(rom))
{ {
this[game].Add(rom); Add(game, rom);
} }
break; break;
@@ -681,7 +670,7 @@ namespace SabreTools.Library.Dats
sample.Machine = musheen; sample.Machine = musheen;
if (this[game].Where(i => i.Name == sample.Name).Count() == 0 && !this[game].Contains(sample)) if (this[game].Where(i => i.Name == sample.Name).Count() == 0 && !this[game].Contains(sample))
{ {
this[game].Add(sample); Add(game, sample);
} }
break; break;
@@ -737,7 +726,7 @@ namespace SabreTools.Library.Dats
archive.Machine = currentMachine; archive.Machine = currentMachine;
if (this[game].Where(i => i.Name == archive.Name).Count() == 0 && !this[game].Contains(archive)) if (this[game].Where(i => i.Name == archive.Name).Count() == 0 && !this[game].Contains(archive))
{ {
this[game].Add(archive); Add(game, archive);
} }
break; break;
@@ -746,7 +735,7 @@ namespace SabreTools.Library.Dats
biosSet.Machine = currentMachine; biosSet.Machine = currentMachine;
if (this[game].Where(i => i.Name == biosSet.Name).Count() == 0 && !this[game].Contains(biosSet)) if (this[game].Where(i => i.Name == biosSet.Name).Count() == 0 && !this[game].Contains(biosSet))
{ {
this[game].Add(biosSet); Add(game, biosSet);
} }
break; break;
@@ -755,7 +744,7 @@ namespace SabreTools.Library.Dats
disk.Machine = currentMachine; disk.Machine = currentMachine;
if (this[game].Where(i => i.Name == disk.Name).Count() == 0 && !this[game].Contains(disk)) if (this[game].Where(i => i.Name == disk.Name).Count() == 0 && !this[game].Contains(disk))
{ {
this[game].Add(disk); Add(game, disk);
} }
break; break;
@@ -764,7 +753,7 @@ namespace SabreTools.Library.Dats
release.Machine = currentMachine; release.Machine = currentMachine;
if (this[game].Where(i => i.Name == release.Name).Count() == 0 && !this[game].Contains(release)) if (this[game].Where(i => i.Name == release.Name).Count() == 0 && !this[game].Contains(release))
{ {
this[game].Add(release); Add(game, release);
} }
break; break;
@@ -773,7 +762,7 @@ namespace SabreTools.Library.Dats
rom.Machine = currentMachine; rom.Machine = currentMachine;
if (this[game].Where(i => i.Name == rom.Name).Count() == 0 && !this[game].Contains(rom)) if (this[game].Where(i => i.Name == rom.Name).Count() == 0 && !this[game].Contains(rom))
{ {
this[game].Add(rom); Add(game, rom);
} }
break; break;
@@ -782,7 +771,7 @@ namespace SabreTools.Library.Dats
sample.Machine = currentMachine; sample.Machine = currentMachine;
if (this[game].Where(i => i.Name == sample.Name).Count() == 0 && !this[game].Contains(sample)) if (this[game].Where(i => i.Name == sample.Name).Count() == 0 && !this[game].Contains(sample))
{ {
this[game].Add(sample); Add(game, sample);
} }
break; 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))) if (item.Type == ItemType.Disk && (item.MergeTag == null || !this[parent].Select(i => i.Name).Contains(item.MergeTag)))
{ {
item.Machine = parentMachine; item.Machine = parentMachine;
this[parent].Add(item); Add(parent, item);
} }
// Otherwise, if the parent doesn't already contain the non-disk, add it // Otherwise, if the parent doesn't already contain the non-disk, add it
@@ -842,7 +831,7 @@ namespace SabreTools.Library.Dats
item.Machine = parentMachine; item.Machine = parentMachine;
// Add the rom to the parent set // Add the rom to the parent set
this[parent].Add(item); Add(parent, item);
} }
} }
@@ -907,56 +896,32 @@ namespace SabreTools.Library.Dats
List<DatItem> parentItems = this[parent]; List<DatItem> parentItems = this[parent];
foreach (DatItem item in parentItems) 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) switch (item.Type)
{ {
case ItemType.Archive: case ItemType.Archive:
Archive archive = ((Archive)item).Clone() as Archive; Archive archive = ((Archive)item).Clone() as Archive;
if (this[game].Contains(archive)) Remove(game, archive);
{
this[game].Remove(archive);
}
break; break;
case ItemType.BiosSet: case ItemType.BiosSet:
BiosSet biosSet = ((BiosSet)item).Clone() as BiosSet; BiosSet biosSet = ((BiosSet)item).Clone() as BiosSet;
if (this[game].Contains(biosSet)) Remove(game, biosSet);
{
this[game].Remove(biosSet);
}
break; break;
case ItemType.Disk: case ItemType.Disk:
Disk disk = ((Disk)item).Clone() as Disk; Disk disk = ((Disk)item).Clone() as Disk;
if (this[game].Contains(disk)) Remove(game, disk);
{
this[game].Remove(disk);
}
break; break;
case ItemType.Release: case ItemType.Release:
Release release = ((Release)item).Clone() as Release; Release release = ((Release)item).Clone() as Release;
if (this[game].Contains(release)) Remove(game, release);
{
this[game].Remove(release);
}
break; break;
case ItemType.Rom: case ItemType.Rom:
Rom rom = ((Rom)item).Clone() as Rom; Rom rom = ((Rom)item).Clone() as Rom;
if (this[game].Contains(rom)) Remove(game, rom);
{
this[game].Remove(rom);
}
break; break;
case ItemType.Sample: case ItemType.Sample:
Sample sample = ((Sample)item).Clone() as Sample; Sample sample = ((Sample)item).Clone() as Sample;
if (this[game].Contains(sample)) Remove(game, sample);
{
this[game].Remove(sample);
}
break; break;
} }
} }
@@ -1006,51 +971,27 @@ namespace SabreTools.Library.Dats
{ {
case ItemType.Archive: case ItemType.Archive:
Archive archive = ((Archive)item).Clone() as Archive; Archive archive = ((Archive)item).Clone() as Archive;
while (this[game].Contains(archive)) Remove(game, archive);
{
this[game].Remove(archive);
}
break; break;
case ItemType.BiosSet: case ItemType.BiosSet:
BiosSet biosSet = ((BiosSet)item).Clone() as BiosSet; BiosSet biosSet = ((BiosSet)item).Clone() as BiosSet;
while (this[game].Contains(biosSet)) Remove(game, biosSet);
{
this[game].Remove(biosSet);
}
break; break;
case ItemType.Disk: case ItemType.Disk:
Disk disk = ((Disk)item).Clone() as Disk; Disk disk = ((Disk)item).Clone() as Disk;
while (this[game].Contains(disk)) Remove(game, disk);
{
this[game].Remove(disk);
}
break; break;
case ItemType.Release: case ItemType.Release:
Release release = ((Release)item).Clone() as Release; Release release = ((Release)item).Clone() as Release;
while (this[game].Contains(release)) Remove(game, release);
{
this[game].Remove(release);
}
break; break;
case ItemType.Rom: case ItemType.Rom:
Rom rom = ((Rom)item).Clone() as Rom; Rom rom = ((Rom)item).Clone() as Rom;
while (this[game].Contains(rom)) Remove(game, rom);
{
this[game].Remove(rom);
}
break; break;
case ItemType.Sample: case ItemType.Sample:
Sample sample = ((Sample)item).Clone() as Sample; Sample sample = ((Sample)item).Clone() as Sample;
while (this[game].Contains(sample)) Remove(game, sample);
{
this[game].Remove(sample);
}
break; break;
} }
} }