using BetterGenshinImpact.Core.Script.Dependence;
using BetterGenshinImpact.Core.Script.Dependence.Model;
using Microsoft.ClearScript;
using System.Reflection;
using System.Threading.Tasks;
using OpenCvSharp;
using BetterGenshinImpact.Core.Recognition;
using BetterGenshinImpact.GameTask.Model.Area;
namespace BetterGenshinImpact.Core.Script;
public class EngineExtend
{
///
/// !!! 注意:这个方法会添加一些全局方法和对象,不要随便添加,以免安全风险!!!
///
///
///
///
public static void InitHost(IScriptEngine engine, string workDir, string[]? searchPaths = null)
{
// engine.AddHostObject("xHost", new ExtendedHostFunctions()); // 有越权的安全风险
// 添加我的自定义实例化对象
engine.AddHostObject("keyMouseScript", new KeyMouseScript(workDir));
engine.AddHostObject("autoPathingScript", new AutoPathingScript(workDir));
engine.AddHostObject("genshin", new Dependence.Genshin());
engine.AddHostObject("log", new Log());
engine.AddHostObject("file", new LimitedFile(workDir)); // 限制文件访问
// 任务调度器
engine.AddHostObject("dispatcher", new Dispatcher());
engine.AddHostType("RealtimeTimer", typeof(RealtimeTimer));
engine.AddHostType("SoloTask", typeof(SoloTask));
// PostMessage 作为类型实例化
engine.AddHostType("PostMessage", typeof(Dependence.Simulator.PostMessage));
// 直接添加方法
AddAllGlobalMethod(engine);
// 识图模块相关
engine.AddHostType("Mat", typeof(Mat));
engine.AddHostType("RecognitionObject", typeof(RecognitionObject));
engine.AddHostType("DesktopRegion", typeof(DesktopRegion));
engine.AddHostType("GameCaptureRegion", typeof(GameCaptureRegion));
engine.AddHostType("ImageRegion", typeof(ImageRegion));
engine.AddHostType("Region", typeof(Region));
// 添加C#的类型
engine.AddHostType(typeof(Task));
// 导入 CommonJS 模块
// https://microsoft.github.io/ClearScript/2023/01/24/module-interop.html
// https://github.com/microsoft/ClearScript/blob/master/ClearScriptTest/V8ModuleTest.cs
engine.DocumentSettings.AccessFlags = DocumentAccessFlags.EnableFileLoading | DocumentAccessFlags.AllowCategoryMismatch;
if (searchPaths != null)
{
engine.DocumentSettings.SearchPath = string.Join(';', searchPaths);
}
}
public static void AddAllGlobalMethod(IScriptEngine engine)
{
// 获取GlobalMethod类的所有静态方法
var methods = typeof(GlobalMethod).GetMethods(BindingFlags.Static | BindingFlags.Public);
foreach (var method in methods)
{
// 使用方法名首字母小写作为HostObject的名称
var methodName = char.ToLowerInvariant(method.Name[0]) + method.Name[1..];
engine.AddHostObject(methodName, method);
}
}
}