Files
better-genshin-impact/BetterGenshinImpact/Core/Script/Project/ScriptProject.cs
辉鸭蛋 6b41b52008 warning when script version number is too high
更新程序集版本号并添加版本号比较逻辑

更新了 `BetterGenshinImpact.csproj` 文件中的程序集版本号,从 `0.35.0` 更新到 `0.35.1`。

在 `Manifest.cs` 文件中,增加了对 `BetterGenshinImpact.GameTask.Common` 和 `Microsoft.Extensions.Logging` 的引用,并在 `Manifest` 类中增加了版本号比较的逻辑,如果脚本要求的版本号大于当前版本号,则记录错误日志。

在 `ScriptProject` 类的构造函数中,增加了对脚本文件夹是否存在的检查,如果不存在则抛出 `DirectoryNotFoundException` 异常。

在 `PathingTask.cs` 文件中,增加了对 `BetterGenshinImpact.Core.Config`、`BetterGenshinImpact.GameTask.Common` 和 `Microsoft.Extensions.Logging` 的引用,并在 `PathingTask` 类中增加了版本号比较的逻辑,如果路径追踪任务要求的版本号大于当前版本号,则记录错误日志。
2024-10-20 15:47:03 +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;
}
}