Пример #1
0
        /// <summary>
        /// Registers all public static methods in a class tagged with <see cref="LuaGlobalAttribute"/> as Lua global functions
        /// </summary>
        /// <param name="lua">The Lua VM to add the methods to</param>
        /// <param name="type">The class type to get the methods from</param>
        public static void TaggedStaticMethods(LuaInterface lua, Type type)
        {
            #region Sanity checks
            if (lua == null)
            {
                throw new ArgumentNullException("lua");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (!type.IsClass)
            {
                throw new ArgumentException("The type must be a class!", "type");
            }
            #endregion

            foreach (MethodInfo method in type.GetMethods(BindingFlags.Static | BindingFlags.Public))
            {
                foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), false))
                {
                    if (string.IsNullOrEmpty(attribute.Name))
                    {
                        lua.RegisterFunction(method.Name, null, method); // CLR name
                    }
                    else
                    {
                        lua.RegisterFunction(attribute.Name, null, method); // Custom name
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Registers all public instance methods in an object tagged with <see cref="LuaGlobalAttribute"/> as Lua global functions
        /// </summary>
        /// <param name="lua">The Lua VM to add the methods to</param>
        /// <param name="o">The object to get the methods from</param>
        public static void TaggedInstanceMethods(LuaInterface lua, object o)
        {
            #region Sanity checks
            if (lua == null)
            {
                throw new ArgumentNullException("lua");
            }
            if (o == null)
            {
                throw new ArgumentNullException("o");
            }
            #endregion

            foreach (MethodInfo method in o.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), true))
                {
                    if (string.IsNullOrEmpty(attribute.Name))
                    {
                        lua.RegisterFunction(method.Name, o, method); // CLR name
                    }
                    else
                    {
                        lua.RegisterFunction(attribute.Name, o, method); // Custom name
                    }
                }
            }
        }
Пример #3
0
        /*
         * Sample test script that shows some of the capabilities of
         * LuaInterface
         */
        public static void Main()
        {
            Console.WriteLine("Starting interpreter...");
            LuaInterface l = new LuaInterface();


            // Pause so we can connect with the debugger
            // Thread.Sleep(30000);


            Console.WriteLine("Reading test.lua file...");
            l.DoFile("test.lua");
            double width = l.GetNumber("width");
            double height = l.GetNumber("height");
            string message = l.GetString("message");
            double color_r = l.GetNumber("color.r");
            double color_g = l.GetNumber("color.g");
            double color_b = l.GetNumber("color.b");
            Console.WriteLine("Printing values of global variables width, height and message...");
            Console.WriteLine("width: " + width);
            Console.WriteLine("height: " + height);
            Console.WriteLine("message: " + message);
            Console.WriteLine("Printing values of the 'color' table's fields...");
            Console.WriteLine("color.r: " + color_r);
            Console.WriteLine("color.g: " + color_g);
            Console.WriteLine("color.b: " + color_b);
            width = 150;
            Console.WriteLine("Changing width's value and calling Lua function print to show it...");
            l["width"] = width;
            l.GetFunction("print").Call(width);
            message = "LuaNet Interface Test";
            Console.WriteLine("Changing message's value and calling Lua function print to show it...");
            l["message"] = message;
            l.GetFunction("print").Call(message);
            color_r = 30;
            color_g = 10;
            color_b = 200;
            Console.WriteLine("Changing color's fields' values and calling Lua function print to show it...");
            l["color.r"] = color_r;
            l["color.g"] = color_g;
            l["color.b"] = color_b;
            l.DoString("print(color.r,color.g,color.b)");
            Console.WriteLine("Printing values of the tree table's fields...");
            double leaf1 = l.GetNumber("tree.branch1.leaf1");
            string leaf2 = l.GetString("tree.branch1.leaf2");
            string leaf3 = l.GetString("tree.leaf3");
            Console.WriteLine("leaf1: " + leaf1);
            Console.WriteLine("leaf2: " + leaf2);
            Console.WriteLine("leaf3: " + leaf3);
            leaf1 = 30; leaf2 = "new leaf2";
            Console.WriteLine("Changing tree's fields' values and calling Lua function print to show it...");
            l["tree.branch1.leaf1"] = leaf1; l["tree.branch1.leaf2"] = leaf2;
            l.DoString("print(tree.branch1.leaf1,tree.branch1.leaf2)");
            Console.WriteLine("Returning values from Lua with 'return'...");
            object[] vals = l.DoString("return 2,3");
            Console.WriteLine("Returned: " + vals[0] + " and " + vals[1]);
            Console.WriteLine("Calling a Lua function that returns multiple values...");
            object[] vals1 = l.GetFunction("func").Call(2, 3);
            Console.WriteLine("Returned: " + vals1[0] + " and " + vals1[1]);
            Console.WriteLine("Creating a table and filling it from C#...");
            l.NewTable("tab");
            l.NewTable("tab.tab");
            l["tab.a"] = "a!";
            l["tab.b"] = 5.5;
            l["tab.tab.c"] = 6.5;
            l.DoString("print(tab.a,tab.b,tab.tab.c)");
            Console.WriteLine("Setting a table as another table's field...");
            l["tab.a"] = l["tab.tab"];
            l.DoString("print(tab.a.c)");
            Console.WriteLine("Registering a C# static method and calling it from Lua...");

            // Pause so we can connect with the debugger
            // Thread.Sleep(30000);

            l.RegisterFunction("func1", null, typeof(TestLuaInterface).GetMethod("func"));
            vals1 = l.GetFunction("func1").Call(2, 3);
            Console.WriteLine("Returned: " + vals1[0]);
            TestLuaInterface obj = new TestLuaInterface();
            Console.WriteLine("Registering a C# instance method and calling it from Lua...");
            l.RegisterFunction("func2", obj, typeof(TestLuaInterface).GetMethod("funcInstance"));
            vals1 = l.GetFunction("func2").Call(2, 3);
            Console.WriteLine("Returned: " + vals1[0]);

            Console.WriteLine("Testing throwing an exception...");
            obj.ThrowUncaughtException();

            Console.WriteLine("Testing catching an exception...");
            obj.ThrowException();

            Console.WriteLine("Testing inheriting a method from Lua...");
            obj.LuaTableInheritedMethod();

            Console.WriteLine("Testing overriding a C# method with Lua...");
            obj.LuaTableOverridedMethod();

            Console.WriteLine("Stress test RegisterFunction (based on a reported bug)..");
            obj.RegisterFunctionStressTest();

            Console.WriteLine("Test structures...");
            obj.TestStructs();

            Console.WriteLine("Test Nullable types...");
            obj.TestNullable();

            Console.WriteLine("Test functions...");
            obj.TestFunctions();

            Console.WriteLine("Test event exceptions...");
            obj.TestEventException();

            Console.Write("Press any key to continue...");
            Console.ReadKey(true);
            //Console.ReadLine();
        }