Files
better-genshin-impact/BetterGenshinImpact/ViewModel/Pages/CommonSettingsPageViewModel.cs
ShadowLemoon f7976b0bbd feat: 根据文件夹名字和内容重合度区分仓库;启动时自动更新仓库和订阅 (#2767)
* feat: 实现启动时自动更新已订阅脚本及多仓库分离存储

- ScriptConfig: 新增 AutoUpdateSubscribedScripts 配置项
- ScriptRepoUpdater: 动态 CenterRepoPath, 按仓库URL分离存储
  - 内容重合度检测(Jaccard系数)判断仓库异同
  - URL→文件夹名持久化映射(repo_folder_mapping.json)
  - repo_updated.json 存放于各自仓库文件夹内
  - AutoUpdateSubscribedScripts 启动时自动更新订阅脚本
  - 静默同步仓库、渠道URL解析、检出更新脚本
- RepoWebBridge: 使用动态路径, 辅助方法改为 internal
- MainWindowViewModel: 启动时调用自动更新

* feat: 基于内容重合度的导入zip仓库

* perf: 合并默认仓库url映射

* perf: 清理兼容字段

* perf: 添加线程锁以避免并发调用

* fix: 缓存FolderMapping、修复重合度异常返回值、目录扫描异常隔离、移除未使用变量

* perf: 优化更新流程

* perf: 内存缓存添加锁

* fix: 修复更新状态逻辑,确保克隆失败时不标记为已更新

* perf: 文件夹映射先写磁盘再写缓存

* refactor: 简化生成唯一文件夹名称的方法,移除不必要的参数

* fix: ResetRepo加写锁并清理URL映射条目

* perf: 优化重合度算法

* docs: 更新注释

* fix: 仅重置实际更新成功的脚本的 hasUpdate 标记

* feat: 手动一键更新按钮

* feat: 订阅路径迁移至独立文件存储并简化更新逻辑

- 订阅数据从 config.json 迁移到 User/subscriptions/{repo}.json 独立文件
- 添加 ReaderWriterLockSlim 保护订阅文件并发读写
- 使用 System.Text.Json + ConfigService.JsonOptions 序列化
- 新增 RepoWebBridge.GetSubscribedScriptPaths() 桥接方法
- 启动时自动从旧 config.json 迁移订阅数据到独立文件
- 合并手动/自动更新为 UpdateAllSubscribedScriptsCore 共用核心
- 移除 hasUpdate 检查,直接全量更新所有订阅脚本
- 移除冗余 logPrefix 参数

* refactor: 简化启动时自动更新调用

- 移除 Task.Run + try-catch 包装,异常处理已内置于方法中
- 直接使用 fire-and-forget 异步调用

* fix: 订阅目录命名改为 PascalCase (Subscriptions)

* refactor: 移除死代码和冗余中间层方法

* fix: ReadSubscriptionFile 异常时记录日志避免订阅数据静默丢失

* fix: 进度条改为Indeterminate模式、异常日志补全、订阅去重、迁移批量写入、锁注释

* refactor: 提取 ReadFolderMappingFromDisk 消除映射方法嵌套 try

* fix: 补全静态方法异常日志、WriteSubscriptionFile异常保护、ManualUpdate注释

* fix: ManualUpdateSubscribedScripts 加 try-catch 兜底并提示用户重置仓库

* fix: Dialog打开时检测后台自动更新状态,自动禁用按钮并显示进度提示

- ScriptRepoUpdater 新增 IsAutoUpdating 标志和 AutoUpdateStateChanged 事件
- ScriptRepoWindow 订阅事件,自动更新期间显示进度条、禁用所有操作按钮并 Toast 提示
- 更新仓库/重置仓库按钮也加上 IsEnabled 绑定 IsUpdating

* fix: 将自动更新调用包裹在 Task.Run 中避免 UI 线程阻塞

AutoUpdateSubscribedScripts 的 await 后续会被 WPF SynchronizationContext
调度回 UI 线程,导致大量 Git checkout 和文件 IO 操作阻塞界面。
用 Task.Run 确保整个流程在线程池执行。

* fix: 进度条分离 IsProgressIndeterminate 属性,按操作类型正确切换确定/不确定模式

* refactor: 消除 pathing 展开重复逻辑、用布尔字段替换字符串比较追踪状态来源、补全锁注释、统一日志方式

* fix: ExpandTopLevelPaths 泛化展开所有 PathMapper 顶层 key 防止误删用户目录,迁移移入锁内

* fix: 命令行启动配置组/一条龙前先等待自动更新订阅脚本完成

* feat: 添加命令行启动时是否先自动更新选项

* fix: 修复按钮位置
2026-02-20 15:09:17 +08:00

459 lines
14 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Windows.System;
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.Core.Recognition;
using BetterGenshinImpact.Core.Recognition.OCR;
using BetterGenshinImpact.Core.Recognition.OCR.Paddle;
using BetterGenshinImpact.Core.Script;
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.GameTask.AutoTrackPath;
using BetterGenshinImpact.GameTask.Common.Element.Assets;
using BetterGenshinImpact.GameTask.LogParse;
using BetterGenshinImpact.Helpers;
using BetterGenshinImpact.Helpers.Http;
using BetterGenshinImpact.Model;
using BetterGenshinImpact.Service.Interface;
using BetterGenshinImpact.Service.Notification;
using BetterGenshinImpact.View.Controls.Webview;
using BetterGenshinImpact.View.Converters;
using BetterGenshinImpact.View.Pages;
using BetterGenshinImpact.View.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Win32;
using Newtonsoft.Json;
using Wpf.Ui;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class CommonSettingsPageViewModel : ViewModel
{
private readonly INavigationService _navigationService;
private readonly NotificationService _notificationService;
private readonly TpConfig _tpConfig = TaskContext.Instance().Config.TpConfig;
private string _selectedArea = string.Empty;
private string _selectedCountry = string.Empty;
[ObservableProperty] private List<string> _adventurersGuildCountry = ["无", "枫丹", "稻妻", "璃月", "蒙德"];
[ObservableProperty] private List<Tuple<TimeSpan, string>> _serverTimeZones =
[
Tuple.Create(TimeSpan.FromHours(8), "其他 UTC+08"),
Tuple.Create(TimeSpan.FromHours(1), "欧服 UTC+01"),
Tuple.Create(TimeSpan.FromHours(-5), "美服 UTC-05")
];
public CommonSettingsPageViewModel(IConfigService configService, INavigationService navigationService,
NotificationService notificationService)
{
Config = configService.Get();
_navigationService = navigationService;
_notificationService = notificationService;
InitializeCountries();
InitializeMiyousheCookie();
// 初始化OCR模型选择
SelectedPaddleOcrModelConfig = Config.OtherConfig.OcrConfig.PaddleOcrModelConfig;
}
public AllConfig Config { get; set; }
public ObservableCollection<string> CountryList { get; } = new();
public ObservableCollection<string> Areas { get; } = new();
public ObservableCollection<string> MapPathingTypes { get; } = ["SIFT", "TemplateMatch"];
[ObservableProperty] private FrozenDictionary<string, string> _languageDict =
new string[] { "zh-Hans", "zh-Hant", "en"}
.ToFrozenDictionary(
c => c,
c =>
{
CultureInfo.CurrentUICulture = new CultureInfo(c);
var stringLocalizer = App.GetService<IStringLocalizer<CultureInfoNameToKVPConverter>>() ??
throw new NullReferenceException();
return stringLocalizer["简体中文"].ToString();
}
);
[RelayCommand]
private async Task OnUpdateUiLanguageAsync()
{
var cultureName = Config.OtherConfig.UiCultureInfoName ?? string.Empty;
if (string.IsNullOrWhiteSpace(cultureName))
{
throw new InvalidOperationException("当前UI语言为空无法更新语言文件。");
}
if (cultureName == "zh-Hans")
{
await ThemedMessageBox.InformationAsync("zh-Hans 无语言文件,无需更新。");
return;
}
var urls = new[]
{
$"https://raw.githubusercontent.com/babalae/bettergi-i18n/refs/heads/main/i18n/{cultureName}.json",
$"https://cnb.cool/bettergi/bettergi-i18n/-/git/raw/main/i18n/{cultureName}.json"
};
using var httpClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(30)
};
byte[]? bytes = null;
Exception? lastError = null;
var allNotFound = true;
foreach (var url in urls)
{
try
{
using var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.UserAgent.ParseAdd("BetterGenshinImpact");
using var response = await httpClient.SendAsync(request);
if (response.StatusCode == HttpStatusCode.NotFound)
{
lastError = new HttpRequestException("Language file not found.", null, response.StatusCode);
continue;
}
allNotFound = false;
response.EnsureSuccessStatusCode();
bytes = await response.Content.ReadAsByteArrayAsync();
var json = Encoding.UTF8.GetString(bytes);
_ = JsonConvert.DeserializeObject<Dictionary<string, string>>(json)
?? throw new JsonException("翻译文件不是有效的 JSON 字典。");
break;
}
catch (Exception e)
{
lastError = e;
allNotFound = false;
}
}
if (bytes == null)
{
if (allNotFound)
{
await ThemedMessageBox.WarningAsync($"语言文件不存在:{cultureName}.json");
return;
}
throw new Exception($"下载语言文件失败:{cultureName}.json", lastError);
}
var dir = Global.Absolute(@"User\I18n");
Directory.CreateDirectory(dir);
var path = Path.Combine(dir, $"{cultureName}.json");
var tmp = $"{path}.{Guid.NewGuid():N}.tmp";
await File.WriteAllBytesAsync(tmp, bytes);
if (File.Exists(path))
{
File.Replace(tmp, path, null);
}
else
{
File.Move(tmp, path);
}
var translator = App.GetService<ITranslationService>() ?? throw new NullReferenceException();
translator.Reload();
}
public string SelectedCountry
{
get => _selectedCountry;
set
{
if (SetProperty(ref _selectedCountry, value))
{
UpdateAreas(value);
SelectedArea = Areas.FirstOrDefault() ?? string.Empty;
}
}
}
public string SelectedArea
{
get => _selectedArea;
set
{
if (SetProperty(ref _selectedArea, value))
{
UpdateRevivePoint(SelectedCountry, SelectedArea);
}
}
}
public ObservableCollection<PaddleOcrModelConfig> PaddleOcrModelConfigs { get; } =
new(Enum.GetValues(typeof(PaddleOcrModelConfig)).Cast<PaddleOcrModelConfig>());
[ObservableProperty] private PaddleOcrModelConfig _selectedPaddleOcrModelConfig;
[RelayCommand]
public void OnQuestionButtonOnClick()
{
// Owner = this,
WebpageWindow cookieWin = new()
{
Title = "日志分析",
Width = 800,
Height = 600,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
cookieWin.NavigateToHtml(TravelsDiaryDetailManager.generHtmlMessage());
cookieWin.Show();
}
private void InitializeMiyousheCookie()
{
OtherConfig.Miyoushe mcfg = TaskContext.Instance().Config.OtherConfig.MiyousheConfig;
if (mcfg.Cookie == string.Empty &&
mcfg.LogSyncCookie)
{
var config = LogParse.LoadConfig();
mcfg.Cookie = config.Cookie;
}
}
private void InitializeCountries()
{
var countries = MapLazyAssets.Instance.GoddessPositions.Values
.OrderBy(g => int.TryParse(g.Id, out var id) ? id : int.MaxValue)
.GroupBy(g => g.Country)
.Select(grp => grp.Key);
CountryList.Clear();
foreach (var country in countries)
{
if (!string.IsNullOrEmpty(country))
{
CountryList.Add(country);
}
}
_selectedCountry = _tpConfig.ReviveStatueOfTheSevenCountry;
UpdateAreas(SelectedCountry);
_selectedArea = _tpConfig.ReviveStatueOfTheSevenArea;
UpdateRevivePoint(SelectedCountry, SelectedArea);
}
private void UpdateAreas(string country)
{
Areas.Clear();
SelectedArea = string.Empty;
if (string.IsNullOrEmpty(country)) return;
var areas = MapLazyAssets.Instance.GoddessPositions.Values
.Where(g => g.Country == country)
.OrderBy(g => int.TryParse(g.Id, out var id) ? id : int.MaxValue)
.GroupBy(g => g.Level1Area)
.Select(grp => grp.Key);
foreach (var area in areas)
{
if (!string.IsNullOrEmpty(area))
{
Areas.Add(area);
}
}
}
// 当国家或区域改变时更新坐标
private void UpdateRevivePoint(string country, string area)
{
if (string.IsNullOrEmpty(country) || string.IsNullOrEmpty(area)) return;
var goddess = MapLazyAssets.Instance.GoddessPositions.Values
.FirstOrDefault(g => g.Country == country && g.Level1Area == area);
if (goddess == null) return;
_tpConfig.ReviveStatueOfTheSevenCountry = country;
_tpConfig.ReviveStatueOfTheSevenArea = area;
_tpConfig.ReviveStatueOfTheSevenPointX = goddess.X;
_tpConfig.ReviveStatueOfTheSevenPointY = goddess.Y;
_tpConfig.ReviveStatueOfTheSeven = goddess;
}
[RelayCommand]
public void OnRefreshMaskSettings()
{
WeakReferenceMessenger.Default.Send(
new PropertyChangedMessage<object>(this, "RefreshSettings", new object(), "重新计算控件位置"));
}
[RelayCommand]
private void OnResetMaskOverlayLayout()
{
var c = Config.MaskWindowConfig;
c.StatusListLeftRatio = 20.0 / 1920;
c.StatusListTopRatio = 807.0 / 1080;
c.StatusListWidthRatio = 477.0 / 1920;
c.StatusListHeightRatio = 24.0 / 1080;
c.LogTextBoxLeftRatio = 20.0 / 1920;
c.LogTextBoxTopRatio = 832.0 / 1080;
c.LogTextBoxWidthRatio = 477.0 / 1920;
c.LogTextBoxHeightRatio = 188.0 / 1080;
OnRefreshMaskSettings();
}
[RelayCommand]
private void OnSwitchMaskEnabled()
{
// if (Config.MaskWindowConfig.MaskEnabled)
// {
// MaskWindow.Instance().Show();
// }
// else
// {
// MaskWindow.Instance().Hide();
// }
}
[RelayCommand]
public void OnGoToHotKeyPage()
{
_navigationService.Navigate(typeof(HotKeyPage));
}
[RelayCommand]
public void OnSwitchTakenScreenshotEnabled()
{
}
[RelayCommand]
public void OnGoToFolder()
{
var path = Global.Absolute(@"log\screenshot\");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
Process.Start("explorer.exe", path);
}
[RelayCommand]
public void OnGoToLogFolder()
{
var path = Global.Absolute(@"log");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
Process.Start("explorer.exe", path);
}
[RelayCommand]
private async Task ImportLocalScriptsRepoZip()
{
Directory.CreateDirectory(ScriptRepoUpdater.ReposPath);
var dialog = new OpenFileDialog
{
Title = "选择脚本仓库压缩包",
Filter = "Zip Files (*.zip)|*.zip",
Multiselect = false
};
if (dialog.ShowDialog() == true)
{
try
{
await ScriptRepoUpdater.Instance.ImportLocalRepoZip(dialog.FileName);
ThemedMessageBox.Information("脚本仓库离线包导入成功!");
}
catch (Exception ex)
{
ThemedMessageBox.Error($"脚本仓库离线包导入失败:{ex.Message}");
}
}
}
[RelayCommand]
private void OpenAboutWindow()
{
var aboutWindow = new AboutWindow();
aboutWindow.Owner = Application.Current.MainWindow;
aboutWindow.ShowDialog();
}
[RelayCommand]
private void OpenKeyBindingsWindow()
{
var keyBindingsWindow = KeyBindingsWindow.Instance;
keyBindingsWindow.Owner = Application.Current.MainWindow;
keyBindingsWindow.ShowDialog();
}
[RelayCommand]
private async Task CheckUpdateAsync()
{
await App.GetService<IUpdateService>()!.CheckUpdateAsync(new UpdateOption
{
Trigger = UpdateTrigger.Manual,
Channel = UpdateChannel.Stable
});
}
[RelayCommand]
private async Task CheckUpdateAlphaAsync()
{
var result = await ThemedMessageBox.ShowAsync("测试版本非常不稳定!\n测试版本非常不稳定\n测试版本非常不稳定\n\n是否继续检查更新", "警告", MessageBoxButton.YesNo, ThemedMessageBox.MessageBoxIcon.Warning);
if (result != MessageBoxResult.Yes)
{
return;
}
await App.GetService<IUpdateService>()!.CheckUpdateAsync(new UpdateOption
{
Trigger = UpdateTrigger.Manual,
Channel = UpdateChannel.Alpha,
});
}
// [RelayCommand]
// private async Task GotoGithubActionAsync()
// {
// await Launcher.LaunchUriAsync(
// new Uri("https://github.com/babalae/better-genshin-impact/actions/workflows/publish.yml"));
// }
[RelayCommand]
private async Task OnGameLangSelectionChanged(KeyValuePair<string, string> type)
{
await App.ServiceProvider.GetRequiredService<OcrFactory>().Unload();
}
[RelayCommand]
private async Task OnPaddleOcrModelConfigChanged(PaddleOcrModelConfig value)
{
Config.OtherConfig.OcrConfig.PaddleOcrModelConfig = value;
await App.ServiceProvider.GetRequiredService<OcrFactory>().Unload();
}
}