UpdateService.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Reflection;
  2. using Serilog;
  3. namespace YZWater.Core.Services;
  4. /// <summary>
  5. /// 在线升级服务(借鉴 AutoFMA UpdateService,委托注入模式)
  6. ///
  7. /// 设计原则:
  8. /// 1. Core 层只负责版本管理和更新触发,不依赖具体 UI 框架
  9. /// 2. 实际的更新检查/下载/安装逻辑由 UI 层通过 SetTriggerAction 注入
  10. /// 3. config.json 配置 Update.Enabled / CheckOnStartup / CheckIntervalHours
  11. ///
  12. /// Avalonia 无 AutoUpdater.NET 官方支持,UI 层可接入 Squirrel 或自研下载器后注入。
  13. /// </summary>
  14. public static class UpdateService
  15. {
  16. private static DateTime? _lastCheckTime;
  17. private static Action? _triggerAction;
  18. private static System.Threading.Timer? _checkTimer;
  19. /// <summary>
  20. /// 更新配置
  21. /// </summary>
  22. public class UpdateConfig
  23. {
  24. /// <summary>是否启用在线更新</summary>
  25. public bool Enabled { get; set; }
  26. /// <summary>更新清单地址(appcast URL)</summary>
  27. public string AppcastUrl { get; set; } = string.Empty;
  28. /// <summary>启动时是否检查更新</summary>
  29. public bool CheckOnStartup { get; set; } = true;
  30. /// <summary>检查间隔(小时,默认 24)</summary>
  31. public int CheckIntervalHours { get; set; } = 24;
  32. }
  33. /// <summary>
  34. /// 当前程序版本(Major.Minor.Build)
  35. /// </summary>
  36. public static string GetCurrentVersion()
  37. {
  38. var version = Assembly.GetEntryAssembly()?.GetName().Version;
  39. return version != null ? $"{version.Major}.{version.Minor}.{version.Build}" : "0.0.0";
  40. }
  41. /// <summary>上次检查时间</summary>
  42. public static DateTime? LastCheckTime => _lastCheckTime;
  43. /// <summary>更新触发器是否已注入</summary>
  44. public static bool IsTriggerRegistered => _triggerAction != null;
  45. /// <summary>
  46. /// 注入实际更新触发逻辑(由 UI 层调用,如 Squirrel 下载安装流程)
  47. /// </summary>
  48. public static void SetTriggerAction(Action triggerAction)
  49. {
  50. _triggerAction = triggerAction;
  51. Log.Information("在线更新触发器已注入");
  52. }
  53. /// <summary>
  54. /// 触发在线更新
  55. /// </summary>
  56. public static bool TriggerUpdate()
  57. {
  58. if (_triggerAction == null)
  59. {
  60. Log.Warning("更新触发器未注入,无法执行在线更新");
  61. return false;
  62. }
  63. Log.Information("触发在线更新,当前版本: {Version}", GetCurrentVersion());
  64. _lastCheckTime = DateTime.Now;
  65. try
  66. {
  67. _triggerAction();
  68. return true;
  69. }
  70. catch (Exception ex)
  71. {
  72. Log.Error(ex, "触发在线更新失败");
  73. return false;
  74. }
  75. }
  76. /// <summary>
  77. /// 启动定期检查(启动时检查一次 + 按 CheckIntervalHours 定时)
  78. /// </summary>
  79. public static void StartPeriodicCheck(Action onCheckDue)
  80. {
  81. var config = ConfigService.GetConfig()?.Update;
  82. if (config is not { Enabled: true })
  83. {
  84. Log.Debug("在线更新未启用,跳过定期检查");
  85. return;
  86. }
  87. var interval = TimeSpan.FromHours(Math.Max(1, config.CheckIntervalHours));
  88. // 启动时检查一次
  89. if (config.CheckOnStartup)
  90. {
  91. Task.Run(() =>
  92. {
  93. try
  94. {
  95. _lastCheckTime = DateTime.Now;
  96. onCheckDue();
  97. }
  98. catch (Exception ex)
  99. {
  100. Log.Error(ex, "启动时检查更新失败");
  101. }
  102. });
  103. }
  104. // 定时检查
  105. _checkTimer ??= new System.Threading.Timer(_ =>
  106. {
  107. try
  108. {
  109. _lastCheckTime = DateTime.Now;
  110. onCheckDue();
  111. }
  112. catch (Exception ex)
  113. {
  114. Log.Error(ex, "定时检查更新失败");
  115. }
  116. }, null, interval, interval);
  117. Log.Information("在线更新定期检查已启动,间隔 {Hours} 小时", config.CheckIntervalHours);
  118. }
  119. /// <summary>
  120. /// 停止定期检查
  121. /// </summary>
  122. public static void StopPeriodicCheck()
  123. {
  124. _checkTimer?.Dispose();
  125. _checkTimer = null;
  126. }
  127. }