Remove public add-to-key functionality

This commit is contained in:
Matt Nadareski
2025-01-14 15:32:14 -05:00
parent f3e1a6a19a
commit de00d31319
15 changed files with 254 additions and 333 deletions

View File

@@ -70,7 +70,7 @@ namespace SabreTools.DatFiles.Test
ValidateMachine(actualMachine); ValidateMachine(actualMachine);
// Aggregate for easier validation // Aggregate for easier validation
DatItems.DatItem[] datItems = datFile.Items.Keys DatItems.DatItem[] datItems = datFile.Items.SortedKeys
.SelectMany(key => datFile.GetItemsForBucket(key)) .SelectMany(key => datFile.GetItemsForBucket(key))
.ToArray(); .ToArray();

View File

@@ -28,7 +28,7 @@ namespace SabreTools.DatFiles.Test
DatFile datFile = new Formats.Logiqx(null, deprecated: false); DatFile datFile = new Formats.Logiqx(null, deprecated: false);
datFile.SetHeader(header); datFile.SetHeader(header);
datFile.Add("key", new Rom()); datFile.AddItem(new Rom(), statsOnly: false);
Models.Metadata.MetadataFile? actual = datFile.ConvertToMetadata(); Models.Metadata.MetadataFile? actual = datFile.ConvertToMetadata();
Assert.NotNull(actual); Assert.NotNull(actual);
@@ -76,7 +76,7 @@ namespace SabreTools.DatFiles.Test
]; ];
DatFile datFile = new Formats.SabreJSON(null); DatFile datFile = new Formats.SabreJSON(null);
datFile.Add("key", datItems); datItems.ForEach(item => datFile.AddItem(item, statsOnly: false));
Models.Metadata.MetadataFile? actual = datFile.ConvertToMetadata(); Models.Metadata.MetadataFile? actual = datFile.ConvertToMetadata();
Assert.NotNull(actual); Assert.NotNull(actual);

View File

