Beispiel #1
0
        public static void Dispose(ref T[] disposing)
        {
                        #if DEV_MODE && PI_ASSERTATIONS
            Debug.Assert(!typeof(IDrawer).IsAssignableFrom(typeof(T)), "Use DrawerArrayPool instead");
            Debug.Assert(typeof(T) != Types.UnityObject || !LinkedMemberHierarchy.AnyHierarchyTargetsArrayEquals(disposing as UnityEngine.Object[]));
                        #endif

            int length = disposing.Length;

            //don't pool zero-length arrays since we'll be using ZeroSizeArray field for those purposes
            if (length > 0)
            {
                PolymorphicPool <T[]> pool;

                                #if DEV_MODE && WARN_IF_POOLING_EXTERNALLY_CREATED_ITEMS
                if (!created.TryGetValue(length, out pool) || !pool.Contains(disposing))
                {
                    Debug.LogWarning("ArrayPool<" + StringUtils.ToString(typeof(T)) + ">.Dispose was called for array that was not created by ArrayPool. This could lead to bugs:\ndisposing: " + StringUtils.ToString(disposing));
                }
                else
                {
                    pool.Remove(disposing);
                }
                                #endif

                if (!pools.TryGetValue(length, out pool))
                {
                    pool          = new PolymorphicPool <T[]>(1, 25);
                    pools[length] = pool;
                }
                pool.Pool(ref disposing);
            }
            disposing = null;
        }
Beispiel #2
0
        public static LinkedMemberInfo Create(Object unityObject, MemberInfo[] parents)
        {
            var hierarchy = LinkedMemberHierarchy.Get(unityObject);
            LinkedMemberInfo memberInfo = null;

            for (int n = 0, lastIndex = parents.Length - 1; n <= lastIndex; n++)
            {
                var current   = parents[n];
                var fieldInfo = current as FieldInfo;
                if (fieldInfo != null)
                {
                    memberInfo = hierarchy.Get(memberInfo, fieldInfo);
                    continue;
                }
                var propertyInfo = current as PropertyInfo;
                if (propertyInfo != null)
                {
                    memberInfo = hierarchy.Get(memberInfo, propertyInfo);
                    continue;
                }

                var methodInfo = current as MethodInfo;
                if (methodInfo != null)
                {
                    memberInfo = hierarchy.Get(memberInfo, methodInfo);
                    continue;
                }

                Debug.LogError("LinkedMemberInfo.Create - MemberInfo #" + n + " type unsupported: " + StringUtils.ToString(current.GetType()));
            }
            return(memberInfo);
        }
Beispiel #3
0
        public static LinkedMemberInfo Create(LinkedMemberHierarchy hierarchy, [CanBeNull] LinkedMemberInfo parent, MethodInfo getMethodInfo, MethodInfo setMethodInfo, LinkedMemberParent setParentType)
        {
            MethodData memberData;

            if (!methodPool.TryGet(out memberData))
            {
                memberData = new MethodData();
            }
            var created = Create(hierarchy, memberData);

            created.Setup(parent, getMethodInfo, setMethodInfo, setParentType);
            return(created);
        }
Beispiel #4
0
        public static LinkedMemberInfo CreateIndexer(LinkedMemberHierarchy hierarchy, LinkedMemberInfo parent, [NotNull] PropertyInfo propertyInfo, LinkedMemberParent parentType)
        {
            IndexerData memberData;

            if (!indexerPool.TryGet(out memberData))
            {
                memberData = new IndexerData();
            }
            var created = Create(hierarchy, memberData);

            created.SetupIndexer(parent, propertyInfo, parentType);
            return(created);
        }
Beispiel #5
0
        public static LinkedMemberInfo Create(LinkedMemberHierarchy hierarchy, LinkedMemberInfo parent, [NotNull] PropertyInfo propertyInfo, LinkedMemberParent parentType, string serializedPropertyPath = null)
        {
            PropertyData memberData;

            if (!propertyPool.TryGet(out memberData))
            {
                memberData = new PropertyData();
            }
            var created = Create(hierarchy, memberData);

            created.Setup(parent, propertyInfo, parentType, serializedPropertyPath);
            return(created);
        }
