[DatFile, DatItem] Add better removal logic

This commit is contained in:
Matt Nadareski
2017-10-31 14:53:02 -07:00
parent 178ddcd7d9
commit 67b7c277b5
2 changed files with 42 additions and 8 deletions

View File

@@ -2534,6 +2534,29 @@ namespace SabreTools.Library.DatFiles
}
}
/// <summary>
/// Remove all items marked for removal from the DAT
/// </summary>
private void RemoveMarkedItems()
{
List<string> keys = Keys;
foreach (string key in keys)
{
List<DatItem> items = this[key];
List<DatItem> newItems = new List<DatItem>();
foreach (DatItem item in items)
{
if (!item.Remove)
{
newItems.Add(item);
}
}
Remove(key);
AddRange(key, newItems);
}
}
/// <summary>
/// Strip the given hash types from the DAT
/// </summary>
@@ -4031,6 +4054,7 @@ namespace SabreTools.Library.DatFiles
FileName = "fixDAT_" + FileName;
Name = "fixDAT_" + Name;
Description = "fixDAT_" + Description;
RemoveMarkedItems();
WriteToFile(outDir);
}
@@ -4178,6 +4202,7 @@ namespace SabreTools.Library.DatFiles
FileName = "fixDAT_" + FileName;
Name = "fixDAT_" + Name;
Description = "fixDAT_" + Description;
RemoveMarkedItems();
WriteToFile(outDir);
}

View File

@@ -23,7 +23,7 @@ namespace SabreTools.Library.Items
// Standard item information
protected string _name;
private string _merge;
protected string _merge;
protected ItemType _itemType;
protected DupeType _dupeType;
@@ -45,6 +45,7 @@ namespace SabreTools.Library.Items
protected string _systemName;
protected int _sourceId;
protected string _sourceName;
protected bool _remove;
#endregion
@@ -431,6 +432,11 @@ namespace SabreTools.Library.Items
get { return _sourceName; }
set { _sourceName = value; }
}
public bool Remove
{
get { return _remove; }
set { _remove = value; }
}
#endregion
@@ -582,7 +588,7 @@ namespace SabreTools.Library.Items
/// List all duplicates found in a DAT based on a rom
/// </summary>
/// <param name="datdata">Dat to match against</param>
/// <param name="remove">True to remove matched roms from the input, false otherwise (default)</param>
/// <param name="remove">True to mark matched roms for removal from the input, false otherwise (default)</param>
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
/// <returns>List of matched DatItem objects</returns>
public List<DatItem> GetDuplicates(DatFile datdata, bool remove = false, bool sorted = false)
@@ -607,23 +613,26 @@ namespace SabreTools.Library.Items
// Try to find duplicates
List<DatItem> roms = datdata[key];
List<DatItem> left = new List<DatItem>();
for (int i = 0; i < roms.Count; i++)
{
DatItem datItem = roms[i];
foreach (DatItem rom in roms)
if (this.Equals(datItem))
{
if (this.Equals(rom))
{
output.Add(rom);
datItem.Remove = true;
output.Add(datItem);
}
else
{
left.Add(rom);
left.Add(datItem);
}
}
// If we're in removal mode, replace the list with the new one
// If we're in removal mode, add back all roms with the proper flags
if (remove)
{
datdata.Remove(key);
datdata.AddRange(key, output);
datdata.AddRange(key, left);
}