feat: 添加资源更新提示

This commit is contained in:
uye
2025-02-01 15:26:04 +08:00
parent d7deb04c74
commit 9c196d080a
3 changed files with 67 additions and 44 deletions

View File

@@ -492,61 +492,52 @@ namespace MaaWpfGui.Models
}
// 从 MirrorChyan 下载完整包
public static async Task<bool> UpdateFromMirrorChyanAsync()
public static async Task<(bool HaveUpdate, string? UpdateUrl)> CheckFromMirrorChyanAsync()
{
// https://mirrorc.top/api/resources/MaaResource/latest?current_version=<当前版本日期,从 version.json 里拿时间戳>&cdk=<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<bool> 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<bool> UpdateFromMirrorChyanAsync()
{
var (haveUpdate, uri) = await CheckFromMirrorChyanAsync();
if (!haveUpdate)
{
return false;
}
return await DownloadFromMirrorChyanAsync(uri);
}
private static async Task<bool> DownloadFullPackageAsync(string url, string saveTo)
{
using var response = await Instances.HttpService.GetAsync(

View File

@@ -15,6 +15,9 @@
<Geometry x:Key="FolderAdd20Regular">
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
</Geometry>
<Geometry x:Key="BrowserUpdatedFilled">
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
</Geometry>
<!-- 牛牛喝酒 -->
<Geometry x:Key="HangoverGeometry">

View File

@@ -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;