Beispiel #1
0
        static GUIHelper()
        {
            var guiLayoutEntryType = typeof(GUI).Assembly.GetType("UnityEngine.GUILayoutEntry");
            var hostViewType       = typeof(Editor).Assembly.GetType("UnityEditor.HostView");
            var guiViewType        = typeof(Editor).Assembly.GetType("UnityEditor.GUIView");

            inspectorWindowType = typeof(Editor).Assembly.GetType("UnityEditor.InspectorWindow");

            GetRectOnGUILayoutEntry       = EmitUtilities.CreateWeakInstanceFieldGetter <Rect>(guiLayoutEntryType, guiLayoutEntryType.GetField("rect", Flags.AllMembers));
            EditorScreenPointOffsetGetter = DeepReflection.CreateValueGetter <Vector2>(typeof(GUIUtility), "s_EditorScreenPointOffset");
            CurrentWindowIDGetter         = DeepReflection.CreateValueGetter <int>(guiViewType, "current.GetInstanceID()");
            CurrentWindowHasFocusGetter   = DeepReflection.CreateValueGetter <bool>(guiViewType, "current.hasFocus");
            GetIsDockedWindowGetter       = DeepReflection.CreateValueGetter <EditorWindow, bool>("docked");
            ContextWidthGetter            = DeepReflection.CreateValueGetter <float>(typeof(EditorGUIUtility), "contextWidth");
            ActualLabelWidthGetter        = EmitUtilities.CreateStaticFieldGetter <float>(typeof(EditorGUIUtility).GetField("s_LabelWidth", Flags.AllMembers));
            //ActualLabelWidthSetter = EmitUtilities.CreateStaticFieldSetter<float>(typeof(EditorGUIUtility).GetField("s_LabelWidth", Flags.AllMembers));

            var guiViewGetter    = DeepReflection.CreateWeakStaticValueGetter(guiViewType, guiViewType, "current");
            var actualViewGetter = DeepReflection.CreateWeakInstanceValueGetter <EditorWindow>(hostViewType, "actualView");
            var borderSizeGetter = DeepReflection.CreateWeakInstanceValueGetter <RectOffset>(hostViewType, "borderSize");

            CurrentWindowGetter           = () => actualViewGetter(guiViewGetter());
            CurrentWindowBorderSizeGetter = () => borderSizeGetter(guiViewGetter());

            SetBoldDefaultFontSetter = (Action <bool>)Delegate.CreateDelegate(typeof(Action <bool>),
                                                                              typeof(EditorGUIUtility).FindMember()
                                                                              .IsStatic()
                                                                              .IsMethod()
                                                                              .HasParameters <bool>()
                                                                              .ReturnsVoid()
                                                                              .IsNamed("SetBoldDefaultFont")
                                                                              .GetMember <MethodInfo>(), true);

            GetBoldDefaultFontGetter = (Func <bool>)Delegate.CreateDelegate(typeof(Func <bool>),
                                                                            typeof(EditorGUIUtility).FindMember()
                                                                            .IsStatic()
                                                                            .IsMethod()
                                                                            .HasNoParameters()
                                                                            .HasReturnType <bool>()
                                                                            .IsNamed("GetBoldDefaultFont")
                                                                            .GetMember <MethodInfo>(), true);

            GUILayoutEntriesCursorIndexGetter = DeepReflection.CreateValueGetter <int>(typeof(GUILayoutUtility), "current.topLevel.m_Cursor");
            GUILayoutEntriesGetter            = DeepReflection.CreateValueGetter <IList>(typeof(GUILayoutUtility), "current.topLevel.entries");
            CurrentIndentAmountGetter         = DeepReflection.CreateValueGetter <float>(typeof(EditorGUI), "indent");
            GetTopLevelLayoutRectGetter       = DeepReflection.CreateValueGetter <Rect>(typeof(GUILayoutUtility), "current.topLevel.rect");
            GetEditorWindowRectGetter         = DeepReflection.CreateValueGetter <Rect>(typeof(Editor).Assembly.GetType("UnityEditor.Toolbar"), "get.parent.screenPosition");
        }
        /// <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!";
                        }
                    }
                }
            }
        }