moonsharp-devs / moonsharp

An interpreter for the Lua language, written entirely in C# for the .NET, Mono, Xamarin and Unity3D platforms, including handy remote debugger facilities.

Home Page:http://www.moonsharp.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implement all a namespace in the interpreter

06Games opened this issue · comments

commented

Hello,
I wanted to implement, in the Lua interpreter, all classes inside a namespace dynamically. So I wrote this script but I can not make it work.

            string code =
                @"function call ()
                    myMethod();
                end";
            Script interpreter = new Script();
            foreach (System.Type type in Tools.TypeExtensions.GetTypesInNamespace("myNamespace"))
            {
                foreach (var methodInfo in type.GetMethods())
                    interpreter.Globals[methodInfo.Name] = methodInfo;
            }
            interpreter.DoString(code);
            interpreter.Call("call");

I get this error:

ScriptRuntimeException: cannot convert clr type System.Reflection.MonoMethod
MoonSharp.Interpreter.Interop.Converters.ClrToScriptConversions.ObjectToDynValue (MoonSharp.Interpreter.Script script, System.Object obj) (at Assets/Plugins/MoonSharp/Interpreter/Interop/Converters/ClrToScriptConversions.cs:151)
MoonSharp.Interpreter.DynValue.FromObject (MoonSharp.Interpreter.Script script, System.Object obj) (at Assets/Plugins/MoonSharp/Interpreter/DataTypes/DynValue.cs:852)
MoonSharp.Interpreter.Table.set_Item (System.Object key, System.Object value) (at Assets/Plugins/MoonSharp/Interpreter/DataTypes/Table.cs:121)
AngryDash.Game.Event.myTestScript.Start () (at Assets/Scripts/myTestScript.cs:26)

EDIT: I'm using Unity 2019.1.8f1

commented

I managed to solve my problem with the code below:

            string code =
                @"function call ()
                    myClass_myMethod()
                end";
            Script interpreter = new Script();
            string Namespace = "myNamespace";
            foreach (System.Type type in Tools.TypeExtensions.GetTypesInNamespace(Namespace))
            {
                foreach (var methodInfo in type.GetMethods())
                {
                    string methodName = GetMethodNameRelativeTo(methodInfo, Namespace).Replace(".", "_"); //Replace the dots with underscores because the Lua interpreter does not understand the structure of the C#
                    interpreter.Globals[methodName] = CallbackFunction.FromMethodInfo(interpreter, methodInfo);
                }
            }

            //Returns the path to the method from the given namespace
            string GetMethodNameRelativeTo(MethodInfo methodInfo, string parentNamespace)
            {
                string _namespace = methodInfo.DeclaringType.FullName;
                if (_namespace.StartsWith(parentNamespace)) _namespace = _namespace.Remove(0, parentNamespace.Length + 1);
                return _namespace + "." + methodInfo.Name;
            }
            interpreter.DoString(code);
            interpreter.Call(interpreter.Globals["call"]);

Thanks for this library !