@@ -195,7 +195,7 @@ namespace SabreTools.DatFiles.Test
{ {
DatFile datFile = new Formats.Logiqx(datFile: null, deprecated: false); DatFile datFile = new Formats.Logiqx(datFile: null, deprecated: false);
datFile.Header.SetFieldValue(Models.Metadata.Header.NameKey, "name"); datFile.Header.SetFieldValue(Models.Metadata.Header.NameKey, "name");
datFile.Add("key", new Rom()); datFile.AddItem(new Rom(), statsOnly: false);
datFile.AddItemDB(new Rom(), 0, 0, false); datFile.AddItemDB(new Rom(), 0, 0, false);
datFile.ResetDictionary(); datFile.ResetDictionary();

View File

@@ -102,11 +102,11 @@ namespace SabreTools.DatFiles
Dictionary<string, string> mapping = []; Dictionary<string, string> mapping = [];
#endif #endif
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(Items.Keys, Globals.ParallelOptions, key => Parallel.ForEach(Items.SortedKeys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(Items.Keys, key => Parallel.ForEach(Items.SortedKeys, key =>
#else #else
foreach (var key in Items.Keys) foreach (var key in Items.SortedKeys)
#endif #endif
{ {
var items = GetItemsForBucket(key); var items = GetItemsForBucket(key);
@@ -191,7 +191,7 @@ namespace SabreTools.DatFiles
/// <remarks>Applies to <see cref="Items"/></remarks> /// <remarks>Applies to <see cref="Items"/></remarks>
private void ExecuteFiltersImpl(FilterRunner filterRunner) private void ExecuteFiltersImpl(FilterRunner filterRunner)
{ {
List<string> keys = [.. Items.Keys]; List<string> keys = [.. Items.SortedKeys];
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(keys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
@@ -327,7 +327,7 @@ namespace SabreTools.DatFiles
// Then we want to get a mapping of all machines to parents // Then we want to get a mapping of all machines to parents
Dictionary<string, List<string>> parents = []; Dictionary<string, List<string>> parents = [];
foreach (string key in Items.Keys) foreach (string key in Items.SortedKeys)
{ {
DatItem item = GetItemsForBucket(key)[0]; DatItem item = GetItemsForBucket(key)[0];
@@ -389,7 +389,7 @@ namespace SabreTools.DatFiles
parents[key].Remove(machine); parents[key].Remove(machine);
// Remove the rest of the items from this key // Remove the rest of the items from this key
parents[key].ForEach(k => Remove(k)); parents[key].ForEach(k => RemoveBucket(k));
} }
// Finally, strip out the parent tags // Finally, strip out the parent tags
@@ -483,11 +483,11 @@ namespace SabreTools.DatFiles
{ {
// For each rom, we want to update the game to be "<game name>/<rom name>" // For each rom, we want to update the game to be "<game name>/<rom name>"
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(Items.Keys, Globals.ParallelOptions, key => Parallel.ForEach(Items.SortedKeys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(Items.Keys, key => Parallel.ForEach(Items.SortedKeys, key =>
#else #else
foreach (var key in Items.Keys) foreach (var key in Items.SortedKeys)
#endif #endif
{ {
var items = GetItemsForBucket(key); var items = GetItemsForBucket(key);
@@ -640,11 +640,11 @@ namespace SabreTools.DatFiles
{ {
// Now process all of the roms // Now process all of the roms
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(Items.Keys, Globals.ParallelOptions, key => Parallel.ForEach(Items.SortedKeys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(Items.Keys, key => Parallel.ForEach(Items.SortedKeys, key =>
#else #else
foreach (var key in Items.Keys) foreach (var key in Items.SortedKeys)
#endif #endif
{ {
var items = GetItemsForBucket(key); var items = GetItemsForBucket(key);
@@ -725,11 +725,11 @@ namespace SabreTools.DatFiles
private void UpdateMachineNamesFromDescriptions(IDictionary<string, string> mapping) private void UpdateMachineNamesFromDescriptions(IDictionary<string, string> mapping)
{ {
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(Items.Keys, Globals.ParallelOptions, key => Parallel.ForEach(Items.SortedKeys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(Items.Keys, key => Parallel.ForEach(Items.SortedKeys, key =>
#else #else
foreach (var key in Items.Keys) foreach (var key in Items.SortedKeys)
#endif #endif
{ {
var items = GetItemsForBucket(key); var items = GetItemsForBucket(key);

View File

@@ -240,7 +240,7 @@ namespace SabreTools.DatFiles
/// </remarks> /// </remarks>
private void AddItemsFromChildrenImpl(bool subfolder, bool skipDedup) private void AddItemsFromChildrenImpl(bool subfolder, bool skipDedup)
{ {
List<string> buckets = [.. Items.Keys]; List<string> buckets = [.. Items.SortedKeys];
buckets.Sort(); buckets.Sort();
foreach (string bucket in buckets) foreach (string bucket in buckets)
@@ -298,14 +298,14 @@ namespace SabreTools.DatFiles
.Contains(mergeTag)) .Contains(mergeTag))
{ {
disk.CopyMachineInformation(copyFrom); disk.CopyMachineInformation(copyFrom);
Add(cloneOf, disk); AddItem(disk, statsOnly: false);
} }
// If there is no merge tag, add to parent // If there is no merge tag, add to parent
else if (mergeTag == null) else if (mergeTag == null)
{ {
disk.CopyMachineInformation(copyFrom); disk.CopyMachineInformation(copyFrom);
Add(cloneOf, disk); AddItem(disk, statsOnly: false);
} }
} }
@@ -331,7 +331,7 @@ namespace SabreTools.DatFiles
rom.SetName($"{rom.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}\\{rom.GetName()}"); rom.SetName($"{rom.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}\\{rom.GetName()}");
rom.CopyMachineInformation(copyFrom); rom.CopyMachineInformation(copyFrom);
Add(cloneOf, rom); AddItem(rom, statsOnly: false);
} }
// If the parent doesn't already contain this item, add to subfolder of parent // If the parent doesn't already contain this item, add to subfolder of parent
@@ -341,7 +341,7 @@ namespace SabreTools.DatFiles
rom.SetName($"{item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}\\{rom.GetName()}"); rom.SetName($"{item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}\\{rom.GetName()}");
rom.CopyMachineInformation(copyFrom); rom.CopyMachineInformation(copyFrom);
Add(cloneOf, rom); AddItem(rom, statsOnly: false);
} }
} }
@@ -352,12 +352,12 @@ namespace SabreTools.DatFiles
item.SetName($"{item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}\\{item.GetName()}"); item.SetName($"{item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}\\{item.GetName()}");
item.CopyMachineInformation(copyFrom); item.CopyMachineInformation(copyFrom);
Add(cloneOf, item); AddItem(item, statsOnly: false);
} }
} }
// Then, remove the old game so it's not picked up by the writer // Then, remove the old game so it's not picked up by the writer
Remove(bucket); RemoveBucket(bucket);
} }
} }
@@ -503,7 +503,7 @@ namespace SabreTools.DatFiles
/// </remarks> /// </remarks>
private void AddItemsFromCloneOfParentImpl() private void AddItemsFromCloneOfParentImpl()
{ {
List<string> buckets = [.. Items.Keys]; List<string> buckets = [.. Items.SortedKeys];
buckets.Sort(); buckets.Sort();
foreach (string bucket in buckets) foreach (string bucket in buckets)
@@ -533,7 +533,7 @@ namespace SabreTools.DatFiles
if (!items.Exists(i => string.Equals(i.GetName(), datItem.GetName(), StringComparison.OrdinalIgnoreCase)) if (!items.Exists(i => string.Equals(i.GetName(), datItem.GetName(), StringComparison.OrdinalIgnoreCase))
&& !items.Contains(datItem)) && !items.Contains(datItem))
{ {
Add(bucket, datItem); AddItem(datItem, statsOnly: false);
} }
} }
@@ -625,7 +625,7 @@ namespace SabreTools.DatFiles
private bool AddItemsFromDevicesImpl(bool deviceOnly, bool useSlotOptions) private bool AddItemsFromDevicesImpl(bool deviceOnly, bool useSlotOptions)
{ {
bool foundnew = false; bool foundnew = false;
List<string> buckets = [.. Items.Keys]; List<string> buckets = [.. Items.SortedKeys];
buckets.Sort(); buckets.Sort();
foreach (string bucket in buckets) foreach (string bucket in buckets)
@@ -686,7 +686,7 @@ namespace SabreTools.DatFiles
// Clone the item and then add it // Clone the item and then add it
DatItem datItem = (DatItem)item.Clone(); DatItem datItem = (DatItem)item.Clone();
datItem.CopyMachineInformation(copyFrom); datItem.CopyMachineInformation(copyFrom);
Add(bucket, datItem); AddItem(datItem, statsOnly: false);
} }
} }
} }
@@ -734,7 +734,7 @@ namespace SabreTools.DatFiles
// Clone the item and then add it // Clone the item and then add it
DatItem datItem = (DatItem)item.Clone(); DatItem datItem = (DatItem)item.Clone();
datItem.CopyMachineInformation(copyFrom); datItem.CopyMachineInformation(copyFrom);
Add(bucket, datItem); AddItem(datItem, statsOnly: false);
} }
} }
} }
@@ -937,7 +937,7 @@ namespace SabreTools.DatFiles
/// </remarks> /// </remarks>
private void AddItemsFromRomOfParentImpl() private void AddItemsFromRomOfParentImpl()
{ {
List<string> buckets = [.. Items.Keys]; List<string> buckets = [.. Items.SortedKeys];
buckets.Sort(); buckets.Sort();
foreach (string bucket in buckets) foreach (string bucket in buckets)
@@ -965,7 +965,7 @@ namespace SabreTools.DatFiles
DatItem datItem = (DatItem)item.Clone(); DatItem datItem = (DatItem)item.Clone();
datItem.CopyMachineInformation(copyFrom); datItem.CopyMachineInformation(copyFrom);
if (!items.Exists(i => i.GetName() == datItem.GetName()) && !items.Contains(datItem)) if (!items.Exists(i => i.GetName() == datItem.GetName()) && !items.Contains(datItem))
Add(bucket, datItem); AddItem(datItem, statsOnly: false);
} }
} }
} }
@@ -1023,7 +1023,7 @@ namespace SabreTools.DatFiles
/// </remarks> /// </remarks>
private void RemoveBiosAndDeviceSetsImpl() private void RemoveBiosAndDeviceSetsImpl()
{ {
List<string> buckets = [.. Items.Keys]; List<string> buckets = [.. Items.SortedKeys];
buckets.Sort(); buckets.Sort();
foreach (string bucket in buckets) foreach (string bucket in buckets)
@@ -1042,7 +1042,7 @@ namespace SabreTools.DatFiles
if ((machine.GetBoolFieldValue(Models.Metadata.Machine.IsBiosKey) == true) if ((machine.GetBoolFieldValue(Models.Metadata.Machine.IsBiosKey) == true)
|| (machine.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true)) || (machine.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))
{ {
Remove(bucket); RemoveBucket(bucket);
} }
} }
} }
@@ -1093,7 +1093,7 @@ namespace SabreTools.DatFiles
/// </remarks> /// </remarks>
private void RemoveItemsFromCloneOfChildImpl() private void RemoveItemsFromCloneOfChildImpl()
{ {
List<string> buckets = [.. Items.Keys]; List<string> buckets = [.. Items.SortedKeys];
buckets.Sort(); buckets.Sort();
foreach (string bucket in buckets) foreach (string bucket in buckets)
@@ -1120,7 +1120,7 @@ namespace SabreTools.DatFiles
var matchedItems = items.FindAll(i => i.Equals(item)); var matchedItems = items.FindAll(i => i.Equals(item));
foreach (var match in matchedItems) foreach (var match in matchedItems)
{ {
Items.Remove(bucket, match); RemoveItem(bucket, match);
} }
} }
@@ -1200,7 +1200,7 @@ namespace SabreTools.DatFiles
private void RemoveItemsFromRomOfChildImpl() private void RemoveItemsFromRomOfChildImpl()
{ {
// Loop through the romof tags // Loop through the romof tags
List<string> buckets = [.. Items.Keys]; List<string> buckets = [.. Items.SortedKeys];
buckets.Sort(); buckets.Sort();
foreach (string bucket in buckets) foreach (string bucket in buckets)
@@ -1227,7 +1227,7 @@ namespace SabreTools.DatFiles
var matchedItems = items.FindAll(i => i.Equals(item)); var matchedItems = items.FindAll(i => i.Equals(item));
foreach (var match in matchedItems) foreach (var match in matchedItems)
{ {
Items.Remove(bucket, match); RemoveItem(bucket, match);
} }
} }
} }
@@ -1281,7 +1281,7 @@ namespace SabreTools.DatFiles
/// <remarks>Applies to <see cref="Items"/></remarks> /// <remarks>Applies to <see cref="Items"/></remarks>
private void RemoveMachineRelationshipTagsImpl() private void RemoveMachineRelationshipTagsImpl()
{ {
List<string> buckets = [.. Items.Keys]; List<string> buckets = [.. Items.SortedKeys];
buckets.Sort(); buckets.Sort();
foreach (string bucket in buckets) foreach (string bucket in buckets)

View File

@@ -142,26 +142,6 @@ namespace SabreTools.DatFiles
#region Item Dictionary Passthrough - Accessors #region Item Dictionary Passthrough - Accessors
/// <summary>
/// Add a value to the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to add to</param>
/// <param name="value">Value to add to the dictionary</param>
public void Add(string key, DatItem value)
{
Items.Add(key, value);
}
/// <summary>
/// Add a range of values to the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to add to</param>
/// <param name="value">Value to add to the dictionary</param>
public void Add(string key, List<DatItem>? value)
{
Items.Add(key, value);
}
/// <summary> /// <summary>
/// Add a DatItem to the dictionary after checking /// Add a DatItem to the dictionary after checking
/// </summary> /// </summary>
@@ -242,9 +222,19 @@ namespace SabreTools.DatFiles
/// Remove a key from the file dictionary if it exists /// Remove a key from the file dictionary if it exists
/// </summary> /// </summary>
/// <param name="key">Key in the dictionary to remove</param> /// <param name="key">Key in the dictionary to remove</param>
public bool Remove(string key) public bool RemoveBucket(string key)
{ {
return Items.Remove(key); return Items.RemoveBucket(key);
}
/// <summary>
/// Remove the first instance of a value from the file dictionary if it exists
/// </summary>
/// <param name="key">Key in the dictionary to remove from</param>
/// <param name="value">Value to remove from the dictionary</param>
public bool RemoveItem(string key, DatItem value)
{
return Items.RemoveItem(key, value);
} }
/// <summary> /// <summary>

View File

@@ -311,7 +311,7 @@ namespace SabreTools.DatFiles
/// <param name="inputs">List of inputs to use for renaming</param> /// <param name="inputs">List of inputs to use for renaming</param>
public static void ApplySuperDAT(DatFile datFile, List<ParentablePath> inputs) public static void ApplySuperDAT(DatFile datFile, List<ParentablePath> inputs)
{ {
List<string> keys = [.. datFile.Items.Keys]; List<string> keys = [.. datFile.Items.SortedKeys];
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(keys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
@@ -366,8 +366,8 @@ namespace SabreTools.DatFiles
newItems.Add(newItem); newItems.Add(newItem);
} }
datFile.Remove(key); datFile.RemoveBucket(key);
datFile.Add(key, newItems); newItems.ForEach(item => datFile.AddItem(item, statsOnly: false));
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else
@@ -467,11 +467,11 @@ namespace SabreTools.DatFiles
// Then we do a hashwise comparison against the base DAT // Then we do a hashwise comparison against the base DAT
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(intDat.Items.Keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(intDat.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(intDat.Items.Keys, key => Parallel.ForEach(intDat.Items.SortedKeys, key =>
#else #else
foreach (var key in intDat.Items.Keys) foreach (var key in intDat.Items.SortedKeys)
#endif #endif
{ {
List<DatItem>? datItems = intDat.GetItemsForBucket(key); List<DatItem>? datItems = intDat.GetItemsForBucket(key);
@@ -497,8 +497,8 @@ namespace SabreTools.DatFiles
} }
// Now add the new list to the key // Now add the new list to the key
intDat.Remove(key); intDat.RemoveBucket(key);
intDat.Add(key, newDatItems); newDatItems.ForEach(item => intDat.AddItem(item, statsOnly: false));
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else
@@ -515,11 +515,11 @@ namespace SabreTools.DatFiles
// Then we do a namewise comparison against the base DAT // Then we do a namewise comparison against the base DAT
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(intDat.Items.Keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(intDat.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(intDat.Items.Keys, key => Parallel.ForEach(intDat.Items.SortedKeys, key =>
#else #else
foreach (var key in intDat.Items.Keys) foreach (var key in intDat.Items.SortedKeys)
#endif #endif
{ {
List<DatItem>? datItems = intDat.GetItemsForBucket(key); List<DatItem>? datItems = intDat.GetItemsForBucket(key);
@@ -547,8 +547,8 @@ namespace SabreTools.DatFiles
} }
// Now add the new list to the key // Now add the new list to the key
intDat.Remove(key); intDat.RemoveBucket(key);
intDat.Add(key, newDatItems); newDatItems.ForEach(item => intDat.AddItem(item, statsOnly: false));
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else
@@ -685,7 +685,7 @@ namespace SabreTools.DatFiles
intDat.BucketBy(ItemKey.CRC, DedupeType.Full); intDat.BucketBy(ItemKey.CRC, DedupeType.Full);
// Then we compare against the base DAT // Then we compare against the base DAT
List<string> keys = [.. intDat.Items.Keys]; List<string> keys = [.. intDat.Items.SortedKeys];
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(keys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
@@ -737,7 +737,7 @@ namespace SabreTools.DatFiles
// If we have an exact match, remove the game // If we have an exact match, remove the game
if (exactMatch) if (exactMatch)
intDat.Remove(key); intDat.RemoveBucket(key);
} }
// Standard Against uses hashes // Standard Against uses hashes
@@ -759,8 +759,8 @@ namespace SabreTools.DatFiles
} }
// Now add the new list to the key // Now add the new list to the key
intDat.Remove(key); intDat.RemoveBucket(key);
intDat.Add(key, keepDatItems); keepDatItems.ForEach(item => intDat.AddItem(item, statsOnly: false));
} }
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
@@ -859,11 +859,11 @@ namespace SabreTools.DatFiles
watch.Start("Populating duplicate DAT"); watch.Start("Populating duplicate DAT");
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(datFile.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key => Parallel.ForEach(datFile.Items.SortedKeys, key =>
#else #else
foreach (var key in datFile.Items.Keys) foreach (var key in datFile.Items.SortedKeys)
#endif #endif
{ {
List<DatItem> items = Merge(datFile.GetItemsForBucket(key)); List<DatItem> items = Merge(datFile.GetItemsForBucket(key));
@@ -891,7 +891,7 @@ namespace SabreTools.DatFiles
if (item.GetFieldValue<Source?>(DatItem.SourceKey) != null) if (item.GetFieldValue<Source?>(DatItem.SourceKey) != null)
newrom.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, newrom.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey) + $" ({Path.GetFileNameWithoutExtension(inputs[item.GetFieldValue<Source?>(DatItem.SourceKey)!.Index].CurrentPath)})"); newrom.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, newrom.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey) + $" ({Path.GetFileNameWithoutExtension(inputs[item.GetFieldValue<Source?>(DatItem.SourceKey)!.Index].CurrentPath)})");
dupeData.Add(key, newrom); dupeData.AddItem(newrom, statsOnly: false);
} }
} }
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
@@ -1084,11 +1084,11 @@ namespace SabreTools.DatFiles
watch.Start("Populating all individual DATs"); watch.Start("Populating all individual DATs");
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(datFile.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key => Parallel.ForEach(datFile.Items.SortedKeys, key =>
#else #else
foreach (var key in datFile.Items.Keys) foreach (var key in datFile.Items.SortedKeys)
#endif #endif
{ {
List<DatItem> items = Merge(datFile.GetItemsForBucket(key)); List<DatItem> items = Merge(datFile.GetItemsForBucket(key));
@@ -1112,7 +1112,7 @@ namespace SabreTools.DatFiles
#else #else
if (item.GetFieldValue<DupeType>(DatItem.DupeTypeKey).HasFlag(DupeType.Internal) || item.GetFieldValue<DupeType>(DatItem.DupeTypeKey) == 0x00) if (item.GetFieldValue<DupeType>(DatItem.DupeTypeKey).HasFlag(DupeType.Internal) || item.GetFieldValue<DupeType>(DatItem.DupeTypeKey) == 0x00)
#endif #endif
outDats[item.GetFieldValue<Source?>(DatItem.SourceKey)!.Index].Add(key, item); outDats[item.GetFieldValue<Source?>(DatItem.SourceKey)!.Index].AddItem(item, statsOnly: false);
} }
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
@@ -1294,11 +1294,11 @@ namespace SabreTools.DatFiles
watch.Start("Populating no duplicate DAT"); watch.Start("Populating no duplicate DAT");
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(datFile.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key => Parallel.ForEach(datFile.Items.SortedKeys, key =>
#else #else
foreach (var key in datFile.Items.Keys) foreach (var key in datFile.Items.SortedKeys)
#endif #endif
{ {
List<DatItem> items = Merge(datFile.GetItemsForBucket(key)); List<DatItem> items = Merge(datFile.GetItemsForBucket(key));
@@ -1324,7 +1324,7 @@ namespace SabreTools.DatFiles
continue; continue;
newrom.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, newrom.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey) + $" ({Path.GetFileNameWithoutExtension(inputs[newrom.GetFieldValue<Source?>(DatItem.SourceKey)!.Index].CurrentPath)})"); newrom.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, newrom.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey) + $" ({Path.GetFileNameWithoutExtension(inputs[newrom.GetFieldValue<Source?>(DatItem.SourceKey)!.Index].CurrentPath)})");
outerDiffData.Add(key, newrom); outerDiffData.AddItem(newrom, statsOnly: false);
} }
} }
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
@@ -1522,15 +1522,15 @@ namespace SabreTools.DatFiles
private static void AddFromExisting(DatFile addTo, DatFile addFrom, bool delete = false) private static void AddFromExisting(DatFile addTo, DatFile addFrom, bool delete = false)
{ {
// Get the list of keys from the DAT // Get the list of keys from the DAT
List<string> keys = [.. addFrom.Items.Keys]; List<string> keys = [.. addFrom.Items.SortedKeys];
foreach (string key in keys) foreach (string key in keys)
{ {
// Add everything from the key to the internal DAT // Add everything from the key to the internal DAT
addTo.Add(key, addFrom.GetItemsForBucket(key)); addFrom.GetItemsForBucket(key).ForEach(item => addTo.AddItem(item, statsOnly: false));
// Now remove the key from the source DAT // Now remove the key from the source DAT
if (delete) if (delete)
addFrom.Remove(key); addFrom.RemoveBucket(key);
} }
// Now remove the file dictionary from the source DAT // Now remove the file dictionary from the source DAT
@@ -1612,11 +1612,11 @@ namespace SabreTools.DatFiles
{ {
// Loop through and add the items for this index to the output // Loop through and add the items for this index to the output
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(datFile.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key => Parallel.ForEach(datFile.Items.SortedKeys, key =>
#else #else
foreach (var key in datFile.Items.Keys) foreach (var key in datFile.Items.SortedKeys)
#endif #endif
{ {
List<DatItem> items = Merge(datFile.GetItemsForBucket(key)); List<DatItem> items = Merge(datFile.GetItemsForBucket(key));
@@ -1633,7 +1633,7 @@ namespace SabreTools.DatFiles
{ {
var source = item.GetFieldValue<Source?>(DatItem.SourceKey); var source = item.GetFieldValue<Source?>(DatItem.SourceKey);
if (source != null && source.Index == index) if (source != null && source.Index == index)
indexDat.Add(key, item); indexDat.AddItem(item, statsOnly: false);
} }
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });

View File

@@ -54,16 +54,6 @@ namespace SabreTools.DatFiles
#region Keys #region Keys
/// <summary>
/// Get the keys from the file dictionary
/// </summary>
/// <returns>List of the keys</returns>
[JsonIgnore, XmlIgnore]
public ICollection<string> Keys
{
get { return _items.Keys; }
}
/// <summary> /// <summary>
/// Get the keys in sorted order from the file dictionary /// Get the keys in sorted order from the file dictionary
/// </summary> /// </summary>
@@ -109,59 +99,6 @@ namespace SabreTools.DatFiles
#region Accessors #region Accessors
/// <summary>
/// Add a value to the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to add to</param>
/// <param name="value">Value to add to the dictionary</param>
public void Add(string key, DatItem value)
{
// Explicit lock for some weird corner cases
lock (key)
{
// Ensure the key exists
EnsureBucketingKey(key);
// If item is null, don't add it
if (value == null)
return;
// Now add the value
_items[key]!.Add(value);
// Now update the statistics
DatStatistics.AddItemStatistics(value);
}
}
/// <summary>
/// Add a range of values to the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to add to</param>
/// <param name="value">Value to add to the dictionary</param>
public void Add(string key, List<DatItem>? value)
{
// Explicit lock for some weird corner cases
lock (key)
{
// If the value is null or empty, just return
if (value == null || value.Count == 0)
return;
// Ensure the key exists
EnsureBucketingKey(key);
// Now add the value
_items[key]!.AddRange(value);
// Now update the statistics
foreach (DatItem item in value)
{
DatStatistics.AddItemStatistics(item);
}
}
}
/// <summary> /// <summary>
/// Add a DatItem to the dictionary after checking /// Add a DatItem to the dictionary after checking
/// </summary> /// </summary>
@@ -256,7 +193,7 @@ namespace SabreTools.DatFiles
} }
else else
{ {
Add(key, item); AddItem(key, item);
} }
return key; return key;
@@ -267,7 +204,7 @@ namespace SabreTools.DatFiles
/// </summary> /// </summary>
internal void ClearEmpty() internal void ClearEmpty()
{ {
string[] keys = [.. Keys]; string[] keys = [.. SortedKeys];
foreach (string key in keys) foreach (string key in keys)
{ {
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
@@ -303,15 +240,21 @@ namespace SabreTools.DatFiles
/// </summary> /// </summary>
internal void ClearMarked() internal void ClearMarked()
{ {
string[] keys = [.. Keys]; string[] keys = [.. SortedKeys];
foreach (string key in keys) foreach (string key in keys)
{ {
// Skip invalid item lists // Perform filtering on items
List<DatItem> oldItemList = GetItemsForBucket(key); List<DatItem> list = GetItemsForBucket(key, filter: true);
List<DatItem> newItemList = oldItemList.FindAll(i => i.GetBoolFieldValue(DatItem.RemoveKey) != true);
Remove(key); // Remove the current list
Add(key, newItemList); RemoveBucket(key);
// Add the filtered list back
#if NET40_OR_GREATER || NETCOREAPP
_ = _items.TryAdd(key, list);
#else
_items[key] = list;
#endif
} }
} }
@@ -333,33 +276,6 @@ namespace SabreTools.DatFiles
} }
} }
/// <summary>
/// Get if the file dictionary contains the key and value
/// </summary>
/// <param name="key">Key in the dictionary to check</param>
/// <param name="value">Value in the dictionary to check</param>
/// <returns>True if the key exists, false otherwise</returns>
public bool Contains(string key, DatItem value)
{
// If the key is null, we return false since keys can't be null
if (key == null)
return false;
// Explicit lock for some weird corner cases
lock (key)
{
#if NET40_OR_GREATER || NETCOREAPP
if (_items.TryGetValue(key, out var list) && list != null)
return list.Exists(i => i.Equals(value));
#else
if (_items.ContainsKey(key) && _items[key] != null)
return _items[key]!.Exists(i => i.Equals(value));
#endif
}
return false;
}
/// <summary> /// <summary>
/// Ensure the key exists in the items dictionary /// Ensure the key exists in the items dictionary
/// </summary> /// </summary>
@@ -378,6 +294,9 @@ namespace SabreTools.DatFiles
/// <summary> /// <summary>
/// Get the items associated with a bucket name /// Get the items associated with a bucket name
/// </summary> /// </summary>
/// <param name="bucketName">Name of the bucket to retrive items for</param>
/// <param name="filter">Indicates if RemoveKey filtering is performed</param>
/// <returns>List representing the bucket items, empty on missing</returns>
public List<DatItem> GetItemsForBucket(string? bucketName, bool filter = false) public List<DatItem> GetItemsForBucket(string? bucketName, bool filter = false)
{ {
if (bucketName == null) if (bucketName == null)
@@ -410,28 +329,27 @@ namespace SabreTools.DatFiles
/// Remove a key from the file dictionary if it exists /// Remove a key from the file dictionary if it exists
/// </summary> /// </summary>
/// <param name="key">Key in the dictionary to remove</param> /// <param name="key">Key in the dictionary to remove</param>
public bool Remove(string key) public bool RemoveBucket(string key)
{ {
// Explicit lock for some weird corner cases #if NET40_OR_GREATER || NETCOREAPP
lock (key) bool removed = _items.TryRemove(key, out var list);
{ #else
// If the key doesn't exist, return if (!_items.ContainsKey(key))
if (!ContainsKey(key) || _items[key] == null)
return false; return false;
// Remove the statistics first bool removed = true;
foreach (DatItem item in _items[key]!) var list = _items[key];
_items.Remove(key);
#endif
if (list == null)
return removed;
foreach (var item in list)
{ {
DatStatistics.RemoveItemStatistics(item); DatStatistics.RemoveItemStatistics(item);
} }
// Remove the key from the dictionary return removed;
#if NET40_OR_GREATER || NETCOREAPP
return _items.TryRemove(key, out _);
#else
return _items.Remove(key);
#endif
}
} }
/// <summary> /// <summary>
@@ -439,43 +357,35 @@ namespace SabreTools.DatFiles
/// </summary> /// </summary>
/// <param name="key">Key in the dictionary to remove from</param> /// <param name="key">Key in the dictionary to remove from</param>
/// <param name="value">Value to remove from the dictionary</param> /// <param name="value">Value to remove from the dictionary</param>
public bool Remove(string key, DatItem value) public bool RemoveItem(string key, DatItem value)
{ {
// Explicit lock for some weird corner cases // Explicit lock for some weird corner cases
lock (key) lock (key)
{ {
// If the key and value doesn't exist, return // If the key doesn't exist, return
if (!Contains(key, value) || _items[key] == null) #if NET40_OR_GREATER || NETCOREAPP
if (!_items.TryGetValue(key, out var list) || list == null)
return false;
#else
if (!_items.ContainsKey(key))
return false;
var list = _items[key];
if (list == null)
return false;
#endif
// If the value doesn't exist in the key, return
if (!list.Exists(i => i.Equals(value)))
return false; return false;
// Remove the statistics first // Remove the statistics first
DatStatistics.RemoveItemStatistics(value); DatStatistics.RemoveItemStatistics(value);
return _items[key]!.Remove(value); return list.Remove(value);
} }
} }
/// <summary>
/// Reset a key from the file dictionary if it exists
/// </summary>
/// <param name="key">Key in the dictionary to reset</param>
public bool Reset(string key)
{
// If the key doesn't exist, return
if (!ContainsKey(key) || _items[key] == null)
return false;
// Remove the statistics first
foreach (DatItem item in _items[key]!)
{
DatStatistics.RemoveItemStatistics(item);
}
// Remove the key from the dictionary
_items[key] = [];
return true;
}
/// <summary> /// <summary>
/// Override the internal ItemKey value /// Override the internal ItemKey value
/// </summary> /// </summary>
@@ -485,6 +395,31 @@ namespace SabreTools.DatFiles
_bucketedBy = newBucket; _bucketedBy = newBucket;
} }
/// <summary>
/// Add a value to the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to add to</param>
/// <param name="value">Value to add to the dictionary</param>
internal void AddItem(string key, DatItem value)
{
// Explicit lock for some weird corner cases
lock (key)
{
// Ensure the key exists
EnsureBucketingKey(key);
// If item is null, don't add it
if (value == null)
return;
// Now add the value
_items[key]!.Add(value);
// Now update the statistics
DatStatistics.AddItemStatistics(value);
}
}
#endregion #endregion
#region Bucketing #region Bucketing
@@ -532,28 +467,29 @@ namespace SabreTools.DatFiles
/// <remarks>This also sets the remove flag on any duplicates found</remarks> /// <remarks>This also sets the remove flag on any duplicates found</remarks>
internal List<DatItem> GetDuplicates(DatItem datItem, bool sorted = false) internal List<DatItem> GetDuplicates(DatItem datItem, bool sorted = false)
{ {
List<DatItem> output = [];
// Check for an empty rom list first // Check for an empty rom list first
if (DatStatistics.TotalCount == 0) if (DatStatistics.TotalCount == 0)
return output; return [];
// We want to get the proper key for the DatItem // We want to get the proper key for the DatItem
string key = SortAndGetKey(datItem, sorted); string key = SortAndGetKey(datItem, sorted);
// If the key doesn't exist, return the empty list // Get the items for the current key, if possible
if (!ContainsKey(key)) List<DatItem> roms = GetItemsForBucket(key, filter: false);
return output; if (roms.Count == 0)
return [];
// Remove the current key
RemoveBucket(key);
// Try to find duplicates // Try to find duplicates
List<DatItem> roms = GetItemsForBucket(key); List<DatItem> output = [];
List<DatItem> left = [];
for (int i = 0; i < roms.Count; i++) for (int i = 0; i < roms.Count; i++)
{ {
DatItem other = roms[i]; DatItem other = roms[i];
if (other.GetBoolFieldValue(DatItem.RemoveKey) == true) if (other.GetBoolFieldValue(DatItem.RemoveKey) == true)
{ {
left.Add(other); AddItem(key, other);
continue; continue;
} }
@@ -562,16 +498,9 @@ namespace SabreTools.DatFiles
other.SetFieldValue<bool?>(DatItem.RemoveKey, true); other.SetFieldValue<bool?>(DatItem.RemoveKey, true);
output.Add(other); output.Add(other);
} }
else
{
left.Add(other);
}
}
// Add back all roms with the proper flags AddItem(key, other);
Remove(key); }
Add(key, output);
Add(key, left);
return output; return output;
} }
@@ -651,7 +580,7 @@ namespace SabreTools.DatFiles
/// <param name="lower">True if the key should be lowercased, false otherwise</param> /// <param name="lower">True if the key should be lowercased, false otherwise</param>
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param> /// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
/// </summary> /// </summary>
private string GetBucketKey(DatItem datItem, ItemKey bucketBy, bool lower, bool norename) private static string GetBucketKey(DatItem datItem, ItemKey bucketBy, bool lower, bool norename)
{ {
if (datItem == null) if (datItem == null)
return string.Empty; return string.Empty;
@@ -679,7 +608,7 @@ namespace SabreTools.DatFiles
_mergedBy = DedupeType.None; _mergedBy = DedupeType.None;
// First do the initial sort of all of the roms inplace // First do the initial sort of all of the roms inplace
List<string> oldkeys = [.. Keys]; List<string> oldkeys = [.. SortedKeys];
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.For(0, oldkeys.Count, Core.Globals.ParallelOptions, k => Parallel.For(0, oldkeys.Count, Core.Globals.ParallelOptions, k =>
@@ -691,7 +620,7 @@ namespace SabreTools.DatFiles
{ {
string key = oldkeys[k]; string key = oldkeys[k];
if (GetItemsForBucket(key).Count == 0) if (GetItemsForBucket(key).Count == 0)
Remove(key); RemoveBucket(key);
// Now add each of the roms to their respective keys // Now add each of the roms to their respective keys
for (int i = 0; i < GetItemsForBucket(key).Count; i++) for (int i = 0; i < GetItemsForBucket(key).Count; i++)
@@ -706,8 +635,8 @@ namespace SabreTools.DatFiles
// If the key is different, move the item to the new key // If the key is different, move the item to the new key
if (newkey != key) if (newkey != key)
{ {
Add(newkey, item); AddItem(newkey, item);
bool removed = Remove(key, item); bool removed = RemoveItem(key, item);
if (!removed) if (!removed)
break; break;
@@ -717,7 +646,7 @@ namespace SabreTools.DatFiles
// If the key is now empty, remove it // If the key is now empty, remove it
if (GetItemsForBucket(key).Count == 0) if (GetItemsForBucket(key).Count == 0)
Remove(key); RemoveBucket(key);
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else
@@ -735,7 +664,7 @@ namespace SabreTools.DatFiles
// Set the sorted type // Set the sorted type
_mergedBy = dedupeType; _mergedBy = dedupeType;
List<string> keys = [.. Keys]; List<string> keys = [.. SortedKeys];
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(keys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
@@ -755,8 +684,8 @@ namespace SabreTools.DatFiles
sortedList = DatFileTool.Merge(sortedList); sortedList = DatFileTool.Merge(sortedList);
// Add the list back to the dictionary // Add the list back to the dictionary
Reset(key); RemoveBucket(key);
Add(key, sortedList); sortedList.ForEach(item => AddItem(key, item));
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else
@@ -769,7 +698,7 @@ namespace SabreTools.DatFiles
/// </summary> /// </summary>
private void PerformSorting() private void PerformSorting()
{ {
List<string> keys = [.. Keys]; List<string> keys = [.. SortedKeys];
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(keys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
@@ -785,8 +714,8 @@ namespace SabreTools.DatFiles
DatFileTool.Sort(ref sortedList, false); DatFileTool.Sort(ref sortedList, false);
// Add the list back to the dictionary // Add the list back to the dictionary
Reset(key); RemoveBucket(key);
Add(key, sortedList); sortedList.ForEach(item => AddItem(key, item));
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else

View File

@@ -152,7 +152,7 @@ namespace SabreTools.DatTools
/// <param name="datFile">Current DatFile object to run operations on</param> /// <param name="datFile">Current DatFile object to run operations on</param>
internal void CleanDatItems(DatFile datFile) internal void CleanDatItems(DatFile datFile)
{ {
List<string> keys = [.. datFile.Items.Keys]; List<string> keys = [.. datFile.Items.SortedKeys];
foreach (string key in keys) foreach (string key in keys)
{ {
// For every item in the current key // For every item in the current key

View File

@@ -230,7 +230,7 @@ namespace SabreTools.DatTools
{ {
// Add the list if it doesn't exist already // Add the list if it doesn't exist already
Rom rom = baseFile.ConvertToRom(); Rom rom = baseFile.ConvertToRom();
datFile.Add(rom.GetKey(ItemKey.CRC), rom); datFile.AddItem(rom, statsOnly: false);
_staticLogger.Verbose($"File added: {Path.GetFileNameWithoutExtension(item)}"); _staticLogger.Verbose($"File added: {Path.GetFileNameWithoutExtension(item)}");
} }
else else
@@ -387,7 +387,7 @@ namespace SabreTools.DatTools
blankRom.SetName(romname); blankRom.SetName(romname);
blankRom.SetFieldValue<Machine?>(DatItem.MachineKey, blankMachine); blankRom.SetFieldValue<Machine?>(DatItem.MachineKey, blankMachine);
datFile.Add("null", blankRom); datFile.AddItem(blankRom, statsOnly: false);
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else
@@ -440,8 +440,7 @@ namespace SabreTools.DatTools
SetDatItemInfo(datFile, datItem, item, parent, basepath); SetDatItemInfo(datFile, datItem, item, parent, basepath);
// Add the file information to the DAT // Add the file information to the DAT
string key = datItem.GetKey(ItemKey.CRC); datFile.AddItem(datItem, statsOnly: false);
datFile.Add(key, datItem);
_staticLogger.Verbose($"File added: {datItem.GetName() ?? string.Empty}"); _staticLogger.Verbose($"File added: {datItem.GetName() ?? string.Empty}");
} }

View File

@@ -166,7 +166,7 @@ namespace SabreTools.DatTools
if (itemDictionary == null || (MachineFieldNames.Count == 0 && ItemFieldNames.Count == 0)) if (itemDictionary == null || (MachineFieldNames.Count == 0 && ItemFieldNames.Count == 0))
return; return;
foreach (var key in itemDictionary.Keys) foreach (var key in itemDictionary.SortedKeys)
{ {
List<DatItem>? items = itemDictionary.GetItemsForBucket(key); List<DatItem>? items = itemDictionary.GetItemsForBucket(key);
if (items == null) if (items == null)

View File

@@ -2,8 +2,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
#endif #endif
using SabreTools.Core.Tools; using SabreTools.Core.Tools;
@@ -66,11 +66,11 @@ namespace SabreTools.DatTools
// Now separate the roms accordingly // Now separate the roms accordingly
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(datFile.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key => Parallel.ForEach(datFile.Items.SortedKeys, key =>
#else #else
foreach (var key in datFile.Items.Keys) foreach (var key in datFile.Items.SortedKeys)
#endif #endif
{ {
var items = datFile.GetItemsForBucket(key); var items = datFile.GetItemsForBucket(key);
@@ -85,16 +85,16 @@ namespace SabreTools.DatTools
{ {
if (Array.IndexOf(newExtA, (item.GetName() ?? string.Empty).GetNormalizedExtension()) > -1) if (Array.IndexOf(newExtA, (item.GetName() ?? string.Empty).GetNormalizedExtension()) > -1)
{ {
extADat.Add(key, item); extADat.AddItem(item, statsOnly: false);
} }
if (Array.IndexOf(newExtB, (item.GetName() ?? string.Empty).GetNormalizedExtension()) > -1) if (Array.IndexOf(newExtB, (item.GetName() ?? string.Empty).GetNormalizedExtension()) > -1)
{ {
extBDat.Add(key, item); extBDat.AddItem(item, statsOnly: false);
} }
else else
{ {
extADat.Add(key, item); extADat.AddItem(item, statsOnly: false);
extBDat.Add(key, item); extBDat.AddItem(item, statsOnly: false);
} }
} }
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
@@ -242,11 +242,11 @@ namespace SabreTools.DatTools
// Now populate each of the DAT objects in turn // Now populate each of the DAT objects in turn
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(datFile.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key => Parallel.ForEach(datFile.Items.SortedKeys, key =>
#else #else
foreach (var key in datFile.Items.Keys) foreach (var key in datFile.Items.SortedKeys)
#endif #endif
{ {
var items = datFile.GetItemsForBucket(key); var items = datFile.GetItemsForBucket(key);
@@ -263,49 +263,49 @@ namespace SabreTools.DatTools
{ {
case Disk disk: case Disk disk:
if (disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump) if (disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
fieldDats[Models.Metadata.Disk.StatusKey].Add(key, item); fieldDats[Models.Metadata.Disk.StatusKey].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key))) else if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key)))
fieldDats[Models.Metadata.Disk.SHA1Key].Add(key, item); fieldDats[Models.Metadata.Disk.SHA1Key].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key))) else if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key)))
fieldDats[Models.Metadata.Disk.MD5Key].Add(key, item); fieldDats[Models.Metadata.Disk.MD5Key].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key))) else if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key)))
fieldDats[Models.Metadata.Disk.MD5Key].Add(key, item); fieldDats[Models.Metadata.Disk.MD5Key].AddItem(item, statsOnly: false);
else else
fieldDats["null"].Add(key, item); fieldDats["null"].AddItem(item, statsOnly: false);
break; break;
case Media media: case Media media:
if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA256Key))) if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA256Key)))
fieldDats[Models.Metadata.Media.SHA256Key].Add(key, item); fieldDats[Models.Metadata.Media.SHA256Key].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA1Key))) else if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA1Key)))
fieldDats[Models.Metadata.Media.SHA1Key].Add(key, item); fieldDats[Models.Metadata.Media.SHA1Key].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.MD5Key))) else if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.MD5Key)))
fieldDats[Models.Metadata.Media.MD5Key].Add(key, item); fieldDats[Models.Metadata.Media.MD5Key].AddItem(item, statsOnly: false);
else else
fieldDats["null"].Add(key, item); fieldDats["null"].AddItem(item, statsOnly: false);
break; break;
case Rom rom: case Rom rom:
if (rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump) if (rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
fieldDats[Models.Metadata.Rom.StatusKey].Add(key, item); fieldDats[Models.Metadata.Rom.StatusKey].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA512Key))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA512Key)))
fieldDats[Models.Metadata.Rom.SHA512Key].Add(key, item); fieldDats[Models.Metadata.Rom.SHA512Key].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA384Key))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA384Key)))
fieldDats[Models.Metadata.Rom.SHA384Key].Add(key, item); fieldDats[Models.Metadata.Rom.SHA384Key].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA256Key))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA256Key)))
fieldDats[Models.Metadata.Rom.SHA256Key].Add(key, item); fieldDats[Models.Metadata.Rom.SHA256Key].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key)))
fieldDats[Models.Metadata.Rom.SHA1Key].Add(key, item); fieldDats[Models.Metadata.Rom.SHA1Key].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.MD5Key))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.MD5Key)))
fieldDats[Models.Metadata.Rom.MD5Key].Add(key, item); fieldDats[Models.Metadata.Rom.MD5Key].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.MD4Key))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.MD4Key)))
fieldDats[Models.Metadata.Rom.MD4Key].Add(key, item); fieldDats[Models.Metadata.Rom.MD4Key].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.MD2Key))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.MD2Key)))
fieldDats[Models.Metadata.Rom.MD2Key].Add(key, item); fieldDats[Models.Metadata.Rom.MD2Key].AddItem(item, statsOnly: false);
else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.CRCKey))) else if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.CRCKey)))
fieldDats[Models.Metadata.Rom.CRCKey].Add(key, item); fieldDats[Models.Metadata.Rom.CRCKey].AddItem(item, statsOnly: false);
else else
fieldDats["null"].Add(key, item); fieldDats["null"].AddItem(item, statsOnly: false);
break; break;
default: default:
@@ -500,7 +500,7 @@ namespace SabreTools.DatTools
tempDat.Header.SetFieldValue<string?>(Models.Metadata.Header.NameKey, null); tempDat.Header.SetFieldValue<string?>(Models.Metadata.Header.NameKey, null);
// Sort the input keys // Sort the input keys
List<string> keys = [.. datFile.Items.Keys]; List<string> keys = [.. datFile.Items.SortedKeys];
keys.Sort(SplitByLevelSort); keys.Sort(SplitByLevelSort);
// Then, we loop over the games // Then, we loop over the games
@@ -532,7 +532,7 @@ namespace SabreTools.DatTools
items.ForEach(item => item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, Path.GetFileName(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)))); items.ForEach(item => item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, Path.GetFileName(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey))));
// Now add the game to the output DAT // Now add the game to the output DAT
tempDat.Add(key, items); items.ForEach(item => tempDat.AddItem(item, statsOnly: false));
// Then set the DAT name to be the parent directory name // Then set the DAT name to be the parent directory name
tempDat.Header.SetFieldValue<string?>(Models.Metadata.Header.NameKey, Path.GetDirectoryName(key)); tempDat.Header.SetFieldValue<string?>(Models.Metadata.Header.NameKey, Path.GetDirectoryName(key));
@@ -631,11 +631,11 @@ namespace SabreTools.DatTools
// Now populate each of the DAT objects in turn // Now populate each of the DAT objects in turn
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(datFile.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key => Parallel.ForEach(datFile.Items.SortedKeys, key =>
#else #else
foreach (var key in datFile.Items.Keys) foreach (var key in datFile.Items.SortedKeys)
#endif #endif
{ {
List<DatItem>? items = datFile.GetItemsForBucket(key); List<DatItem>? items = datFile.GetItemsForBucket(key);
@@ -649,19 +649,19 @@ namespace SabreTools.DatTools
{ {
// If the file is not a Rom, it automatically goes in the "lesser" dat // If the file is not a Rom, it automatically goes in the "lesser" dat
if (item is not Rom rom) if (item is not Rom rom)
lessThan.Add(key, item); lessThan.AddItem(item, statsOnly: false);
// If the file is a Rom and has no size, put it in the "lesser" dat // If the file is a Rom and has no size, put it in the "lesser" dat
else if (rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey) == null) else if (rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey) == null)
lessThan.Add(key, item); lessThan.AddItem(item, statsOnly: false);
// If the file is a Rom and less than the radix, put it in the "lesser" dat // If the file is a Rom and less than the radix, put it in the "lesser" dat
else if (rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey) < radix) else if (rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey) < radix)
lessThan.Add(key, item); lessThan.AddItem(item, statsOnly: false);
// If the file is a Rom and greater than or equal to the radix, put it in the "greater" dat // If the file is a Rom and greater than or equal to the radix, put it in the "greater" dat
else if (rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey) >= radix) else if (rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey) >= radix)
greaterThan.Add(key, item); greaterThan.AddItem(item, statsOnly: false);
} }
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
@@ -838,7 +838,7 @@ namespace SabreTools.DatTools
} }
// Add the current machine to the current DatFile // Add the current machine to the current DatFile
currentDat.Add(machine, items); items.ForEach(item => currentDat.AddItem(item, statsOnly: false));
currentSize += machineSize; currentSize += machineSize;
} }
@@ -913,11 +913,11 @@ namespace SabreTools.DatTools
{ {
// Loop through and add the items for this index to the output // Loop through and add the items for this index to the output
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(datFile.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key => Parallel.ForEach(datFile.Items.SortedKeys, key =>
#else #else
foreach (var key in datFile.Items.Keys) foreach (var key in datFile.Items.SortedKeys)
#endif #endif
{ {
List<DatItem> items = DatFileTool.Merge(datFile.GetItemsForBucket(key)); List<DatItem> items = DatFileTool.Merge(datFile.GetItemsForBucket(key));
@@ -933,7 +933,7 @@ namespace SabreTools.DatTools
foreach (DatItem item in items) foreach (DatItem item in items)
{ {
if (item.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>() == itemType) if (item.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>() == itemType)
indexDat.Add(key, item); indexDat.AddItem(item, statsOnly: false);
} }
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });

View File

@@ -83,17 +83,17 @@ namespace SabreTools.DatTools
{ {
DatStatistics individualStats = datdata.DatStatistics; DatStatistics individualStats = datdata.DatStatistics;
individualStats.DisplayName = datdata.Header.GetStringFieldValue(DatHeader.FileNameKey); individualStats.DisplayName = datdata.Header.GetStringFieldValue(DatHeader.FileNameKey);
individualStats.MachineCount = datdata.Items.Keys.Count; individualStats.MachineCount = datdata.Items.SortedKeys.Count;
stats.Add(individualStats); stats.Add(individualStats);
} }
// Add single DAT stats to dir // Add single DAT stats to dir
dirStats.AddStatistics(datdata.DatStatistics); dirStats.AddStatistics(datdata.DatStatistics);
dirStats.GameCount += datdata.Items.Keys.Count; dirStats.GameCount += datdata.Items.SortedKeys.Count;
// Add single DAT stats to totals // Add single DAT stats to totals
totalStats.AddStatistics(datdata.DatStatistics); totalStats.AddStatistics(datdata.DatStatistics);
totalStats.GameCount += datdata.Items.Keys.Count; totalStats.GameCount += datdata.Items.SortedKeys.Count;
// Make sure to assign the new directory // Make sure to assign the new directory
lastdir = thisdir; lastdir = thisdir;

View File

@@ -141,7 +141,7 @@ namespace SabreTools.DatTools
datFile.BucketBy(ItemKey.Machine, DedupeType.None, norename: true); datFile.BucketBy(ItemKey.Machine, DedupeType.None, norename: true);
datFile.DatStatistics.DisplayName = datFile.Header.GetStringFieldValue(DatHeader.FileNameKey); datFile.DatStatistics.DisplayName = datFile.Header.GetStringFieldValue(DatHeader.FileNameKey);
datFile.DatStatistics.MachineCount = datFile.Items.Keys.Count; datFile.DatStatistics.MachineCount = datFile.Items.SortedKeys.Count;
List<DatStatistics> statsList = List<DatStatistics> statsList =
[ [

View File

@@ -52,11 +52,13 @@ namespace SabreTools.Test.DatFiles
// Setup the dictionary // Setup the dictionary
var dict = new ItemDictionary(); var dict = new ItemDictionary();
dict.Add("game-1", [rom1, rom2]); dict.AddItem(rom1, statsOnly: false);
dict.Add("game-2", [rom3, rom4]); dict.AddItem(rom2, statsOnly: false);
dict.AddItem(rom3, statsOnly: false);
dict.AddItem(rom4, statsOnly: false);
dict.BucketBy(itemKey, DedupeType.None); dict.BucketBy(itemKey, DedupeType.None);
Assert.Equal(expected, dict.Keys.Count); Assert.Equal(expected, dict.SortedKeys.Count);
} }
[Fact] [Fact]
@@ -64,12 +66,10 @@ namespace SabreTools.Test.DatFiles
{ {
// Setup the dictionary // Setup the dictionary
var dict = new ItemDictionary(); var dict = new ItemDictionary();
dict.Add("game-1", [new Rom()]); dict.AddItem("game-1", new Rom());
dict.Add("game-2", []);
dict.Add("game-3", (List<DatItem>?)null);
dict.ClearEmpty(); dict.ClearEmpty();
Assert.Single(dict.Keys); Assert.Single(dict.SortedKeys);
} }
[Fact] [Fact]
@@ -96,10 +96,11 @@ namespace SabreTools.Test.DatFiles
// Setup the dictionary // Setup the dictionary
var dict = new ItemDictionary(); var dict = new ItemDictionary();
dict.Add("game-1", [rom1, rom2]); dict.AddItem("game-1", rom1);
dict.AddItem("game-1", rom2);
dict.ClearMarked(); dict.ClearMarked();
string key = Assert.Single(dict.Keys); string key = Assert.Single(dict.SortedKeys);
Assert.Equal("game-1", key); Assert.Equal("game-1", key);
List<DatItem> items = dict.GetItemsForBucket(key); List<DatItem> items = dict.GetItemsForBucket(key);
Assert.Single(items); Assert.Single(items);
@@ -128,7 +129,8 @@ namespace SabreTools.Test.DatFiles
// Setup the dictionary // Setup the dictionary
var dict = new ItemDictionary(); var dict = new ItemDictionary();
dict.Add("game-1", [rom1, rom2]); dict.AddItem("game-1", rom1);
dict.AddItem("game-1", rom2);
// Setup the test item // Setup the test item
var rom = new Rom(); var rom = new Rom();
@@ -164,7 +166,8 @@ namespace SabreTools.Test.DatFiles
// Setup the dictionary // Setup the dictionary
var dict = new ItemDictionary(); var dict = new ItemDictionary();
dict.Add("game-1", [rom1, rom2]); dict.AddItem("game-1", rom1);
dict.AddItem("game-1", rom2);
// Setup the test item // Setup the test item
var rom = new Rom(); var rom = new Rom();