From 9c196d080ac563ebf30da487f4388bd98110587f Mon Sep 17 00:00:00 2001 From: uye <99072975+ABA2396@users.noreply.github.com> Date: Sat, 1 Feb 2025 15:26:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/Models/ResourceUpdater.cs | 75 +++++++++---------- .../Res/Styles/Basic/Geometries.xaml | 3 + .../ViewModels/UI/VersionUpdateViewModel.cs | 33 ++++++-- 3 files changed, 67 insertions(+), 44 deletions(-) diff --git a/src/MaaWpfGui/Models/ResourceUpdater.cs b/src/MaaWpfGui/Models/ResourceUpdater.cs index b850f5c2d3..611c036c74 100644 --- a/src/MaaWpfGui/Models/ResourceUpdater.cs +++ b/src/MaaWpfGui/Models/ResourceUpdater.cs @@ -492,61 +492,52 @@ namespace MaaWpfGui.Models } // 从 MirrorChyan 下载完整包 - public static async Task UpdateFromMirrorChyanAsync() + public static async Task<(bool HaveUpdate, string? UpdateUrl)> CheckFromMirrorChyanAsync() { // https://mirrorc.top/api/resources/MaaResource/latest?current_version=<当前版本日期,从 version.json 里拿时间戳>&cdk=&sp_id=<唯一识别码> // 响应格式为 {"code":0,"msg":"success","data":{"version_name":"2025-01-22 14:28:32.839","version_number":9,"url":"<增量更新网址>"}} - var versionFilePath = Path.Combine(Environment.CurrentDirectory, "MirrorChyanResourceVersion"); - string? currentVersion = null; - if (File.Exists(versionFilePath)) - { - try - { - currentVersion = await File.ReadAllTextAsync(versionFilePath); - currentVersion = DateTime.Parse(currentVersion).ToString("yyyy-MM-dd+HH:mm:ss"); - } - catch (Exception e) - { - _logger.Error("Failed to read or parse version file: " + e.Message); - currentVersion = null; - } - } - else - { - _logger.Information("Version file not found, assuming first time download."); - } - + var currentVersionDateTime = VersionUpdateSettingsUserControlModel + .GetResourceVersionByClientType(SettingsViewModel.GameSettings.ClientType) + .DateTime; + var currentVersion = currentVersionDateTime.ToString("yyyy-MM-dd+HH:mm:ss.fff"); var cdk = SettingsViewModel.VersionUpdateSettings.MirrorChyanCdk; - var url = currentVersion == null - ? $"{MaaUrls.MirrorChyanResourceUpdate}?cdk={cdk}" - : $"{MaaUrls.MirrorChyanResourceUpdate}?current_version={currentVersion}&cdk={cdk}"; + + var url = $"{MaaUrls.MirrorChyanResourceUpdate}?current_version={currentVersion}&cdk={cdk}"; var response = await Instances.HttpService.GetAsync(new(url), logUri: false); if (response is null) { ToastNotification.ShowDirect(LocalizationHelper.GetString("GameResourceFailed")); - return false; + return (false, null); } var json = await response.Content.ReadAsStringAsync(); var data = (JObject?)JsonConvert.DeserializeObject(json); + if (data is null) { - ToastNotification.ShowDirect(LocalizationHelper.GetString("GameResourceFailed")); - return false; + return (false, null); } - var code = data["code"]?.ToString(); - if (code is not null && code != "0") + if (data["code"]?.ToString() != "0") { - ToastNotification.ShowDirect(data["msg"]?.ToString()); - return false; + ToastNotification.ShowDirect(data["msg"]?.ToString() ?? LocalizationHelper.GetString("GameResourceFailed")); + return (false, null); } - url = data["data"]?["url"]?.ToString(); + if (DateTime.TryParse(data["data"]?["version_name"]?.ToString(), out var version) && + DateTime.Compare(currentVersionDateTime, version) <= 0) + { + return (false, null); + } + + return (true, data["data"]?["url"]?.ToString()); + } + + public static async Task DownloadFromMirrorChyanAsync(string? url) + { if (string.IsNullOrEmpty(url)) { - ToastNotification.ShowDirect(LocalizationHelper.GetString("AlreadyLatest")); return false; } @@ -558,7 +549,6 @@ namespace MaaWpfGui.Models return false; } - // 解压到 MaaResource 文件夹 try { if (Directory.Exists("MaaResource")) @@ -575,12 +565,11 @@ namespace MaaWpfGui.Models return false; } - // 没有 cache 文件夹,直接复制 resource 文件夹复制到当前目录 try { - string sourcePath = new("MaaResource"); + const string SourcePath = "MaaResource"; string destinationPath = Directory.GetCurrentDirectory(); - DirectoryMerge(sourcePath, destinationPath); + DirectoryMerge(SourcePath, destinationPath); } catch (Exception e) { @@ -589,7 +578,6 @@ namespace MaaWpfGui.Models return false; } - // 删除 MaaResource 文件夹 和 MaaResource.zip try { Directory.Delete("MaaResource", true); @@ -603,6 +591,17 @@ namespace MaaWpfGui.Models return true; } + public static async Task UpdateFromMirrorChyanAsync() + { + var (haveUpdate, uri) = await CheckFromMirrorChyanAsync(); + if (!haveUpdate) + { + return false; + } + + return await DownloadFromMirrorChyanAsync(uri); + } + private static async Task DownloadFullPackageAsync(string url, string saveTo) { using var response = await Instances.HttpService.GetAsync( diff --git a/src/MaaWpfGui/Res/Styles/Basic/Geometries.xaml b/src/MaaWpfGui/Res/Styles/Basic/Geometries.xaml index 6df0e5eb0f..65def62673 100644 --- a/src/MaaWpfGui/Res/Styles/Basic/Geometries.xaml +++ b/src/MaaWpfGui/Res/Styles/Basic/Geometries.xaml @@ -15,6 +15,9 @@ M4.5 3A2.5 2.5 0 0 0 2 5.5v9A2.5 2.5 0 0 0 4.5 17h5.1a5.465 5.465 0 0 1-.393-1H4.5A1.5 1.5 0 0 1 3 14.5V8h4.086a1.5 1.5 0 0 0 1.06-.44L9.707 6H15.5A1.5 1.5 0 0 1 17 7.5v2.1c.358.183.693.404 1 .657V7.5A2.5 2.5 0 0 0 15.5 5H9.707L8.22 3.513A1.75 1.75 0 0 0 6.982 3H4.5ZM3 5.5A1.5 1.5 0 0 1 4.5 4h2.482a.75.75 0 0 1 .53.22l1.28 1.28L7.44 6.854A.5.5 0 0 1 7.086 7H3V5.5Zm16 9a4.5 4.5 0 1 1-9 0a4.5 4.5 0 0 1 9 0Zm-4-2a.5.5 0 0 0-1 0V14h-1.5a.5.5 0 0 0 0 1H14v1.5a.5.5 0 0 0 1 0V15h1.5a.5.5 0 0 0 0-1H15v-1.5Z + + M22 13v3c0 1.1-.9 2-2 2h-3l1 1v2H6v-2l1-1H4c-1.1 0-2-.9-2-2V5c0-1.1.9-2 2-2h8v2H4v11h16v-3h2zm-7 2l-5-5h4V3h2v7h4l-5 5z + diff --git a/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs b/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs index 9ebad0d576..6740656ac5 100644 --- a/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs @@ -23,6 +23,8 @@ using System.Runtime.InteropServices; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; +using HandyControl.Controls; +using HandyControl.Data; using MaaWpfGui.Constants; using MaaWpfGui.Helper; using MaaWpfGui.Main; @@ -440,6 +442,7 @@ public class VersionUpdateViewModel : Screen } else { + var ret = await CheckAndDownloadUpdate(); // 跑个空任务避免 async warning await Task.Run(() => { }); } @@ -467,16 +470,34 @@ public class VersionUpdateViewModel : Screen */ // 可以用 MirrorChyan 资源更新了喵 + var (haveUpdate, uri) = await ResourceUpdater.CheckFromMirrorChyanAsync(); + if (!haveUpdate) + { + SettingsViewModel.VersionUpdateSettings.IsCheckingForUpdates = false; + return ret; + } + switch (SettingsViewModel.VersionUpdateSettings.ResourceUpdateSource) { - case "MirrorChyan": - if (!string.IsNullOrEmpty(SettingsViewModel.VersionUpdateSettings.MirrorChyanCdk)) + case "Github": + _ = Execute.OnUIThreadAsync(() => { - if (await ResourceUpdater.UpdateFromMirrorChyanAsync()) + var growlInfo = new GrowlInfo { - SettingsViewModel.VersionUpdateSettings.IsCheckingForUpdates = false; - return CheckUpdateRetT.OnlyGameResourceUpdated; - } + IsCustom = true, + Message = LocalizationHelper.GetString("Mirror 酱检查到资源版本更新,但未填写 cdk,可前往「设置-软件更新」使用 github 源进行版本更新"), + IconKey = "BrowserUpdatedFilled", + IconBrushKey = "PallasBrush", + WaitTime = 10, + }; + Growl.Info(growlInfo); + }); + break; + case "MirrorChyan": + if (await ResourceUpdater.DownloadFromMirrorChyanAsync(uri)) + { + SettingsViewModel.VersionUpdateSettings.IsCheckingForUpdates = false; + return CheckUpdateRetT.OnlyGameResourceUpdated; } break;