新增自动检查更新功能

This commit is contained in:
Hgnim 2024-12-16 17:25:37 +08:00
parent 810ff86999
commit bd74118489
3 changed files with 84 additions and 0 deletions

View File

@ -77,6 +77,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="*" /> <PackageReference Include="CommunityToolkit.Mvvm" Version="*" />
<PackageReference Include="EasyUpdateFromGithub" Version="1.3.3.20241216" />
<PackageReference Include="MaterialDesignThemes" Version="*" /> <PackageReference Include="MaterialDesignThemes" Version="*" />
<PackageReference Include="NginxConfigParser" Version="*" /> <PackageReference Include="NginxConfigParser" Version="*" />
<PackageReference Include="YamlDotNet" Version="*" /> <PackageReference Include="YamlDotNet" Version="*" />

79
Utils/AutoCheckUpdate.cs Normal file
View File

@ -0,0 +1,79 @@
using EasyUpdateFromGithub;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using YamlDotNet.Core;
using MessageBox = System.Windows.MessageBox;
namespace Sheas_Cealer.Utils {
internal class AutoCheckUpdate:Window {
/// <summary>
/// 对话框标题
/// </summary>
const string msgboxTitle = "更新检查";
readonly UpdateFromGithub ufg = new() {
RepositoryURL = "https://github.com/SpaceTimee/Sheas-Cealer",
ProgramVersion= Assembly.GetExecutingAssembly().GetName().Version!.ToString(),
EasySetCacheDir= "SheasCealer",
};
/// <summary>
/// 表示当前是否正在执行程序更新函数如果正在执行则为true阻止多次调用函数
/// </summary>
bool isCheckingUpdate=false;
/// <summary>
/// 程序更新函数
/// </summary>
/// <param name="isAuto">是否是自动检查更新,如果不是,则在检查完后反馈</param>
internal async void ProgramUpdateAsync(bool isAuto = false) {
if (!isCheckingUpdate) {
isCheckingUpdate = true;
try {
UpdateFromGithub.CheckUpdateValue cuv = await ufg.CheckUpdateAsync();
//获取Release页面latest版本中文件名符合正则表达式的文件
UpdateFromGithub.InfoOfDownloadFile iodf=await ufg.GetDownloadFileInfoAsync(fileRegex:new(@"Sheas-Cealer-Zip-.+"));
if (cuv.HaveUpdate) {
switch (MessageBox.Show(
@$"检查到可用的更新,是否进行更新?
: V{ufg.ProgramVersion}
: {cuv.LatestVersionStr}
: {cuv.PublishedTime_Local}
: {iodf.Size}"
, msgboxTitle, MessageBoxButton.YesNo, MessageBoxImage.Information)) {
case MessageBoxResult.Yes:
static void errorMsg() {
MessageBox.Show("下载更新失败!", msgboxTitle, MessageBoxButton.OK, MessageBoxImage.Error);
}
try {
//下载目标文件
UpdateFromGithub.InfoOfInstall? ioi = await ufg.DownloadReleaseAsync(iodf);
if (ioi != null) {
if (MessageBox.Show("最新版本下载完毕,是否执行安装?", msgboxTitle, MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes) {
ufg.InstallFile(ioi, waitTime: 900);
this.Close();//执行安装时需要退出主程序
}
}
else
errorMsg();
} catch { errorMsg(); }
break;
default:
break;
}
}
else if (!isAuto)
MessageBox.Show("当前已是最新版本", msgboxTitle, MessageBoxButton.OK, MessageBoxImage.Information);
} catch {
if (!isAuto)
MessageBox.Show("更新检查失败!", msgboxTitle, MessageBoxButton.OK, MessageBoxImage.Error);
}
isCheckingUpdate = false;
}
}
}
}

View File

@ -82,6 +82,10 @@ public partial class MainWin : Window
MihomoConfWatcher_Changed(null!, null!); MihomoConfWatcher_Changed(null!, null!);
}); });
//这是一个检查更新的示例,后续可以将其放置在检查更新按钮的点击事件内,或者放在窗口加载事件内
AutoCheckUpdate acu = new();
//当isAuto参数为false时检查完更新后会进行一个反馈弹窗为true则不会
acu.ProgramUpdateAsync(false);
} }
private void MainWin_Closing(object sender, CancelEventArgs e) => Application.Current.Shutdown(); private void MainWin_Closing(object sender, CancelEventArgs e) => Application.Current.Shutdown();