Beispiel #6
0
        public static LinkedMemberInfo Create([NotNull] LinkedMemberHierarchy hierarchy, [CanBeNull] LinkedMemberInfo parent, [NotNull] FieldInfo fieldInfo, LinkedMemberParent parentType, string serializedPropertyPath = null)
        {
            FieldData memberData;

            if (!fieldPool.TryGet(out memberData))
            {
                memberData = new FieldData();
            }
            var created = Create(hierarchy, memberData);

            created.Setup(parent, fieldInfo, parentType, serializedPropertyPath);
            return(created);
        }
Beispiel #7
0
        public static LinkedMemberInfo CreateForCollectionMember([NotNull] LinkedMemberHierarchy hierarchy, [NotNull] LinkedMemberInfo parent, Type type, int collectionIndex, GetCollectionMember getDelegate, SetCollectionMember setDelegate)
        {
            CollectionMemberData memberData;

            if (!collectionMemberPool.TryGet(out memberData))
            {
                memberData = new CollectionMemberData();
            }
            var created = Create(hierarchy, memberData);

            created.SetupCollectionMember(parent, type, collectionIndex, getDelegate, setDelegate);
            return(created);
        }
Beispiel #8
0
        public static LinkedMemberInfo Create(LinkedMemberHierarchy hierarchy, [CanBeNull] LinkedMemberInfo parent, ParameterInfo parameterInfo)
        {
            ParameterData memberData;

            if (!parameterPool.TryGet(out memberData))
            {
                memberData = new ParameterData();
            }
            var created = Create(hierarchy, memberData);

            created.Setup(parent, parameterInfo);
            return(created);
        }
        public static LinkedMemberInfo Create(LinkedMemberHierarchy hierarchy, [CanBeNull] LinkedMemberInfo parent, Type genericTypeArgument, int argumentIndex)
        {
            GenericTypeArgumentData memberData;

            if (!genericTypeArgumentPool.TryGet(out memberData))
            {
                memberData = new GenericTypeArgumentData();
            }
            var created = Create(hierarchy, memberData);

            created.Setup(parent, genericTypeArgument, argumentIndex);
            return(created);
        }
Beispiel #10
0
        private static LinkedMemberInfo Create([NotNull] LinkedMemberHierarchy hierarchy, [NotNull] MemberData memberData)
        {
                        #if DEV_MODE && PI_ASSERTATIONS
            Debug.Assert(hierarchy != null);
            Debug.Assert(memberData != null);
                        #endif

            LinkedMemberInfo created;
            if (!pool.TryGet(out created))
            {
                return(new LinkedMemberInfo(hierarchy, memberData));
            }
            created.Hierarchy = hierarchy;
            created.Data      = memberData;
            return(created);
        }
Beispiel #11
0
        public static LinkedMemberInfo CreateForCollectionResizer(LinkedMemberHierarchy hierarchy, [NotNull] LinkedMemberInfo parent, [NotNull] Type type, GetSize getSizeDelegate, SetSize setSizeDelegate)
        {
                        #if DEV_MODE && PI_ASSERTATIONS
            Debug.Assert(parent != null, " CreateForCollectionResizer was called with null parent!");
            Debug.Assert(type != null, " CreateForCollectionResizer was called with null type!");
                        #endif

            CollectionResizerData memberData;
            if (!resizerPool.TryGet(out memberData))
            {
                memberData = new CollectionResizerData();
            }
            var created = Create(hierarchy, memberData);
            created.SetupCollectionResizer(parent, type, getSizeDelegate, setSizeDelegate);
            return(created);
        }
