using ApplicationUpgradeManager.Core.Model;
using System.Diagnostics;
using System.Reflection;
using XFEExtension.NetCore.UpgradeHelper.Utilities;
namespace HaloPixelToolBox.Client.Utilities.Helpers;
public static class UpgradeHelper
{
public static string RequestAddress => "http://upgrade.api.xfe.studio/upgrade";
public static Upgrader Upgrader { get; set; } = new(RequestAddress);
public static Version Version => Assembly.GetExecutingAssembly().GetName().Version ?? new Version(1, 0, 0);
///
/// 检测是否需要更新
///
///
public static async Task CheckUpgrade()
{
try
{
return (await Upgrader.GetReleaseNotes("HaloPixelToolBox.Client", Version.ToString())).IsLatest;
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR]检查更新时发生错误:{ex.Message}");
// 发生错误时默认返回 true(表示已是最新版本),避免影响用户使用
return true;
}
}
///
/// 获取更新信息
///
///
public static async Task GetReleaseNotes()
{
try
{
return await Upgrader.GetReleaseNotes("HaloPixelToolBox.Client", Version.ToString());
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR]获取更新信息时发生错误:{ex.Message}");
// 返回 null 表示获取失败
return null;
}
}
///
/// 开始更新
///
public static void StartUpdate(string downloadUrl)
{
var startInfo = new ProcessStartInfo("Installer.exe")
{
UseShellExecute = true,
Verb = "runas"
};
startInfo.ArgumentList.Add("Upgrade");
startInfo.ArgumentList.Add(downloadUrl);
startInfo.ArgumentList.Add("");
Process.Start(startInfo);
Process.GetCurrentProcess().Kill();
Application.Current.Exit();
}
}