using System; namespace UnityEngine.Purchasing { /// /// Utility class for comparing Unity versions. This class only compares the major, minor, and patch versions and /// ignores the suffixes (e.g. f2, p3, b1) /// internal static class VersionCheck { /// /// Represents a three-part version number as three ints /// internal struct Version { public int major; public int minor; public int patch; } /// /// Returns true if versionA is greater than or equal to versionB /// public static bool GreaterThanOrEqual(string versionA, string versionB) { return !LessThan(versionA, versionB); } /// /// Returns true if versionA is greater than versionB /// public static bool GreaterThan(string versionA, string versionB) { return !LessThanOrEqual(versionA, versionB); } /// /// Returns true if versionA is less than versionB /// public static bool LessThan(string versionA, string versionB) { var va = Parse(versionA); var vb = Parse(versionB); if (va.major > vb.major) { return false; } else if (va.major < vb.major) { return true; } else if (va.minor > vb.minor) { return false; } else if (va.minor < vb.minor) { return true; } else if (va.patch > vb.patch) { return false; } else if (va.patch < vb.patch) { return true; } else { return false; } } /// /// Returns true if versionA is less than or equal to versionB /// public static bool LessThanOrEqual(string versionA, string versionB) { return LessThan(versionA, versionB) || !LessThan(versionB, versionA); } /// /// Returns true if versionA is equal to versionB /// public static bool Equal(string versionA, string versionB) { return !LessThan(versionA, versionB) && !LessThan(versionB, versionA); } /// /// Parse the major version from a version string as an int. If the version is "3.2.1", this function returns 3. /// /// The major version, or 0 if it cannot be parsed public static int MajorVersion(string version) { return PartialVersion(version, 0); } /// /// Parse the minor version from a version string as an int. If the version is "3.2.1", this function returns 2. /// /// The minor version, or 0 if it cannot be parsed public static int MinorVersion(string version) { return PartialVersion(version, 1); } /// /// Parse the patch version from a version string as an int. If the version is "3.2.1", this function returns 1. /// /// The patch version, or 0 if it cannot be parsed public static int PatchVersion(string version) { return PartialVersion(version, 2); } public static Version Parse(string version) { var v = new Version { major = MajorVersion(version), minor = MinorVersion(version), patch = PatchVersion(version) }; return v; } /// /// Retrieve a part of a version number by index /// /// The parsed version, or 0 if the number can't be parsed. private static int PartialVersion(string version, int index) { // remove suffix var parts = version.Split(new char[] { 'a', 'b', 'f', 'p' }); var prefix = parts[0]; var result = 0; var versions = prefix.Split('.'); if (versions.Length < index + 1) { return result; } int.TryParse(versions[index], out result); return result; } } }