diff --git a/BetterGenshinImpact/Core/Script/Dependence/CustomHostFunctions.cs b/BetterGenshinImpact/Core/Script/Dependence/CustomHostFunctions.cs new file mode 100644 index 00000000..be572296 --- /dev/null +++ b/BetterGenshinImpact/Core/Script/Dependence/CustomHostFunctions.cs @@ -0,0 +1,34 @@ +using Microsoft.ClearScript; +using System; +using System.Reflection; + +namespace BetterGenshinImpact.Core.Script.Dependence; + +public class CustomHostFunctions : HostFunctions +{ + /// + /// 创建指定维度的交错数组变量 + /// + /// 数组元素类型 + /// 数组维度 + /// 交错数组变量 + public object NewVarOfArr(int dimensions) + { + try + { + Type arrayType = typeof(T); + for (int i = 0; i < dimensions; i++) + { + arrayType = arrayType.MakeArrayType(); + } + + MethodInfo newVarMethod = typeof(HostFunctions).GetMethod(nameof(newVar))!; + MethodInfo genericMethod = newVarMethod.MakeGenericMethod(arrayType); + return genericMethod.Invoke(this, new object?[] { null })!; + } + catch (Exception ex) + { + throw new InvalidOperationException($"创建维度为 {dimensions} 的数组失败: {ex.Message}", ex); + } + } +} diff --git a/BetterGenshinImpact/Core/Script/EngineExtend.cs b/BetterGenshinImpact/Core/Script/EngineExtend.cs index 176f6737..e5c487f6 100644 --- a/BetterGenshinImpact/Core/Script/EngineExtend.cs +++ b/BetterGenshinImpact/Core/Script/EngineExtend.cs @@ -87,6 +87,7 @@ public class EngineExtend engine.AddHostType("BvLocator", typeof(BvLocator)); engine.AddHostType("BvImage", typeof(BvImage)); + engine.AddHostObject("host", new CustomHostFunctions()); // 导入 JavaScript 模块 // https://microsoft.github.io/ClearScript/2023/01/24/module-interop.html