diff --git a/Sheas-Cealer.csproj b/Sheas-Cealer.csproj index 72e4a73..94b8e37 100644 --- a/Sheas-Cealer.csproj +++ b/Sheas-Cealer.csproj @@ -77,6 +77,7 @@ + diff --git a/Utils/AutoCheckUpdate.cs b/Utils/AutoCheckUpdate.cs new file mode 100644 index 0000000..6855871 --- /dev/null +++ b/Utils/AutoCheckUpdate.cs @@ -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 { + /// + /// 对话框标题 + /// + const string msgboxTitle = "更新检查"; + readonly UpdateFromGithub ufg = new() { + RepositoryURL = "https://github.com/SpaceTimee/Sheas-Cealer", + ProgramVersion= Assembly.GetExecutingAssembly().GetName().Version!.ToString(), + EasySetCacheDir= "SheasCealer", + }; + /// + /// 表示当前是否正在执行程序更新函数,如果正在执行,则为true,阻止多次调用函数 + /// + bool isCheckingUpdate=false; + /// + /// 程序更新函数 + /// + /// 是否是自动检查更新,如果不是,则在检查完后反馈 + 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; + } + } + } +} diff --git a/Wins/MainWin.xaml.cs b/Wins/MainWin.xaml.cs index 0d181b2..01d458e 100644 --- a/Wins/MainWin.xaml.cs +++ b/Wins/MainWin.xaml.cs @@ -82,6 +82,10 @@ public partial class MainWin : Window MihomoConfWatcher_Changed(null!, null!); }); + //这是一个检查更新的示例,后续可以将其放置在检查更新按钮的点击事件内,或者放在窗口加载事件内 + AutoCheckUpdate acu = new(); + //当isAuto参数为false时,检查完更新后会进行一个反馈弹窗,为true则不会 + acu.ProgramUpdateAsync(false); } private void MainWin_Closing(object sender, CancelEventArgs e) => Application.Current.Shutdown();