first commit
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SingularityGroup.HotReload.Editor.Semver;
|
||||
using SingularityGroup.HotReload.Newtonsoft.Json;
|
||||
using SingularityGroup.HotReload.Newtonsoft.Json.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace SingularityGroup.HotReload.Editor {
|
||||
internal class PackageUpdateChecker {
|
||||
const string persistedFile = PackageConst.LibraryCachePath + "/updateChecker.json";
|
||||
readonly JsonSerializer jsonSerializer = JsonSerializer.CreateDefault();
|
||||
SemVersion newVersionDetected;
|
||||
bool started;
|
||||
|
||||
private static TimeSpan RetryInterval => TimeSpan.FromSeconds(30);
|
||||
private static TimeSpan CheckInterval => TimeSpan.FromHours(1);
|
||||
|
||||
private static readonly HttpClient client = HttpClientUtils.CreateHttpClient();
|
||||
|
||||
private static string _lastRemotePackageVersion;
|
||||
|
||||
public static string lastRemotePackageVersion => _lastRemotePackageVersion;
|
||||
|
||||
public async void StartCheckingForNewVersion() {
|
||||
if(started) {
|
||||
return;
|
||||
}
|
||||
started = true;
|
||||
|
||||
for (;;) {
|
||||
try {
|
||||
await PerformVersionCheck();
|
||||
if(newVersionDetected != null) {
|
||||
break;
|
||||
}
|
||||
} catch(Exception ex) {
|
||||
Log.Warning("encountered exception when checking for new Hot Reload package version:\n{0}", ex);
|
||||
}
|
||||
await Task.Delay(RetryInterval);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetNewVersion(out SemVersion version) {
|
||||
var currentVersion = SemVersion.Parse(PackageConst.Version, strict: true);
|
||||
return !ReferenceEquals(version = newVersionDetected, null) && newVersionDetected > currentVersion;
|
||||
}
|
||||
|
||||
async Task PerformVersionCheck() {
|
||||
var state = await LoadPersistedState();
|
||||
var currentVersion = SemVersion.Parse(PackageConst.Version, strict: true);
|
||||
if(state != null) {
|
||||
_lastRemotePackageVersion = state.lastRemotePackageVersion;
|
||||
var newVersion = SemVersion.Parse(state.lastRemotePackageVersion);
|
||||
if(newVersion > currentVersion) {
|
||||
newVersionDetected = newVersion;
|
||||
return;
|
||||
}
|
||||
if(DateTime.UtcNow - state.lastVersionCheck < CheckInterval) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var response = await GetLatestPackageVersion();
|
||||
if(response.err != null) {
|
||||
if(response.statusCode == 0 || response.statusCode == 404) {
|
||||
// probably no internet, fail silently and retry
|
||||
} else {
|
||||
Log.Warning("version check failed: {0}", response.err);
|
||||
}
|
||||
} else {
|
||||
var newVersion = response.data;
|
||||
if (response.data > currentVersion) {
|
||||
newVersionDetected = newVersion;
|
||||
}
|
||||
await Task.Run(() => PersistState(response.data));
|
||||
}
|
||||
}
|
||||
|
||||
void PersistState(SemVersion newVersion) {
|
||||
// ReSharper disable once AssignNullToNotNullAttribute
|
||||
var fi = new FileInfo(persistedFile);
|
||||
fi.Directory.Create();
|
||||
using (var streamWriter = new StreamWriter(fi.OpenWrite()))
|
||||
using (var writer = new JsonTextWriter(streamWriter)) {
|
||||
jsonSerializer.Serialize(writer, new State {
|
||||
lastVersionCheck = DateTime.UtcNow,
|
||||
lastRemotePackageVersion = newVersion.ToString()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Task<State> LoadPersistedState() {
|
||||
return Task.Run(() => {
|
||||
var fi = new FileInfo(persistedFile);
|
||||
if(!fi.Exists) {
|
||||
return null;
|
||||
}
|
||||
|
||||
using(var streamReader = fi.OpenText())
|
||||
using(var reader = new JsonTextReader(streamReader)) {
|
||||
return jsonSerializer.Deserialize<State>(reader);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
static async Task<Response<SemVersion>> GetLatestPackageVersion() {
|
||||
string versionUrl;
|
||||
|
||||
if (PackageConst.IsAssetStoreBuild) {
|
||||
// version updates are synced with asset store
|
||||
versionUrl = "https://d2tc55zjhw51ly.cloudfront.net/releases/latest/asset-store-version.json";
|
||||
} else {
|
||||
versionUrl = "https://gitlab.hotreload.net/root/hot-reload-releases/-/raw/production/package.json";
|
||||
}
|
||||
try {
|
||||
using(var resp = await client.GetAsync(versionUrl).ConfigureAwait(false)) {
|
||||
if(resp.StatusCode != HttpStatusCode.OK) {
|
||||
return Response.FromError<SemVersion>($"Request failed with statusCode: {resp.StatusCode} {resp.ReasonPhrase}");
|
||||
}
|
||||
|
||||
var json = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
var o = await JObject.LoadAsync(new JsonTextReader(new StringReader(json))).ConfigureAwait(false);
|
||||
SemVersion newVersion;
|
||||
JToken value;
|
||||
if (!o.TryGetValue("version", out value)) {
|
||||
return Response.FromError<SemVersion>("Invalid package.json");
|
||||
} else if(!SemVersion.TryParse(value.Value<string>(), out newVersion, strict: true)) {
|
||||
return Response.FromError<SemVersion>($"Invalid version in package.json: '{value.Value<string>()}'");
|
||||
} else {
|
||||
return Response.FromResult(newVersion);
|
||||
}
|
||||
}
|
||||
} catch(Exception ex) {
|
||||
return Response.FromError<SemVersion>($"{ex.GetType().Name} {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdatePackageAsync(SemVersion newVersion) {
|
||||
//Package can be updated by updating the git url via the package manager
|
||||
if(EditorUtility.DisplayDialog($"Update To v{newVersion}", $"By pressing 'Update' the Hot Reload package will be updated to v{newVersion}", "Update", "Cancel")) {
|
||||
var resp = await GetLatestPackageVersion();
|
||||
if(resp.err == null && resp.data > newVersion) {
|
||||
newVersion = resp.data;
|
||||
}
|
||||
|
||||
if(await IsUsingGitRepo()) {
|
||||
var err = UpdateGitUrlInManifest(newVersion);
|
||||
if(err != null) {
|
||||
Log.Warning("Encountered issue when updating Hot Reload: {0}", err);
|
||||
} else {
|
||||
//Delete state to force another version check after the package is installed
|
||||
File.Delete(persistedFile);
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
UnityEditor.PackageManager.Client.Resolve();
|
||||
#else
|
||||
CompileMethodDetourer.Reset();
|
||||
AssetDatabase.Refresh();
|
||||
#endif
|
||||
}
|
||||
} else {
|
||||
var err = await UpdateUtility.Update(newVersion.ToString(), null, CancellationToken.None);
|
||||
if(err != null) {
|
||||
Log.Warning("Failed to update package: {0}", err);
|
||||
} else {
|
||||
CompileMethodDetourer.Reset();
|
||||
AssetDatabase.Refresh();
|
||||
}
|
||||
}
|
||||
|
||||
//open changelog
|
||||
HotReloadPrefs.ShowChangeLog = true;
|
||||
HotReloadWindow.Current.SelectTab(typeof(HotReloadAboutTab));
|
||||
}
|
||||
}
|
||||
|
||||
string UpdateGitUrlInManifest(SemVersion newVersion) {
|
||||
const string repoUrl = "git+https://gitlab.hotreload.net/root/hot-reload-releases.git";
|
||||
const string manifestJsonPath = "Packages/manifest.json";
|
||||
var repoUrlToNewVersion = $"{repoUrl}#{newVersion}";
|
||||
if(!File.Exists(manifestJsonPath)) {
|
||||
return "Unable to find manifest.json";
|
||||
}
|
||||
|
||||
var root = JObject.Load(new JsonTextReader(new StringReader(File.ReadAllText(manifestJsonPath))));
|
||||
JObject deps;
|
||||
var err = TryGetManfestDeps(root, out deps);
|
||||
if(err != null) {
|
||||
return err;
|
||||
}
|
||||
deps[PackageConst.PackageName] = repoUrlToNewVersion;
|
||||
root["dependencies"] = deps;
|
||||
File.WriteAllText(manifestJsonPath, root.ToString(Formatting.Indented));
|
||||
return null;
|
||||
}
|
||||
|
||||
static string TryGetManfestDeps(JObject root, out JObject deps) {
|
||||
JToken value;
|
||||
if(!root.TryGetValue("dependencies", out value)) {
|
||||
deps = null;
|
||||
return "no dependencies object found in manifest.json";
|
||||
}
|
||||
deps = value.Value<JObject>();
|
||||
if(deps == null) {
|
||||
return "dependencies object null in manifest.json";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static async Task<bool> IsUsingGitRepo() {
|
||||
var respose = await Task.Run(() => IsUsingGitRepoThreaded(PackageConst.PackageName));
|
||||
if(respose.err != null) {
|
||||
Log.Warning("Unable to find package. message: {0}", respose.err);
|
||||
return false;
|
||||
} else {
|
||||
return respose.data;
|
||||
}
|
||||
}
|
||||
|
||||
static Response<bool> IsUsingGitRepoThreaded(string packageId) {
|
||||
var fi = new FileInfo("Packages/manifest.json");
|
||||
if(!fi.Exists) {
|
||||
return "Unable to find manifest.json";
|
||||
}
|
||||
|
||||
using(var reader = fi.OpenText()) {
|
||||
var root = JObject.Load(new JsonTextReader(reader));
|
||||
JObject deps;
|
||||
var err = TryGetManfestDeps(root, out deps);
|
||||
if(err != null) {
|
||||
return "no dependencies specified in manifest.json";
|
||||
}
|
||||
JToken value;
|
||||
if(!deps.TryGetValue(packageId, out value)) {
|
||||
//Likely a local package directly in the packages folder of the unity project
|
||||
//or the package got moved into the Assets folder
|
||||
return Response.FromResult(false);
|
||||
}
|
||||
var pathToPackage = value.Value<string>();
|
||||
if(pathToPackage.StartsWith("git+", StringComparison.Ordinal)) {
|
||||
return Response.FromResult(true);
|
||||
}
|
||||
if(pathToPackage.StartsWith("https://", StringComparison.Ordinal)) {
|
||||
return Response.FromResult(true);
|
||||
}
|
||||
return Response.FromResult(false);
|
||||
}
|
||||
}
|
||||
|
||||
class Response<T> {
|
||||
public readonly T data;
|
||||
public readonly string err;
|
||||
public readonly long statusCode;
|
||||
public Response(T data, string err, long statusCode) {
|
||||
this.data = data;
|
||||
this.err = err;
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
public static implicit operator Response<T>( string err) {
|
||||
return Response.FromError<T>(err);
|
||||
}
|
||||
}
|
||||
|
||||
static class Response {
|
||||
public static Response<T> FromError<T>(string error) {
|
||||
return new Response<T>(default(T), error, -1);
|
||||
}
|
||||
public static Response<T> FromResult<T>(T result) {
|
||||
return new Response<T>(result, null, 200);
|
||||
}
|
||||
}
|
||||
|
||||
class State {
|
||||
public DateTime lastVersionCheck;
|
||||
public string lastRemotePackageVersion;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0cecc9e1c2704904691175cdd2f450e6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,536 @@
|
||||
using System;
|
||||
#if !NETSTANDARD
|
||||
using System.Globalization;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Security.Permissions;
|
||||
#endif
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SingularityGroup.HotReload.Editor.Semver
|
||||
{
|
||||
/// <summary>
|
||||
/// A semantic version implementation.
|
||||
/// Conforms to v2.0.0 of http://semver.org/
|
||||
/// </summary>
|
||||
#if NETSTANDARD
|
||||
sealed class SemVersion : IComparable<SemVersion>, IComparable
|
||||
#else
|
||||
[Serializable]
|
||||
sealed class SemVersion : IComparable<SemVersion>, IComparable, ISerializable
|
||||
#endif
|
||||
{
|
||||
public static SemVersion None = new SemVersion(0, 0, 0);
|
||||
public static string NoneString = new SemVersion(0, 0, 0).ToString();
|
||||
|
||||
static Regex parseEx =
|
||||
new Regex(@"^(?<major>\d+)" +
|
||||
@"(\.(?<minor>\d+))?" +
|
||||
@"(\.(?<patch>\d+))?" +
|
||||
@"(\-(?<pre>[0-9A-Za-z\-\.]+))?" +
|
||||
@"(\+(?<build>[0-9A-Za-z\-\.]+))?$",
|
||||
#if NETSTANDARD
|
||||
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
|
||||
#else
|
||||
RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.ExplicitCapture);
|
||||
#endif
|
||||
|
||||
#if !NETSTANDARD
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SemVersion" /> class.
|
||||
/// </summary>
|
||||
/// <param name="info"></param>
|
||||
/// <param name="context"></param>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
private SemVersion(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
if (info == null) throw new ArgumentNullException("info");
|
||||
var semVersion = Parse(info.GetString("SemVersion"));
|
||||
Major = semVersion.Major;
|
||||
Minor = semVersion.Minor;
|
||||
Patch = semVersion.Patch;
|
||||
Prerelease = semVersion.Prerelease;
|
||||
Build = semVersion.Build;
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SemVersion" /> class.
|
||||
/// </summary>
|
||||
/// <param name="major">The major version.</param>
|
||||
/// <param name="minor">The minor version.</param>
|
||||
/// <param name="patch">The patch version.</param>
|
||||
/// <param name="prerelease">The prerelease version (eg. "alpha").</param>
|
||||
/// <param name="build">The build eg ("nightly.232").</param>
|
||||
public SemVersion(int major, int minor = 0, int patch = 0, string prerelease = "", string build = "")
|
||||
{
|
||||
this.Major = major;
|
||||
this.Minor = minor;
|
||||
this.Patch = patch;
|
||||
|
||||
this.Prerelease = prerelease ?? "";
|
||||
this.Build = build ?? "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SemVersion"/> class.
|
||||
/// </summary>
|
||||
/// <param name="version">The <see cref="System.Version"/> that is used to initialize
|
||||
/// the Major, Minor, Patch and Build properties.</param>
|
||||
public SemVersion(Version version)
|
||||
{
|
||||
if (version == null)
|
||||
throw new ArgumentNullException("version");
|
||||
|
||||
this.Major = version.Major;
|
||||
this.Minor = version.Minor;
|
||||
|
||||
if (version.Revision >= 0)
|
||||
{
|
||||
this.Patch = version.Revision;
|
||||
}
|
||||
|
||||
this.Prerelease = String.Empty;
|
||||
|
||||
if (version.Build > 0)
|
||||
{
|
||||
this.Build = version.Build.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Build = String.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the specified string to a semantic version.
|
||||
/// </summary>
|
||||
/// <param name="version">The version string.</param>
|
||||
/// <param name="strict">If set to <c>true</c> minor and patch version are required, else they default to 0.</param>
|
||||
/// <returns>The SemVersion object.</returns>
|
||||
/// <exception cref="System.InvalidOperationException">When a invalid version string is passed.</exception>
|
||||
public static SemVersion Parse(string version, bool strict = false)
|
||||
{
|
||||
var match = parseEx.Match(version);
|
||||
if (!match.Success)
|
||||
throw new ArgumentException("Invalid version.", "version");
|
||||
|
||||
#if NETSTANDARD
|
||||
var major = int.Parse(match.Groups["major"].Value);
|
||||
#else
|
||||
var major = int.Parse(match.Groups["major"].Value, CultureInfo.InvariantCulture);
|
||||
#endif
|
||||
|
||||
var minorMatch = match.Groups["minor"];
|
||||
int minor = 0;
|
||||
if (minorMatch.Success)
|
||||
{
|
||||
#if NETSTANDARD
|
||||
minor = int.Parse(minorMatch.Value);
|
||||
#else
|
||||
minor = int.Parse(minorMatch.Value, CultureInfo.InvariantCulture);
|
||||
#endif
|
||||
}
|
||||
else if (strict)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid version (no minor version given in strict mode)");
|
||||
}
|
||||
|
||||
var patchMatch = match.Groups["patch"];
|
||||
int patch = 0;
|
||||
if (patchMatch.Success)
|
||||
{
|
||||
#if NETSTANDARD
|
||||
patch = int.Parse(patchMatch.Value);
|
||||
#else
|
||||
patch = int.Parse(patchMatch.Value, CultureInfo.InvariantCulture);
|
||||
#endif
|
||||
}
|
||||
else if (strict)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid version (no patch version given in strict mode)");
|
||||
}
|
||||
|
||||
var prerelease = match.Groups["pre"].Value;
|
||||
var build = match.Groups["build"].Value;
|
||||
|
||||
return new SemVersion(major, minor, patch, prerelease, build);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the specified string to a semantic version.
|
||||
/// </summary>
|
||||
/// <param name="version">The version string.</param>
|
||||
/// <param name="semver">When the method returns, contains a SemVersion instance equivalent
|
||||
/// to the version string passed in, if the version string was valid, or <c>null</c> if the
|
||||
/// version string was not valid.</param>
|
||||
/// <param name="strict">If set to <c>true</c> minor and patch version are required, else they default to 0.</param>
|
||||
/// <returns><c>False</c> when a invalid version string is passed, otherwise <c>true</c>.</returns>
|
||||
public static bool TryParse(string version, out SemVersion semver, bool strict = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
semver = Parse(version, strict);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
semver = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the specified versions for equality.
|
||||
/// </summary>
|
||||
/// <param name="versionA">The first version.</param>
|
||||
/// <param name="versionB">The second version.</param>
|
||||
/// <returns>If versionA is equal to versionB <c>true</c>, else <c>false</c>.</returns>
|
||||
public static bool Equals(SemVersion versionA, SemVersion versionB)
|
||||
{
|
||||
if (ReferenceEquals(versionA, null))
|
||||
return ReferenceEquals(versionB, null);
|
||||
return versionA.Equals(versionB);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares the specified versions.
|
||||
/// </summary>
|
||||
/// <param name="versionA">The version to compare to.</param>
|
||||
/// <param name="versionB">The version to compare against.</param>
|
||||
/// <returns>If versionA < versionB <c>< 0</c>, if versionA > versionB <c>> 0</c>,
|
||||
/// if versionA is equal to versionB <c>0</c>.</returns>
|
||||
public static int Compare(SemVersion versionA, SemVersion versionB)
|
||||
{
|
||||
if (ReferenceEquals(versionA, null))
|
||||
return ReferenceEquals(versionB, null) ? 0 : -1;
|
||||
return versionA.CompareTo(versionB);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Make a copy of the current instance with optional altered fields.
|
||||
/// </summary>
|
||||
/// <param name="major">The major version.</param>
|
||||
/// <param name="minor">The minor version.</param>
|
||||
/// <param name="patch">The patch version.</param>
|
||||
/// <param name="prerelease">The prerelease text.</param>
|
||||
/// <param name="build">The build text.</param>
|
||||
/// <returns>The new version object.</returns>
|
||||
public SemVersion Change(int? major = null, int? minor = null, int? patch = null,
|
||||
string prerelease = null, string build = null)
|
||||
{
|
||||
return new SemVersion(
|
||||
major ?? this.Major,
|
||||
minor ?? this.Minor,
|
||||
patch ?? this.Patch,
|
||||
prerelease ?? this.Prerelease,
|
||||
build ?? this.Build);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the major version.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The major version.
|
||||
/// </value>
|
||||
public int Major { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the minor version.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The minor version.
|
||||
/// </value>
|
||||
public int Minor { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the patch version.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The patch version.
|
||||
/// </value>
|
||||
public int Patch { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the pre-release version.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The pre-release version.
|
||||
/// </value>
|
||||
public string Prerelease { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the build version.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The build version.
|
||||
/// </value>
|
||||
public string Build { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="System.String" /> that represents this instance.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A <see cref="System.String" /> that represents this instance.
|
||||
/// </returns>
|
||||
public override string ToString()
|
||||
{
|
||||
var version = "" + Major + "." + Minor + "." + Patch;
|
||||
if (!String.IsNullOrEmpty(Prerelease))
|
||||
version += "-" + Prerelease;
|
||||
if (!String.IsNullOrEmpty(Build))
|
||||
version += "+" + Build;
|
||||
return version;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares the current instance with another object of the same type and returns an integer that indicates
|
||||
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
|
||||
/// other object.
|
||||
/// </summary>
|
||||
/// <param name="obj">An object to compare with this instance.</param>
|
||||
/// <returns>
|
||||
/// A value that indicates the relative order of the objects being compared.
|
||||
/// The return value has these meanings: Value Meaning Less than zero
|
||||
/// This instance precedes <paramref name="obj" /> in the sort order.
|
||||
/// Zero This instance occurs in the same position in the sort order as <paramref name="obj" />. i
|
||||
/// Greater than zero This instance follows <paramref name="obj" /> in the sort order.
|
||||
/// </returns>
|
||||
public int CompareTo(object obj)
|
||||
{
|
||||
return CompareTo((SemVersion)obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares the current instance with another object of the same type and returns an integer that indicates
|
||||
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
|
||||
/// other object.
|
||||
/// </summary>
|
||||
/// <param name="other">An object to compare with this instance.</param>
|
||||
/// <returns>
|
||||
/// A value that indicates the relative order of the objects being compared.
|
||||
/// The return value has these meanings: Value Meaning Less than zero
|
||||
/// This instance precedes <paramref name="other" /> in the sort order.
|
||||
/// Zero This instance occurs in the same position in the sort order as <paramref name="other" />. i
|
||||
/// Greater than zero This instance follows <paramref name="other" /> in the sort order.
|
||||
/// </returns>
|
||||
public int CompareTo(SemVersion other)
|
||||
{
|
||||
if (ReferenceEquals(other, null))
|
||||
return 1;
|
||||
|
||||
var r = this.CompareByPrecedence(other);
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
r = CompareComponent(this.Build, other.Build);
|
||||
return r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares to semantic versions by precedence. This does the same as a Equals, but ignores the build information.
|
||||
/// </summary>
|
||||
/// <param name="other">The semantic version.</param>
|
||||
/// <returns><c>true</c> if the version precedence matches.</returns>
|
||||
public bool PrecedenceMatches(SemVersion other)
|
||||
{
|
||||
return CompareByPrecedence(other) == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares to semantic versions by precedence. This does the same as a Equals, but ignores the build information.
|
||||
/// </summary>
|
||||
/// <param name="other">The semantic version.</param>
|
||||
/// <returns>
|
||||
/// A value that indicates the relative order of the objects being compared.
|
||||
/// The return value has these meanings: Value Meaning Less than zero
|
||||
/// This instance precedes <paramref name="other" /> in the version precedence.
|
||||
/// Zero This instance has the same precedence as <paramref name="other" />. i
|
||||
/// Greater than zero This instance has creater precedence as <paramref name="other" />.
|
||||
/// </returns>
|
||||
public int CompareByPrecedence(SemVersion other)
|
||||
{
|
||||
if (ReferenceEquals(other, null))
|
||||
return 1;
|
||||
|
||||
var r = this.Major.CompareTo(other.Major);
|
||||
if (r != 0) return r;
|
||||
|
||||
r = this.Minor.CompareTo(other.Minor);
|
||||
if (r != 0) return r;
|
||||
|
||||
r = this.Patch.CompareTo(other.Patch);
|
||||
if (r != 0) return r;
|
||||
|
||||
r = CompareComponent(this.Prerelease, other.Prerelease, true);
|
||||
return r;
|
||||
}
|
||||
|
||||
static int CompareComponent(string a, string b, bool lower = false)
|
||||
{
|
||||
var aEmpty = String.IsNullOrEmpty(a);
|
||||
var bEmpty = String.IsNullOrEmpty(b);
|
||||
if (aEmpty && bEmpty)
|
||||
return 0;
|
||||
|
||||
if (aEmpty)
|
||||
return lower ? 1 : -1;
|
||||
if (bEmpty)
|
||||
return lower ? -1 : 1;
|
||||
|
||||
var aComps = a.Split('.');
|
||||
var bComps = b.Split('.');
|
||||
|
||||
var minLen = Math.Min(aComps.Length, bComps.Length);
|
||||
for (int i = 0; i < minLen; i++)
|
||||
{
|
||||
var ac = aComps[i];
|
||||
var bc = bComps[i];
|
||||
int anum, bnum;
|
||||
var isanum = Int32.TryParse(ac, out anum);
|
||||
var isbnum = Int32.TryParse(bc, out bnum);
|
||||
int r;
|
||||
if (isanum && isbnum)
|
||||
{
|
||||
r = anum.CompareTo(bnum);
|
||||
if (r != 0) return anum.CompareTo(bnum);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isanum)
|
||||
return -1;
|
||||
if (isbnum)
|
||||
return 1;
|
||||
r = String.CompareOrdinal(ac, bc);
|
||||
if (r != 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
return aComps.Length.CompareTo(bComps.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
|
||||
/// </summary>
|
||||
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (ReferenceEquals(obj, null))
|
||||
return false;
|
||||
|
||||
if (ReferenceEquals(this, obj))
|
||||
return true;
|
||||
|
||||
var other = (SemVersion)obj;
|
||||
|
||||
return this.Major == other.Major &&
|
||||
this.Minor == other.Minor &&
|
||||
this.Patch == other.Patch &&
|
||||
string.Equals(this.Prerelease, other.Prerelease, StringComparison.Ordinal) &&
|
||||
string.Equals(this.Build, other.Build, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a hash code for this instance.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
|
||||
/// </returns>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
int result = this.Major.GetHashCode();
|
||||
result = result * 31 + this.Minor.GetHashCode();
|
||||
result = result * 31 + this.Patch.GetHashCode();
|
||||
result = result * 31 + this.Prerelease.GetHashCode();
|
||||
result = result * 31 + this.Build.GetHashCode();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
#if !NETSTANDARD
|
||||
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
|
||||
public void GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
if (info == null) throw new ArgumentNullException("info");
|
||||
info.AddValue("SemVersion", ToString());
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// The override of the equals operator.
|
||||
/// </summary>
|
||||
/// <param name="left">The left value.</param>
|
||||
/// <param name="right">The right value.</param>
|
||||
/// <returns>If left is equal to right <c>true</c>, else <c>false</c>.</returns>
|
||||
public static bool operator ==(SemVersion left, SemVersion right)
|
||||
{
|
||||
if(ReferenceEquals(right, null)) {
|
||||
return ReferenceEquals(left, null);
|
||||
}
|
||||
if(ReferenceEquals(left, null)) {
|
||||
return false;
|
||||
}
|
||||
return left.PrecedenceMatches(right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The override of the un-equal operator.
|
||||
/// </summary>
|
||||
/// <param name="left">The left value.</param>
|
||||
/// <param name="right">The right value.</param>
|
||||
/// <returns>If left is not equal to right <c>true</c>, else <c>false</c>.</returns>
|
||||
public static bool operator !=(SemVersion left, SemVersion right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The override of the greater operator.
|
||||
/// </summary>
|
||||
/// <param name="left">The left value.</param>
|
||||
/// <param name="right">The right value.</param>
|
||||
/// <returns>If left is greater than right <c>true</c>, else <c>false</c>.</returns>
|
||||
public static bool operator >(SemVersion left, SemVersion right)
|
||||
{
|
||||
return left.CompareByPrecedence(right) > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The override of the greater than or equal operator.
|
||||
/// </summary>
|
||||
/// <param name="left">The left value.</param>
|
||||
/// <param name="right">The right value.</param>
|
||||
/// <returns>If left is greater than or equal to right <c>true</c>, else <c>false</c>.</returns>
|
||||
public static bool operator >=(SemVersion left, SemVersion right)
|
||||
{
|
||||
return left == right || left > right;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The override of the less operator.
|
||||
/// </summary>
|
||||
/// <param name="left">The left value.</param>
|
||||
/// <param name="right">The right value.</param>
|
||||
/// <returns>If left is less than right <c>true</c>, else <c>false</c>.</returns>
|
||||
public static bool operator <(SemVersion left, SemVersion right)
|
||||
{
|
||||
return left.CompareByPrecedence(right) < 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The override of the less than or equal operator.
|
||||
/// </summary>
|
||||
/// <param name="left">The left value.</param>
|
||||
/// <param name="right">The right value.</param>
|
||||
/// <returns>If left is less than or equal to right <c>true</c>, else <c>false</c>.</returns>
|
||||
public static bool operator <=(SemVersion left, SemVersion right)
|
||||
{
|
||||
return left == right || left < right;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0b49a1188451e7745af9f636d854efc8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user