Avoid to update core if current version is newer than remote version

pull/3834/head
qhy040404 2023-05-09 21:54:56 +08:00
parent 7845569319
commit b3fe13c97a
No known key found for this signature in database
GPG Key ID: B1E14E493E9BB96C
3 changed files with 216 additions and 19 deletions

View File

@ -46,5 +46,29 @@ namespace v2rayN.Base
{ {
return value == null ? string.Empty : value.Trim(); return value == null ? string.Empty : value.Trim();
} }
public static string RemovePrefix(this string value, char prefix)
{
if (value.StartsWith(prefix))
{
return value.Substring(1);
}
else
{
return value;
}
}
public static string RemovePrefix(this string value, string prefix)
{
if (value.StartsWith(prefix))
{
return value.Substring(prefix.Length);
}
else
{
return value;
}
}
} }
} }

View File

@ -9,6 +9,7 @@ using System.Windows;
using v2rayN.Base; using v2rayN.Base;
using v2rayN.Mode; using v2rayN.Mode;
using v2rayN.Resx; using v2rayN.Resx;
using v2rayN.Tool;
namespace v2rayN.Handler namespace v2rayN.Handler
{ {
@ -331,7 +332,7 @@ namespace v2rayN.Handler
/// <summary> /// <summary>
/// 获取V2RayCore版本 /// 获取V2RayCore版本
/// </summary> /// </summary>
private string getCoreVersion(ECoreType type) private SemanticVersion getCoreVersion(ECoreType type)
{ {
try try
{ {
@ -352,7 +353,7 @@ namespace v2rayN.Handler
{ {
string msg = string.Format(ResUI.NotFoundCore, @"", "", ""); string msg = string.Format(ResUI.NotFoundCore, @"", "", "");
//ShowMsg(true, msg); //ShowMsg(true, msg);
return ""; return new SemanticVersion("");
} }
using Process p = new(); using Process p = new();
@ -385,13 +386,13 @@ namespace v2rayN.Handler
version = Regex.Match(echo, $"([0-9.]+)").Groups[1].Value; version = Regex.Match(echo, $"([0-9.]+)").Groups[1].Value;
break; break;
} }
return version; return new SemanticVersion(version);
} }
catch (Exception ex) catch (Exception ex)
{ {
Utils.SaveLog(ex.Message, ex); Utils.SaveLog(ex.Message, ex);
_updateFunc(false, ex.Message); _updateFunc(false, ex.Message);
return ""; return new SemanticVersion("");
} }
} }
@ -400,18 +401,18 @@ namespace v2rayN.Handler
try try
{ {
var gitHubReleases = Utils.FromJson<List<GitHubRelease>>(gitHubReleaseApi); var gitHubReleases = Utils.FromJson<List<GitHubRelease>>(gitHubReleaseApi);
string version; SemanticVersion version;
if (preRelease) if (preRelease)
{ {
version = gitHubReleases!.First().TagName; version = new SemanticVersion(gitHubReleases!.First().TagName);
} }
else else
{ {
version = gitHubReleases!.First(r => r.Prerelease == false).TagName; version = new SemanticVersion(gitHubReleases!.First(r => r.Prerelease == false).TagName);
} }
var coreInfo = LazyConfig.Instance.GetCoreInfo(type); var coreInfo = LazyConfig.Instance.GetCoreInfo(type);
string curVersion; SemanticVersion curVersion;
string message; string message;
string url; string url;
switch (type) switch (type)
@ -421,8 +422,8 @@ namespace v2rayN.Handler
case ECoreType.Xray: case ECoreType.Xray:
case ECoreType.v2fly_v5: case ECoreType.v2fly_v5:
{ {
curVersion = "v" + getCoreVersion(type); curVersion = getCoreVersion(type);
message = string.Format(ResUI.IsLatestCore, curVersion); message = string.Format(ResUI.IsLatestCore, curVersion.ToVersionString("v"));
string osBit = "64"; string osBit = "64";
switch (RuntimeInformation.ProcessArchitecture) switch (RuntimeInformation.ProcessArchitecture)
{ {
@ -439,7 +440,7 @@ namespace v2rayN.Handler
break; break;
} }
url = string.Format(coreInfo.coreDownloadUrl64, version, osBit); url = string.Format(coreInfo.coreDownloadUrl64, version.ToVersionString("v"), osBit);
break; break;
} }
case ECoreType.clash: case ECoreType.clash:
@ -466,8 +467,8 @@ namespace v2rayN.Handler
} }
case ECoreType.sing_box: case ECoreType.sing_box:
{ {
curVersion = "v" + getCoreVersion(type); curVersion = getCoreVersion(type);
message = string.Format(ResUI.IsLatestCore, curVersion); message = string.Format(ResUI.IsLatestCore, curVersion.ToVersionString("v"));
switch (RuntimeInformation.ProcessArchitecture) switch (RuntimeInformation.ProcessArchitecture)
{ {
case Architecture.Arm64: case Architecture.Arm64:
@ -482,12 +483,12 @@ namespace v2rayN.Handler
url = coreInfo.coreDownloadUrl64; url = coreInfo.coreDownloadUrl64;
break; break;
} }
url = string.Format(url, version, version.Replace("v", "")); url = string.Format(url, version.ToVersionString("v"), version);
break; break;
} }
case ECoreType.v2rayN: case ECoreType.v2rayN:
{ {
curVersion = FileVersionInfo.GetVersionInfo(Utils.GetExePath()).FileVersion.ToString(); curVersion = new SemanticVersion(FileVersionInfo.GetVersionInfo(Utils.GetExePath()).FileVersion.ToString());
message = string.Format(ResUI.IsLatestN, curVersion); message = string.Format(ResUI.IsLatestN, curVersion);
switch (RuntimeInformation.ProcessArchitecture) switch (RuntimeInformation.ProcessArchitecture)
{ {
@ -511,16 +512,15 @@ namespace v2rayN.Handler
if (type == ECoreType.v2rayN) if (type == ECoreType.v2rayN)
{ {
decimal.TryParse(curVersion, out decimal decCur); decimal.TryParse(curVersion.ToString(), out decimal decCur);
decimal.TryParse(version, out decimal dec); decimal.TryParse(version.ToString(), out decimal dec);
if (decCur >= dec) if (decCur >= dec)
{ {
AbsoluteCompleted?.Invoke(this, new ResultEventArgs(false, message)); AbsoluteCompleted?.Invoke(this, new ResultEventArgs(false, message));
return; return;
} }
} }
if (curVersion >= version)
if (curVersion == version)
{ {
AbsoluteCompleted?.Invoke(this, new ResultEventArgs(false, message)); AbsoluteCompleted?.Invoke(this, new ResultEventArgs(false, message));
return; return;

View File

@ -0,0 +1,173 @@
using v2rayN.Base;
namespace v2rayN.Tool
{
public class SemanticVersion
{
private int major;
private int minor;
private int patch;
private string version;
public SemanticVersion(int major, int minor, int patch)
{
this.major = major;
this.minor = minor;
this.patch = patch;
this.version = $"{major}.{minor}.{patch}";
}
public SemanticVersion(string version)
{
this.version = version.RemovePrefix('v');
try
{
string[] parts = this.version.Split('.');
if (parts.Length == 2)
{
this.major = int.Parse(parts[0]);
this.minor = int.Parse(parts[1]);
this.patch = 0;
}
else if (parts.Length == 3)
{
this.major = int.Parse(parts[0]);
this.minor = int.Parse(parts[1]);
this.patch = int.Parse(parts[2]);
}
else
{
throw new ArgumentException("Invalid version string");
}
}
catch
{
this.major = 0;
this.minor = 0;
this.patch = 0;
this.version = "0.0.0";
}
}
public override bool Equals(object? obj)
{
if (obj is SemanticVersion other)
{
return this.major == other.major && this.minor == other.minor && this.patch == other.patch;
}
else
{
return false;
}
}
public override int GetHashCode()
{
return this.major.GetHashCode() ^ this.minor.GetHashCode() ^ this.patch.GetHashCode();
}
/// <summary>
/// Use ToVersionString(string? prefix) instead if possible.
/// </summary>
/// <returns>major.minor.patch</returns>
public override string ToString()
{
return this.version;
}
public string ToVersionString(string? prefix = null)
{
if (prefix == null)
{
return this.version;
}
else
{
return $"{prefix}{this.version}";
}
}
public static bool operator ==(SemanticVersion v1, SemanticVersion v2) { return v1.Equals(v2); }
public static bool operator !=(SemanticVersion v1, SemanticVersion v2) { return !v1.Equals(v2); }
public static bool operator >=(SemanticVersion v1, SemanticVersion v2) { return v1.GreaterEquals(v2); }
public static bool operator <=(SemanticVersion v1, SemanticVersion v2) { return v1.LessEquals(v2); }
#region Private
private bool GreaterEquals(SemanticVersion other)
{
if (this.major < other.major)
{
return false;
}
else if (this.major > other.major)
{
return true;
}
else
{
if (this.minor < other.minor)
{
return false;
}
else if (this.minor > other.minor)
{
return true;
}
else
{
if (this.patch < other.patch)
{
return false;
}
else if (this.patch > other.patch)
{
return true;
}
else
{
return true;
}
}
}
}
private bool LessEquals(SemanticVersion other)
{
if (this.major < other.major)
{
return true;
}
else if (this.major > other.major)
{
return false;
}
else
{
if (this.minor < other.minor)
{
return true;
}
else if (this.minor > other.minor)
{
return false;
}
else
{
if (this.patch < other.patch)
{
return true;
}
else if (this.patch > other.patch)
{
return false;
}
else
{
return true;
}
}
}
}
#endregion Private
}
}