Class used to describe and register Lua functions. Mainly related to attribute-based registering.
Example #1
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);
                }
            }
        }
Example #2
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);
                }
            }
        }