From 021d1f937a2c497b3d4ed7fcf90bfe7fc886a6ec Mon Sep 17 00:00:00 2001 From: Takaranoao Date: Sat, 12 Jul 2025 01:32:28 -0500 Subject: [PATCH] =?UTF-8?q?=E9=80=89=E4=B8=AD=E7=9A=84=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=B8=A0=E9=81=93URL=E5=86=99=E5=85=A5=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=B9=B6=E5=85=81=E8=AE=B8=E4=BB=8E=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89URL=E6=9B=B4=E6=96=B0=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E4=BB=93=E5=BA=93=20(#1864)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 辉鸭蛋 --- .../Core/Config/ScriptConfig.cs | 3 + .../Core/Script/ScriptRepoUpdater.cs | 13 +- .../View/Windows/ScriptRepoWindow.xaml | 115 +++++++++-------- .../View/Windows/ScriptRepoWindow.xaml.cs | 122 +++++++++++++++--- 4 files changed, 176 insertions(+), 77 deletions(-) diff --git a/BetterGenshinImpact/Core/Config/ScriptConfig.cs b/BetterGenshinImpact/Core/Config/ScriptConfig.cs index 59daf5d4..0f6aaead 100644 --- a/BetterGenshinImpact/Core/Config/ScriptConfig.cs +++ b/BetterGenshinImpact/Core/Config/ScriptConfig.cs @@ -25,4 +25,7 @@ public partial class ScriptConfig : ObservableObject // 已订阅的脚本路径列表 [ObservableProperty] private List _subscribedScriptPaths = []; + + // 选择的更新渠道URL + [ObservableProperty] private string _selectedRepoUrl = ""; } diff --git a/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs b/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs index 0b50c46c..e9dad71b 100644 --- a/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs +++ b/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs @@ -117,7 +117,7 @@ public class ScriptRepoUpdater : Singleton // 如果仓库不存在,执行浅克隆操作 _logger.LogInformation($"浅克隆仓库: {repoUrl} 到 {repoPath}"); - SimpleCloneRepository(repoUrl, repoPath, onCheckoutProgress); + CloneRepository(repoUrl, repoPath, onCheckoutProgress); // CloneRepository(repoUrl, repoPath); updated = true; @@ -211,8 +211,9 @@ public class ScriptRepoUpdater : Singleton /// /// /// + /// /// - private void CloneRepository(string repoUrl, string repoPath) + private void CloneRepository(string repoUrl, string repoPath, CheckoutProgressHandler? onCheckoutProgress) { // 1. 创建目录 Directory.CreateDirectory(repoPath); @@ -229,9 +230,9 @@ public class ScriptRepoUpdater : Singleton // 4. 获取数据(使用浅克隆选项) var fetchOptions = new FetchOptions { - Depth = 1, // 浅克隆,只获取最新的提交 TagFetchMode = TagFetchMode.None // 不获取标签 }; + fetchOptions.ProxyOptions.ProxyType = ProxyType.None; // 5. 执行获取操作 Commands.Fetch(repo, remote.Name, ["+refs/heads/*:refs/remotes/origin/*"], fetchOptions, "初始化拉取"); @@ -250,7 +251,11 @@ public class ScriptRepoUpdater : Singleton repo.Branches.Update(localBranch, b => b.TrackedBranch = remoteBranch.CanonicalName); // 9. 检出分支 - Commands.Checkout(repo, localBranch); + CheckoutOptions checkoutOptions = new CheckoutOptions + { + OnCheckoutProgress = onCheckoutProgress + }; + Commands.Checkout(repo, localBranch, checkoutOptions); } private void GitConfig(Repository repo) diff --git a/BetterGenshinImpact/View/Windows/ScriptRepoWindow.xaml b/BetterGenshinImpact/View/Windows/ScriptRepoWindow.xaml index d24b8caa..59d4248c 100644 --- a/BetterGenshinImpact/View/Windows/ScriptRepoWindow.xaml +++ b/BetterGenshinImpact/View/Windows/ScriptRepoWindow.xaml @@ -6,15 +6,14 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" xmlns:vio="http://schemas.lepo.co/wpfui/2022/xaml/violeta" - xmlns:emoji="clr-namespace:Emoji.Wpf;assembly=Emoji.Wpf" Title="脚本仓库" - Width = "350" + Width="350" MinWidth="350" MinHeight="50" - SizeToContent="Height" Background="#202020" ExtendsContentIntoTitleBar="True" FontFamily="{DynamicResource TextThemeFontFamily}" + SizeToContent="Height" WindowBackdropType="None" WindowStartupLocation="CenterOwner" mc:Ignorable="d"> @@ -28,58 +27,70 @@ CornerRadius="8"> - - - + + + + - - - + - - - - - - - - - - - - - - - + - + Foreground="{ui:ThemeResource TextFillColorPrimaryBrush}" + Text="更新渠道:" /> + + + + + + + + + + + + + + + + + + + + diff --git a/BetterGenshinImpact/View/Windows/ScriptRepoWindow.xaml.cs b/BetterGenshinImpact/View/Windows/ScriptRepoWindow.xaml.cs index c153148c..9ea9b7b2 100644 --- a/BetterGenshinImpact/View/Windows/ScriptRepoWindow.xaml.cs +++ b/BetterGenshinImpact/View/Windows/ScriptRepoWindow.xaml.cs @@ -1,7 +1,8 @@ using System; using System.Collections.ObjectModel; -using System.Diagnostics; +using System.ComponentModel; using System.IO; +using System.Linq; using System.Threading.Tasks; using System.Windows; using CommunityToolkit.Mvvm.ComponentModel; @@ -35,35 +36,110 @@ public partial class ScriptRepoWindow public ObservableCollection RepoChannels => _repoChannels; // 选中的渠道 - [ObservableProperty] private RepoChannel _selectedRepoChannel; - + [ObservableProperty] private RepoChannel? _selectedRepoChannel; + + // 控制仓库地址是否只读 + [ObservableProperty] private bool _isRepoUrlReadOnly = true; + // 添加进度相关的可观察属性 [ObservableProperty] private bool _isUpdating; [ObservableProperty] private int _updateProgressValue; [ObservableProperty] private string _updateProgressText = "准备更新..."; + [ObservableProperty] private ScriptConfig _config = TaskContext.Instance().Config.ScriptConfig; public ScriptRepoWindow() { InitializeRepoChannels(); InitializeComponent(); DataContext = this; + Config.PropertyChanged += OnConfigPropertyChanged; + PropertyChanged += OnPropertyChanged; + } + + private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) + { + //OnSelectedRepoChannelChanged + if (e.PropertyName == nameof(SelectedRepoChannel)) + { + OnSelectedRepoChannelChanged(); + } + } + + ~ScriptRepoWindow() + { + Config.PropertyChanged -= OnConfigPropertyChanged; + PropertyChanged -= OnPropertyChanged; + } + + private void OnConfigPropertyChanged(object? sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == nameof(ScriptConfig.SelectedRepoUrl)) + { + OnConfigSelectedRepoUrlChanged(); + } } private void InitializeRepoChannels() { _repoChannels = new ObservableCollection { - new RepoChannel("CNB", "https://cnb.cool/bettergi/bettergi-scripts-list"), - new RepoChannel("GitCode", "https://gitcode.com/huiyadanli/bettergi-scripts-list"), - new RepoChannel("Gitee", "https://gitee.com/babalae/bettergi-scripts-list"), - new RepoChannel("GitHub", "https://github.com/babalae/bettergi-scripts-list"), + new("CNB", "https://cnb.cool/bettergi/bettergi-scripts-list"), + new("GitCode", "https://gitcode.com/huiyadanli/bettergi-scripts-list"), + new("Gitee", "https://gitee.com/babalae/bettergi-scripts-list"), + new("GitHub", "https://github.com/babalae/bettergi-scripts-list"), + new("自定义", "https://example.com/custom-repo") }; - SelectedRepoChannel = _repoChannels[0]; + + if (string.IsNullOrEmpty(Config.SelectedRepoUrl)) + { + // 默认选中第一个渠道 + SelectedRepoChannel = _repoChannels[0]; + Config.SelectedRepoUrl = SelectedRepoChannel.Url; + } + else + { + // 尝试根据配置中的URL找到对应的渠道 + OnConfigSelectedRepoUrlChanged(); + } + } + + // Config.SelectedRepoUrl 变化 + private void OnConfigSelectedRepoUrlChanged() + { + // 如果配置中的URL与当前选中渠道不一致,更新选中渠道 + if (string.IsNullOrEmpty(SelectedRepoChannel?.Url) || SelectedRepoChannel.Url != Config.SelectedRepoUrl) + { + SelectedRepoChannel = _repoChannels.FirstOrDefault(c => c.Url == Config.SelectedRepoUrl) ?? + _repoChannels.FirstOrDefault(c => c.Name == "自定义") ?? _repoChannels[0]; + } + } + + private void OnSelectedRepoChannelChanged() + { + if (SelectedRepoChannel is null) + { + return; + } + + // 更新仓库地址只读状态 + IsRepoUrlReadOnly = SelectedRepoChannel.Name != "自定义"; + + // 更新配置中的选中仓库URL + if (SelectedRepoChannel.Name != "自定义") + { + // 如果不是自定义渠道,直接使用选中渠道的URL + Config.SelectedRepoUrl = SelectedRepoChannel.Url; + } } [RelayCommand] private async Task UpdateRepo() { + if (SelectedRepoChannel is null) + { + Toast.Warning("请选择一个脚本仓库更新渠道。"); + return; + } try { // 使用选定渠道的URL进行更新 @@ -76,18 +152,17 @@ public partial class ScriptRepoWindow IsUpdating = true; UpdateProgressValue = 0; UpdateProgressText = "准备更新..."; - // 执行更新 - var (repoPath, updated) = await ScriptRepoUpdater.Instance.UpdateCenterRepoByGit(repoUrl, (path, steps, totalSteps) => - { - // 更新进度显示 - double progressPercentage = totalSteps > 0 ? Math.Min(100, (double)steps / totalSteps * 100) : 0; - UpdateProgressValue = (int)progressPercentage; - UpdateProgressText = $"{path}"; - }); + // 执行更新 (repoPath, updated) + var (_, updated) = await ScriptRepoUpdater.Instance.UpdateCenterRepoByGit(repoUrl, + (path, steps, totalSteps) => + { + // 更新进度显示 + double progressPercentage = totalSteps > 0 ? Math.Min(100, (double)steps / totalSteps * 100) : 0; + UpdateProgressValue = (int)progressPercentage; + UpdateProgressText = $"{path}"; + }); + - // 隐藏进度条 - IsUpdating = false; - // 更新结果提示 if (updated) { @@ -102,6 +177,11 @@ public partial class ScriptRepoWindow { await MessageBox.ErrorAsync($"更新失败,可尝试重置仓库后重新更新。失败原因:: {ex.Message}"); } + finally + { + // 隐藏进度条 + IsUpdating = false; + } } [RelayCommand] @@ -120,14 +200,14 @@ public partial class ScriptRepoWindow Toast.Warning("请等待当前更新完成后再进行重置操作。"); return; } - + // 添加确认对话框 var result = await MessageBox.ShowAsync( "确定要重置脚本仓库吗?无法正常更新时候可以使用本功能,重置后请重新更新脚本仓库。", "确认重置", MessageBoxButton.YesNo, MessageBoxImage.Warning); - + if (result == MessageBoxResult.Yes) { try