mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-03-30 10:19:51 +08:00
调度器支持路径追踪功能及多项代码优化 在 `ScriptGroupProject.cs` 中: - 添加对多个命名空间的引用。 - 修改构造函数,增加 `folder` 和 `type` 参数,删除 `kmName` 参数。 - 添加 `BuildKeyMouseProject` 和 `BuildPathingProject` 静态方法。 - 在 `Run` 方法中增加 `Pathing` 类型处理逻辑。 - 在 `ScriptGroupProjectExtensions` 中增加 `Pathing` 类型描述。 在 `PathingTask.cs` 中: - 添加 `FileName` 属性,并在 `BuildFromFilePath` 方法中设置。 在 `Navigation.cs` 中: - 将 `Navigation` 类改为 `public`。 - 添加 `_isWarmUp` 静态字段,防止重复加载地图特征点。 - 将 `GetPosition`、`GetTargetOrientation` 和 `GetDistance` 方法改为 `public`。 在 `GameTaskManager.cs` 中: - 在 `AddTrigger` 方法中添加注释,指出清理 `TriggerDictionary` 的问题。 在 `ScriptService.cs` 中: - 在 `RunMulti` 方法中增加 `hasTimer` 变量,并在处理 `Pathing` 类型时设置。 - 修改 `RunMulti` 方法逻辑,确保无定时操作时检查 JS 脚本。 - 在 `Run` 方法中增加 `Pathing` 类型处理逻辑。 在 `MapPathingPage.xaml` 中: - 注释掉第四列定义和相关 `Grid` 元素。 - 修改 `ListView` 中 `GridViewColumn` 的 `DisplayMemberBinding`。 在 `ScriptControlPage.xaml` 中: - 在 `ContextMenu` 中添加新的 `MenuItem`,用于添加路径追踪任务。 在 `PromptDialog.xaml.cs` 中: - 添加新的 `Prompt` 方法,允许指定对话框大小。 - 修改 `ResponseText` 属性逻辑,增加默认返回值。 在 `MapPathingViewModel.cs` 中: - 调整 `using` 语句顺序,删除重复引用。 在 `ScriptControlViewModel.cs` 中: - 修改 `OnAddKmScript` 方法,使用 `BuildKeyMouseProject` 方法创建键鼠脚本项目。 - 添加 `OnAddPathing` 方法及其辅助方法,用于添加路径追踪任务。 - 在 `RunSelectedScriptGroup` 方法中,将脚本运行逻辑放入 `Task.Run` 中,避免阻塞 UI 线程。
192 lines
5.3 KiB
C#
192 lines
5.3 KiB
C#
using BetterGenshinImpact.Core.Config;
|
|
using BetterGenshinImpact.Core.Recorder;
|
|
using BetterGenshinImpact.Core.Script.Project;
|
|
using BetterGenshinImpact.GameTask;
|
|
using BetterGenshinImpact.GameTask.AutoPathing;
|
|
using BetterGenshinImpact.GameTask.AutoPathing.Model;
|
|
using BetterGenshinImpact.ViewModel.Pages;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Dynamic;
|
|
using System.IO;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BetterGenshinImpact.Core.Script.Group;
|
|
|
|
public partial class ScriptGroupProject : ObservableObject
|
|
{
|
|
[ObservableProperty]
|
|
private int _index;
|
|
|
|
public string Name { get; set; } = string.Empty;
|
|
|
|
public string FolderName { get; set; } = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _type = string.Empty;
|
|
|
|
[JsonIgnore]
|
|
public string TypeDesc => ScriptGroupProjectExtensions.TypeDescriptions[Type];
|
|
|
|
[ObservableProperty]
|
|
private string _status = string.Empty;
|
|
|
|
[JsonIgnore]
|
|
public string StatusDesc => ScriptGroupProjectExtensions.StatusDescriptions[Status];
|
|
|
|
/// <summary>
|
|
/// 执行周期
|
|
/// 不在 ScheduleDescriptions 中则会被视为自定义Cron表达式
|
|
/// </summary>
|
|
[ObservableProperty]
|
|
private string _schedule = string.Empty;
|
|
|
|
[JsonIgnore]
|
|
public string ScheduleDesc => ScriptGroupProjectExtensions.ScheduleDescriptions.GetValueOrDefault(Schedule, "自定义周期");
|
|
|
|
[ObservableProperty]
|
|
private int _runNum = 1;
|
|
|
|
[JsonIgnore]
|
|
public ScriptProject? Project { get; set; }
|
|
|
|
public ExpandoObject? JsScriptSettingsObject { get; set; }
|
|
|
|
public ScriptGroupProject()
|
|
{
|
|
}
|
|
|
|
public ScriptGroupProject(ScriptProject project)
|
|
{
|
|
Name = project.Manifest.Name;
|
|
FolderName = project.FolderName;
|
|
Status = "Enabled";
|
|
Schedule = "Daily";
|
|
Project = project;
|
|
Type = "Javascript";
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="folder"></param>
|
|
/// <param name="type">KeyMouse|Pathing</param>
|
|
public ScriptGroupProject(string name, string folder, string type)
|
|
{
|
|
Name = name;
|
|
FolderName = folder;
|
|
Status = "Enabled";
|
|
Schedule = "Daily";
|
|
Project = null; // 不是JS脚本
|
|
Type = type;
|
|
}
|
|
|
|
public static ScriptGroupProject BuildKeyMouseProject(string name)
|
|
{
|
|
return new ScriptGroupProject(name, name, "KeyMouse");
|
|
}
|
|
|
|
public static ScriptGroupProject BuildPathingProject(string name, string folder)
|
|
{
|
|
return new ScriptGroupProject(name, folder, "Pathing");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过 FolderName 查找 ScriptProject
|
|
/// </summary>
|
|
public void BuildScriptProjectRelation()
|
|
{
|
|
if (string.IsNullOrEmpty(FolderName))
|
|
{
|
|
throw new Exception("FolderName 为空");
|
|
}
|
|
Project = new ScriptProject(FolderName);
|
|
}
|
|
|
|
public async Task Run()
|
|
{
|
|
if (Type == "Javascript")
|
|
{
|
|
if (Project == null)
|
|
{
|
|
throw new Exception("JS脚本未初始化");
|
|
}
|
|
await Project.ExecuteAsync(JsScriptSettingsObject);
|
|
}
|
|
if (Type == "KeyMouse")
|
|
{
|
|
// 加载并执行
|
|
var json = await File.ReadAllTextAsync(Global.Absolute(@$"User\KeyMouseScript\{Name}"));
|
|
await KeyMouseMacroPlayer.PlayMacro(json, CancellationContext.Instance.Cts.Token, false);
|
|
}
|
|
if (Type == "Pathing")
|
|
{
|
|
// 加载并执行
|
|
var task = PathingTask.BuildFromFilePath(Path.Combine(MapPathingViewModel.PathJsonPath, FolderName, Name));
|
|
TaskTriggerDispatcher.Instance().AddTrigger("AutoPick", null);
|
|
await new PathExecutor(CancellationContext.Instance.Cts).Pathing(task);
|
|
}
|
|
else
|
|
{
|
|
//throw new Exception("不支持的脚本类型");
|
|
}
|
|
}
|
|
|
|
partial void OnTypeChanged(string value)
|
|
{
|
|
OnPropertyChanged(nameof(TypeDesc));
|
|
}
|
|
|
|
partial void OnStatusChanged(string value)
|
|
{
|
|
OnPropertyChanged(nameof(StatusDesc));
|
|
}
|
|
|
|
partial void OnScheduleChanged(string value)
|
|
{
|
|
OnPropertyChanged(nameof(ScheduleDesc));
|
|
}
|
|
}
|
|
|
|
public class ScriptGroupProjectExtensions
|
|
{
|
|
public static readonly Dictionary<string, string> TypeDescriptions = new()
|
|
{
|
|
{ "Javascript", "JS脚本" },
|
|
{ "KeyMouse", "键鼠脚本" },
|
|
{ "Pathing", "路径追踪" }
|
|
};
|
|
|
|
public static readonly Dictionary<string, string> StatusDescriptions = new()
|
|
{
|
|
{ "Enabled", "启用" },
|
|
{ "Disabled", "禁用" }
|
|
};
|
|
|
|
public Dictionary<string, string> GetStatusDescriptions()
|
|
{
|
|
return StatusDescriptions;
|
|
}
|
|
|
|
public static readonly Dictionary<string, string> ScheduleDescriptions = new()
|
|
{
|
|
{ "Daily", "每日" },
|
|
{ "EveryTwoDays", "间隔两天" },
|
|
{ "Monday", "每周一" },
|
|
{ "Tuesday", "每周二" },
|
|
{ "Wednesday", "每周三" },
|
|
{ "Thursday", "每周四" },
|
|
{ "Friday", "每周五" },
|
|
{ "Saturday", "每周六" },
|
|
{ "Sunday", "每周日" }
|
|
};
|
|
|
|
public Dictionary<string, string> GetScheduleDescriptions()
|
|
{
|
|
return ScheduleDescriptions;
|
|
}
|
|
}
|