Files
DarkFlameMaster 8b7d2353e5 feat(js):地图追踪增加读取目录下的内容等方法 (#2875)
* feat(js):地图追踪增加读取目录下的内容等方法

* refactor(AutoPathingScript): 重构文件操作方法到LimitedFile类

* fix

* docs(AutoPathingScript): 修正方法参数注释中的默认值描述

* 避免重复初始化实例,给恢复游戏窗口焦点的日志也加上当前窗口名
2026-03-05 01:13:52 +08:00

100 lines
3.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using BetterGenshinImpact.GameTask.AutoPathing;
using BetterGenshinImpact.GameTask.AutoPathing.Model;
using System.Threading.Tasks;
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.GameTask.Common;
using Microsoft.Extensions.Logging;
namespace BetterGenshinImpact.Core.Script.Dependence;
public class AutoPathingScript
{
private object? _config = null;
private string _rootPath;
private readonly LimitedFile _autoPathingFile;
public AutoPathingScript(string rootPath, object? config)
{
_config = config;
_rootPath = rootPath;
_autoPathingFile = new LimitedFile(Global.Absolute(@"User\AutoPathing"));
}
public async Task Run(string json)
{
try
{
var task = PathingTask.BuildFromJson(json);
var pathExecutor = new PathExecutor(CancellationContext.Instance.Cts.Token);
if (_config != null && _config is PathingPartyConfig patyConfig)
{
pathExecutor.PartyConfig = patyConfig;
}
await pathExecutor.Pathing(task);
}
catch (Exception e)
{
TaskControl.Logger.LogDebug(e,"执行地图追踪时候发生错误");
TaskControl.Logger.LogError("执行地图追踪时候发生错误: {Msg}",e.Message);
}
}
public async Task RunFile(string path)
{
try
{
var json = await new LimitedFile(_rootPath).ReadText(path);
await Run(json);
}
catch (Exception e)
{
TaskControl.Logger.LogDebug(e,"读取文件时发生错误");
TaskControl.Logger.LogError("读取文件时发生错误: {Msg}",e.Message);
}
}
/// <summary>
/// 从已订阅的内容中获取文件
/// </summary>
/// <param name="path">在 `\User\AutoPathing` 目录下获取文件</param>
public async Task RunFileFromUser(string path)
{
var json = await AutoPathingFile.ReadText(path);
await Run(json);
}
/// <summary>
/// 判断 AutoPathing 目录下的路径是否存在
/// </summary>
/// <param name="subPath">相对于 User\AutoPathing 的路径</param>
/// <returns>存在返回 true否则返回 false</returns>
public bool IsExists(string subPath) => AutoPathingFile.IsExists(subPath);
/// <summary>
/// 判断 AutoPathing 目录下的路径是否为文件
/// </summary>
/// <param name="subPath">相对于 User\AutoPathing 的路径</param>
/// <returns>是文件返回 true否则返回 false</returns>
public bool IsFile(string subPath) => AutoPathingFile.IsFile(subPath);
/// <summary>
/// 判断 AutoPathing 目录下的路径是否为文件夹
/// </summary>
/// <param name="subPath">相对于 User\AutoPathing 的路径</param>
/// <returns>是文件夹返回 true否则返回 false</returns>
public bool IsFolder(string subPath) => AutoPathingFile.IsFolder(subPath);
/// <summary>
/// 读取 AutoPathing 目录下指定文件夹的内容(非递归方式)
/// </summary>
/// <param name="subPath">相对于 User\AutoPathing 的子目录路径,默认为相对根目录</param>
/// <returns>文件夹内所有文件和文件夹的相对路径数组,出错时返回空数组</returns>
public string[] ReadPathSync(string subPath = "./") => AutoPathingFile.ReadPathSync(subPath);
/// <summary>
/// LimitedFile 实例,用于操作 AutoPathing 目录
/// </summary>
private LimitedFile AutoPathingFile => _autoPathingFile;
}