Beispiel #1
0
        private static Func <TValue> GetCachedStaticGetter(MemberInfo member)
        {
            Delegate del;

            if (GetterSetterCaches <TOwner> .Getters.TryGetInnerValue(member, typeof(TValue), out del))
            {
                return((Func <TValue>)del);
            }
            else
            {
                Func <TValue> result;

                var fieldInfo    = member as FieldInfo;
                var propertyInfo = member as PropertyInfo;

                if (fieldInfo != null)
                {
                    result = EmitUtilities.CreateStaticFieldGetter <TValue>(fieldInfo);
                }
                else if (propertyInfo != null)
                {
                    result = EmitUtilities.CreateStaticPropertyGetter <TValue>(propertyInfo);
                }
                else
                {
                    throw new ArgumentException("Cannot create a GetterSetter for a member of type + " + member.GetType().Name + "!");
                }

                GetterSetterCaches <TOwner> .Getters.AddInner(member, typeof(TValue), result);

                return(result);
            }
        }
        /// <summary>
        /// Draws the property.
        /// </summary>
        protected override void DrawPropertyLayout(InspectorProperty property, InfoBoxAttribute attribute, GUIContent label)
        {
            PropertyContext <InfoBoxContext> context = null;

            context = property.Context.Get(this, "Config_" + this.GetHashCode(), (InfoBoxContext)null);
            if (context.Value == null)
            {
                context.Value = new InfoBoxContext()
                {
                    MessageHelper = new StringMemberHelper(property.ParentType, attribute.Message)
                };

                context.Value.ErrorMessage = context.Value.MessageHelper.ErrorMessage;

                MemberInfo memberInfo;

                if (attribute.VisibleIf != null)
                {
                    // Parameter functions
                    if (property.ValueEntry != null &&
                        property.ParentType.FindMember()
                        .IsMethod()
                        .HasReturnType <bool>()
                        .HasParameters(property.ValueEntry.BaseValueType)
                        .IsNamed(attribute.VisibleIf)
                        .TryGetMember(out memberInfo, out context.Value.ErrorMessage))
                    {
                        if (context.Value.ErrorMessage == null)
                        {
                            if (memberInfo is MethodInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationParameterMethod = memberInfo as MethodInfo;
                                }
                                else
                                {
                                    context.Value.InstanceValidationParameterMethod = memberInfo as MethodInfo;
                                }
                            }
                            else
                            {
                                context.Value.ErrorMessage = "Invalid member type!";
                            }
                        }
                    }
                    // Fields, properties, and no-parameter functions.
                    else if (property.ParentType.FindMember()
                             .HasReturnType <bool>()
                             .HasNoParameters()
                             .IsNamed(attribute.VisibleIf)
                             .TryGetMember(out memberInfo, out context.Value.ErrorMessage))
                    {
                        if (context.Value.ErrorMessage == null)
                        {
                            if (memberInfo is FieldInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationCaller = EmitUtilities.CreateStaticFieldGetter <bool>(memberInfo as FieldInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValueGetter = EmitUtilities.CreateWeakInstanceFieldGetter(property.ParentType, memberInfo as FieldInfo);
                                }
                            }
                            else if (memberInfo is PropertyInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationCaller = EmitUtilities.CreateStaticPropertyGetter <bool>(memberInfo as PropertyInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValueGetter = EmitUtilities.CreateWeakInstancePropertyGetter(property.ParentType, memberInfo as PropertyInfo);
                                }
                            }
                            else if (memberInfo is MethodInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationCaller = (Func <bool>)Delegate.CreateDelegate(typeof(Func <bool>), memberInfo as MethodInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValidationMethodCaller = EmitUtilities.CreateWeakInstanceMethodCallerFunc <bool>(memberInfo as MethodInfo);
                                }
                            }
                            else
                            {
                                context.Value.ErrorMessage = "Invalid member type!";
                            }
                        }
                    }
                }
            }

            if (context.Value.ErrorMessage != null)
            {
                SirenixEditorGUI.ErrorMessageBox(context.Value.ErrorMessage);
            }
            else
            {
                if (Event.current.type == EventType.Layout)
                {
                    var parentValue = property.ParentValues[0];

                    try
                    {
                        context.Value.DrawMessageBox =
                            attribute.VisibleIf == null ||
                            (context.Value.StaticValidationParameterMethod != null && (bool)context.Value.StaticValidationParameterMethod.Invoke(null, new object[] { property.ValueEntry.WeakSmartValue })) ||
                            (context.Value.InstanceValidationParameterMethod != null && (bool)context.Value.InstanceValidationParameterMethod.Invoke(null, new object[] { property.ParentValues[0], property.ValueEntry.WeakSmartValue })) ||
                            (context.Value.InstanceValidationMethodCaller != null && context.Value.InstanceValidationMethodCaller(property.ParentValues[0])) ||
                            (context.Value.InstanceValueGetter != null && (bool)context.Value.InstanceValueGetter(ref parentValue)) ||
                            (context.Value.StaticValidationCaller != null && context.Value.StaticValidationCaller());
                    }
                    catch (System.Exception ex)
                    {
                        Debug.LogException(ex);
                    }
                }

                if (context.Value.DrawMessageBox)
                {
                    switch (attribute.InfoMessageType)
                    {
                    case InfoMessageType.None:
                        SirenixEditorGUI.MessageBox(context.Value.MessageHelper.GetString(property));
                        break;

                    case InfoMessageType.Info:
                        SirenixEditorGUI.InfoMessageBox(context.Value.MessageHelper.GetString(property));
                        break;

                    case InfoMessageType.Warning:
                        SirenixEditorGUI.WarningMessageBox(context.Value.MessageHelper.GetString(property));
                        break;

                    case InfoMessageType.Error:
                        SirenixEditorGUI.ErrorMessageBox(context.Value.MessageHelper.GetString(property));
                        break;

                    default:
                        SirenixEditorGUI.ErrorMessageBox("Unknown InfoBoxType: " + attribute.InfoMessageType.ToString());
                        break;
                    }
                }
            }

            this.CallNextDrawer(property, label);
        }
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        protected override void Initialize()
        {
            this.hideDetailedMessage = true;
            this.messageHelper       = new StringMemberHelper(this.Property, this.Attribute.Message);
            this.detailsHelper       = new StringMemberHelper(this.Property, this.Attribute.Details);
            this.errorMessage        = this.messageHelper.ErrorMessage ?? this.detailsHelper.ErrorMessage;

            if (this.Attribute.VisibleIf != null)
            {
                MemberInfo memberInfo;

                // Parameter functions
                if (this.Property.ValueEntry != null && this.Property.ParentType.FindMember()
                    .IsMethod()
                    .HasReturnType <bool>()
                    .HasParameters(this.Property.ValueEntry.BaseValueType)
                    .IsNamed(this.Attribute.VisibleIf)
                    .TryGetMember(out memberInfo, out this.errorMessage))
                {
                    if (this.errorMessage == null)
                    {
                        if (memberInfo is MethodInfo)
                        {
                            var method = memberInfo as MethodInfo;

                            if (memberInfo.IsStatic())
                            {
                                // TODO: Emit dis.
                                this.staticValidationParameterMethodCaller = p => (bool)method.Invoke(null, new object[] { p });
                            }
                            else
                            {
                                // TODO: Emit dis.
                                this.instanceValidationParameterMethodCaller = (i, p) => (bool)method.Invoke(i, new object[] { p });
                            }
                        }
                        else
                        {
                            this.errorMessage = "Invalid member type!";
                        }
                    }
                }

                // Fields, properties, and no-parameter functions.
                else if (this.Property.ParentType.FindMember()
                         .HasReturnType <bool>()
                         .HasNoParameters()
                         .IsNamed(this.Attribute.VisibleIf)
                         .TryGetMember(out memberInfo, out this.errorMessage))
                {
                    if (this.errorMessage == null)
                    {
                        if (memberInfo is FieldInfo)
                        {
                            if (memberInfo.IsStatic())
                            {
                                this.staticValidationCaller = EmitUtilities.CreateStaticFieldGetter <bool>(memberInfo as FieldInfo);
                            }
                            else
                            {
                                this.instanceValueGetter = EmitUtilities.CreateWeakInstanceFieldGetter(this.Property.ParentType, memberInfo as FieldInfo);
                            }
                        }
                        else if (memberInfo is PropertyInfo)
                        {
                            if (memberInfo.IsStatic())
                            {
                                this.staticValidationCaller = EmitUtilities.CreateStaticPropertyGetter <bool>(memberInfo as PropertyInfo);
                            }
                            else
                            {
                                this.instanceValueGetter = EmitUtilities.CreateWeakInstancePropertyGetter(this.Property.ParentType, memberInfo as PropertyInfo);
                            }
                        }
                        else if (memberInfo is MethodInfo)
                        {
                            if (memberInfo.IsStatic())
                            {
                                this.staticValidationCaller = (Func <bool>)Delegate.CreateDelegate(typeof(Func <bool>), memberInfo as MethodInfo);
                            }
                            else
                            {
                                this.instanceValidationMethodCaller = EmitUtilities.CreateWeakInstanceMethodCallerFunc <bool>(memberInfo as MethodInfo);
                            }
                        }
                        else
                        {
                            this.errorMessage = "Invalid member type!";
                        }
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Draws the property.
        /// </summary>
        protected override void DrawPropertyLayout(IPropertyValueEntry <T> entry, DetailedInfoBoxAttribute attribute, GUIContent label)
        {
            PropertyContext <InfoBoxContext> context = entry.Property.Context.Get(this, "Context", (InfoBoxContext)null);

            if (context.Value == null)
            {
                context.Value = new InfoBoxContext()
                {
                    MessageHelper = new StringMemberHelper(entry.ParentType, attribute.Message),
                    DetailsHelper = new StringMemberHelper(entry.ParentType, attribute.Details)
                };

                context.Value.ErrorMessage = context.Value.MessageHelper.ErrorMessage ?? context.Value.DetailsHelper.ErrorMessage;

                if (attribute.VisibleIf != null)
                {
                    MemberInfo memberInfo;

                    // Parameter functions
                    if (entry.ParentType.FindMember()
                        .IsMethod()
                        .HasReturnType <bool>()
                        .HasParameters <T>()
                        .IsNamed(attribute.VisibleIf)
                        .TryGetMember(out memberInfo, out context.Value.ErrorMessage))
                    {
                        if (context.Value.ErrorMessage == null)
                        {
                            if (memberInfo is MethodInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationParameterMethodCaller = (Func <T, bool>)Delegate.CreateDelegate(typeof(Func <T, bool>), memberInfo as MethodInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValidationParameterMethodCaller = EmitUtilities.CreateWeakInstanceMethodCaller <bool, T>(memberInfo as MethodInfo);
                                }
                            }
                            else
                            {
                                context.Value.ErrorMessage = "Invalid member type!";
                            }
                        }
                    }
                    // Fields, properties, and no-parameter functions.
                    else if (entry.ParentType.FindMember()
                             .HasReturnType <bool>()
                             .HasNoParameters()
                             .IsNamed(attribute.VisibleIf)
                             .TryGetMember(out memberInfo, out context.Value.ErrorMessage))
                    {
                        if (context.Value.ErrorMessage == null)
                        {
                            if (memberInfo is FieldInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationCaller = EmitUtilities.CreateStaticFieldGetter <bool>(memberInfo as FieldInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValueGetter = EmitUtilities.CreateWeakInstanceFieldGetter(entry.ParentType, memberInfo as FieldInfo);
                                }
                            }
                            else if (memberInfo is PropertyInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationCaller = EmitUtilities.CreateStaticPropertyGetter <bool>(memberInfo as PropertyInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValueGetter = EmitUtilities.CreateWeakInstancePropertyGetter(entry.ParentType, memberInfo as PropertyInfo);
                                }
                            }
                            else if (memberInfo is MethodInfo)
                            {
                                if (memberInfo.IsStatic())
                                {
                                    context.Value.StaticValidationCaller = (Func <bool>)Delegate.CreateDelegate(typeof(Func <bool>), memberInfo as MethodInfo);
                                }
                                else
                                {
                                    context.Value.InstanceValidationMethodCaller = EmitUtilities.CreateWeakInstanceMethodCallerFunc <bool>(memberInfo as MethodInfo);
                                }
                            }
                            else
                            {
                                context.Value.ErrorMessage = "Invalid member type!";
                            }
                        }
                    }
                }
            }

            if (context.Value.ErrorMessage != null)
            {
                AllEditorGUI.ErrorMessageBox(context.Value.ErrorMessage);
            }
            else
            {
                if (Event.current.type == EventType.Layout)
                {
                    var parentValue = entry.Property.ParentValues[0];

                    try
                    {
                        if (entry.Property.ValueEntry != null)
                        {
                            context.Value.DrawMessageBox =
                                attribute.VisibleIf == null ||
                                (context.Value.StaticValidationParameterMethodCaller != null && context.Value.StaticValidationParameterMethodCaller(entry.SmartValue)) ||
                                (context.Value.InstanceValidationParameterMethodCaller != null && context.Value.InstanceValidationParameterMethodCaller(entry.Property.ParentValues[0], entry.SmartValue)) ||
                                (context.Value.InstanceValidationMethodCaller != null && context.Value.InstanceValidationMethodCaller(entry.Property.ParentValues[0])) ||
                                (context.Value.InstanceValueGetter != null && (bool)context.Value.InstanceValueGetter(ref parentValue)) ||
                                (context.Value.StaticValidationCaller != null && context.Value.StaticValidationCaller());
                        }
                    }
                    catch (System.Exception ex)
                    {
                        Debug.LogException(ex);
                    }
                }

                if (context.Value.DrawMessageBox)
                {
                    var foldedConfig = entry.Property.Context.GetPersistent <bool>(this, "InfoBoxExpanded", true);

                    switch (attribute.InfoMessageType)
                    {
                    case InfoMessageType.None:
                        foldedConfig.Value = AllEditorGUI.DetailedMessageBox(context.Value.MessageHelper.GetString(entry), context.Value.DetailsHelper.GetString(entry), UnityEditor.MessageType.None, foldedConfig.Value);
                        break;

                    case InfoMessageType.Info:
                        foldedConfig.Value = AllEditorGUI.DetailedMessageBox(context.Value.MessageHelper.GetString(entry), context.Value.DetailsHelper.GetString(entry), UnityEditor.MessageType.Info, foldedConfig.Value);
                        break;

                    case InfoMessageType.Warning:
                        foldedConfig.Value = AllEditorGUI.DetailedMessageBox(context.Value.MessageHelper.GetString(entry), context.Value.DetailsHelper.GetString(entry), UnityEditor.MessageType.Warning, foldedConfig.Value);
                        break;

                    case InfoMessageType.Error:
                        foldedConfig.Value = AllEditorGUI.DetailedMessageBox(context.Value.MessageHelper.GetString(entry), context.Value.DetailsHelper.GetString(entry), UnityEditor.MessageType.Error, foldedConfig.Value);
                        break;

                    default:
                        AllEditorGUI.ErrorMessageBox("Unknown InfoBoxType: " + attribute.InfoMessageType.ToString());
                        break;
                    }
                }
            }

            this.CallNextDrawer(entry.Property, label);
        }