fix: 使用命令行切换配置时有可能使关卡选择被覆盖

fix #11199
This commit is contained in:
uye
2024-11-20 22:47:32 +08:00
parent 219459eb7e
commit 3a311dbd6d
3 changed files with 58 additions and 36 deletions

View File

@@ -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();
}
/// <summary>
/// 检查配置并切换,如果成功切换则重启
/// </summary>
/// <param name="desiredConfig">配置名</param>
/// <returns>切换并重启</returns>
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;
}
/// <summary>
/// 切换配置
/// </summary>
/// <param name="desiredConfig">配置名</param>
/// <returns>是否成功切换配置</returns>
private static bool UpdateConfiguration(string desiredConfig)
{
// 配置名可能就包在引号中,需要转义符,如 \"a\"

View File

@@ -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<string, StageInfo>
{

View File

@@ -278,7 +278,7 @@ namespace MaaWpfGui.ViewModels.UI
_stageManager = _container.Get<StageManager>();
DisplayName = LocalizationHelper.GetString("Farming");
LogItemViewModels = new ObservableCollection<LogItemViewModel>();
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;
}
/// <summary>
/// 更新日期提示和关卡列表
/// </summary>
public void UpdateDatePromptAndStagesLocally()
{
UpdateDatePrompt();
UpdateStageList();
}
/// <summary>
/// 访问 api 获取更新后更新日期提示和关卡列表
/// </summary>
/// <returns>可等待</returns>
public async Task UpdateDatePromptAndStagesWeb()
{
await _stageManager.UpdateStageWeb();
UpdateDatePromptAndStagesLocally();
}
/// <summary>
/// Updates stage list.
/// 使用手动输入时,只更新关卡列表,不更新关卡选择