diff --git a/BetterGenshinImpact/GameTask/AutoPathing/PathExecutor.cs b/BetterGenshinImpact/GameTask/AutoPathing/PathExecutor.cs index 39bf5af3..c5555500 100644 --- a/BetterGenshinImpact/GameTask/AutoPathing/PathExecutor.cs +++ b/BetterGenshinImpact/GameTask/AutoPathing/PathExecutor.cs @@ -202,7 +202,11 @@ public class PathExecutor catch (NormalEndException normalEndException) { Logger.LogInformation(normalEndException.Message); - break; + throw; + } + catch (TaskCanceledException e) + { + throw; } catch (RetryException retryException) { @@ -216,7 +220,6 @@ public class PathExecutor StartSkipOtherOperations(); Logger.LogWarning(retryException.Message); } - finally { // 不管咋样,松开所有按键 diff --git a/BetterGenshinImpact/GameTask/TaskRunner.cs b/BetterGenshinImpact/GameTask/TaskRunner.cs index 3a275774..b5d0c16d 100644 --- a/BetterGenshinImpact/GameTask/TaskRunner.cs +++ b/BetterGenshinImpact/GameTask/TaskRunner.cs @@ -68,6 +68,21 @@ public class TaskRunner { _logger.LogInformation("任务中断:{Msg}", e.Message); SendNotification(); + if (RunnerContext.Instance.IsContinuousRunGroup) + { + // 连续执行时,抛出异常,终止执行 + throw; + } + } + catch (TaskCanceledException e) + { + _logger.LogInformation("任务中断:{Msg}", "任务被取消"); + SendNotification(); + if (RunnerContext.Instance.IsContinuousRunGroup) + { + // 连续执行时,抛出异常,终止执行 + throw; + } } catch (Exception e) { diff --git a/BetterGenshinImpact/Service/ScriptService.cs b/BetterGenshinImpact/Service/ScriptService.cs index 14ec3321..676eabf6 100644 --- a/BetterGenshinImpact/Service/ScriptService.cs +++ b/BetterGenshinImpact/Service/ScriptService.cs @@ -12,6 +12,7 @@ using System.Diagnostics; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; +using BetterGenshinImpact.GameTask.AutoGeniusInvokation.Exception; using BetterGenshinImpact.GameTask.Common; using BetterGenshinImpact.GameTask.Common.BgiVision; @@ -45,60 +46,71 @@ public partial class ScriptService : IScriptService { _logger.LogInformation("配置组 {Name} 包含实时任务操作调用", groupName); } + _logger.LogInformation("配置组 {Name} 加载完成,共{Cnt}个脚本,开始执行", groupName, list.Count); } var timerOperation = hasTimer ? DispatcherTimerOperationEnum.UseCacheImageWithTriggerEmpty : DispatcherTimerOperationEnum.UseSelfCaptureImage; await new TaskRunner(timerOperation) - .RunThreadAsync(async () => - { - var stopwatch = new Stopwatch(); + .RunThreadAsync(async () => + { + var stopwatch = new Stopwatch(); - foreach (var project in list) - { - if (project.Status != "Enabled") - { - _logger.LogInformation("脚本 {Name} 状态为禁用,跳过执行", project.Name); - continue; - } - if (CancellationContext.Instance.Cts.IsCancellationRequested) - { - _logger.LogInformation("执行被取消"); - break; - } + foreach (var project in list) + { + if (project.Status != "Enabled") + { + _logger.LogInformation("脚本 {Name} 状态为禁用,跳过执行", project.Name); + continue; + } - for (var i = 0; i < project.RunNum; i++) - { - try - { - if (hasTimer) - { - TaskTriggerDispatcher.Instance().ClearTriggers(); - } + if (CancellationContext.Instance.Cts.IsCancellationRequested) + { + _logger.LogInformation("执行被取消"); + break; + } - _logger.LogInformation("------------------------------"); + for (var i = 0; i < project.RunNum; i++) + { + try + { + if (hasTimer) + { + TaskTriggerDispatcher.Instance().ClearTriggers(); + } - stopwatch.Reset(); - stopwatch.Start(); - await ExecuteProject(project); - } - catch (Exception e) - { - _logger.LogDebug(e, "执行脚本时发生异常"); - _logger.LogError("执行脚本时发生异常: {Msg}", e.Message); - } - finally - { - stopwatch.Stop(); - _logger.LogInformation("→ 脚本执行结束: {Name}, 耗时: {ElapsedMilliseconds} 毫秒", project.Name, stopwatch.ElapsedMilliseconds); - _logger.LogInformation("------------------------------"); - } + _logger.LogInformation("------------------------------"); - await Task.Delay(2000); - } - } - }); + stopwatch.Reset(); + stopwatch.Start(); + await ExecuteProject(project); + } + catch (NormalEndException e) + { + throw; + } + catch (TaskCanceledException e) + { + _logger.LogInformation("取消执行配置组: {Msg}", e.Message); + throw; + } + catch (Exception e) + { + _logger.LogDebug(e, "执行脚本时发生异常"); + _logger.LogError("执行脚本时发生异常: {Msg}", e.Message); + } + finally + { + stopwatch.Stop(); + _logger.LogInformation("→ 脚本执行结束: {Name}, 耗时: {ElapsedMilliseconds} 毫秒", project.Name, stopwatch.ElapsedMilliseconds); + _logger.LogInformation("------------------------------"); + } + + await Task.Delay(2000); + } + } + }); if (!string.IsNullOrEmpty(groupName)) { @@ -131,6 +143,7 @@ public partial class ScriptService : IScriptService hasTimer = true; } } + return list; } @@ -153,6 +166,7 @@ public partial class ScriptService : IScriptService jsProjects.Add(project.Project); } } + return jsProjects; } @@ -199,8 +213,8 @@ public partial class ScriptService : IScriptService [GeneratedRegex(@"^(?!\s*\/\/)\s*dispatcher\.\s*addTimer", RegexOptions.Multiline)] private static partial Regex DispatcherAddTimerRegex(); - - + + public static async Task StartGameTask() { // 没启动时候,启动截图器 @@ -234,4 +248,4 @@ public partial class ScriptService : IScriptService }); } } -} +} \ No newline at end of file diff --git a/BetterGenshinImpact/View/Pages/MapPathingPage.xaml b/BetterGenshinImpact/View/Pages/MapPathingPage.xaml index 0e7b2fd7..62fda7f4 100644 --- a/BetterGenshinImpact/View/Pages/MapPathingPage.xaml +++ b/BetterGenshinImpact/View/Pages/MapPathingPage.xaml @@ -43,7 +43,7 @@ Margin="0,0,0,8" Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}" TextWrapping="Wrap"> - 可以实现自动采集、自动挖矿(开发中)、自动锄地(开发中)等功能。请在调度器中使用! + 可以实现自动采集、自动挖矿、自动锄地等功能。请在调度器中使用! 点击查看地图追踪与录制使用教程 diff --git a/BetterGenshinImpact/ViewModel/Pages/MapPathingViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/MapPathingViewModel.cs index 709c9c07..4fd46302 100644 --- a/BetterGenshinImpact/ViewModel/Pages/MapPathingViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/MapPathingViewModel.cs @@ -13,6 +13,7 @@ using Microsoft.Extensions.Logging; using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; +using System.Threading.Tasks; using BetterGenshinImpact.Core.Script; using BetterGenshinImpact.ViewModel.Message; using CommunityToolkit.Mvvm.Messaging; @@ -105,7 +106,7 @@ public partial class MapPathingViewModel : ObservableObject, INavigationAware, I } [RelayCommand] - public async void OnStart() + public async Task OnStart() { var item = SelectNode; if (item == null) diff --git a/BetterGenshinImpact/ViewModel/Pages/ScriptControlViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/ScriptControlViewModel.cs index 0454272f..ca9ed401 100644 --- a/BetterGenshinImpact/ViewModel/Pages/ScriptControlViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/ScriptControlViewModel.cs @@ -97,19 +97,21 @@ public partial class ScriptControlViewModel : ObservableObject, INavigationAware } } } + [RelayCommand] private void ClearTasks() { SelectedScriptGroup.Projects.Clear(); WriteScriptGroup(SelectedScriptGroup); } + private void UpdateTasks() { //PromptDialog.Prompt - // SelectedScriptGroup.Projects.Clear(); - // WriteScriptGroup(SelectedScriptGroup); + // SelectedScriptGroup.Projects.Clear(); + // WriteScriptGroup(SelectedScriptGroup); } - + [RelayCommand] public void OnRenameScriptGroup(ScriptGroup? item) { @@ -867,14 +869,23 @@ public partial class ScriptControlViewModel : ObservableObject, INavigationAware _logger.LogInformation("开始连续执行选中配置组:{Names}", string.Join(",", selectedGroups.Select(x => x.Name))); - RunnerContext.Instance.IsContinuousRunGroup = true; - foreach (var scriptGroup in selectedGroups) + try { - await _scriptService.RunMulti(GetNextProjects(scriptGroup), scriptGroup.Name); - await Task.Delay(2000); + RunnerContext.Instance.IsContinuousRunGroup = true; + foreach (var scriptGroup in selectedGroups) + { + await _scriptService.RunMulti(GetNextProjects(scriptGroup), scriptGroup.Name); + await Task.Delay(2000); + } + } + catch (Exception e) + { + Debug.WriteLine(e.Message); + } + finally + { + RunnerContext.Instance.Reset(); } - - RunnerContext.Instance.Reset(); } } } \ No newline at end of file