/// <summary> /// Registers a module type to the specified table /// </summary> /// <param name="gtable">The table.</param> /// <param name="t">The type</param> /// <exception cref="ArgumentException">If the module contains some incompatibility</exception> public static Table RegisterModuleType(this Table gtable, Type t) { var table = CreateModuleNamespace(gtable, t); foreach (var mi in Framework.Do.GetMethods(t).Where(__mi => __mi.IsStatic)) { if (mi.GetCustomAttributes(typeof(MoonSharpModuleMethodAttribute), false).ToArray().Length > 0) { var attr = (MoonSharpModuleMethodAttribute)mi .GetCustomAttributes(typeof(MoonSharpModuleMethodAttribute), false).First(); if (!CallbackFunction.CheckCallbackSignature(mi, true)) { throw new ArgumentException($"Method {mi.Name} does not have the right signature."); } var deleg = Delegate.CreateDelegate( typeof(Func <ScriptExecutionContext, CallbackArguments, DynValue>), mi); var func = (Func <ScriptExecutionContext, CallbackArguments, DynValue>)deleg; string name = (!string.IsNullOrEmpty(attr.Name)) ? attr.Name : mi.Name; table.Set(name, DynValue.NewCallback(func, name)); } else if (mi.Name == "MoonSharpInit") { var args = new object[] { gtable, table }; mi.Invoke(null, args); } } foreach (var fi in Framework.Do.GetFields(t).Where(_mi => _mi.IsStatic && _mi.GetCustomAttributes(typeof(MoonSharpModuleMethodAttribute), false).ToArray().Length > 0)) { var attr = (MoonSharpModuleMethodAttribute)fi .GetCustomAttributes(typeof(MoonSharpModuleMethodAttribute), false).First(); string name = (!string.IsNullOrEmpty(attr.Name)) ? attr.Name : fi.Name; RegisterScriptField(fi, null, table, t, name); } foreach (var fi in Framework.Do.GetFields(t).Where(_mi => _mi.IsStatic && _mi.GetCustomAttributes(typeof(MoonSharpModuleConstantAttribute), false).ToArray() .Length > 0)) { var attr = (MoonSharpModuleConstantAttribute)fi .GetCustomAttributes(typeof(MoonSharpModuleConstantAttribute), false).First(); string name = (!string.IsNullOrEmpty(attr.Name)) ? attr.Name : fi.Name; RegisterScriptFieldAsConst(fi, null, table, t, name); } return(gtable); }
/// <summary> /// Registers a module type to the specified table /// </summary> /// <param name="gtable">The table.</param> /// <param name="t">The type</param> /// <returns></returns> /// <exception cref="System.ArgumentException">If the module contains some incompatibility</exception> public static Table RegisterModuleType(this Table gtable, Type t) { Table table = CreateModuleNamespace(gtable, t); foreach (MethodInfo mi in t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) { if (mi.GetCustomAttributes(typeof(MoonSharpModuleMethodAttribute), false).Length > 0) { MoonSharpModuleMethodAttribute attr = (MoonSharpModuleMethodAttribute)mi.GetCustomAttributes(typeof(MoonSharpModuleMethodAttribute), false).First(); if (!CallbackFunction.CheckCallbackSignature(mi, true)) { throw new ArgumentException(string.Format("Method {0} does not have the right signature.", mi.Name)); } Func <ScriptExecutionContext, CallbackArguments, DynValue> func = (Func <ScriptExecutionContext, CallbackArguments, DynValue>)Delegate.CreateDelegate(typeof(Func <ScriptExecutionContext, CallbackArguments, DynValue>), mi); string name = (!string.IsNullOrEmpty(attr.Name)) ? attr.Name : mi.Name; table.Set(name, DynValue.NewCallback(func, name)); } else if (mi.Name == "MoonSharpInit") { object[] args = new object[2] { gtable, table }; mi.Invoke(null, args); } } foreach (FieldInfo fi in t.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).Where(_mi => _mi.GetCustomAttributes(typeof(MoonSharpModuleMethodAttribute), false).Length > 0)) { MoonSharpModuleMethodAttribute attr = (MoonSharpModuleMethodAttribute)fi.GetCustomAttributes(typeof(MoonSharpModuleMethodAttribute), false).First(); string name = (!string.IsNullOrEmpty(attr.Name)) ? attr.Name : fi.Name; RegisterScriptField(fi, null, table, t, name); } foreach (FieldInfo fi in t.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).Where(_mi => _mi.GetCustomAttributes(typeof(MoonSharpModuleConstantAttribute), false).Length > 0)) { MoonSharpModuleConstantAttribute attr = (MoonSharpModuleConstantAttribute)fi.GetCustomAttributes(typeof(MoonSharpModuleConstantAttribute), false).First(); string name = (!string.IsNullOrEmpty(attr.Name)) ? attr.Name : fi.Name; RegisterScriptFieldAsConst(fi, null, table, t, name); } return(gtable); }