ItemDictionary is no longer IDictionary

This commit is contained in:
Matt Nadareski
2025-01-14 10:38:46 -05:00
parent b29f7c65a4
commit 8c3c6ab3e8
8 changed files with 36 additions and 126 deletions

View File

@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SabreTools.Core.Tools;
@@ -16,7 +15,7 @@ namespace SabreTools.DatFiles
internal Models.Metadata.MetadataFile? ConvertToMetadata(bool ignoreblanks = false)
{
// If we don't have items, we can't do anything
if (Items.Count == 0)
if (DatStatistics.TotalCount == 0)
return null;
// Create an object to hold the data
@@ -41,7 +40,7 @@ namespace SabreTools.DatFiles
internal Models.Metadata.MetadataFile? ConvertToMetadataDB(bool ignoreblanks = false)
{
// If we don't have items, we can't do anything
if (ItemsDB.GetItems().Count == 0)
if (ItemsDB.DatStatistics.TotalCount == 0)
return null;
// Create an object to hold the data

View File

@@ -29,7 +29,7 @@ namespace SabreTools.DatFiles
/// DatItems and related statistics
/// </summary>
[JsonProperty("items"), XmlElement("items")]
public ItemDictionary Items { get; private set; } = [];
public ItemDictionary Items { get; private set; } = new ItemDictionary();
/// <summary>
/// DatItems and related statistics
@@ -252,7 +252,7 @@ namespace SabreTools.DatFiles
/// </summary>
public void ResetDictionary()
{
Items.Clear();
Items = new ItemDictionary();
ItemsDB = new ItemDictionaryDB();
}

View File

@@ -536,7 +536,8 @@ namespace SabreTools.DatFiles
if (datItem.Clone() is not DatItem newDatItem)
continue;
if (!datFile.Items.TryGetValue(key, out var list) || list == null)
var list = datFile.GetItemsForBucket(key);
if (list.Count == 0)
continue;
if (datFile.Items.ContainsKey(key) && list.Count > 0)
@@ -697,7 +698,8 @@ namespace SabreTools.DatFiles
if (useGames)
{
// If the key is null, keep it
if (!intDat.Items.TryGetValue(key, out var intList) || intList == null)
var intList = intDat.GetItemsForBucket(key);
if (intList.Count == 0)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
@@ -705,7 +707,8 @@ namespace SabreTools.DatFiles
#endif
// If the base DAT doesn't contain the key, keep it
if (!datFile.Items.TryGetValue(key, out var list) || list == null)
var list = datFile.GetItemsForBucket(key);
if (list.Count == 0)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else

View File

@@ -1,5 +1,4 @@
using System.Collections;
#if NET40_OR_GREATER || NETCOREAPP
#if NET40_OR_GREATER || NETCOREAPP
using System.Collections.Concurrent;
#endif
using System.Collections.Generic;
@@ -15,14 +14,13 @@ using SabreTools.Hashing;
using SabreTools.IO.Logging;
using SabreTools.Matching.Compare;
// TODO: Remove IDictionary implementation
namespace SabreTools.DatFiles
{
/// <summary>
/// Item dictionary with statistics, bucketing, and sorting
/// </summary>
[JsonObject("items"), XmlRoot("items")]
public class ItemDictionary : IDictionary<string, List<DatItem>?>
public class ItemDictionary
{
#region Private instance variables
@@ -111,34 +109,6 @@ namespace SabreTools.DatFiles
#region Accessors
/// <summary>
/// Passthrough to access the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to reference</param>
public List<DatItem>? this[string key]
{
get
{
// Explicit lock for some weird corner cases
lock (key)
{
// Ensure the key exists
EnsureKey(key);
// Now return the value
return _items[key];
}
}
set
{
Remove(key);
if (value == null)
_items[key] = null;
else
Add(key, value);
}
}
/// <summary>
/// Add a value to the file dictionary
/// </summary>
@@ -842,55 +812,5 @@ namespace SabreTools.DatFiles
}
#endregion
#region IDictionary Implementations
public ICollection<List<DatItem>?> Values => ((IDictionary<string, List<DatItem>?>)_items).Values;
public int Count => ((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).Count;
public bool IsReadOnly => ((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).IsReadOnly;
public bool TryGetValue(string key, out List<DatItem>? value)
{
return ((IDictionary<string, List<DatItem>?>)_items).TryGetValue(key, out value);
}
public void Add(KeyValuePair<string, List<DatItem>?> item)
{
((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).Add(item);
}
public void Clear()
{
((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).Clear();
}
public bool Contains(KeyValuePair<string, List<DatItem>?> item)
{
return ((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).Contains(item);
}
public void CopyTo(KeyValuePair<string, List<DatItem>?>[] array, int arrayIndex)
{
((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<string, List<DatItem>?> item)
{
return ((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).Remove(item);
}
public IEnumerator<KeyValuePair<string, List<DatItem>?>> GetEnumerator()
{
return ((IEnumerable<KeyValuePair<string, List<DatItem>?>>)_items).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_items).GetEnumerator();
}
#endregion
}
}