Unity: Add equality implementation for UnityVersionRange, UnityHeaders

This commit is contained in:
Katy Coe
2020-08-07 04:56:47 +02:00
parent 1b0255bb95
commit f9c236f143
2 changed files with 48 additions and 7 deletions

View File

@@ -16,7 +16,7 @@ namespace Il2CppInspector.Cpp.UnityHeaders
{
// Each instance of UnityHeaders represents all of the header files needed to build for a specific range of Unity versions
// Also provides helper functions to fetch various types of resources
public class UnityHeaders
public class UnityHeaders : IEquatable<UnityHeaders>
{
// Metadata version for which this group of headers are valid. Multiple headers may have the same metadata version
public double MetadataVersion { get; }
@@ -196,5 +196,22 @@ namespace Il2CppInspector.Cpp.UnityHeaders
// Get the metadata version from a type header resource name
private static double GetMetadataVersionFromFilename(string resourceName)
=> double.Parse(resourceName.Substring(typeof(UnityHeaders).Namespace.Length + 1).Split('-')[0], NumberFormatInfo.InvariantInfo);
// Equality comparisons
public static bool operator ==(UnityHeaders first, UnityHeaders second) {
if (ReferenceEquals(first, second))
return true;
if (ReferenceEquals(first, null) || ReferenceEquals(second, null))
return false;
return first.VersionRange.Equals(second.VersionRange);
}
public static bool operator !=(UnityHeaders first, UnityHeaders second) => !(first == second);
public override bool Equals(object obj) => Equals(obj as UnityHeaders);
public bool Equals(UnityHeaders other) => VersionRange == other?.VersionRange;
public override int GetHashCode() => VersionRange.GetHashCode();
}
}

View File

@@ -97,10 +97,19 @@ namespace Il2CppInspector.Cpp.UnityHeaders
return 0;
}
public override bool Equals(object obj) {
return Equals(obj as UnityVersion);
// Equality comparisons
public static bool operator ==(UnityVersion first, UnityVersion second) {
if (ReferenceEquals(first, second))
return true;
if (ReferenceEquals(first, null) || ReferenceEquals(second, null))
return false;
return first.Equals(second);
}
public static bool operator !=(UnityVersion first, UnityVersion second) => !(first == second);
public override bool Equals(object obj) => Equals(obj as UnityVersion);
public bool Equals(UnityVersion other) {
return other != null &&
Major == other.Major &&
@@ -110,13 +119,11 @@ namespace Il2CppInspector.Cpp.UnityHeaders
BuildNumber == other.BuildNumber;
}
public override int GetHashCode() {
return HashCode.Combine(Major, Minor, Update, BuildType, BuildNumber);
}
public override int GetHashCode() => HashCode.Combine(Major, Minor, Update, BuildType, BuildNumber);
}
// A range of Unity versions
public class UnityVersionRange : IComparable<UnityVersionRange>
public class UnityVersionRange : IComparable<UnityVersionRange>, IEquatable<UnityVersionRange>
{
// Minimum and maximum Unity version numbers for this range. Both endpoints are inclusive
// Max can be null to specify no upper bound
@@ -184,5 +191,22 @@ namespace Il2CppInspector.Cpp.UnityHeaders
res += $" - {Max}";
return res;
}
// Equality comparisons
public static bool operator ==(UnityVersionRange first, UnityVersionRange second) {
if (ReferenceEquals(first, second))
return true;
if (ReferenceEquals(first, null) || ReferenceEquals(second, null))
return false;
return first.Equals(second);
}
public static bool operator !=(UnityVersionRange first, UnityVersionRange second) => !(first == second);
public override bool Equals(object obj) => Equals(obj as UnityVersionRange);
public bool Equals(UnityVersionRange other) => Min.Equals(other?.Min) && Max.Equals(other?.Max);
public override int GetHashCode() => HashCode.Combine(Min, Max);
}
}