Ejemplo n.º 1
0
        public RantFunctionInfo(string name, string description, MethodInfo method)
        {
            // Sanity checks
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            if (!method.IsStatic)
            {
                throw new ArgumentException($"({method.Name}) Method is not static.");
            }

            _rawMethod = method;

            var parameters = method.GetParameters();

            if (!parameters.Any())
            {
                throw new ArgumentException($"({method.Name}) Cannot use a parameter-less method for a function.");
            }
            if (parameters[0].ParameterType != typeof(Sandbox))
            {
                throw new ArgumentException($"({method.Name}) The first parameter must be of type '{typeof(Sandbox)}'.");
            }

            // Sort out the parameter types for the function
            _params    = new RantParameter[parameters.Length - 1];
            _rawParams = parameters;
            Type type;
            RantParameterType rantType;

            for (int i = 1; i < parameters.Length; i++)
            {
                // Resolve Rant parameter type from .NET type
                type = parameters[i].ParameterType;
                if (type.IsArray && i == parameters.Length - 1)
                {
                    type = type.GetElementType();
                }

                if (type == typeof(RantAction) || type.IsSubclassOf(typeof(RantAction)))
                {
                    rantType = RantParameterType.Pattern;
                }
                else if (type == typeof(string))
                {
                    rantType = RantParameterType.String;
                }
                else if (type.IsEnum)
                {
                    rantType = type.GetCustomAttributes(typeof(FlagsAttribute), false).Any()
                        ? RantParameterType.Flags
                        : RantParameterType.Mode;
                }
                else if (IOUtil.IsNumericType(type))
                {
                    rantType = RantParameterType.Number;
                }
                else if (type == typeof(ObjectModel.RantObject))
                {
                    rantType = RantParameterType.RantObject;
                }
                else
                {
                    throw new ArgumentException($"({method.Name}) Unsupported type '{type}' for parameter '{parameters[i].Name}'. Must be a string, number, or RantAction.");
                }

                // If there is a [RantDescription] attribute on the parameter, retrieve its value. Default to empty string if there isn't one.
                string paramDescription = (parameters[i].GetCustomAttributes(typeof(RantDescriptionAttribute), false).FirstOrDefault() as RantDescriptionAttribute)?.Description ?? "";

                // Create Rant parameter
                _params[i - 1] = new RantParameter(parameters[i].Name, type, rantType,
                                                   HasParamArray = (i == parameters.Length - 1 && parameters[i].GetCustomAttributes(typeof(ParamArrayAttribute), false).FirstOrDefault() != null))
                {
                    Description = paramDescription
                };
            }
            _delegate   = Witchcraft.Create(method);
            Name        = name;
            Description = description;
        }
Ejemplo n.º 2
0
        public RantFunctionInfo(string name, string description, MethodInfo method)
        {
            // Sanity checks
            if (method == null) throw new ArgumentNullException(nameof(method));
            if (!method.IsStatic) throw new ArgumentException($"({method.Name}) Method is not static.");

            _rawMethod = method;

            var parameters = method.GetParameters();
            if (!parameters.Any())
                throw new ArgumentException($"({method.Name}) Cannot use a parameter-less method for a function.");
            if (parameters[0].ParameterType != typeof(Sandbox))
                throw new ArgumentException($"({method.Name}) The first parameter must be of type '{typeof(Sandbox)}'.");

            // Sort out the parameter types for the function
            _params = new RantParameter[parameters.Length - 1];
            _rawParams = parameters;
            Type type;
            RantParameterType rantType;
            for (int i = 1; i < parameters.Length; i++)
            {
                // Resolve Rant parameter type from .NET type
                type = parameters[i].ParameterType;
                if (type.IsArray && i == parameters.Length - 1)
                    type = type.GetElementType();

                if (type == typeof(RantAction) || type.IsSubclassOf(typeof(RantAction)))
                {
                    rantType = RantParameterType.Pattern;
                }
                else if (type == typeof(string))
                {
                    rantType = RantParameterType.String;
                }
                else if (type.IsEnum)
                {
                    rantType = type.GetCustomAttributes(typeof(FlagsAttribute), false).Any()
                        ? RantParameterType.Flags
                        : RantParameterType.Mode;
                }
                else if (IOUtil.IsNumericType(type))
                {
                    rantType = RantParameterType.Number;
                }
                else if (type == typeof(ObjectModel.RantObject))
                {
                    rantType = RantParameterType.RantObject;
                }
                else
                {
                    throw new ArgumentException($"({method.Name}) Unsupported type '{type}' for parameter '{parameters[i].Name}'. Must be a string, number, or RantAction.");
                }

                // If there is a [RantDescription] attribute on the parameter, retrieve its value. Default to empty string if there isn't one.
                string paramDescription = (parameters[i].GetCustomAttributes(typeof(RantDescriptionAttribute), false).FirstOrDefault() as RantDescriptionAttribute)?.Description ?? "";

                // Create Rant parameter
                _params[i - 1] = new RantParameter(parameters[i].Name, type, rantType,
                    HasParamArray = (i == parameters.Length - 1 && parameters[i].GetCustomAttributes(typeof(ParamArrayAttribute), false).FirstOrDefault() != null))
                {
                    Description = paramDescription
                };
            }
            _delegate = Witchcraft.Create(method);
            Name = name;
            Description = description;
        }