UpdateChecker.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. namespace PixiEditor.UpdateModule;
  9. public class UpdateChecker
  10. {
  11. public UpdateChecker(string currentVersionTag, UpdateChannel channel)
  12. {
  13. CurrentVersionTag = currentVersionTag;
  14. Channel = channel;
  15. }
  16. public ReleaseInfo LatestReleaseInfo { get; private set; }
  17. private UpdateChannel _channel;
  18. public UpdateChannel Channel
  19. {
  20. get => _channel;
  21. set
  22. {
  23. bool changed = _channel != value;
  24. if (changed)
  25. {
  26. _channel = value;
  27. LatestReleaseInfo = null;
  28. }
  29. }
  30. }
  31. public string CurrentVersionTag { get; }
  32. /// <summary>
  33. /// Compares version strings and returns true if newVer > originalVer.
  34. /// </summary>
  35. /// <param name="originalVer">Version to compare.</param>
  36. /// <param name="newVer">Version to compare with.</param>
  37. /// <returns>True if semantic version is higher.</returns>
  38. public static bool VersionDifferent(string originalVer, string newVer)
  39. {
  40. return ExtractVersionString(originalVer) != ExtractVersionString(newVer);
  41. }
  42. /// <summary>
  43. /// Checks if originalVer is smaller than newVer
  44. /// </summary>
  45. /// <param name="originalVer">Version on the left side of the equation</param>
  46. /// <param name="newVer">Version to compare to</param>
  47. /// <returns>True if originalVer is smaller than newVer.</returns>
  48. public static bool VersionSmaller(string originalVer, string newVer)
  49. {
  50. string normalizedOriginal = ExtractVersionString(originalVer);
  51. string normalizedNew = ExtractVersionString(newVer);
  52. if (normalizedOriginal == normalizedNew) return false;
  53. bool parsed = TryParseToFloatVersion(normalizedOriginal, out float orgFloat);
  54. if (!parsed) throw new Exception($"Couldn't parse version {originalVer} to float.");
  55. parsed = TryParseToFloatVersion(normalizedNew, out float newFloat);
  56. if (!parsed) throw new Exception($"Couldn't parse version {newVer} to float.");
  57. return orgFloat < newFloat;
  58. }
  59. private static bool TryParseToFloatVersion(string normalizedString, out float ver)
  60. {
  61. return float.TryParse(normalizedString.Replace(".", string.Empty).Insert(1, "."), NumberStyles.Any, CultureInfo.InvariantCulture, out ver);
  62. }
  63. public async Task<bool> CheckUpdateAvailable()
  64. {
  65. LatestReleaseInfo = await GetLatestReleaseInfoAsync(Channel.ApiUrl);
  66. return CheckUpdateAvailable(LatestReleaseInfo);
  67. }
  68. public bool CheckUpdateAvailable(ReleaseInfo latestRelease)
  69. {
  70. if (latestRelease == null || string.IsNullOrEmpty(latestRelease.TagName)) return false;
  71. if (CurrentVersionTag == null) return false;
  72. return latestRelease.WasDataFetchSuccessful && VersionDifferent(CurrentVersionTag, latestRelease.TagName);
  73. }
  74. public bool IsUpdateCompatible(string[] incompatibleVersions)
  75. {
  76. return !incompatibleVersions.Select(x => x.Trim()).Contains(ExtractVersionString(CurrentVersionTag));
  77. }
  78. public async Task<bool> IsUpdateCompatible()
  79. {
  80. string[] incompatibleVersions = await GetUpdateIncompatibleVersionsAsync(LatestReleaseInfo.TagName);
  81. bool isDowngrading = VersionSmaller(LatestReleaseInfo.TagName, CurrentVersionTag);
  82. return IsUpdateCompatible(incompatibleVersions) && !isDowngrading; // Incompatible.json doesn't support backwards compatibility, thus downgrading always means update is not compatble
  83. }
  84. public async Task<string[]> GetUpdateIncompatibleVersionsAsync(string tag)
  85. {
  86. using (HttpClient client = new HttpClient())
  87. {
  88. client.DefaultRequestHeaders.Add("User-Agent", "PixiEditor");
  89. HttpResponseMessage response = await client.GetAsync(string.Format(Channel.IncompatibleFileApiUrl, tag));
  90. if (response.StatusCode == HttpStatusCode.OK)
  91. {
  92. string content = await response.Content.ReadAsStringAsync();
  93. return JsonSerializer.Deserialize<string[]>(content);
  94. }
  95. }
  96. return Array.Empty<string>();
  97. }
  98. private static async Task<ReleaseInfo> GetLatestReleaseInfoAsync(string apiUrl)
  99. {
  100. using (HttpClient client = new HttpClient())
  101. {
  102. client.DefaultRequestHeaders.Add("User-Agent", "PixiEditor");
  103. HttpResponseMessage response = await client.GetAsync(apiUrl);
  104. if (response.StatusCode == HttpStatusCode.OK)
  105. {
  106. string content = await response.Content.ReadAsStringAsync();
  107. return JsonSerializer.Deserialize<ReleaseInfo>(content);
  108. }
  109. }
  110. return new ReleaseInfo(false);
  111. }
  112. private static string ExtractVersionString(string versionString)
  113. {
  114. if (string.IsNullOrEmpty(versionString)) return string.Empty;
  115. for (int i = 0; i < versionString.Length; i++)
  116. {
  117. if (!char.IsDigit(versionString[i]) && versionString[i] != '.')
  118. {
  119. return versionString[..i];
  120. }
  121. }
  122. return versionString;
  123. }
  124. }