The Swartz-Manning’s first exhibit will provide a detailed history of Aaron Swartz Day. https://www.aaronswartzday.org/vr
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.

pb_VersionInfo.cs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Text.RegularExpressions;
  2. namespace ProBuilder2.EditorCommon
  3. {
  4. public enum VersionType
  5. {
  6. Final = 3,
  7. Beta = 2,
  8. Patch = 1
  9. }
  10. [System.Serializable]
  11. public struct pb_VersionInfo : System.IEquatable<pb_VersionInfo>, System.IComparable<pb_VersionInfo>
  12. {
  13. public int major;
  14. public int minor;
  15. public int patch;
  16. public int build;
  17. public VersionType type;
  18. public string text;
  19. public bool valid;
  20. public override bool Equals(object o)
  21. {
  22. return o is pb_VersionInfo && this.Equals((pb_VersionInfo) o);
  23. }
  24. public override int GetHashCode()
  25. {
  26. int hash = 13;
  27. unchecked
  28. {
  29. if(valid)
  30. {
  31. hash = (hash * 7) + major.GetHashCode();
  32. hash = (hash * 7) + minor.GetHashCode();
  33. hash = (hash * 7) + patch.GetHashCode();
  34. hash = (hash * 7) + build.GetHashCode();
  35. hash = (hash * 7) + type.GetHashCode();
  36. }
  37. else
  38. {
  39. return text.GetHashCode();
  40. }
  41. }
  42. return hash;
  43. }
  44. public bool Equals(pb_VersionInfo version)
  45. {
  46. if(valid != version.valid)
  47. return false;
  48. if(valid)
  49. {
  50. return major == version.major &&
  51. minor == version.minor &&
  52. patch == version.patch &&
  53. type == version.type &&
  54. build == version.build;
  55. }
  56. else
  57. {
  58. if( string.IsNullOrEmpty(text) || string.IsNullOrEmpty(version.text) )
  59. return false;
  60. return text.Equals(version.text);
  61. }
  62. }
  63. public int CompareTo(pb_VersionInfo version)
  64. {
  65. const int GREATER = 1;
  66. const int LESS = -1;
  67. if(this.Equals(version))
  68. return 0;
  69. else if(major > version.major)
  70. return GREATER;
  71. else if(major < version.major)
  72. return LESS;
  73. else if(minor > version.minor)
  74. return GREATER;
  75. else if(minor < version.minor)
  76. return LESS;
  77. else if(patch > version.patch)
  78. return GREATER;
  79. else if(patch < version.patch)
  80. return LESS;
  81. else if((int)type > (int)version.type)
  82. return GREATER;
  83. else if((int)type < (int)version.type)
  84. return LESS;
  85. else if(build > version.build)
  86. return GREATER;
  87. else
  88. return LESS;
  89. }
  90. public override string ToString()
  91. {
  92. return string.Format("{0}.{1}.{2}{3}{4}", major, minor, patch, type.ToString().ToLower()[0], build);
  93. }
  94. /**
  95. * Create a pb_VersionInfo type from a string.
  96. * Ex: "2.5.3b1"
  97. */
  98. public static pb_VersionInfo FromString(string str)
  99. {
  100. pb_VersionInfo version = new pb_VersionInfo();
  101. version.text = str;
  102. try
  103. {
  104. string[] split = Regex.Split(str, @"[\.A-Za-z]");
  105. Match type = Regex.Match(str, @"A-Za-z");
  106. int.TryParse(split[0], out version.major);
  107. int.TryParse(split[1], out version.minor);
  108. int.TryParse(split[2], out version.patch);
  109. int.TryParse(split[3], out version.build);
  110. version.type = GetVersionType(type != null && type.Success ? type.Value : "");
  111. version.valid = true;
  112. }
  113. catch
  114. {
  115. version.valid = false;
  116. }
  117. return version;
  118. }
  119. static VersionType GetVersionType(string type)
  120. {
  121. if( type.Equals("b") || type.Equals("B") )
  122. return VersionType.Beta;
  123. else if( type.Equals("p") || type.Equals("P") )
  124. return VersionType.Patch;
  125. return VersionType.Final;
  126. }
  127. }
  128. }