Move GetDuplicateStatus implementations

This commit is contained in:
Matt Nadareski
2025-05-02 19:54:23 -04:00
parent 7d35594040
commit f38fe9b007
8 changed files with 504 additions and 425 deletions

View File

@@ -1869,31 +1869,32 @@ namespace SabreTools.DatFiles.Test
[Fact] [Fact]
public void ResolveNamesDB_AllDuplicate_Single() public void ResolveNamesDB_AllDuplicate_Single()
{ {
DatFile datFile = new Formats.Logiqx(null, useGame: false);
Machine machine = new Machine(); Machine machine = new Machine();
machine.SetFieldValue(Models.Metadata.Machine.NameKey, "machine"); machine.SetFieldValue(Models.Metadata.Machine.NameKey, "machine");
long machineIndex = datFile.AddMachineDB(machine);
Source source = new Source(0); Source source = new Source(0);
long sourceIndex = datFile.AddSourceDB(source);
Rom romA = new Rom(); Rom romA = new Rom();
romA.SetName("rom"); romA.SetName("rom");
romA.SetFieldValue(Models.Metadata.Rom.SizeKey, 12345); romA.SetFieldValue(Models.Metadata.Rom.SizeKey, 12345);
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "crc"); romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "crc");
romA.SetFieldValue(DatItem.MachineKey, (Machine)machine.Clone()); long romAIndex = datFile.AddItemDB(romA, machineIndex, sourceIndex, statsOnly: false);
romA.SetFieldValue(DatItem.SourceKey, (Source)source.Clone());
Rom romB = new Rom(); Rom romB = new Rom();
romB.SetName("rom"); romB.SetName("rom");
romB.SetFieldValue(Models.Metadata.Rom.SizeKey, 12345); romB.SetFieldValue(Models.Metadata.Rom.SizeKey, 12345);
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "crc"); romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "crc");
romB.SetFieldValue(DatItem.MachineKey, (Machine)machine.Clone()); long romBIndex = datFile.AddItemDB(romB, machineIndex, sourceIndex, statsOnly: false);
romB.SetFieldValue(DatItem.SourceKey, (Source)source.Clone());
List<KeyValuePair<long, DatItem>> mappings = List<KeyValuePair<long, DatItem>> mappings =
[ [
new KeyValuePair<long, DatItem>(0, romA), new KeyValuePair<long, DatItem>(romAIndex, romA),
new KeyValuePair<long, DatItem>(1, romB), new KeyValuePair<long, DatItem>(romBIndex, romB),
]; ];
DatFile datFile = new Formats.Logiqx(null, useGame: false);
List<KeyValuePair<long, DatItem>> actual = datFile.ResolveNamesDB(mappings); List<KeyValuePair<long, DatItem>> actual = datFile.ResolveNamesDB(mappings);
KeyValuePair<long, DatItem> actualItemA = Assert.Single(actual); KeyValuePair<long, DatItem> actualItemA = Assert.Single(actual);

View File

