[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);
if (datroot.ContainsKey(hash))
{
datroot[hash] = null;
datroot.Remove(hash);
databaseDats.Add(hash);
}
else if (!databaseDats.Contains(hash))

View File

@@ -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
/// </summary>
/// <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]
{
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<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>
@@ -475,7 +457,7 @@ namespace SabreTools.Library.Dats
/// <summary>
/// Remove a key from the file dictionary
/// </summary>
/// <param name="key"></param>
/// <param name="key">Key in the dictionary to remove</param>
public void Remove(string key)
{
// 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>
/// Reset the file dictionary
/// </summary>
@@ -564,6 +585,44 @@ namespace SabreTools.Library.Dats
#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
}
}

View File

@@ -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;

View File

@@ -45,8 +45,9 @@ namespace SabreTools.Library.Dats
// Set the sorted type
_sortedBy = bucketBy;
// Create the temporary dictionary to sort into
SortedDictionary<string, List<DatItem>> sortable = new SortedDictionary<string, List<DatItem>>();
// Clone the current dictionary into a new one for sorting then reset the internal one
SortedDictionary<string, List<DatItem>> 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<DatItem>());
}
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<DatItem> sortedlist = sortable[key];
List<DatItem> 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<DatItem> 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;
}
}