mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-04-05 11:25:20 +08:00
更新日志和黑名单 移除 `PathExecutor.cs` 中的无用引用,重构 `Pathing` 方法,添加 `InitializePathing` 和 `ConvertWaypoints` 方法,提取传送点处理逻辑到 `HandleTeleportWaypoint` 方法。修改 `TpTask.cs` 中的日志格式为浮点数格式。将 `TaskRunner.cs` 中的 `FireAndForgetAsync` 方法重命名为 `RunThreadAsync`,并在 `ScriptService.cs` 中相应替换调用。为 `DpiHelper.cs` 中的 `ScaleY` 属性添加注释。更新 `pick_black_lists.json`,添加新的黑名单项 `"摆放巧像"`。在 `MapPathingViewModel.cs` 中添加 `_mapViewer` 字段,并在 `OnOpenMapViewer` 方法中使用。
234 lines
9.0 KiB
C#
234 lines
9.0 KiB
C#
using BetterGenshinImpact.Core.Script.Project;
|
|
using BetterGenshinImpact.GameTask;
|
|
using BetterGenshinImpact.GameTask.Model.Enum;
|
|
using BetterGenshinImpact.Service.Interface;
|
|
using BetterGenshinImpact.ViewModel.Pages;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Documents;
|
|
using BetterGenshinImpact.Core.Script.Group;
|
|
using BetterGenshinImpact.Core.Script;
|
|
using BetterGenshinImpact.Helpers;
|
|
|
|
namespace BetterGenshinImpact.Service;
|
|
|
|
public partial class ScriptService(HomePageViewModel homePageViewModel) : IScriptService
|
|
{
|
|
private readonly ILogger<ScriptService> _logger = App.GetLogger<ScriptService>();
|
|
|
|
public async Task RunMulti(List<string> folderNameList, string? groupName = null)
|
|
{
|
|
// 重新加载脚本项目
|
|
var projects = folderNameList.Select(name => new ScriptProject(name)).ToList();
|
|
|
|
var codeList = await ReadCodeList(projects);
|
|
var hasTimer = HasTimerOperation(codeList);
|
|
|
|
// 没启动时候,启动截图器
|
|
await homePageViewModel.OnStartTriggerAsync();
|
|
|
|
if (!string.IsNullOrEmpty(groupName))
|
|
{
|
|
if (hasTimer)
|
|
{
|
|
_logger.LogInformation("配置组 {Name} 包含实时任务操作调用", groupName);
|
|
}
|
|
|
|
_logger.LogInformation("配置组 {Name} 加载完成,共{Cnt}个脚本,开始执行", groupName, projects.Count);
|
|
}
|
|
|
|
// 循环执行所有脚本
|
|
var timerOperation = hasTimer ? DispatcherTimerOperationEnum.UseCacheImageWithTriggerEmpty : DispatcherTimerOperationEnum.UseSelfCaptureImage;
|
|
await new TaskRunner(timerOperation)
|
|
.RunThreadAsync(async () =>
|
|
{
|
|
foreach (var project in projects)
|
|
{
|
|
try
|
|
{
|
|
if (hasTimer)
|
|
{
|
|
TaskTriggerDispatcher.Instance().ClearTriggers();
|
|
}
|
|
|
|
_logger.LogInformation("------------------------------");
|
|
_logger.LogInformation("→ 开始执行脚本: {Name}", project.Manifest.Name);
|
|
await project.ExecuteAsync();
|
|
await Task.Delay(1000);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogDebug(e, "执行脚本时发生异常");
|
|
_logger.LogError("执行脚本时发生异常: {Msg}", e.Message);
|
|
}
|
|
finally
|
|
{
|
|
_logger.LogInformation("→ 脚本执行结束: {Name}", project.Manifest.Name);
|
|
_logger.LogInformation("------------------------------");
|
|
}
|
|
}
|
|
});
|
|
|
|
if (!string.IsNullOrEmpty(groupName))
|
|
{
|
|
_logger.LogInformation("配置组 {Name} 执行结束", groupName);
|
|
}
|
|
}
|
|
|
|
public async Task RunMulti(IEnumerable<ScriptGroupProject> projectList, string groupName)
|
|
{
|
|
var hasTimer = false;
|
|
|
|
// 重新加载脚本项目 并放入一个新的列表
|
|
var list = new List<ScriptGroupProject>();
|
|
foreach (var project in projectList)
|
|
{
|
|
if (project.Type == "Javascript")
|
|
{
|
|
var newProject = new ScriptGroupProject(new ScriptProject(project.FolderName)); ;
|
|
newProject.Status = project.Status;
|
|
newProject.Schedule = project.Schedule;
|
|
newProject.RunNum = project.RunNum;
|
|
newProject.JsScriptSettingsObject = project.JsScriptSettingsObject;
|
|
list.Add(newProject);
|
|
}
|
|
else if (project.Type == "KeyMouse")
|
|
{
|
|
var newProject = ScriptGroupProject.BuildKeyMouseProject(project.FolderName);
|
|
newProject.Status = project.Status;
|
|
newProject.Schedule = project.Schedule;
|
|
newProject.RunNum = project.RunNum;
|
|
list.Add(newProject);
|
|
}
|
|
else if (project.Type == "Pathing")
|
|
{
|
|
var newProject = ScriptGroupProject.BuildPathingProject(project.Name, project.FolderName);
|
|
newProject.Status = project.Status;
|
|
newProject.Schedule = project.Schedule;
|
|
newProject.RunNum = project.RunNum;
|
|
list.Add(newProject);
|
|
hasTimer = true; // 路径追踪任务一定有实时任务操作
|
|
}
|
|
}
|
|
|
|
// 判断其中的
|
|
List<ScriptProject> jsProjects = [];
|
|
foreach (var project in list)
|
|
{
|
|
if (project is { Type: "Javascript", Project: not null })
|
|
{
|
|
jsProjects.Add(project.Project);
|
|
}
|
|
}
|
|
|
|
if (!hasTimer && jsProjects.Count > 0)
|
|
{
|
|
var codeList = await ReadCodeList(jsProjects);
|
|
hasTimer = HasTimerOperation(codeList);
|
|
}
|
|
|
|
// 没启动时候,启动截图器
|
|
await homePageViewModel.OnStartTriggerAsync();
|
|
|
|
if (hasTimer)
|
|
{
|
|
_logger.LogInformation("配置组 {Name} 包含实时任务操作调用", groupName);
|
|
}
|
|
|
|
_logger.LogInformation("配置组 {Name} 加载完成,共{Cnt}个脚本,开始执行", groupName, list.Count);
|
|
|
|
// 循环执行所有脚本
|
|
var timerOperation = hasTimer ? DispatcherTimerOperationEnum.UseCacheImageWithTriggerEmpty : DispatcherTimerOperationEnum.UseSelfCaptureImage;
|
|
|
|
await new TaskRunner(timerOperation)
|
|
.RunThreadAsync(async () =>
|
|
{
|
|
foreach (var project in list)
|
|
{
|
|
if (project.Status != "Enabled")
|
|
{
|
|
_logger.LogInformation("脚本 {Name} 状态为禁用,跳过执行", project.Name);
|
|
continue;
|
|
}
|
|
if (CancellationContext.Instance.Cts.IsCancellationRequested)
|
|
{
|
|
_logger.LogInformation("执行被取消");
|
|
break;
|
|
}
|
|
|
|
for (var i = 0; i < project.RunNum; i++)
|
|
{
|
|
try
|
|
{
|
|
if (hasTimer)
|
|
{
|
|
TaskTriggerDispatcher.Instance().ClearTriggers();
|
|
}
|
|
|
|
_logger.LogInformation("------------------------------");
|
|
|
|
if (project.Type == "Javascript")
|
|
{
|
|
if (project.Project == null)
|
|
{
|
|
throw new Exception("Project 为空");
|
|
}
|
|
|
|
_logger.LogInformation("→ 开始执行JS脚本: {Name}", project.Name);
|
|
await project.Run();
|
|
}
|
|
else if (project.Type == "KeyMouse")
|
|
{
|
|
_logger.LogInformation("→ 开始执行键鼠脚本: {Name}", project.Name);
|
|
await project.Run();
|
|
}
|
|
else if (project.Type == "Pathing")
|
|
{
|
|
_logger.LogInformation("→ 开始执行路径追踪任务: {Name}", project.Name);
|
|
await project.Run();
|
|
}
|
|
|
|
await Task.Delay(2000);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogDebug(e, "执行脚本时发生异常");
|
|
_logger.LogError("执行脚本时发生异常: {Msg}", e.Message);
|
|
}
|
|
finally
|
|
{
|
|
_logger.LogInformation("→ 脚本执行结束: {Name}", project.Name);
|
|
_logger.LogInformation("------------------------------");
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
_logger.LogInformation("配置组 {Name} 执行结束", groupName);
|
|
}
|
|
|
|
private async Task<List<string>> ReadCodeList(List<ScriptProject> list)
|
|
{
|
|
var codeList = new List<string>();
|
|
foreach (var project in list)
|
|
{
|
|
var code = await project.LoadCode();
|
|
codeList.Add(code);
|
|
}
|
|
|
|
return codeList;
|
|
}
|
|
|
|
private bool HasTimerOperation(IEnumerable<string> codeList)
|
|
{
|
|
return codeList.Any(code => DispatcherAddTimerRegex().IsMatch(code));
|
|
}
|
|
|
|
[GeneratedRegex(@"^(?!\s*\/\/)\s*dispatcher\.\s*addTimer", RegexOptions.Multiline)]
|
|
private static partial Regex DispatcherAddTimerRegex();
|
|
}
|