Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VersionCheck.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. namespace UnityEngine.Purchasing
  3. {
  4. /// <summary>
  5. /// Utility class for comparing Unity versions. This class only compares the major, minor, and patch versions and
  6. /// ignores the suffixes (e.g. f2, p3, b1)
  7. /// </summary>
  8. internal static class VersionCheck
  9. {
  10. /// <summary>
  11. /// Represents a three-part version number as three ints
  12. /// </summary>
  13. internal struct Version
  14. {
  15. public int major;
  16. public int minor;
  17. public int patch;
  18. }
  19. /// <summary>
  20. /// Returns true if versionA is greater than or equal to versionB
  21. /// </summary>
  22. public static bool GreaterThanOrEqual(string versionA, string versionB)
  23. {
  24. return !LessThan(versionA, versionB);
  25. }
  26. /// <summary>
  27. /// Returns true if versionA is greater than versionB
  28. /// </summary>
  29. public static bool GreaterThan(string versionA, string versionB)
  30. {
  31. return !LessThanOrEqual(versionA, versionB);
  32. }
  33. /// <summary>
  34. /// Returns true if versionA is less than versionB
  35. /// </summary>
  36. public static bool LessThan(string versionA, string versionB)
  37. {
  38. var va = Parse(versionA);
  39. var vb = Parse(versionB);
  40. if (va.major > vb.major)
  41. {
  42. return false;
  43. }
  44. else if (va.major < vb.major)
  45. {
  46. return true;
  47. }
  48. else if (va.minor > vb.minor)
  49. {
  50. return false;
  51. }
  52. else if (va.minor < vb.minor)
  53. {
  54. return true;
  55. }
  56. else if (va.patch > vb.patch)
  57. {
  58. return false;
  59. }
  60. else if (va.patch < vb.patch)
  61. {
  62. return true;
  63. }
  64. else
  65. {
  66. return false;
  67. }
  68. }
  69. /// <summary>
  70. /// Returns true if versionA is less than or equal to versionB
  71. /// </summary>
  72. public static bool LessThanOrEqual(string versionA, string versionB)
  73. {
  74. return LessThan(versionA, versionB) || !LessThan(versionB, versionA);
  75. }
  76. /// <summary>
  77. /// Returns true if versionA is equal to versionB
  78. /// </summary>
  79. public static bool Equal(string versionA, string versionB)
  80. {
  81. return !LessThan(versionA, versionB) && !LessThan(versionB, versionA);
  82. }
  83. /// <summary>
  84. /// Parse the major version from a version string as an int. If the version is "3.2.1", this function returns 3.
  85. /// </summary>
  86. /// <returns>The major version, or 0 if it cannot be parsed</returns>
  87. public static int MajorVersion(string version)
  88. {
  89. return PartialVersion(version, 0);
  90. }
  91. /// <summary>
  92. /// Parse the minor version from a version string as an int. If the version is "3.2.1", this function returns 2.
  93. /// </summary>
  94. /// <returns>The minor version, or 0 if it cannot be parsed</returns>
  95. public static int MinorVersion(string version)
  96. {
  97. return PartialVersion(version, 1);
  98. }
  99. /// <summary>
  100. /// Parse the patch version from a version string as an int. If the version is "3.2.1", this function returns 1.
  101. /// </summary>
  102. /// <returns>The patch version, or 0 if it cannot be parsed</returns>
  103. public static int PatchVersion(string version)
  104. {
  105. return PartialVersion(version, 2);
  106. }
  107. public static Version Parse(string version)
  108. {
  109. var v = new Version
  110. {
  111. major = MajorVersion(version),
  112. minor = MinorVersion(version),
  113. patch = PatchVersion(version)
  114. };
  115. return v;
  116. }
  117. /// <summary>
  118. /// Retrieve a part of a version number by index
  119. /// </summary>
  120. /// <returns>The parsed version, or 0 if the number can't be parsed.</returns>
  121. private static int PartialVersion(string version, int index)
  122. {
  123. // remove suffix
  124. var parts = version.Split(new char[] { 'a', 'b', 'f', 'p' });
  125. var prefix = parts[0];
  126. var result = 0;
  127. var versions = prefix.Split('.');
  128. if (versions.Length < index + 1)
  129. {
  130. return result;
  131. }
  132. int.TryParse(versions[index], out result);
  133. return result;
  134. }
  135. }
  136. }