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.

323 lines
16 KiB

  1. #nullable enable
  2. using IPA.Config.Data;
  3. using IPA.Config.Stores;
  4. using IPA.Config.Stores.Converters;
  5. using System;
  6. using System.Collections.Generic;
  7. using SVersion = SemVer.Version;
  8. using Version = Hive.Versioning.Version;
  9. namespace IPA.Utilities
  10. {
  11. /// <summary>
  12. /// A type that wraps <see cref="Version"/> so that the string of the version is stored when the string is
  13. /// not a valid <see cref="Version"/>.
  14. /// </summary>
  15. public class AlmostVersion : IComparable<AlmostVersion>, IComparable<Version>,
  16. #pragma warning disable CS0618 // Type or member is obsolete
  17. IComparable<SVersion>
  18. #pragma warning restore CS0618 // Type or member is obsolete
  19. {
  20. /// <summary>
  21. /// Represents a storage type of either parsed <see cref="Version"/> object or raw <see cref="String"/>.
  22. /// </summary>
  23. public enum StoredAs
  24. {
  25. /// <summary>
  26. /// The version was stored as a <see cref="SVersion"/>.
  27. /// </summary>
  28. SemVer,
  29. /// <summary>
  30. /// The version was stored as a <see cref="String"/>.
  31. /// </summary>
  32. String
  33. }
  34. /// <summary>
  35. /// Creates a new <see cref="AlmostVersion"/> with the version string provided in <paramref name="vertext"/>.
  36. /// </summary>
  37. /// <param name="vertext">the version string to store</param>
  38. public AlmostVersion(string vertext)
  39. {
  40. if (!TryParseFrom(vertext, StoredAs.SemVer))
  41. _ = TryParseFrom(vertext, StoredAs.String);
  42. }
  43. /// <summary>
  44. /// Creates an <see cref="AlmostVersion"/> from the <see cref="Version"/> provided in <paramref name="ver"/>.
  45. /// </summary>
  46. /// <param name="ver">the <see cref="Version"/> to store</param>
  47. public AlmostVersion(Version ver)
  48. {
  49. SemverValue = ver;
  50. StorageMode = StoredAs.SemVer;
  51. }
  52. /// <summary>
  53. /// Creates an <see cref="AlmostVersion"/> from the <see cref="SVersion"/> provided in <paramref name="ver"/>.
  54. /// </summary>
  55. /// <param name="ver">the <see cref="SVersion"/> to store</param>
  56. [Obsolete("Use Hive.Versioning.Version constructor instead.")]
  57. public AlmostVersion(SVersion ver) : this(ver?.UnderlyingVersion ?? throw new ArgumentNullException(nameof(ver))) { }
  58. /// <summary>
  59. /// Creates an <see cref="AlmostVersion"/> from the version string in <paramref name="vertext"/> stored using
  60. /// the storage mode specified in <paramref name="mode"/>.
  61. /// </summary>
  62. /// <param name="vertext">the text to parse as an <see cref="AlmostVersion"/></param>
  63. /// <param name="mode">the storage mode to store the version in</param>
  64. public AlmostVersion(string vertext, StoredAs mode)
  65. {
  66. if (!TryParseFrom(vertext, mode))
  67. throw new ArgumentException($"{nameof(vertext)} could not be stored as {mode}!");
  68. }
  69. /// <summary>
  70. /// Creates a new <see cref="AlmostVersion"/> from the version string in <paramref name="vertext"/> stored the
  71. /// same way as the <see cref="AlmostVersion"/> passed in <paramref name="copyMode"/>.
  72. /// </summary>
  73. /// <param name="vertext">the text to parse as an <see cref="AlmostVersion"/></param>
  74. /// <param name="copyMode">an <see cref="AlmostVersion"/> to copy the storage mode of</param>
  75. public AlmostVersion(string vertext, AlmostVersion copyMode)
  76. {
  77. if (copyMode is null)
  78. throw new ArgumentNullException(nameof(copyMode));
  79. if (!TryParseFrom(vertext, copyMode.StorageMode))
  80. _ = TryParseFrom(vertext, StoredAs.String); // silently parse differently
  81. }
  82. private bool TryParseFrom(string str, StoredAs mode)
  83. {
  84. if (mode == StoredAs.SemVer)
  85. {
  86. StorageMode = StoredAs.SemVer;
  87. var result = Version.TryParse(str, out var version);
  88. SemverValue = version;
  89. return result;
  90. }
  91. else
  92. {
  93. StringValue = str;
  94. StorageMode = StoredAs.String;
  95. return true;
  96. }
  97. }
  98. /// <summary>
  99. /// The value of the <see cref="AlmostVersion"/> if it was stored as a <see cref="string"/>.
  100. /// </summary>
  101. /// <value>the stored value as a <see cref="string"/>, or <see langword="null"/> if not stored as a string.</value>
  102. public string? StringValue { get; private set; }
  103. /// <summary>
  104. /// The value of the <see cref="AlmostVersion"/> if it was stored as a <see cref="Version"/>.
  105. /// </summary>
  106. /// <value>the stored value as a <see cref="Version"/>, or <see langword="null"/> if not stored as a version.</value>
  107. public Version? SemverValue { get; private set; }
  108. /// <summary>
  109. /// The way the value is stored, whether it be as a <see cref="Version"/> or a <see cref="string"/>.
  110. /// </summary>
  111. /// <value>the storage mode used to store this value</value>
  112. public StoredAs StorageMode { get; private set; }
  113. /// <summary>
  114. /// Gets a string representation of the current version. If the value is stored as a string, this returns it. If it is
  115. /// stored as a <see cref="Version"/>, it is equivalent to calling <see cref="Version.ToString()"/>.
  116. /// </summary>
  117. /// <returns>a string representation of the current version</returns>
  118. /// <seealso cref="object.ToString"/>
  119. public override string ToString() =>
  120. StorageMode == StoredAs.SemVer ? SemverValue!.ToString() : StringValue!;
  121. /// <summary>
  122. /// Compares <see langword="this"/> to the <see cref="AlmostVersion"/> in <paramref name="other"/> using <see cref="Version.CompareTo(Version)"/>
  123. /// or <see cref="string.CompareTo(string)"/>, depending on the current store.
  124. /// </summary>
  125. /// <remarks>
  126. /// The storage methods of the two objects must be the same, or this will throw an <see cref="InvalidOperationException"/>.
  127. /// </remarks>
  128. /// <param name="other">the <see cref="AlmostVersion"/> to compare to</param>
  129. /// <returns>less than 0 if <paramref name="other"/> is considered bigger than <see langword="this"/>, 0 if equal, and greater than zero if smaller</returns>
  130. /// <seealso cref="CompareTo(Version)"/>
  131. public int CompareTo(AlmostVersion other)
  132. {
  133. if (other is null) return 1;
  134. return StorageMode == StoredAs.SemVer && other.StorageMode == StoredAs.SemVer
  135. ? SemverValue!.CompareTo(other.SemverValue!)
  136. : string.Compare(ToString(), other.ToString(), StringComparison.Ordinal);
  137. }
  138. /// <summary>
  139. /// Compares <see langword="this"/> to the <see cref="Version"/> in <paramref name="other"/> using <see cref="Version.CompareTo(Version)"/>.
  140. /// </summary>
  141. /// <remarks>
  142. /// The storage method of <see langword="this"/> must be <see cref="StoredAs.SemVer"/>, else an <see cref="InvalidOperationException"/> will
  143. /// be thrown.
  144. /// </remarks>
  145. /// <param name="other">the <see cref="Version"/> to compare to</param>
  146. /// <returns>less than 0 if <paramref name="other"/> is considered bigger than <see langword="this"/>, 0 if equal, and greater than zero if smaller</returns>
  147. /// <seealso cref="CompareTo(AlmostVersion)"/>
  148. public int CompareTo(Version other)
  149. {
  150. if (StorageMode != StoredAs.SemVer)
  151. throw new InvalidOperationException("Cannot compare a SemVer version with an AlmostVersion stored as a string!");
  152. return SemverValue!.CompareTo(other);
  153. }
  154. /// <summary>
  155. /// Compares <see langword="this"/> to the <see cref="SVersion"/> in <paramref name="other"/> using <see cref="Version.CompareTo(Version)"/>.
  156. /// </summary>
  157. /// <remarks>
  158. /// The storage method of <see langword="this"/> must be <see cref="StoredAs.SemVer"/>, else an <see cref="InvalidOperationException"/> will
  159. /// be thrown.
  160. /// </remarks>
  161. /// <param name="other">the <see cref="SVersion"/> to compare to</param>
  162. /// <returns>less than 0 if <paramref name="other"/> is considered bigger than <see langword="this"/>, 0 if equal, and greater than zero if smaller</returns>
  163. /// <seealso cref="CompareTo(AlmostVersion)"/>
  164. [Obsolete("Use the Hive.Versioning.Version overload instead.")]
  165. public int CompareTo(SVersion other) => CompareTo(other.UnderlyingVersion);
  166. /// <summary>
  167. /// Performs a strict equality check between <see langword="this"/> and <paramref name="obj"/>.
  168. /// </summary>
  169. /// <remarks>
  170. /// This may return <see langword="false"/> where <see cref="operator ==(AlmostVersion, AlmostVersion)"/> returns <see langword="true"/>
  171. /// </remarks>
  172. /// <param name="obj">the object to compare to</param>
  173. /// <returns><see langword="true"/> if they are equal, <see langword="false"/> otherwise</returns>
  174. /// <seealso cref="object.Equals(object)"/>
  175. public override bool Equals(object obj)
  176. {
  177. return obj is AlmostVersion version &&
  178. SemverValue == version.SemverValue &&
  179. StringValue == version.StringValue &&
  180. StorageMode == version.StorageMode;
  181. }
  182. /// <summary>
  183. /// Default generated hash code function generated by VS.
  184. /// </summary>
  185. /// <returns>a value unique to each object, except those that are considered equal by <see cref="Equals(object)"/></returns>
  186. /// <seealso cref="object.GetHashCode"/>
  187. public override int GetHashCode()
  188. {
  189. var hashCode = -126402897;
  190. hashCode = (hashCode * -1521134295) + EqualityComparer<Version?>.Default.GetHashCode(SemverValue);
  191. hashCode = (hashCode * -1521134295) + EqualityComparer<string?>.Default.GetHashCode(StringValue);
  192. hashCode = (hashCode * -1521134295) + StorageMode.GetHashCode();
  193. return hashCode;
  194. }
  195. /// <summary>
  196. /// Compares two versions, only taking into account the numeric part of the version if they are stored as <see cref="Version"/>s,
  197. /// or strict equality if they are stored as <see cref="string"/>s.
  198. /// </summary>
  199. /// <remarks>
  200. /// This is a looser equality than <see cref="Equals(object)"/>, meaning that this may return <see langword="true"/> where <see cref="Equals(object)"/>
  201. /// does not.
  202. /// </remarks>
  203. /// <param name="l">the first value to compare</param>
  204. /// <param name="r">the second value to compare</param>
  205. /// <returns><see langword="true"/> if they are mostly equal, <see langword="false"/> otherwise</returns>
  206. /// <seealso cref="Equals(object)"/>
  207. public static bool operator==(AlmostVersion l, AlmostVersion r)
  208. {
  209. if (l is null && r is null) return true;
  210. if (l is null || r is null) return false;
  211. if (l.StorageMode != r.StorageMode) return false;
  212. return l.StorageMode == StoredAs.SemVer
  213. ? Utils.VersionCompareNoPrerelease(l.SemverValue!, r.SemverValue!) == 0
  214. : l.StringValue == r.StringValue;
  215. }
  216. /// <summary>
  217. /// The opposite of <see cref="operator ==(AlmostVersion, AlmostVersion)"/>. Equivalent to <c>!(l == r)</c>.
  218. /// </summary>
  219. /// <param name="l">the first value to compare</param>
  220. /// <param name="r">the second value to compare</param>
  221. /// <returns><see langword="true"/> if they are not mostly equal, <see langword="false"/> otherwise</returns>
  222. /// <seealso cref="operator ==(AlmostVersion, AlmostVersion)"/>
  223. public static bool operator!=(AlmostVersion l, AlmostVersion r) => !(l == r);
  224. // implicitly convertible from Version
  225. #pragma warning disable CS0618 // Type or member is obsolete
  226. #pragma warning disable CA2225 // Operator overloads have named alternates
  227. /// <summary>
  228. /// Implicitly converts a <see cref="SVersion"/> to <see cref="AlmostVersion"/> using <see cref="AlmostVersion(SVersion)"/>.
  229. /// </summary>
  230. /// <param name="ver">the <see cref="SVersion"/> to convert</param>
  231. /// <seealso cref="AlmostVersion(SVersion)"/>
  232. [Obsolete("Use Hive.Versioning.Version instead of SemVer.Version")]
  233. public static implicit operator AlmostVersion?(SVersion? ver) => ver is null ? null : new(ver);
  234. // implicitly convertible to Version
  235. /// <summary>
  236. /// Implicitly converts an <see cref="AlmostVersion"/> to <see cref="SVersion"/>, if applicable, using <see cref="SemverValue"/>.
  237. /// If not applicable, returns <see langword="null"/>
  238. /// </summary>
  239. /// <param name="av">the <see cref="AlmostVersion"/> to convert to a <see cref="SVersion"/></param>
  240. /// <seealso cref="SemverValue"/>
  241. [Obsolete("Use Hive.Versioning.Version instead of SemVer.Version")]
  242. public static implicit operator SVersion?(AlmostVersion? av) => av?.SemverValue is not null ? SVersion.ForHiveVersion(av.SemverValue) : null;
  243. #pragma warning restore CS0618 // Type or member is obsolete
  244. /// <summary>
  245. /// Implicitly converts a <see cref="SVersion"/> to <see cref="AlmostVersion"/> using <see cref="AlmostVersion(SVersion)"/>.
  246. /// </summary>
  247. /// <param name="ver">the <see cref="SVersion"/> to convert</param>
  248. /// <seealso cref="AlmostVersion(SVersion)"/>
  249. public static implicit operator AlmostVersion?(Version? ver) => ver is null ? null : new(ver);
  250. // implicitly convertible to Version
  251. /// <summary>
  252. /// Implicitly converts an <see cref="AlmostVersion"/> to <see cref="SVersion"/>, if applicable, using <see cref="SemverValue"/>.
  253. /// If not applicable, returns <see langword="null"/>
  254. /// </summary>
  255. /// <param name="av">the <see cref="AlmostVersion"/> to convert to a <see cref="SVersion"/></param>
  256. /// <seealso cref="SemverValue"/>
  257. public static implicit operator Version?(AlmostVersion av) => av?.SemverValue;
  258. #pragma warning restore CA2225 // Operator overloads have named alternates
  259. public static bool operator <(AlmostVersion left, AlmostVersion right)
  260. => left is null ? right is not null : left.CompareTo(right) < 0;
  261. public static bool operator <=(AlmostVersion left, AlmostVersion right)
  262. => left is null || left.CompareTo(right) <= 0;
  263. public static bool operator >(AlmostVersion left, AlmostVersion right)
  264. => left is not null && left.CompareTo(right) > 0;
  265. public static bool operator >=(AlmostVersion left, AlmostVersion right)
  266. => left is null ? right is null : left.CompareTo(right) >= 0;
  267. }
  268. /// <summary>
  269. /// A <see cref="ValueConverter{T}"/> for <see cref="AlmostVersion"/>s.
  270. /// </summary>
  271. public sealed class AlmostVersionConverter : ValueConverter<AlmostVersion>
  272. {
  273. /// <summary>
  274. /// Converts a <see cref="Text"/> node into an <see cref="AlmostVersion"/>.
  275. /// </summary>
  276. /// <param name="value">the <see cref="Text"/> node to convert</param>
  277. /// <param name="parent">the owner of the new object</param>
  278. /// <returns></returns>
  279. public override AlmostVersion? FromValue(Value? value, object parent)
  280. => Converter<string>.Default.FromValue(value, parent) switch
  281. {
  282. { } v => new(v),
  283. _ => null
  284. };
  285. /// <summary>
  286. /// Converts an <see cref="AlmostVersion"/> to a <see cref="Text"/> node.
  287. /// </summary>
  288. /// <param name="obj">the <see cref="AlmostVersion"/> to convert</param>
  289. /// <param name="parent">the parent of <paramref name="obj"/></param>
  290. /// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
  291. public override Value? ToValue(AlmostVersion? obj, object parent)
  292. => Value.From(obj?.ToString());
  293. }
  294. }