Пример #1
0
        /// <summary>
        ///   The equals.
        /// </summary>
        /// <param name="other"> The other. </param>
        /// <returns> The System.Boolean. </returns>
        public bool Equals(TaskParameterDescription other)
        {
            if (ReferenceEquals(null, other))
            {
                return(false);
            }

            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            return(Equals(other.Description, this.Description) && Equals(other.Name, this.Name) &&
                   Equals(other.ParameterName, this.ParameterName) && Equals(other.Type, this.Type) &&
                   Equals(other.Default, this.Default));
        }
Пример #2
0
        /// <summary>
        ///   Generates a task description for the specified task type.
        /// </summary>
        /// <param name="taskType"> Task type. </param>
        /// <returns> Task description. </returns>
        public static TaskDescription Generate(Type taskType)
        {
            // Check for task attribute.
            TaskAttribute[] taskAttributes =
                taskType.GetCustomAttributes(typeof(TaskAttribute), true) as TaskAttribute[];
            if (taskAttributes == null || taskAttributes.Length == 0)
            {
                throw new ArgumentException(
                          "Type {0} doesn't have a task attribute which specifies the class as a behavior tree task.",
                          taskType.Name);
            }

            TaskAttribute taskAttribute = taskAttributes[0];

            TaskDescription description = new TaskDescription
            {
                Name                  = taskAttribute.Name,
                Description           = taskAttribute.Description,
                IsDecorator           = taskAttribute.IsDecorator,
                ClassName             = taskType.Name,
                TypeName              = taskType.AssemblyQualifiedName,
                ParameterDescriptions = new List <TaskParameterDescription>()
            };

            MemberInfo[] parameterMembers = taskType.GetMembers();
            foreach (MemberInfo parameterMember in parameterMembers)
            {
                TaskParameterDescription parameterDescription = TaskParameterDescription.Generate(parameterMember);
                if (parameterDescription == null)
                {
                    continue;
                }

                description.ParameterDescriptions.Add(parameterDescription);
            }

            return(description);
        }
        /// <summary>
        ///   The equals.
        /// </summary>
        /// <param name="other"> The other. </param>
        /// <returns> The System.Boolean. </returns>
        public bool Equals(TaskParameterDescription other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }

            if (ReferenceEquals(this, other))
            {
                return true;
            }

            return Equals(other.Description, this.Description) && Equals(other.Name, this.Name)
                   && Equals(other.ParameterName, this.ParameterName) && Equals(other.Type, this.Type)
                   && Equals(other.Default, this.Default);
        }
        /// <summary>
        ///   The generate.
        /// </summary>
        /// <param name="parameterMember"> The parameter member. </param>
        /// <returns> The SlashGames.AI.BehaviorTrees.Editor.TaskParameterDescription. </returns>
        /// <exception cref="InvalidCastException"></exception>
        public static TaskParameterDescription Generate(MemberInfo parameterMember)
        {
            // Skip members which are no properties.
            if (parameterMember.MemberType != MemberTypes.Property)
            {
                return null;
            }

            PropertyInfo propertyInfo = parameterMember as PropertyInfo;
            if (propertyInfo == null)
            {
                return null;
            }

            // Check if property has task parameter attribute.
            TaskParameterAttribute[] parameterAttributes =
                parameterMember.GetCustomAttributes(typeof(TaskParameterAttribute), true) as TaskParameterAttribute[];
            if (parameterAttributes == null || parameterAttributes.Length == 0)
            {
                return null;
            }

            TaskParameterAttribute parameterAttribute = parameterAttributes[0];

            Type parameterType = propertyInfo.PropertyType;
            string parameterName = string.IsNullOrEmpty(parameterAttribute.Name)
                                       ? parameterMember.Name
                                       : parameterAttribute.Name;

            // Check if default value is of parameter type, otherwise cast it or throw exception.
            if (parameterAttribute.Default != null)
            {
                Type valueType = parameterType;
                if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof(TaskParameter<>))
                {
                    valueType = parameterType.GetGenericArguments()[0];
                }

                if (parameterAttribute.Default.GetType() != valueType)
                {
                    throw new InvalidCastException(
                        string.Format(
                            "Default value of parameter '{0}' (type: '{1}') couldn't be casted to parameter type '{2}'.",
                            parameterName,
                            parameterAttribute.Default.GetType(),
                            parameterType));
                }
            }

            // Determine meta type name.
            Type metaType = null;
            if (parameterAttribute.MetaType != null)
            {
                metaType = parameterAttribute.MetaType;
                if (metaType.IsGenericType)
                {
                    metaType = metaType.GetGenericTypeDefinition();
                }
            }

            // Create description.
            TaskParameterDescription parameterDescription = new TaskParameterDescription
                {
                    ParameterName = parameterMember.Name,
                    Name = parameterName,
                    Description = parameterAttribute.Description,
                    Default = parameterAttribute.Default,
                    MetaTypeString = metaType != null ? metaType.AssemblyQualifiedName : null,
                    Type = parameterType.AssemblyQualifiedName,
                    VisualizationType = parameterAttribute.VisualizationType
                };

            return parameterDescription;
        }
