mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Better equality code
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System.Linq;
|
||||
|
||||
using SabreTools.Library.Data;
|
||||
using SabreTools.Library.FileTypes;
|
||||
using SabreTools.Library.Tools;
|
||||
using Newtonsoft.Json;
|
||||
@@ -282,50 +281,23 @@ namespace SabreTools.Library.DatItems
|
||||
{
|
||||
bool dupefound = false;
|
||||
|
||||
// If we don't have a disk, return false
|
||||
if (this.ItemType != other.ItemType)
|
||||
// If we don't have a rom, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return dupefound;
|
||||
|
||||
// Otherwise, treat it as a Disk
|
||||
Disk newOther = other as Disk;
|
||||
|
||||
// If all hashes are empty but they're both nodump and the names match, then they're dupes
|
||||
if ((this.ItemStatus == ItemStatus.Nodump && newOther.ItemStatus == ItemStatus.Nodump)
|
||||
&& (this.Name == newOther.Name)
|
||||
&& (this._md5.IsNullOrEmpty() && newOther._md5.IsNullOrEmpty())
|
||||
#if NET_FRAMEWORK
|
||||
&& (this._ripemd160.IsNullOrEmpty() && newOther._ripemd160.IsNullOrEmpty())
|
||||
#endif
|
||||
&& (this._sha1.IsNullOrEmpty() && newOther._sha1.IsNullOrEmpty())
|
||||
&& (this._sha256.IsNullOrEmpty() && newOther._sha256.IsNullOrEmpty())
|
||||
&& (this._sha384.IsNullOrEmpty() && newOther._sha384.IsNullOrEmpty())
|
||||
&& (this._sha512.IsNullOrEmpty() && newOther._sha512.IsNullOrEmpty()))
|
||||
if ((ItemStatus == ItemStatus.Nodump && newOther.ItemStatus == ItemStatus.Nodump)
|
||||
&& Name == newOther.Name
|
||||
&& !HasHashes() && !newOther.HasHashes())
|
||||
{
|
||||
dupefound = true;
|
||||
}
|
||||
|
||||
// If we can determine that the disks have no non-empty hashes in common, we return false
|
||||
else if ((this._md5.IsNullOrEmpty() || newOther._md5.IsNullOrEmpty())
|
||||
#if NET_FRAMEWORK
|
||||
&& (this._ripemd160.IsNullOrEmpty() || newOther._ripemd160.IsNullOrEmpty())
|
||||
#endif
|
||||
&& (this._sha1.IsNullOrEmpty() || newOther._sha1.IsNullOrEmpty())
|
||||
&& (this._sha256.IsNullOrEmpty() || newOther._sha256.IsNullOrEmpty())
|
||||
&& (this._sha384.IsNullOrEmpty() || newOther._sha384.IsNullOrEmpty())
|
||||
&& (this._sha512.IsNullOrEmpty() || newOther._sha512.IsNullOrEmpty()))
|
||||
{
|
||||
dupefound = false;
|
||||
}
|
||||
|
||||
// Otherwise if we get a partial match
|
||||
else if (((this._md5.IsNullOrEmpty() || newOther._md5.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._md5, newOther._md5))
|
||||
#if NET_FRAMEWORK
|
||||
&& ((this._ripemd160.IsNullOrEmpty() || newOther._ripemd160.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._ripemd160, newOther._ripemd160))
|
||||
#endif
|
||||
&& ((this._sha1.IsNullOrEmpty() || newOther._sha1.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._sha1, newOther._sha1))
|
||||
&& ((this._sha256.IsNullOrEmpty() || newOther._sha256.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._sha256, newOther._sha256))
|
||||
&& ((this._sha384.IsNullOrEmpty() || newOther._sha384.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._sha384, newOther._sha384))
|
||||
&& ((this._sha512.IsNullOrEmpty() || newOther._sha512.IsNullOrEmpty()) || Enumerable.SequenceEqual(this._sha512, newOther._sha512)))
|
||||
else if (HashMatch(newOther))
|
||||
{
|
||||
dupefound = true;
|
||||
}
|
||||
@@ -333,6 +305,65 @@ namespace SabreTools.Library.DatItems
|
||||
return dupefound;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if there are no, non-empty hashes in common with another Disk
|
||||
/// </summary>
|
||||
/// <param name="other">Disk to compare against</param>
|
||||
/// <returns>True if at least one hash is not mutually exclusive, false otherwise</returns>
|
||||
private bool HasCommonHash(Disk other)
|
||||
{
|
||||
return !(_md5.IsNullOrEmpty() ^ other._md5.IsNullOrEmpty())
|
||||
#if NET_FRAMEWORK
|
||||
|| !(_ripemd160.IsNullOrEmpty() || other._ripemd160.IsNullOrEmpty())
|
||||
#endif
|
||||
|| !(_sha1.IsNullOrEmpty() ^ other._sha1.IsNullOrEmpty())
|
||||
|| !(_sha256.IsNullOrEmpty() ^ other._sha256.IsNullOrEmpty())
|
||||
|| !(_sha384.IsNullOrEmpty() ^ other._sha384.IsNullOrEmpty())
|
||||
|| !(_sha512.IsNullOrEmpty() ^ other._sha512.IsNullOrEmpty());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if the Disk contains any hashes
|
||||
/// </summary>
|
||||
/// <returns>True if any hash exists, false otherwise</returns>
|
||||
private bool HasHashes()
|
||||
{
|
||||
return !_md5.IsNullOrEmpty()
|
||||
#if NET_FRAMEWORK
|
||||
|| !_ripemd160.IsNullOrEmpty()
|
||||
#endif
|
||||
|| !_sha1.IsNullOrEmpty()
|
||||
|| !_sha256.IsNullOrEmpty()
|
||||
|| !_sha384.IsNullOrEmpty()
|
||||
|| !_sha512.IsNullOrEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if any hashes are common with another Disk
|
||||
/// </summary>
|
||||
/// <param name="other">Disk to compare against</param>
|
||||
/// <returns>True if any hashes are in common, false otherwise</returns>
|
||||
private bool HashMatch(Disk other)
|
||||
{
|
||||
// If either have no hashes, we return false, otherwise this would be a false positive
|
||||
if (!HasHashes() || !other.HasHashes())
|
||||
return false;
|
||||
|
||||
// If neither have hashes in common, we return false, otherwise this would be a false positive
|
||||
if (!HasCommonHash(other))
|
||||
return false;
|
||||
|
||||
// Return if all hashes match according to merge rules
|
||||
return ConditionalHashEquals(_md5, other._md5)
|
||||
#if NET_FRAMEWORK
|
||||
&& ConditionalHashEquals(_ripemd160, other._ripemd160)
|
||||
#endif
|
||||
&& ConditionalHashEquals(_sha1, other._sha1)
|
||||
&& ConditionalHashEquals(_sha256, other._sha256)
|
||||
&& ConditionalHashEquals(_sha384, other._sha384)
|
||||
&& ConditionalHashEquals(_sha512, other._sha512);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user