Beispiel #12
0
        public static LinkedMemberInfo Deserialize(byte[] bytes)
        {
                        #if DEV_MODE && PI_ASSERTATIONS
            UnityEngine.Debug.Assert(bytes != null);
                        #endif

            using (var stream = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                stream.Write(bytes, 0, bytes.Length);
                stream.Seek(0, SeekOrigin.Begin);
                SerializableMemberInfo deserialized;
                deserialized = formatter.Deserialize(stream) as SerializableMemberInfo;
                int count   = deserialized.targetReferences.Length;
                var targets = ArrayPool <Object> .Create(count);

                for (int n = count - 1; n >= 0; n--)
                {
                    targets[n] = ObjectIds.GetTarget(deserialized.targetReferences[n]);
                }

                var hierarchy = LinkedMemberHierarchy.Get(targets);

                LinkedMemberInfo result = null;
                var memberStack         = deserialized.memberStack;

                // TO DO: serialize parent info better for more reliable fetching

                for (int n = memberStack.Count - 1; n >= 0; n--)
                {
                    var member = memberStack.Pop();
                    result = member.Deserialize(hierarchy);
                }
                return(result);
            }
        }
Beispiel #13
0
            public LinkedMemberInfo Deserialize(LinkedMemberHierarchy hierarchy)
            {
                LinkedMemberInfo parent = null;
                var parentType          = parentChainIsBroken ? LinkedMemberParent.Missing : LinkedMemberParent.UnityObject;

                if (parentName != null)
                {
                    var allInfos = hierarchy.Members;
                    for (int n = allInfos.Count - 1; n >= 0; n--)
                    {
                        if (string.Equals(allInfos[n].Name, parentName))
                        {
                            parent = allInfos[n];
                            if (parent.IsStatic)
                            {
                                parentType = LinkedMemberParent.Static;
                            }
                            else
                            {
                                parentType = LinkedMemberParent.LinkedMemberInfo;
                            }
                            break;
                        }
                    }

                                        #if DEV_MODE && PI_ASSERTATIONS
                    UnityEngine.Debug.Assert(parent != null, "SerializableMemberInfo.Deserialize: unable to find parent by name \"" + parentName + "\"");
                                        #endif
                }

                LinkedMemberInfo result;

                switch (linkedMemberType)
                {
                case LinkedMemberType.Field:
                    result = hierarchy.Get(parent, attributeProvider as FieldInfo, parentType);
                    break;

                case LinkedMemberType.CollectionMember:
                    var getMember = getDelegateSerialized.Deserialize <GetCollectionMember>();
                    var setMember = setDelegateSerialized.Deserialize <SetCollectionMember>();
                                                #if DEV_MODE && PI_ASSERTATIONS
                    UnityEngine.Debug.Assert(getMember != null || setMember != null, "CollectionMember with type=" + StringUtils.ToStringSansNamespace(type) + " and parentName=\"" + parentName + "\" get and set were null with getDelegateSerialized=" + getDelegateSerialized);
                                                #endif
                    result = hierarchy.GetCollectionMember(parent, type, collectionIndex, getMember, setMember);
                    break;

                case LinkedMemberType.Property:
                case LinkedMemberType.Indexer:
                    result = hierarchy.Get(parent, attributeProvider as PropertyInfo, parentType);
                    break;

                case LinkedMemberType.Method:
                    result = hierarchy.Get(parent, attributeProvider as MethodInfo, parentType);
                    break;

                case LinkedMemberType.Parameter:
                    result = hierarchy.Get(parent, attributeProvider as ParameterInfo);
                    break;

                case LinkedMemberType.GenericTypeArgument:
                    result = hierarchy.Get(parent, attributeProvider as Type, collectionIndex);
                    break;

                case LinkedMemberType.CollectionResizer:
                    var get = getDelegateSerialized.Deserialize <GetSize>();
                    var set = setDelegateSerialized.Deserialize <SetSize>();
                    result = hierarchy.GetCollectionResizer(parent, type, get, set);
                    break;

                default:
                    throw new NotSupportedException("Deserializing LinkedMemberInfo of type " + linkedMemberType);
                }

                                #if UNITY_EDITOR
                if (serializedPropertyFullPath != null)
                {
                    result.SetSerializedProperty(hierarchy.SerializedObject.FindProperty(serializedPropertyFullPath));
                }
                                #endif

                return(result);
            }
