From 8cd2d4ff4af4ec217f4f44be1f2ab43824c99790 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Mon, 20 Jul 2020 17:06:25 +0200 Subject: [PATCH] Unity: Add version range CompareTo, Intersect and handle no version upper bound --- .../Cpp/UnityHeaders/UnityVersion.cs | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/Il2CppInspector.Common/Cpp/UnityHeaders/UnityVersion.cs b/Il2CppInspector.Common/Cpp/UnityHeaders/UnityVersion.cs index 42ab697..932d5b5 100644 --- a/Il2CppInspector.Common/Cpp/UnityHeaders/UnityVersion.cs +++ b/Il2CppInspector.Common/Cpp/UnityHeaders/UnityVersion.cs @@ -1,5 +1,5 @@ /* - Copyright 2017-2020 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com + Copyright 2017-2020 Katy Coe - http://www.djkaty.com - https://github.com/djkaty Copyright 2020 Robert Xiao - https://robertxiao.ca All rights reserved. @@ -77,6 +77,9 @@ namespace Il2CppInspector.Cpp.UnityHeaders // Compare two version numbers, intransitively (due to the Unspecified build type) public int CompareTo(UnityVersion other) { + // null means maximum possible version + if (other == null) + return -1; int res; if (0 != (res = Major.CompareTo(other.Major))) return res; @@ -114,9 +117,10 @@ namespace Il2CppInspector.Cpp.UnityHeaders } // A range of Unity versions - public class UnityVersionRange + public class UnityVersionRange : IComparable { // Minimum and maximum Unity version numbers for this range. Both endpoints are inclusive + // Max can be null to specify no upper bound public UnityVersion Min { get; } public UnityVersion Max { get; } @@ -157,6 +161,22 @@ namespace Il2CppInspector.Cpp.UnityHeaders return new UnityVersionRange(Min, Max); } + // Compare and sort based on the lowest version number + public int CompareTo(UnityVersionRange other) => Min.CompareTo(other.Min); + + // Intersect two ranges to find the smallest shared set of versions + // Returns null if the two ranges do not intersect + // Max == null means no upper bound on version + public UnityVersionRange Intersect(UnityVersionRange other) { + var highestLow = Min.CompareTo(other.Min) > 0 ? Min : other.Min; + var lowestHigh = Max == null? other.Max : Max.CompareTo(other.Max) < 0 ? Max : other.Max; + + if (highestLow.CompareTo(lowestHigh) > 0) + return null; + + return new UnityVersionRange(highestLow, lowestHigh); + } + public override string ToString() { var res = $"{Min}"; if (Max == null)