Пример #5
0
        /// <summary>
        ///   The generate.
        /// </summary>
        /// <param name="parameterMember"> The parameter member. </param>
        /// <returns> The SlashGames.AI.BehaviorTrees.Editor.TaskParameterDescription. </returns>
        /// <exception cref="InvalidCastException"></exception>
        public static TaskParameterDescription Generate(MemberInfo parameterMember)
        {
            // Skip members which are no properties.
            if (parameterMember.MemberType != MemberTypes.Property)
            {
                return(null);
            }

            PropertyInfo propertyInfo = parameterMember as PropertyInfo;

            if (propertyInfo == null)
            {
                return(null);
            }

            // Check if property has task parameter attribute.
            TaskParameterAttribute[] parameterAttributes =
                parameterMember.GetCustomAttributes(typeof(TaskParameterAttribute), true) as TaskParameterAttribute[];
            if (parameterAttributes == null || parameterAttributes.Length == 0)
            {
                return(null);
            }

            TaskParameterAttribute parameterAttribute = parameterAttributes[0];

            Type   parameterType = propertyInfo.PropertyType;
            string parameterName = string.IsNullOrEmpty(parameterAttribute.Name)
                                       ? parameterMember.Name
                                       : parameterAttribute.Name;

            // Check if default value is of parameter type, otherwise cast it or throw exception.
            if (parameterAttribute.Default != null)
            {
                Type valueType = parameterType;
                if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof(TaskParameter <>))
                {
                    valueType = parameterType.GetGenericArguments()[0];
                }

                if (parameterAttribute.Default.GetType() != valueType)
                {
                    throw new InvalidCastException(
                              string.Format(
                                  "Default value of parameter '{0}' (type: '{1}') couldn't be casted to parameter type '{2}'.",
                                  parameterName,
                                  parameterAttribute.Default.GetType(),
                                  parameterType));
                }
            }

            // Determine meta type name.
            Type metaType = null;

            if (parameterAttribute.MetaType != null)
            {
                metaType = parameterAttribute.MetaType;
                if (metaType.IsGenericType)
                {
                    metaType = metaType.GetGenericTypeDefinition();
                }
            }

            // Create description.
            TaskParameterDescription parameterDescription = new TaskParameterDescription
            {
                ParameterName     = parameterMember.Name,
                Name              = parameterName,
                Description       = parameterAttribute.Description,
                Default           = parameterAttribute.Default,
                MetaTypeString    = metaType != null ? metaType.AssemblyQualifiedName : null,
                Type              = parameterType.AssemblyQualifiedName,
                VisualizationType = parameterAttribute.VisualizationType
            };

            return(parameterDescription);
        }