using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using System; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Threading.Tasks; using System.Windows; using BetterGenshinImpact.Core.Config; using BetterGenshinImpact.Model; using Wpf.Ui.Controls; namespace BetterGenshinImpact.View.Windows; [ObservableObject] public partial class CheckUpdateWindow : FluentWindow { public Func? UserInteraction = null!; [ObservableProperty] private bool showUpdateStatus = false; [ObservableProperty] private string updateStatusMessage = string.Empty; private UpdateOption _option; public CheckUpdateWindow(UpdateOption option) { _option = option ?? throw new ArgumentNullException(nameof(option)); DataContext = this; InitializeComponent(); if (option.Trigger == UpdateTrigger.Manual) { IgnoreButton.Visibility = Visibility.Collapsed; } if (option.Channel == UpdateChannel.Alpha) { WebpagePanel.Visibility = Visibility.Collapsed; } Closing += OnClosing; } protected void OnClosing(object? sender, CancelEventArgs e) { if (ShowUpdateStatus) { e.Cancel = true; } } public void NavigateToHtml(string html) { WebpagePanel?.NavigateToHtml(html); } [RelayCommand] private async Task BackgroundUpdateAsync() { if (UserInteraction != null) { await UserInteraction.Invoke(this, CheckUpdateWindowButton.BackgroundUpdate); } } [RelayCommand] private async Task OtherUpdateAsync() { if (UserInteraction != null) { await UserInteraction.Invoke(this, CheckUpdateWindowButton.OtherUpdate); } } [RelayCommand] private async Task UpdateAsync() { if (UserInteraction != null) { await UserInteraction.Invoke(this, CheckUpdateWindowButton.Update); } } [RelayCommand] private async Task UpdateFromSteambirdAsync() { await RunUpdaterAsync("-I"); } [RelayCommand] private async Task UpdateFromMirrorchanAsync() { if (_option.Channel == UpdateChannel.Stable) { await RunUpdaterAsync("--source mirrorc"); } else { await RunUpdaterAsync("--source mirrorc-alpha"); } } /// /// --source mirrorc /// --source mirrorc-alpha /// --source github /// --dfs-extras {"hutao-token": "...."} /// private async Task RunUpdaterAsync(string parameters) { // 唤起更新程序 string updaterExePath = Global.Absolute("BetterGI.update.exe"); if (!File.Exists(updaterExePath)) { await MessageBox.ErrorAsync("更新程序不存在,请选择其他更新方式!"); return; } // 启动 Process.Start(updaterExePath, parameters); // 退出程序 Application.Current.Shutdown(); } [RelayCommand] private async Task IgnoreAsync() { if (UserInteraction != null) { await UserInteraction.Invoke(this, CheckUpdateWindowButton.Ignore); } } [RelayCommand] private async Task CancelAsync() { if (UserInteraction != null) { await UserInteraction.Invoke(this, CheckUpdateWindowButton.Cancel); } } public enum CheckUpdateWindowButton { BackgroundUpdate, OtherUpdate, Update, Ignore, Cancel, } }