UpdateChecker.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using ClashDotNetFramework.Models.GitHubRelease;
  2. using ClashDotNetFramework.Utils;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. namespace ClashDotNetFramework.Controllers
  14. {
  15. public static class UpdateChecker
  16. {
  17. public const string Owner = @"ClashDotNetFramework";
  18. public const string Repo = @"ClashDotNetFramework";
  19. public const string Name = @"Clash .NET Framework";
  20. public const string Copyright = @"Copyright © 2018 - 2021 Coel Wu";
  21. public const string AssemblyVersion = @"0.0.8";
  22. public static readonly string Version = $"{AssemblyVersion}";
  23. public static string LatestVersionNumber;
  24. public static string LatestVersionUrl;
  25. public static Release LatestRelease;
  26. public static event EventHandler NewVersionFound;
  27. public static event EventHandler NewVersionFoundFailed;
  28. public static event EventHandler NewVersionNotFound;
  29. public static async void Check(bool isPreRelease)
  30. {
  31. try
  32. {
  33. var updater = new GitHubRelease(Owner, Repo);
  34. var url = updater.AllReleaseUrl;
  35. var json = await WebUtil.DownloadStringAsync(WebUtil.CreateRequest(url));
  36. var releases = JsonConvert.DeserializeObject<List<Release>>(json);
  37. LatestRelease = VersionUtil.GetLatestRelease(releases, isPreRelease);
  38. LatestVersionNumber = LatestRelease.tag_name;
  39. LatestVersionUrl = LatestRelease.html_url;
  40. Logging.Info($"Github 最新发布版本: {LatestRelease.tag_name}");
  41. if (VersionUtil.CompareVersion(LatestRelease.tag_name, Version) > 0)
  42. {
  43. Logging.Info("发现新版本");
  44. NewVersionFound?.Invoke(null, new EventArgs());
  45. }
  46. else
  47. {
  48. Logging.Info("目前是最新版本");
  49. NewVersionNotFound?.Invoke(null, new EventArgs());
  50. }
  51. }
  52. catch (Exception e)
  53. {
  54. if (e is WebException)
  55. Logging.Warning($"获取新版本失败: {e.Message}");
  56. else
  57. Logging.Warning(e.ToString());
  58. NewVersionFoundFailed?.Invoke(null, new EventArgs());
  59. }
  60. }
  61. }
  62. }