Beispiel #14
0
        /// <summary>
        /// Invokes the Method, messages the user about it and updates results member if method has a return value.
        /// </summary>
        /// <param name="error"> [out] True if should display message to user about invoking or possible exceptions. </param>
        /// <param name="suppressMessages"> True if should display message to user about invoking or possible exceptions. </param>
        private bool Invoke(out string error, bool suppressMessages)
        {
                        #if DEV_MODE
            if (nowInvoking)
            {
                Debug.LogWarning(ToString() + ".Invoke called with nowInvoking already true.");
            }
                        #endif

            error = "";

            // UPDATE: Temporarily lock the view, so that this drawer won't be disposed even if invoked methods e.g. change the selected objects?
            var inspectorState = Inspector.State;
            var viewWasLocked  = inspectorState.ViewIsLocked;
            inspectorState.ViewIsLocked = true;

                        #if DEV_MODE
            nowInvoking = true;
                        #endif

            bool abort = false;

            if (hasParameters)
            {
                if (memberBuildState != MemberBuildState.MembersBuilt)
                {
                    BuildMembers();
                }
                var parameterDrawer = ParameterDrawer;
                if (!parameterDrawer.DataIsValid)
                {
                                        #if DEV_MODE
                    Debug.LogWarning(parameterDrawer + ".DataIsValid: " + StringUtils.False);
                                        #endif

                    if (!DrawGUI.Active.DisplayDialog("Invalid Parameters", "Method \"" + Name + "\" has invalid parameters. Are you sure you still want to invoke it?", "Invoke", "Cancel"))
                    {
                        error = "Aborted by user.";
                        abort = true;
                    }
                }
            }

            if (!abort)
            {
                KeyboardControlUtility.KeyboardControl = 0;
                DrawGUI.EditingTextField = false;

                if (hasReturnValue)
                {
                    // Make count be at least 1, so it works with static classes that have no targets
                    int count = Mathf.Max(1, MemberHierarchy.TargetCount);

                                        #if DEV_MODE && PI_ASSERTATIONS
                    Debug.Assert(count >= 1);
                                        #endif

                    for (int n = count - 1; n >= 0; n--)
                    {
                        results[n] = GetValue(n, out error);
                    }
                    result = results[0];

                    if (resultMemberHierarchy != null)
                    {
                        resultMemberHierarchy.DisposeMembers();
                    }
                    else
                    {
                        resultMemberHierarchy = LinkedMemberHierarchy.GetForClass(this);
                    }

                                        #if DEV_MODE
                    Debug.Log(ToString() + ".hasResult = " + StringUtils.True);
                                        #endif
                    hasResult = true;

                    if (!suppressMessages)
                    {
                        if (error.Length > 0)
                        {
                            InspectorUtility.ActiveInspector.Message(string.Concat(Name, " ", error), null, MessageType.Error, true);
                        }
                        else
                        {
                            InspectorUtility.ActiveInspector.Message(MakeMessage(string.Concat(Name, " result: ", StringUtils.ToString(result))));
                        }
                    }

                    RebuildMembers();
                }
                else
                {
                    GetValue(0, out error);
                    if (!suppressMessages)
                    {
                        if (error.Length > 0)
                        {
                            InspectorUtility.ActiveInspector.Message(string.Concat(Name, " ", error), null, MessageType.Error, true);
                        }
                        else
                        {
                            InspectorUtility.ActiveInspector.Message(MakeMessage(string.Concat(Name, " invoked.")));
                        }
                    }
                }
            }

                        #if DEV_MODE
            nowInvoking = false;
                        #endif

            UpdateDrawInSingleRow();

            // Release inspector view.
            if (inspectorState.ViewIsLocked && !viewWasLocked)
            {
                inspectorState.ViewIsLocked = false;
            }

            return(error.Length == 0);
        }