diff --git a/src/MaaWpfGui/App.xaml.cs b/src/MaaWpfGui/App.xaml.cs index eed997be51..45c4013c29 100644 --- a/src/MaaWpfGui/App.xaml.cs +++ b/src/MaaWpfGui/App.xaml.cs @@ -44,6 +44,10 @@ namespace MaaWpfGui } } + // TODO: 现在的启动顺序是:OnStartup -> Bootstrapper.OnOnStart -> TaskQueueViewModel.OnInitialActivate, + // 在 OnInitialActivate 中会初始化 StageManager,但是 StageManager 中会异步下载更新,OnInitialActivate 不会等待这个异步操作 + // 导致这里切换了配置之后,StageManager 异步结束后调用 UpdateStageList 时会把 UI 现在的关卡配置存到新配置中 + // 先把 StageManager 的联网更新放到这里,之后看看有没有什么更好的办法 protected override void OnStartup(StartupEventArgs e) { if (WineRuntimeInformation.IsRunningUnderWine && MaaDesktopIntegration.Availabile) @@ -69,32 +73,48 @@ namespace MaaWpfGui } } - Config(configArgs); - } - - private static void Config(string desiredConfig) - { - const string ConfigFile = @".\config\gui.json"; - if (!File.Exists(ConfigFile) || string.IsNullOrEmpty(desiredConfig)) + if (Config(configArgs)) { return; } + _ = Instances.TaskQueueViewModel.UpdateDatePromptAndStagesWeb(); + } + + /// + /// 检查配置并切换,如果成功切换则重启 + /// + /// 配置名 + /// 切换并重启 + private static bool Config(string desiredConfig) + { + const string ConfigFile = @".\config\gui.json"; + if (!File.Exists(ConfigFile) || string.IsNullOrEmpty(desiredConfig)) + { + return false; + } + try { - if (!UpdateConfiguration(desiredConfig)) + if (UpdateConfiguration(desiredConfig)) { - return; + Bootstrapper.ShutdownAndRestartWithoutArgs(); + return true; } - - Bootstrapper.ShutdownAndRestartWithoutArgs(); } catch (Exception ex) { _logger.Error($"Error updating configuration: {desiredConfig}, ex: {ex.Message}"); } + + return false; } + /// + /// 切换配置 + /// + /// 配置名 + /// 是否成功切换配置 private static bool UpdateConfiguration(string desiredConfig) { // 配置名可能就包在引号中,需要转义符,如 \"a\" diff --git a/src/MaaWpfGui/Services/StageManager.cs b/src/MaaWpfGui/Services/StageManager.cs index e0173982ee..bd02588ef5 100644 --- a/src/MaaWpfGui/Services/StageManager.cs +++ b/src/MaaWpfGui/Services/StageManager.cs @@ -52,24 +52,11 @@ namespace MaaWpfGui.Services public StageManager() { UpdateStageLocal(); - - Task.Run(async () => - { - await UpdateStageWeb(); - if (Instances.TaskQueueViewModel != null) - { - _ = Execute.OnUIThreadAsync(() => - { - Instances.TaskQueueViewModel.UpdateDatePrompt(); - Instances.TaskQueueViewModel.UpdateStageList(); - }); - } - }); } public void UpdateStageLocal() { - UpdateStageInternal(LoadLocalStages()); + MergePermanentAndActivityStages(LoadLocalStages()); } public async Task UpdateStageWeb() @@ -81,7 +68,7 @@ namespace MaaWpfGui.Services const string FilePath = "cache/allFileDownloadComplete.json"; await File.WriteAllTextAsync(FilePath, GenerateJsonString(false)); - UpdateStageInternal(await LoadWebStages()); + MergePermanentAndActivityStages(await LoadWebStages()); await File.WriteAllTextAsync(FilePath, GenerateJsonString(true)); _ = Execute.OnUIThreadAsync(() => @@ -113,7 +100,7 @@ namespace MaaWpfGui.Services var clientType = ConfigurationHelper.GetValue(ConfigurationKeys.ClientType, string.Empty); // 官服和B服使用同样的资源 - if (clientType == "Bilibili" || clientType == string.Empty) + if (clientType is "Bilibili" or "") { clientType = "Official"; } @@ -173,7 +160,7 @@ namespace MaaWpfGui.Services return activity; } - private void UpdateStageInternal(JObject activity) + private void MergePermanentAndActivityStages(JObject activity) { var tempStage = new Dictionary { diff --git a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs index 812005a255..a9bd9fe3e3 100644 --- a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs @@ -278,7 +278,7 @@ namespace MaaWpfGui.ViewModels.UI _stageManager = _container.Get(); DisplayName = LocalizationHelper.GetString("Farming"); - LogItemViewModels = new ObservableCollection(); + LogItemViewModels = []; InitializeItems(); InitTimer(); } @@ -386,17 +386,14 @@ namespace MaaWpfGui.ViewModels.UI } _isUpdatingDatePrompt = true; - UpdateDatePrompt(); - UpdateStageList(); + UpdateDatePromptAndStagesLocally(); var delayTime = CalculateRandomDelay(); _ = Task.Run(async () => { await Task.Delay(delayTime); await _runningState.UntilIdleAsync(60000); - await _stageManager.UpdateStageWeb(); - UpdateDatePrompt(); - UpdateStageList(); + await UpdateDatePromptAndStagesWeb(); _isUpdatingDatePrompt = false; }); } @@ -649,8 +646,7 @@ namespace MaaWpfGui.ViewModels.UI InitDrops(); NeedToUpdateDatePrompt(); - UpdateDatePrompt(); - UpdateStageList(); + UpdateDatePromptAndStagesLocally(); RefreshCustomInfrastPlan(); if (DateTime.UtcNow.ToYjDate().IsAprilFoolsDay()) @@ -681,6 +677,25 @@ namespace MaaWpfGui.ViewModels.UI return IsStageOpen(stage) ? stage : string.Empty; } + /// + /// 更新日期提示和关卡列表 + /// + public void UpdateDatePromptAndStagesLocally() + { + UpdateDatePrompt(); + UpdateStageList(); + } + + /// + /// 访问 api 获取更新后更新日期提示和关卡列表 + /// + /// 可等待 + public async Task UpdateDatePromptAndStagesWeb() + { + await _stageManager.UpdateStageWeb(); + UpdateDatePromptAndStagesLocally(); + } + /// /// Updates stage list. /// 使用手动输入时,只更新关卡列表,不更新关卡选择