/// <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(Lua lua, Type type)
        {
            #region Sanity checks
            if (lua.IsNull())
            {
                throw new ArgumentNullException("lua");
            }

            if (type.IsNull())
            {
                throw new ArgumentNullException("type");
            }

            if (!type.IsClass)
            {
                throw new ArgumentException("The type must be a class!", "type");
            }
            #endregion

            foreach (var 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
                    }
                }
            }
        }
        public static void Enumeration <T> (Lua lua)
        {
            #region Sanity checks
            if (lua.IsNull())
            {
                throw new ArgumentNullException("lua");
            }
            #endregion

            var type = typeof(T);

            if (!type.IsEnum)
            {
                throw new ArgumentException("The type must be an enumeration!");
            }

            string[] names  = Enum.GetNames(type);
            var      values = (T[])Enum.GetValues(type);
            lua.NewTable(type.Name);

            for (int i = 0; i < names.Length; i++)
            {
                string path = type.Name + "." + names [i];
                lua [path] = values [i];
            }
        }
        /// <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(Lua lua, object o)
        {
            #region Sanity checks
            if (lua.IsNull())
            {
                throw new ArgumentNullException("lua");
            }

            if (o.IsNull())
            {
                throw new ArgumentNullException("o");
            }
            #endregion

            foreach (var 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
                    }
                }
            }
        }
Example #4
0
        public static void Enumeration <T> (Lua lua)
        {
            #region Sanity checks
            if (lua.IsNull())
            {
                throw new ArgumentNullException("lua");
            }
            #endregion

            var type = typeof(T);

            if (!type.IsEnum)
            {
                throw new ArgumentException("The type must be an enumeration!");
            }

#if SILVERLIGHT
            string[] names  = type.GetFields().Where(x => x.IsLiteral).Select(field => field.Name).ToArray();
            var      values = type.GetFields().Where(x => x.IsLiteral).Select(field => (T)field.GetValue(null)).ToArray();
#else
            string[] names  = Enum.GetNames(type);
            var      values = (T[])Enum.GetValues(type);
#endif
            lua.NewTable(type.Name);

            for (int i = 0; i < names.Length; i++)
            {
                string path = type.Name + "." + names [i];
                lua [path] = values [i];
            }
        }
Example #5
0
        /// <summary>
        /// Registers Lua functions found in the specified target.
        /// </summary>
        /// <param name="luaFunctions">Global lua function table.</param>
        /// <param name="target">Object (class,struct) to search in.</param>
        /// <param name="vm">The Lua virtual machine.</param>
        public static void RegisterLuaFunctions(Lua vm, ref Dictionary<string, LuaFunctionDescriptor> luaFunctions, object target)
        {
            if(vm.IsNull() || luaFunctions.IsNull())
                return;

            var type = target.GetType();

            foreach(var method in type.GetMethods())
            {
                foreach(var attribute in Attribute.GetCustomAttributes(method))
                {
                    var attr = attribute as LuaFunctionAttribute;

                    if(attr.IsNull())
                        continue;

                    var parameters = new List<string>();
                    var paramInfo = method.GetParameters();

                    if(!attr.FunctionParameters.IsNull() && paramInfo.Length != attr.FunctionParameters.Length)
                    {
                        Log.Error("LuaHelper", sLConsole.GetString("Function {0} (exported as {1}): argument number mismatch. Declared {2}, but requires {3}!"), method.Name, attr.FunctionName,
                            attr.FunctionParameters.Length, paramInfo.Length);
                        break;
                    }

                    // build parameter doc hashtable.
                    if(!attr.FunctionParameters.IsNull())
                        parameters.AddRange(paramInfo.Select((t, i) => string.Format("{0} - {1}", t.Name, attr.FunctionParameters[i])));

                    var descriptor = new LuaFunctionDescriptor(attr.FunctionName, attr.FunctionDocumentation, parameters);
                    luaFunctions.Add(attr.FunctionName, descriptor);
                    vm.RegisterFunction(attr.FunctionName, target, method);
                }
            }
        }