mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-03-15 07:43:20 +08:00
fix: enhance RunTask method to support nullable CancellationToken and add linked cancellation token functionality
This commit is contained in:
@@ -79,6 +79,15 @@ public class Dispatcher
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RunTask(SoloTask soloTask, CancellationTokenSource customCts)
|
||||
{
|
||||
// 创建链接的取消令牌源,任何一个取消都会触发
|
||||
CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(
|
||||
customCts.Token,
|
||||
CancellationContext.Instance.Cts.Token);
|
||||
await RunTask(soloTask, linkedCts.Token);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 运行独立任务
|
||||
@@ -90,10 +99,10 @@ public class Dispatcher
|
||||
/// - AutoFight: 启动自动战斗任务
|
||||
/// - AutoDomain: 启动自动秘境任务
|
||||
/// </param>
|
||||
/// <param name="customCts">自定义取消令牌源,允许从JS控制任务取消</param>
|
||||
/// <param name="customCt">自定义取消令牌,允许从JS控制任务取消</param>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
public async Task RunTask(SoloTask soloTask, CancellationTokenSource? customCts = null)
|
||||
public async Task RunTask(SoloTask soloTask, CancellationToken? customCt = null)
|
||||
{
|
||||
if (soloTask == null)
|
||||
{
|
||||
@@ -106,69 +115,56 @@ public class Dispatcher
|
||||
throw new ArgumentNullException(nameof(taskSettingsPageViewModel), "内部视图模型对象为空");
|
||||
}
|
||||
|
||||
// 创建一个链接的取消令牌源,同时监听自定义令牌和全局令牌
|
||||
CancellationTokenSource? linkedCts = null;
|
||||
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
if (customCts != null)
|
||||
if (customCt != null)
|
||||
{
|
||||
// 创建链接的取消令牌源,任何一个取消都会触发
|
||||
linkedCts = CancellationTokenSource.CreateLinkedTokenSource(
|
||||
customCts.Token,
|
||||
CancellationContext.Instance.Cts.Token);
|
||||
cancellationToken = linkedCts.Token;
|
||||
cancellationToken = customCt.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果没有自定义令牌,就使用全局令牌
|
||||
cancellationToken = CancellationContext.Instance.Cts.Token;
|
||||
}
|
||||
|
||||
try
|
||||
|
||||
// 根据名称执行任务
|
||||
switch (soloTask.Name)
|
||||
{
|
||||
// 根据名称执行任务
|
||||
switch (soloTask.Name)
|
||||
{
|
||||
case "AutoGeniusInvokation":
|
||||
if (taskSettingsPageViewModel.GetTcgStrategy(out var content))
|
||||
{
|
||||
return;
|
||||
}
|
||||
case "AutoGeniusInvokation":
|
||||
if (taskSettingsPageViewModel.GetTcgStrategy(out var content))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await new AutoGeniusInvokationTask(new GeniusInvokationTaskParam(content)).Start(cancellationToken);
|
||||
break;
|
||||
await new AutoGeniusInvokationTask(new GeniusInvokationTaskParam(content)).Start(cancellationToken);
|
||||
break;
|
||||
|
||||
case "AutoWood":
|
||||
await new AutoWoodTask(new WoodTaskParam(taskSettingsPageViewModel.AutoWoodRoundNum,
|
||||
taskSettingsPageViewModel.AutoWoodDailyMaxCount)).Start(cancellationToken);
|
||||
break;
|
||||
case "AutoWood":
|
||||
await new AutoWoodTask(new WoodTaskParam(taskSettingsPageViewModel.AutoWoodRoundNum,
|
||||
taskSettingsPageViewModel.AutoWoodDailyMaxCount)).Start(cancellationToken);
|
||||
break;
|
||||
|
||||
case "AutoFight":
|
||||
await new AutoFightHandler().RunAsyncByScript(cancellationToken, null, _config);
|
||||
break;
|
||||
case "AutoFight":
|
||||
await new AutoFightHandler().RunAsyncByScript(cancellationToken, null, _config);
|
||||
break;
|
||||
|
||||
case "AutoDomain":
|
||||
if (taskSettingsPageViewModel.GetFightStrategy(out var path))
|
||||
{
|
||||
return;
|
||||
}
|
||||
case "AutoDomain":
|
||||
if (taskSettingsPageViewModel.GetFightStrategy(out var path))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await new AutoDomainTask(new AutoDomainParam(0, path)).Start(cancellationToken);
|
||||
break;
|
||||
await new AutoDomainTask(new AutoDomainParam(0, path)).Start(cancellationToken);
|
||||
break;
|
||||
|
||||
case "AutoFishing":
|
||||
await new AutoFishingTask(AutoFishingTaskParam.BuildFromSoloTaskConfig(soloTask.Config)).Start(
|
||||
cancellationToken);
|
||||
break;
|
||||
case "AutoFishing":
|
||||
await new AutoFishingTask(AutoFishingTaskParam.BuildFromSoloTaskConfig(soloTask.Config)).Start(
|
||||
cancellationToken);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentException($"未知的任务名称: {soloTask.Name}", nameof(soloTask.Name));
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
// 释放链接的取消令牌源
|
||||
linkedCts?.Dispose();
|
||||
default:
|
||||
throw new ArgumentException($"未知的任务名称: {soloTask.Name}", nameof(soloTask.Name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,10 +173,10 @@ public class Dispatcher
|
||||
// 创建一个新的链接令牌源,链接到全局令牌
|
||||
return CancellationTokenSource.CreateLinkedTokenSource(CancellationContext.Instance.Cts.Token);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public CancellationToken GetLinkedCancellationToken()
|
||||
{
|
||||
return GetLinkedCancellationTokenSource().Token;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user