@@ -696,6 +696,209 @@ namespace SabreTools.DatFiles.Test
#endregion #endregion
#region GetDuplicateStatus
[Fact]
public void GetDuplicateStatus_NullOther_NoDupe()
{
var dict = new ItemDictionaryDB();
Source? selfSource = null;
Source? lastSource = null;
KeyValuePair<long, DatItem>? item = new KeyValuePair<long, DatItem>(0, new Rom());
KeyValuePair<long, DatItem>? lastItem = null;
var actual = dict.GetDuplicateStatus(item, selfSource, lastItem, lastSource);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentTypes_NoDupe()
{
var dict = new ItemDictionaryDB();
Source? selfSource = null;
Source? lastSource = null;
KeyValuePair<long, DatItem>? rom = new KeyValuePair<long, DatItem>(0, new Rom());
KeyValuePair<long, DatItem>? lastItem = new KeyValuePair<long, DatItem>(1, new Disk());
var actual = dict.GetDuplicateStatus(rom, selfSource, lastItem, lastSource);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_MismatchedHashes_NoDupe()
{
var dict = new ItemDictionaryDB();
Source? sourceA = new Source(0);
long sourceAIndex = dict.AddSource(sourceA);
Source? sourceB = new Source(1);
long sourceBIndex = dict.AddSource(sourceB);
Machine? machineA = new Machine();
machineA.SetName("name-same");
long machineAIndex = dict.AddMachine(machineA);
Machine? machineB = new Machine();
machineB.SetName("name-same");
long machineBIndex = dict.AddMachine(machineB);
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "BEEFDEAD");
long romAIndex = dict.AddItem(romA, machineAIndex, sourceAIndex);
KeyValuePair<long, DatItem>? romAPair = new KeyValuePair<long, DatItem>(romAIndex, romA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romBIndex = dict.AddItem(romB, machineBIndex, sourceBIndex);
KeyValuePair<long, DatItem>? romBPair = new KeyValuePair<long, DatItem>(romBIndex, romB);
var actual = dict.GetDuplicateStatus(romAPair, sourceA, romBPair, sourceB);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentSource_NameMatch_ExternalAll()
{
var dict = new ItemDictionaryDB();
Source? sourceA = new Source(0);
long sourceAIndex = dict.AddSource(sourceA);
Source? sourceB = new Source(1);
long sourceBIndex = dict.AddSource(sourceB);
Machine? machineA = new Machine();
machineA.SetName("name-same");
long machineAIndex = dict.AddMachine(machineA);
Machine? machineB = new Machine();
machineB.SetName("name-same");
long machineBIndex = dict.AddMachine(machineB);
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romAIndex = dict.AddItem(romA, machineAIndex, sourceAIndex);
KeyValuePair<long, DatItem>? romAPair = new KeyValuePair<long, DatItem>(romAIndex, romA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romBIndex = dict.AddItem(romB, machineBIndex, sourceBIndex);
KeyValuePair<long, DatItem>? romBPair = new KeyValuePair<long, DatItem>(romBIndex, romB);
var actual = dict.GetDuplicateStatus(romAPair, sourceA, romBPair, sourceB);
Assert.Equal(DupeType.External | DupeType.All, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentSource_NoNameMatch_ExternalHash()
{
var dict = new ItemDictionaryDB();
Source? sourceA = new Source(0);
long sourceAIndex = dict.AddSource(sourceA);
Source? sourceB = new Source(1);
long sourceBIndex = dict.AddSource(sourceB);
Machine? machineA = new Machine();
machineA.SetName("name-same");
long machineAIndex = dict.AddMachine(machineA);
Machine? machineB = new Machine();
machineB.SetName("not-name-same");
long machineBIndex = dict.AddMachine(machineB);
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romAIndex = dict.AddItem(romA, machineAIndex, sourceAIndex);
KeyValuePair<long, DatItem>? romAPair = new KeyValuePair<long, DatItem>(romAIndex, romA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romBIndex = dict.AddItem(romB, machineBIndex, sourceBIndex);
KeyValuePair<long, DatItem>? romBPair = new KeyValuePair<long, DatItem>(romBIndex, romB);
var actual = dict.GetDuplicateStatus(romAPair, sourceA, romBPair, sourceB);
Assert.Equal(DupeType.External | DupeType.Hash, actual);
}
[Fact]
public void GetDuplicateStatus_SameSource_NameMatch_InternalAll()
{
var dict = new ItemDictionaryDB();
Source? sourceA = new Source(0);
long sourceAIndex = dict.AddSource(sourceA);
Source? sourceB = new Source(0);
long sourceBIndex = dict.AddSource(sourceB);
Machine? machineA = new Machine();
machineA.SetName("name-same");
long machineAIndex = dict.AddMachine(machineA);
Machine? machineB = new Machine();
machineB.SetName("name-same");
long machineBIndex = dict.AddMachine(machineB);
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romAIndex = dict.AddItem(romA, machineAIndex, sourceAIndex);
KeyValuePair<long, DatItem>? romAPair = new KeyValuePair<long, DatItem>(romAIndex, romA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romBIndex = dict.AddItem(romB, machineBIndex, sourceBIndex);
KeyValuePair<long, DatItem>? romBPair = new KeyValuePair<long, DatItem>(romBIndex, romB);
var actual = dict.GetDuplicateStatus(romAPair, sourceA, romBPair, sourceB);
Assert.Equal(DupeType.Internal | DupeType.All, actual);
}
[Fact]
public void GetDuplicateStatus_SameSource_NoNameMatch_InternalHash()
{
var dict = new ItemDictionaryDB();
Source? sourceA = new Source(0);
long sourceAIndex = dict.AddSource(sourceA);
Source? sourceB = new Source(0);
long sourceBIndex = dict.AddSource(sourceB);
Machine? machineA = new Machine();
machineA.SetName("name-same");
long machineAIndex = dict.AddMachine(machineA);
Machine? machineB = new Machine();
machineB.SetName("not-name-same");
long machineBIndex = dict.AddMachine(machineB);
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romAIndex = dict.AddItem(romA, machineAIndex, sourceAIndex);
KeyValuePair<long, DatItem>? romAPair = new KeyValuePair<long, DatItem>(romAIndex, romA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romBIndex = dict.AddItem(romB, machineBIndex, sourceBIndex);
KeyValuePair<long, DatItem>? romBPair = new KeyValuePair<long, DatItem>(romBIndex, romB);
var actual = dict.GetDuplicateStatus(romAPair, sourceA, romBPair, sourceB);
Assert.Equal(DupeType.Internal | DupeType.Hash, actual);
}
#endregion
#region GetDuplicates #region GetDuplicates
[Theory] [Theory]

View File

@@ -535,6 +535,165 @@ namespace SabreTools.DatFiles.Test
#endregion #endregion
#region GetDuplicateStatus
[Fact]
public void GetDuplicateStatus_NullOther_NoDupe()
{
var dict = new ItemDictionary();
DatItem item = new Rom();
DatItem? lastItem = null;
var actual = dict.GetDuplicateStatus(item, lastItem);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentTypes_NoDupe()
{
var dict = new ItemDictionary();
var rom = new Rom();
DatItem? lastItem = new Disk();
var actual = dict.GetDuplicateStatus(rom, lastItem);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_MismatchedHashes_NoDupe()
{
var dict = new ItemDictionary();
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "BEEFDEAD");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(1));
romB.CopyMachineInformation(machineB);
var actual = dict.GetDuplicateStatus(romA, romB);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentSource_NameMatch_ExternalAll()
{
var dict = new ItemDictionary();
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(1));
romB.CopyMachineInformation(machineB);
var actual = dict.GetDuplicateStatus(romA, romB);
Assert.Equal(DupeType.External | DupeType.All, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentSource_NoNameMatch_ExternalHash()
{
var dict = new ItemDictionary();
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("not-name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(1));
romB.CopyMachineInformation(machineB);
var actual = dict.GetDuplicateStatus(romA, romB);
Assert.Equal(DupeType.External | DupeType.Hash, actual);
}
[Fact]
public void GetDuplicateStatus_SameSource_NameMatch_InternalAll()
{
var dict = new ItemDictionary();
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romB.CopyMachineInformation(machineB);
var actual = dict.GetDuplicateStatus(romA, romB);
Assert.Equal(DupeType.Internal | DupeType.All, actual);
}
[Fact]
public void GetDuplicateStatus_SameSource_NoNameMatch_InternalHash()
{
var dict = new ItemDictionary();
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("not-name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romB.CopyMachineInformation(machineB);
var actual = dict.GetDuplicateStatus(romA, romB);
Assert.Equal(DupeType.Internal | DupeType.Hash, actual);
}
#endregion
#region GetDuplicates #region GetDuplicates
[Theory] [Theory]

View File

@@ -766,9 +766,9 @@ namespace SabreTools.DatFiles
// If the current item exactly matches the last item, then we don't add it // If the current item exactly matches the last item, then we don't add it
#if NET20 || NET35 #if NET20 || NET35
if ((datItem.GetDuplicateStatus(lastItem) & DupeType.All) != 0) if ((Items.GetDuplicateStatus(datItem, lastItem) & DupeType.All) != 0)
#else #else
if (datItem.GetDuplicateStatus(lastItem).HasFlag(DupeType.All)) if (Items.GetDuplicateStatus(datItem, lastItem).HasFlag(DupeType.All))
#endif #endif
{ {
_logger.Verbose($"Exact duplicate found for '{datItemName}'"); _logger.Verbose($"Exact duplicate found for '{datItemName}'");
@@ -838,7 +838,7 @@ namespace SabreTools.DatFiles
SortDB(ref mappings, true); SortDB(ref mappings, true);
// Now we want to loop through and check names // Now we want to loop through and check names
DatItem? lastItem = null; KeyValuePair<long, DatItem>? lastItem = null;
string? lastrenamed = null; string? lastrenamed = null;
int lastid = 0; int lastid = 0;
foreach (var datItem in mappings) foreach (var datItem in mappings)
@@ -847,13 +847,13 @@ namespace SabreTools.DatFiles
if (lastItem == null) if (lastItem == null)
{ {
output.Add(datItem); output.Add(datItem);
lastItem = datItem.Value; lastItem = datItem;
continue; continue;
} }
// Get the last item name, if applicable // Get the last item name, if applicable
string lastItemName = lastItem.GetName() string lastItemName = lastItem.Value.Value.GetName()
?? lastItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue() ?? lastItem.Value.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
?? string.Empty; ?? string.Empty;
// Get the current item name, if applicable // Get the current item name, if applicable
@@ -861,11 +861,15 @@ namespace SabreTools.DatFiles
?? datItem.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue() ?? datItem.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
?? string.Empty; ?? string.Empty;
// Get sources for both items
var datItemSource = ItemsDB.GetSourceForItem(datItem.Key);
var lastItemSource = ItemsDB.GetSourceForItem(lastItem.Value.Key);
// If the current item exactly matches the last item, then we don't add it // If the current item exactly matches the last item, then we don't add it
#if NET20 || NET35 #if NET20 || NET35
if ((datItem.Value.GetDuplicateStatus(lastItem) & DupeType.All) != 0) if ((ItemsDB.GetDuplicateStatus(datItem, datItemSource.Value, lastItem, lastItemSource.Value) & DupeType.All) != 0)
#else #else
if (datItem.Value.GetDuplicateStatus(lastItem).HasFlag(DupeType.All)) if (ItemsDB.GetDuplicateStatus(datItem, datItemSource.Value, lastItem, lastItemSource.Value).HasFlag(DupeType.All))
#endif #endif
{ {
_logger.Verbose($"Exact duplicate found for '{datItemName}'"); _logger.Verbose($"Exact duplicate found for '{datItemName}'");
@@ -904,7 +908,7 @@ namespace SabreTools.DatFiles
else else
{ {
output.Add(datItem); output.Add(datItem);
lastItem = datItem.Value; lastItem = datItem;
lastrenamed = null; lastrenamed = null;
lastid = 0; lastid = 0;
} }

View File

@@ -447,6 +447,61 @@ namespace SabreTools.DatFiles
#endif #endif
} }
/// <summary>
/// Return the duplicate status of two items
/// </summary>
/// <param name="self">Current DatItem</param>
/// <param name="last">DatItem to check against</param>
/// <returns>The DupeType corresponding to the relationship between the two</returns>
public DupeType GetDuplicateStatus(DatItem? self, DatItem? last)
{
DupeType output = 0x00;
// If either item is null
if (self == null || last == null)
return output;
// If we don't have a duplicate at all, return none
if (!self.Equals(last))
return output;
// Get the sources for comparison
var selfSource = self.GetFieldValue<Source?>(DatItem.SourceKey);
var lastSource = last.GetFieldValue<Source?>(DatItem.SourceKey);
// Get the machines for comparison
var selfMachine = self.GetMachine();
string? selfMachineName = selfMachine?.GetName();
var lastMachine = last.GetMachine();
string? lastMachineName = lastMachine?.GetName();
// If the duplicate is external already
#if NET20 || NET35
if ((last.GetFieldValue<DupeType>(DatItem.DupeTypeKey) & DupeType.External) != 0)
#else
if (last.GetFieldValue<DupeType>(DatItem.DupeTypeKey).HasFlag(DupeType.External))
#endif
output |= DupeType.External;
// If the duplicate should be external
else if (lastSource?.Index != selfSource?.Index)
output |= DupeType.External;
// Otherwise, it's considered an internal dupe
else
output |= DupeType.Internal;
// If the item and machine names match
if (lastMachineName == selfMachineName && last.GetName() == self.GetName())
output |= DupeType.All;
// Otherwise, hash match is assumed
else
output |= DupeType.Hash;
return output;
}
/// <summary> /// <summary>
/// Merge an arbitrary set of DatItems based on the supplied information /// Merge an arbitrary set of DatItems based on the supplied information
/// </summary> /// </summary>
@@ -459,6 +514,9 @@ namespace SabreTools.DatFiles
if (items == null || items.Count == 0) if (items == null || items.Count == 0)
return []; return [];
// Create placeholder object for checking duplicates
var dupDict = new ItemDictionary();
// Create output list // Create output list
List<DatItem> output = []; List<DatItem> output = [];
@@ -492,7 +550,7 @@ namespace SabreTools.DatFiles
} }
// Find the index of the first duplicate, if one exists // Find the index of the first duplicate, if one exists
int pos = output.FindIndex(lastItem => datItem.GetDuplicateStatus(lastItem) != 0x00); int pos = output.FindIndex(lastItem => dupDict.GetDuplicateStatus(datItem, lastItem) != 0x00);
if (pos < 0) if (pos < 0)
{ {
output.Add(datItem); output.Add(datItem);
@@ -501,7 +559,7 @@ namespace SabreTools.DatFiles
// Get the duplicate item // Get the duplicate item
DatItem savedItem = output[pos]; DatItem savedItem = output[pos];
DupeType dupetype = datItem.GetDuplicateStatus(savedItem); DupeType dupetype = dupDict.GetDuplicateStatus(datItem, savedItem);
// Disks, File, Media, and Roms have more information to fill // Disks, File, Media, and Roms have more information to fill
if (datItem is Disk diskItem && savedItem is Disk savedDisk) if (datItem is Disk diskItem && savedItem is Disk savedDisk)

View File

@@ -719,6 +719,59 @@ namespace SabreTools.DatFiles
#endif #endif
} }
/// <summary>
/// Return the duplicate status of two items
/// </summary>
/// <param name="selfItem">Current DatItem</param>
/// <param name="selfSource">Source associated with this item</param>
/// <param name="lastItem">DatItem to check against</param>
/// <param name="lastSource">Source associated with the last item</param>
/// <returns>The DupeType corresponding to the relationship between the two</returns>
public DupeType GetDuplicateStatus(KeyValuePair<long, DatItem>? selfItem, Source? selfSource, KeyValuePair<long, DatItem>? lastItem, Source? lastSource)
{
DupeType output = 0x00;
// If either item is null
if (selfItem == null || lastItem == null)
return output;
// If we don't have a duplicate at all, return none
if (!selfItem.Value.Value.Equals(lastItem.Value.Value))
return output;
// Get the machines for comparison
var selfMachine = GetMachineForItem(selfItem.Value.Key).Value;
string? selfMachineName = selfMachine?.GetName();
var lastMachine = GetMachineForItem(lastItem.Value.Key).Value;
string? lastMachineName = lastMachine?.GetName();
// If the duplicate is external already
#if NET20 || NET35
if ((lastItem.Value.Value.GetFieldValue<DupeType>(DatItem.DupeTypeKey) & DupeType.External) != 0)
#else
if (lastItem.Value.Value.GetFieldValue<DupeType>(DatItem.DupeTypeKey).HasFlag(DupeType.External))
#endif
output |= DupeType.External;
// If the duplicate should be external
else if (lastSource?.Index != selfSource?.Index)
output |= DupeType.External;
// Otherwise, it's considered an internal dupe
else
output |= DupeType.Internal;
// If the item and machine names match
if (lastMachineName == selfMachineName && lastItem.Value.Value.GetName() == selfItem.Value.Value.GetName())
output |= DupeType.All;
// Otherwise, hash match is assumed
else
output |= DupeType.Hash;
return output;
}
/// <summary> /// <summary>
/// List all duplicates found in a DAT based on a DatItem /// List all duplicates found in a DAT based on a DatItem
/// </summary> /// </summary>
@@ -837,7 +890,12 @@ namespace SabreTools.DatFiles
} }
// Find the index of the first duplicate, if one exists // Find the index of the first duplicate, if one exists
int pos = output.FindIndex(lastItem => datItem.GetDuplicateStatus(lastItem.Value) != 0x00); var datItemSource = GetSourceForItem(itemIndex);
int pos = output.FindIndex(lastItem =>
{
var lastItemSource = GetSourceForItem(lastItem.Key);
return GetDuplicateStatus(kvp, datItemSource.Value, lastItem, lastItemSource.Value) != 0x00;
});
if (pos < 0) if (pos < 0)
{ {
output.Add(new KeyValuePair<long, DatItem>(itemIndex, datItem)); output.Add(new KeyValuePair<long, DatItem>(itemIndex, datItem));
@@ -847,7 +905,8 @@ namespace SabreTools.DatFiles
// Get the duplicate item // Get the duplicate item
long savedIndex = output[pos].Key; long savedIndex = output[pos].Key;
DatItem savedItem = output[pos].Value; DatItem savedItem = output[pos].Value;
DupeType dupetype = datItem.GetDuplicateStatus(savedItem); var savedItemSource = GetSourceForItem(savedIndex);
DupeType dupetype = GetDuplicateStatus(kvp, datItemSource.Value, output[pos], savedItemSource.Value);
// Disks, Media, and Roms have more information to fill // Disks, Media, and Roms have more information to fill
if (datItem is Disk diskItem && savedItem is Disk savedDisk) if (datItem is Disk diskItem && savedItem is Disk savedDisk)

View File

@@ -280,312 +280,6 @@ namespace SabreTools.DatItems.Test
#endregion #endregion
#region GetDuplicateStatus
[Fact]
public void GetDuplicateStatus_NullOther_NoDupe()
{
DatItem item = new Rom();
DatItem? lastItem = null;
var actual = item.GetDuplicateStatus(lastItem);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentTypes_NoDupe()
{
var rom = new Rom();
DatItem? lastItem = new Disk();
var actual = rom.GetDuplicateStatus(lastItem);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_MismatchedHashes_NoDupe()
{
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "BEEFDEAD");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(1));
romB.CopyMachineInformation(machineB);
var actual = romA.GetDuplicateStatus(romB);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentSource_NameMatch_ExternalAll()
{
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(1));
romB.CopyMachineInformation(machineB);
var actual = romA.GetDuplicateStatus(romB);
Assert.Equal(DupeType.External | DupeType.All, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentSource_NoNameMatch_ExternalHash()
{
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("not-name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(1));
romB.CopyMachineInformation(machineB);
var actual = romA.GetDuplicateStatus(romB);
Assert.Equal(DupeType.External | DupeType.Hash, actual);
}
[Fact]
public void GetDuplicateStatus_SameSource_NameMatch_InternalAll()
{
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romB.CopyMachineInformation(machineB);
var actual = romA.GetDuplicateStatus(romB);
Assert.Equal(DupeType.Internal | DupeType.All, actual);
}
[Fact]
public void GetDuplicateStatus_SameSource_NoNameMatch_InternalHash()
{
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("not-name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romB.CopyMachineInformation(machineB);
var actual = romA.GetDuplicateStatus(romB);
Assert.Equal(DupeType.Internal | DupeType.Hash, actual);
}
#endregion
// TODO: Change when Machine retrieval gets fixed
#region GetDuplicateStatusDB
[Fact]
public void GetDuplicateStatusDB_NullOther_NoDupe()
{
Source? selfSource = null;
Source? lastSource = null;
DatItem item = new Rom();
DatItem? lastItem = null;
var actual = item.GetDuplicateStatusDB(selfSource, lastItem, lastSource);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatusDB_DifferentTypes_NoDupe()
{
Source? selfSource = null;
Source? lastSource = null;
var rom = new Rom();
DatItem? lastItem = new Disk();
var actual = rom.GetDuplicateStatusDB(selfSource, lastItem, lastSource);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatusDB_MismatchedHashes_NoDupe()
{
Source? sourceA = new Source(0);
Source? sourceB = new Source(1);
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "BEEFDEAD");
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.CopyMachineInformation(machineB);
var actual = romA.GetDuplicateStatusDB(sourceA, romB, sourceB);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatusDB_DifferentSource_NameMatch_ExternalAll()
{
Source? sourceA = new Source(0);
Source? sourceB = new Source(1);
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.CopyMachineInformation(machineB);
var actual = romA.GetDuplicateStatusDB(sourceA, romB, sourceB);
Assert.Equal(DupeType.External | DupeType.All, actual);
}
[Fact]
public void GetDuplicateStatusDB_DifferentSource_NoNameMatch_ExternalHash()
{
Source? sourceA = new Source(0);
Source? sourceB = new Source(1);
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("not-name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.CopyMachineInformation(machineB);
var actual = romA.GetDuplicateStatusDB(sourceA, romB, sourceB);
Assert.Equal(DupeType.External | DupeType.Hash, actual);
}
[Fact]
public void GetDuplicateStatusDB_SameSource_NameMatch_InternalAll()
{
Source? sourceA = new Source(0);
Source? sourceB = new Source(0);
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.CopyMachineInformation(machineB);
var actual = romA.GetDuplicateStatusDB(sourceA, romB, sourceB);
Assert.Equal(DupeType.Internal | DupeType.All, actual);
}
[Fact]
public void GetDuplicateStatusDB_SameSource_NoNameMatch_InternalHash()
{
Source? sourceA = new Source(0);
Source? sourceB = new Source(0);
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("not-name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.CopyMachineInformation(machineB);
var actual = romA.GetDuplicateStatusDB(sourceA, romB, sourceB);
Assert.Equal(DupeType.Internal | DupeType.Hash, actual);
}
#endregion
// TODO: Change when Machine retrieval gets fixed // TODO: Change when Machine retrieval gets fixed
#region GetKey #region GetKey

View File

@@ -234,105 +234,6 @@ namespace SabreTools.DatItems
return _internal.EqualTo(other._internal); return _internal.EqualTo(other._internal);
} }
/// <summary>
/// Return the duplicate status of two items
/// </summary>
/// <param name="lastItem">DatItem to check against</param>
/// <returns>The DupeType corresponding to the relationship between the two</returns>
public DupeType GetDuplicateStatus(DatItem? lastItem)
{
DupeType output = 0x00;
// If we don't have a duplicate at all, return none
if (!Equals(lastItem))
return output;
// Get the sources for comparison
var selfSource = GetFieldValue<Source?>(SourceKey);
var lastSource = lastItem.GetFieldValue<Source?>(SourceKey);
// Get the machines for comparison
var selfMachine = GetMachine();
string? selfMachineName = selfMachine?.GetName();
var lastMachine = lastItem.GetMachine();
string? lastMachineName = lastMachine?.GetName();
// If the duplicate is external already
#if NET20 || NET35
if ((lastItem.GetFieldValue<DupeType>(DupeTypeKey) & DupeType.External) != 0)
#else
if (lastItem.GetFieldValue<DupeType>(DupeTypeKey).HasFlag(DupeType.External))
#endif
output |= DupeType.External;
// If the duplicate should be external
else if (lastSource?.Index != selfSource?.Index)
output |= DupeType.External;
// Otherwise, it's considered an internal dupe
else
output |= DupeType.Internal;
// If the item and machine names match
if (lastMachineName == selfMachineName && lastItem.GetName() == GetName())
output |= DupeType.All;
// Otherwise, hash match is assumed
else
output |= DupeType.Hash;
return output;
}
/// <summary>
/// Return the duplicate status of two items
/// </summary>
/// <param name="selfSource">Source associated with this item</param>
/// <param name="lastItem">DatItem to check against</param>
/// <param name="lastSource">Source associated with the last item</param>
/// <returns>The DupeType corresponding to the relationship between the two</returns>
public DupeType GetDuplicateStatusDB(Source? selfSource, DatItem? lastItem, Source? lastSource)
{
DupeType output = 0x00;
// If we don't have a duplicate at all, return none
if (!Equals(lastItem))
return output;
// TODO: Fix this since machines are determined in a different way
// Get the machines for comparison
var selfMachine = GetMachine();
string? selfMachineName = selfMachine?.GetName();
var lastMachine = lastItem.GetMachine();
string? lastMachineName = lastMachine?.GetName();
// If the duplicate is external already
#if NET20 || NET35
if ((lastItem.GetFieldValue<DupeType>(DupeTypeKey) & DupeType.External) != 0)
#else
if (lastItem.GetFieldValue<DupeType>(DupeTypeKey).HasFlag(DupeType.External))
#endif
output |= DupeType.External;
// If the duplicate should be external
else if (lastSource?.Index != selfSource?.Index)
output |= DupeType.External;
// Otherwise, it's considered an internal dupe
else
output |= DupeType.Internal;
// If the item and machine names match
if (lastMachineName == selfMachineName && lastItem.GetName() == GetName())
output |= DupeType.All;
// Otherwise, hash match is assumed
else
output |= DupeType.Hash;
return output;
}
#endregion #endregion
#region Manipulation #region Manipulation