Files
better-genshin-impact/BetterGenshinImpact/Core/Script/Project/ScriptProject.cs
辉鸭蛋 44190a522b team identification support online
修复错误信息,改进联机状态处理

更新了 `BetterGenshinImpact.csproj` 文件中的程序集版本号,从 `0.35.2` 更新为 `0.35.4`。

修正了 `ScriptProject.cs` 文件中抛出 `FileNotFoundException` 异常时的错误信息,将 "manifest.json文件存在" 改为 "manifest.json文件不存在"。

在 `AutoFightAssets.cs` 文件中:
- 为 `AvatarSideIconRectList` 和 `AvatarIndexRectList` 添加了注释,解释其在非联机状态下的用途。
- 添加了多个新的属性和注释,用于处理联机状态下的角色头像和对应的白色块位置。
- 初始化了 `OnePRa` 和 `PRa` 两个识别对象,用于识别联机状态下的1P和P图标。

在 `Avatar.cs` 文件中:
- 修改了角色切换逻辑,使用 `CombatScenes.ExpectedTeamAvatarNum` 替代硬编码的数字。
- 在 `TrySwitch` 方法中添加了 `needLog` 参数,并在切换成功时记录日志。
- 移除了部分注释代码,并在日志中保存了角色切换和索引区域的截图。
- 添加了 `System.Diagnostics` 的引用。

在 `CombatScenes.cs` 文件中:
- 将 `Avatars` 初始化为空数组。
- 添加了 `ExpectedTeamAvatarNum` 属性,默认值为4。
- 在 `InitializeTeam` 方法中添加了联机状态的判断和处理逻辑。
- 修改了队伍识别逻辑,使用动态数组替代固定长度的数组。
- 修改了 `CheckTeamInitialized` 方法,使用 `ExpectedTeamAvatarNum` 替代硬编码的数字。
- 修改了 `BuildAvatars` 方法,添加了对联机状态下角色编号位置信息的处理。
- 修改了 `SelectAvatar` 方法,使用 `GetValueOrDefault` 替代 `TryGetValue`。

在 `ScriptControlViewModel.cs` 文件中,设置 `WindowStartupLocation` 为 `WindowStartupLocation.CenterOwner`。

添加了 `1p.png` 和 `p.png` 两个新图像文件,用于识别联机状态下的1P和P图标。
2024-10-27 17:15:55 +08:00

101 lines
2.8 KiB
C#
Raw 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 BetterGenshinImpact.Core.Config;
using Microsoft.ClearScript;
using Microsoft.ClearScript.V8;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace BetterGenshinImpact.Core.Script.Project;
public class ScriptProject
{
public string ProjectPath { get; set; }
public string ManifestFile { get; set; }
public Manifest Manifest { get; set; }
public string FolderName { get; set; }
public ScriptProject(string folderName)
{
FolderName = folderName;
ProjectPath = Path.Combine(Global.ScriptPath(), folderName);
if (!Directory.Exists(ProjectPath))
{
throw new DirectoryNotFoundException("脚本文件夹不存在:" + ProjectPath);
}
ManifestFile = Path.GetFullPath(Path.Combine(ProjectPath, "manifest.json"));
if (!File.Exists(ManifestFile))
{
throw new FileNotFoundException("manifest.json文件不存在请确认此脚本是JS脚本类型。" + ManifestFile);
}
Manifest = Manifest.FromJson(File.ReadAllText(ManifestFile));
Manifest.Validate(ProjectPath);
}
public StackPanel? LoadSettingUi(dynamic context)
{
var settingItems = Manifest.LoadSettingItems(ProjectPath);
if (settingItems.Count == 0)
{
return null;
}
var stackPanel = new StackPanel();
foreach (var item in settingItems)
{
var controls = item.ToControl(context);
foreach (var control in controls)
{
stackPanel.Children.Add(control);
}
}
return stackPanel;
}
public IScriptEngine BuildScriptEngine()
{
IScriptEngine engine = new V8ScriptEngine(V8ScriptEngineFlags.UseCaseInsensitiveMemberBinding | V8ScriptEngineFlags.EnableTaskPromiseConversion);
EngineExtend.InitHost(engine, ProjectPath, Manifest.Library);
return engine;
}
public async Task ExecuteAsync(dynamic? context = null)
{
var code = await LoadCode();
var engine = BuildScriptEngine();
if (context != null)
{
// 写入配置的内容
engine.AddHostObject("settings", context);
}
try
{
await (Task)engine.Evaluate(code);
}
catch (Exception e)
{
Debug.WriteLine(e);
throw;
}
finally
{
engine.Dispose();
}
}
public async Task<string> LoadCode()
{
var code = await File.ReadAllTextAsync(Path.Combine(ProjectPath, Manifest.Main));
if (string.IsNullOrEmpty(code))
{
throw new FileNotFoundException("main js is empty.");
}
return code;
}
}