VersionCode.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [Serializable]
  5. public struct VersionCode : IEquatable<VersionCode>
  6. {
  7. public const uint c_Major_Multi = 1000000;
  8. public const uint c_Minor_Multi = 10000;
  9. public const uint c_Release_Multi = 100;
  10. public static readonly VersionCode zeroVersionCode = new VersionCode(0, 0, 0, 0);
  11. public uint major;
  12. public uint minor;
  13. public uint release;
  14. public uint patch;
  15. public VersionCode(uint major, uint minor, uint release, uint patch)
  16. {
  17. this.major = major;
  18. this.minor = minor;
  19. this.release = release;
  20. this.patch = patch;
  21. }
  22. public VersionCode(uint major, uint minor, uint release)
  23. {
  24. this.major = major;
  25. this.minor = minor;
  26. this.release = release;
  27. this.patch = 0;
  28. }
  29. public override int GetHashCode()
  30. {
  31. return major.GetHashCode() ^ (minor.GetHashCode() << 2) ^ (release.GetHashCode() >> 2) ^ (patch.GetHashCode() >> 1);
  32. }
  33. public override bool Equals(object obj)
  34. {
  35. if (obj is int)
  36. {
  37. return Equals((VersionCode)obj);
  38. }
  39. else if (obj is string)
  40. {
  41. return Equals((VersionCode)obj);
  42. }
  43. return base.Equals(obj);
  44. }
  45. public bool Equals(VersionCode other)
  46. {
  47. return major.Equals(other.major) && minor.Equals(other.minor) && release.Equals(other.release);
  48. }
  49. public string ToString(bool shortFormat)
  50. {
  51. if (shortFormat)
  52. {
  53. return major + "." + minor + "." + release;
  54. }
  55. return ToString();
  56. }
  57. public override string ToString()
  58. {
  59. return major + "." + minor + "." + release + "." + patch;
  60. }
  61. public static implicit operator uint(VersionCode code)
  62. {
  63. return code.major * c_Major_Multi + code.minor * c_Minor_Multi + code.release * c_Release_Multi + code.patch;
  64. }
  65. public static implicit operator VersionCode(uint code)
  66. {
  67. uint major = code / c_Major_Multi;
  68. code = code - major * c_Major_Multi;
  69. uint minor = code / c_Minor_Multi;
  70. code = code - minor * c_Minor_Multi;
  71. uint release = code / c_Release_Multi;
  72. code = code - release * c_Release_Multi;
  73. uint patch = code;
  74. return new VersionCode(major, minor, release, patch);
  75. }
  76. public static implicit operator string(VersionCode code)
  77. {
  78. return code.ToString();
  79. }
  80. public static implicit operator VersionCode(string codeStr)
  81. {
  82. uint major = 0;
  83. uint minor = 0;
  84. uint release = 0;
  85. uint patch = 0;
  86. if (!string.IsNullOrEmpty(codeStr))
  87. {
  88. try
  89. {
  90. string[] arr = codeStr.Split('.');
  91. int len = arr.Length;
  92. if (len >= 1)
  93. {
  94. major = (uint.Parse(arr[0]));
  95. }
  96. if (len >= 2)
  97. {
  98. minor = (uint.Parse(arr[1]));
  99. }
  100. if (len >= 3)
  101. {
  102. release = (uint.Parse(arr[2]));
  103. }
  104. if (len >= 4)
  105. {
  106. patch = (uint.Parse(arr[3]));
  107. }
  108. }
  109. catch (Exception e)
  110. {
  111. Debug.LogException(e);
  112. return VersionCode.zeroVersionCode;
  113. }
  114. }
  115. return new VersionCode(major, minor, release, patch);
  116. }
  117. public static bool operator ==(VersionCode lhs, VersionCode rhs)
  118. {
  119. return (lhs.major == rhs.major)
  120. && (lhs.minor == rhs.minor)
  121. && (lhs.release == rhs.release)
  122. && (lhs.patch == rhs.patch);
  123. }
  124. public static bool operator !=(VersionCode lhs, VersionCode rhs)
  125. {
  126. return !(lhs == rhs);
  127. }
  128. public static bool operator <(VersionCode lhs, VersionCode rhs)
  129. {
  130. if (lhs.major != rhs.major) return lhs.major < rhs.major;
  131. if (lhs.minor != rhs.minor) return lhs.minor < rhs.minor;
  132. if (lhs.release != rhs.release) return lhs.release < rhs.release;
  133. return lhs.patch < rhs.patch;
  134. }
  135. public static bool operator <=(VersionCode lhs, VersionCode rhs)
  136. {
  137. return !(lhs > rhs);
  138. }
  139. public static bool operator >(VersionCode lhs, VersionCode rhs)
  140. {
  141. if (lhs.major != rhs.major) return lhs.major > rhs.major;
  142. if (lhs.minor != rhs.minor) return lhs.minor > rhs.minor;
  143. if (lhs.release != rhs.release) return lhs.release > rhs.release;
  144. return lhs.patch > rhs.patch;
  145. }
  146. public static bool operator >=(VersionCode lhs, VersionCode rhs)
  147. {
  148. return !(lhs < rhs);
  149. }
  150. public static VersionCode operator +(VersionCode a, VersionCode b)
  151. {
  152. return new VersionCode(a.major + b.major, a.minor + b.minor, a.release + b.release, a.patch + b.patch);
  153. }
  154. public static VersionCode operator -(VersionCode a, VersionCode b)
  155. {
  156. return new VersionCode(a.major - b.major, a.minor - b.minor, a.release - b.release, a.patch - b.patch);
  157. }
  158. }