From f76c877882f6325ce8662c746c7f30caa81283cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Wed, 5 Feb 2025 22:23:31 +0800 Subject: [PATCH] support only local script repo --- .../Core/Script/ScriptRepoUpdater.cs | 28 ++++++++++++- .../Core/Script/WebView/RepoWebBridge.cs | 40 ++++++++----------- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs b/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs index ba47d8a5..8b697c7e 100644 --- a/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs +++ b/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs @@ -94,7 +94,7 @@ public class ScriptRepoUpdater : Singleton var jsonString = res.Item2; if (string.IsNullOrEmpty(jsonString)) { - throw new Exception("获取仓库信息失败"); + throw new Exception("从互联网下载最新的仓库信息失败"); } var (time, url, file) = ParseJson(jsonString); @@ -323,7 +323,31 @@ public class ScriptRepoUpdater : Singleton Toast.Information("获取最新仓库信息中..."); // 更新仓库 - var (repoPath, _) = await Task.Run(UpdateCenterRepo); + string repoPath; + try + { + (repoPath, _) = await Task.Run(UpdateCenterRepo); + } + catch (Exception e) + { + Toast.Warning("获取最新仓库信息失败,尝试使用本地已有仓库信息"); + try + { + repoPath = FindCenterRepoPath(); + } + catch + { + await MessageBox.ErrorAsync("本地无仓库信息,请至少成功更新一次脚本仓库信息!"); + return; + } + } + + if (string.IsNullOrEmpty(repoPath)) + { + await MessageBox.ErrorAsync("本地无仓库信息,请至少成功更新一次脚本仓库信息!"); + return; + } + // // 收集将被覆盖的文件和文件夹 // var filesToOverwrite = new List(); diff --git a/BetterGenshinImpact/Core/Script/WebView/RepoWebBridge.cs b/BetterGenshinImpact/Core/Script/WebView/RepoWebBridge.cs index 4849e4a8..1c211e6c 100644 --- a/BetterGenshinImpact/Core/Script/WebView/RepoWebBridge.cs +++ b/BetterGenshinImpact/Core/Script/WebView/RepoWebBridge.cs @@ -6,6 +6,7 @@ using System.Security.Policy; using System.Threading.Tasks; using BetterGenshinImpact.ViewModel.Message; using CommunityToolkit.Mvvm.Messaging; +using Wpf.Ui.Violeta.Controls; namespace BetterGenshinImpact.Core.Script.WebView; @@ -21,39 +22,32 @@ public class RepoWebBridge { try { - // var needUpdate = false; - // string? localRepoJsonPath = null; - // if (Directory.Exists(ScriptRepoUpdater.CenterRepoPath)) - // { - // localRepoJsonPath = Directory.GetFiles(ScriptRepoUpdater.CenterRepoPath, "repo.json", SearchOption.AllDirectories).FirstOrDefault(); - // if (localRepoJsonPath is null) - // { - // needUpdate = true; - // } - // } - // else - // { - // needUpdate = true; - // } - // - // if (needUpdate) - // { - // await ScriptRepoUpdater.Instance.UpdateCenterRepo(); - // } - await ScriptRepoUpdater.Instance.UpdateCenterRepo(); + } + catch (Exception e) + { + Toast.Warning($"更新仓库信息失败:{e.Message}"); + } + + try + { + if (!Directory.Exists(ScriptRepoUpdater.CenterRepoPath)) + { + throw new Exception("仓库文件夹不存在,请至少成功更新一次仓库!"); + } var localRepoJsonPath = Directory.GetFiles(ScriptRepoUpdater.CenterRepoPath, "repo.json", SearchOption.AllDirectories).FirstOrDefault(); if (localRepoJsonPath is null) { - throw new Exception("repo.json 不存在,可能是更新仓库信息失败!"); + throw new Exception("repo.json 仓库索引文件不存在,请至少成功更新一次仓库!"); } + var json = await File.ReadAllTextAsync(localRepoJsonPath); return json; } catch (Exception e) { - await MessageBox.ShowAsync(e.Message, "获取仓库信息失败!"); + await MessageBox.ShowAsync(e.Message, "获取仓库信息失败"); return ""; } } @@ -70,4 +64,4 @@ public class RepoWebBridge await MessageBox.ShowAsync(e.Message, "订阅脚本链接失败!"); } } -} +} \ No newline at end of file