/// <summary> /// Tries to convert a CLR object to a MoonSharp value, using more in-depth analysis /// </summary> internal static DynValue ObjectToDynValue(Script script, object obj) { var v = TryObjectToSimpleDynValue(script, obj); if (v != null) { return(v); } v = UserData.Create(obj); if (v != null) { return(v); } if (obj is Type type) { v = UserData.CreateStatic(type); } // unregistered enums go as integers if (obj is Enum) { return(DynValue.NewNumber(NumericConversions.TypeToDouble(Enum.GetUnderlyingType(obj.GetType()), obj))); } if (v != null) { return(v); } if (obj is Delegate d) { return(DynValue.NewCallback(CallbackFunction.FromDelegate(script, d))); } if (obj is MethodInfo mi) { if (mi.IsStatic) { return(DynValue.NewCallback(CallbackFunction.FromMethodInfo(script, mi))); } } if (obj is IList list) { var t = TableConversions.ConvertIListToTable(script, list); return(DynValue.NewTable(t)); } if (obj is IDictionary dict) { var t = TableConversions.ConvertIDictionaryToTable(script, dict); return(DynValue.NewTable(t)); } var enumerator = EnumerationToDynValue(script, obj); if (enumerator != null) { return(enumerator); } throw ScriptRuntimeException.ConvertObjectFailed(obj); }
/// <summary> /// Tries to convert a CLR object to a MoonSharp value, using more in-depth analysis /// </summary> internal static R_VAL ObjectToValue(RubyState state, object obj) { R_VAL v = TryObjectToSimpleValue(script, obj); if (v != null) { return(v); } v = UserData.Create(obj); if (v != null) { return(v); } if (obj is Type) { v = UserData.CreateStatic(obj as Type); } // unregistered enums go as integers if (obj is Enum) { return(R_VAL.NewNumber( NumericConversions.TypeToDouble(Enum.GetUnderlyingType(obj.GetType()), obj))); } if (v != null) { return(v); } if (obj is Delegate) { return(R_VAL.NewCallback(CallbackFunction.FromDelegate(script, ( Delegate )obj))); } if (obj is MethodInfo) { MethodInfo mi = ( MethodInfo )obj; if (mi.IsStatic) { return(R_VAL.NewCallback(CallbackFunction.FromMethodInfo(script, mi))); } } if (obj is System.Collections.IList) { Table t = TableConversions.ConvertIListToTable(script, (System.Collections.IList)obj); return(R_VAL.NewTable(t)); } if (obj is System.Collections.IDictionary) { Table t = TableConversions.ConvertIDictionaryToTable(script, (System.Collections.IDictionary)obj); return(R_VAL.NewTable(t)); } var enumerator = EnumerationToValue(script, obj); if (enumerator != null) { return(enumerator); } throw ScriptRuntimeException.ConvertObjectFailed(obj); }
internal static DynValue ClrObjectToComplexMoonSharpValue(Script script, object obj) { DynValue v = TryClrObjectToSimpleMoonSharpValue(script, obj); if (v != null) { return(v); } v = UserData.Create(obj); if (v != null) { return(v); } if (obj is Type) { v = UserData.CreateStatic(obj as Type); } if (v != null) { return(v); } if (obj is Delegate) { return(DynValue.NewCallback(CallbackFunction.FromDelegate(script, (Delegate)obj))); } if (obj is MethodInfo) { MethodInfo mi = (MethodInfo)obj; if (mi.IsStatic) { return(DynValue.NewCallback(CallbackFunction.FromMethodInfo(script, mi))); } } if (obj is System.Collections.IList) { Table t = ConvertIListToTable(script, (System.Collections.IList)obj); return(DynValue.NewTable(t)); } if (obj is System.Collections.IDictionary) { Table t = ConvertIDictionaryToTable(script, (System.Collections.IDictionary)obj); return(DynValue.NewTable(t)); } if (obj is System.Collections.IEnumerable) { var enumer = (System.Collections.IEnumerable)obj; return(EnumerableWrapper.ConvertIterator(script, enumer.GetEnumerator())); } if (obj is System.Collections.IEnumerator) { var enumer = (System.Collections.IEnumerator)obj; return(EnumerableWrapper.ConvertIterator(script, enumer)); } throw ScriptRuntimeException.ConvertObjectFailed(obj); }
/// <summary> /// Tries to convert a CLR object to a MoonSharp value, using more in-depth analysis /// </summary> internal static DynValue ObjectToDynValue(Script script, object obj) { if (obj == null) { return(DynValue.Nil); } if (obj is DynValue _dyn) { return(_dyn); } if (obj is Task task) { return(ObjectToDynValue(script, new TaskWrapper(task))); } DynValue v = TryObjectToSimpleDynValue(script, obj); if (v.IsNotNil()) { return(v); } v = UserData.Create(obj); if (v.IsNotNil()) { return(v); } if (obj is Type) { v = UserData.CreateStatic(obj as Type); } // unregistered enums go as integers if (obj is Enum) { return(DynValue.NewNumber(NumericConversions.TypeToDouble(Enum.GetUnderlyingType(obj.GetType()), obj))); } if (v.IsNotNil()) { return(v); } if (obj is Delegate) { return(DynValue.NewCallback(CallbackFunction.FromDelegate(script, (Delegate)obj))); } if (obj is MethodInfo) { MethodInfo mi = (MethodInfo)obj; if (mi.IsStatic) { return(DynValue.NewCallback(CallbackFunction.FromMethodInfo(script, mi))); } } if (obj is System.Collections.IList) { Table t = TableConversions.ConvertIListToTable(script, (System.Collections.IList)obj); return(DynValue.NewTable(t)); } if (obj is System.Collections.IDictionary) { Table t = TableConversions.ConvertIDictionaryToTable(script, (System.Collections.IDictionary)obj); return(DynValue.NewTable(t)); } var enumerator = EnumerationToDynValue(script, obj); if (enumerator.IsNotNil()) { return(enumerator); } throw ScriptRuntimeException.ConvertObjectFailed(obj); }
void IMod.Init() { _G.path = Path.GetFullPath(Path.Combine(API.Helper.getModDirectory(this), "..", "..", "Lua Mods")); _G.LuaState.Globals["ScrW"] = new CallbackFunction((ScriptExecutionContext context, CallbackArguments arguments) => { return(DynValue.NewNumber(Screen.PrimaryScreen.Bounds.Width)); }); _G.LuaState.Globals["ScrH"] = new CallbackFunction((ScriptExecutionContext context, CallbackArguments arguments) => { return(DynValue.NewNumber(Screen.PrimaryScreen.Bounds.Height)); }); UserData.RegisterAssembly(); Lua.Enums.Register(_G.LuaState); Lua.Surface surface = new Lua.Surface(_G.LuaState); _G.LuaState.Globals["draw"] = new Lua.Draw(_G.LuaState, surface); _G.LuaState.Globals["surface"] = surface; _G.LuaState.Globals["hook"] = _G.hook; _G.hook.form = form; _G.LuaState.Globals["input"] = new Lua.Input(_G.LuaState); _G.LuaState.Globals["Msg"] = _G.LuaState.Globals["print"]; _G.LuaState.Globals["config"] = new Lua.Config(Path.Combine(_G.path, "config.ini")); _G.LuaState.Globals["AddConsoleCommand"] = new CallbackFunction((ScriptExecutionContext context, CallbackArguments arguments) => { return(DynValue.Nil); }); _G.LuaState.Globals["Derma_StringRequest"] = new CallbackFunction((ScriptExecutionContext context, CallbackArguments arguments) => { string title = arguments.AsStringUsingMeta(context, 0, "Derma_StringRequest"); string subtitle = arguments.AsStringUsingMeta(context, 1, "Derma_StringRequest"); string value = arguments.AsStringUsingMeta(context, 2, "Derma_StringRequest"); Closure confirm = arguments[3].Function; Closure cancel = arguments.Count > 4 ? arguments[4].Function : default(Closure); string confirmText = arguments.Count > 5 ? arguments.AsStringUsingMeta(context, 5, "Derma_StringRequest") : "OK"; string cancelText = arguments.Count > 6 ? arguments.AsStringUsingMeta(context, 6, "Derma_StringRequest") : "Cancel"; DialogResult res = _G.InputBox(ref value, title, subtitle, confirmText, cancelText); if (res == DialogResult.Cancel) { if (cancel != default(Closure)) { cancel.Call(); } } else if (res == DialogResult.OK) { confirm.Call(DynValue.NewString(value)); } return(DynValue.Nil); }); _G.LuaState.Globals["Derma_Message"] = new CallbackFunction((ScriptExecutionContext context, CallbackArguments arguments) => { string text = arguments.AsStringUsingMeta(context, 0, "Derma_Message"); string title = arguments.AsStringUsingMeta(context, 1, "Derma_Message"); string confirm = arguments.AsStringUsingMeta(context, 2, "Derma_Message"); _G.MessageBox(text, title, confirm); return(DynValue.Nil); }); _G.LuaState.Globals["HTTP"] = new CallbackFunction((ScriptExecutionContext context, CallbackArguments arguments) => { if (arguments.Count == 1 && arguments[0].Type == DataType.Table) { return(DynValue.NewBoolean(Lua.Http.Request(arguments[0].Table))); } return(DynValue.False); }); Util.include("http"); Util.include("math"); Util.include("string"); Util.include("table"); Util.include("bit"); Util.include("color"); Util.include("concommand"); Util.include("defaultcmds"); KeyEnums.Load(); GooseProxy.Register(); _G.LuaState.Globals["goose"] = new GooseProxy(_G.LuaState); _G.LuaState.Globals["GetModDirectory"] = new CallbackFunction((ScriptExecutionContext context, CallbackArguments arguments) => { return(DynValue.NewString(_G.path)); }); _G.LuaState.Globals["CurTime"] = new CallbackFunction((ScriptExecutionContext context, CallbackArguments arguments) => { return(DynValue.NewNumber(SamEngine.Time.time)); }); _G.LuaState.Globals["RegisterTask"] = CallbackFunction.FromMethodInfo(_G.LuaState, typeof(Task).GetMethod("Register")); InjectionPoints.PreTickEvent += preTick; InjectionPoints.PostTickEvent += postTick; InjectionPoints.PreRenderEvent += preRender; InjectionPoints.PostRenderEvent += postRender; InjectionPoints.PreUpdateRigEvent += preRig; InjectionPoints.PostUpdateRigEvent += postRig; Thread thread = new Thread(() => { form = new formLoader(); form.ShowDialog(); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); timer.Tick += delegate { while (_G.mainQueue.Count > 0) { try { _G.mainQueue.Dequeue().Invoke(); } catch (InterpreterException ex) { Util.MsgC(form, Color.FromArgb(255, 0, 0), string.Format("[ERROR] {0}: {1}\r\n{2}", ex.Source, ex.DecoratedMessage, ex.StackTrace), "\r\n"); } catch (Exception ex) { MessageBox.Show(ex.ToString(), ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); } } }; timer.Interval = 1; timer.Start(); }
/// <summary> /// Tries to convert a CLR object to a MoonSharp value, using more in-depth analysis /// </summary> internal static DynValue ObjectToDynValue(Script script, object obj) { DynValue v = TryObjectToSimpleDynValue(script, obj); if (v != null) { return(v); } v = UserData.Create(obj); if (v != null) { return(v); } if (obj is Type) { v = UserData.CreateStatic(obj as Type); } if (v != null) { return(v); } if (obj is Delegate) { return(DynValue.NewCallback(CallbackFunction.FromDelegate(script, (Delegate)obj))); } if (obj is MethodInfo) { MethodInfo mi = (MethodInfo)obj; if (mi.IsStatic) { return(DynValue.NewCallback(CallbackFunction.FromMethodInfo(script, mi))); } } if (obj is System.Collections.IList) { Table t = TableConversions.ConvertIListToTable(script, (System.Collections.IList)obj); return(DynValue.NewTable(t)); } if (obj is System.Collections.IDictionary) { Table t = TableConversions.ConvertIDictionaryToTable(script, (System.Collections.IDictionary)obj); return(DynValue.NewTable(t)); } var enumerator = EnumerationToDynValue(script, obj); if (enumerator != null) { return(enumerator); } throw ScriptRuntimeException.ConvertObjectFailed(obj); }
/// <summary> /// Tries to convert a CLR object to a MoonSharp value, using more in-depth analysis /// </summary> internal static DynValue ObjectToDynValue(Script script, object obj) { DynValue v = TryObjectToSimpleDynValue(script, obj); if (v != null) { return(v); } v = UserData.Create(obj); if (v != null) { return(v); } if (obj is Type) { v = UserData.CreateStatic(obj as Type); } // unregistered enums go as integers if (obj is Enum) { return(DynValue.NewNumber(NumericConversions.TypeToDouble(Enum.GetUnderlyingType(obj.GetType()), obj))); } if (v != null) { return(v); } if (obj is Delegate) { return(DynValue.NewCallback(CallbackFunction.FromDelegate(script, (Delegate)obj))); } if (obj is MethodInfo) { MethodInfo mi = (MethodInfo)obj; if (mi.IsStatic) { return(DynValue.NewCallback(CallbackFunction.FromMethodInfo(script, mi))); } } if (obj is System.Collections.IList) { Table t = TableConversions.ConvertIListToTable(script, (System.Collections.IList)obj); return(DynValue.NewTable(t)); } if (obj is System.Collections.IDictionary) { Table t = TableConversions.ConvertIDictionaryToTable(script, (System.Collections.IDictionary)obj); return(DynValue.NewTable(t)); } #if HASDYNAMIC var objType = obj.GetType(); #if PCL var isTuple = objType.IsGenericType && objType.GetInterfaces().Where(f => f.Name == "ITuple").Count() > 0; #else var isTuple = objType.IsGenericType && objType.GetInterface("ITuple") != null; #endif if (isTuple) { var args = objType.GetGenericArguments().Length; var vals = new DynValue[args]; for (int i = 0; i < args; i++) { var prop = objType.GetProperty("Item" + (i + 1)); var val = prop.GetValue(obj, null); vals[i] = DynValue.FromObject(script, val); } return(DynValue.NewTupleNested(vals)); } #endif var enumerator = EnumerationToDynValue(script, obj); if (enumerator != null) { return(enumerator); } throw ScriptRuntimeException.ConvertObjectFailed(obj); }