예제 #1
0
        protected InspectorPropertyInfo GetInfoForPortAtIndex(int index)
        {
            InspectorPropertyInfo childPortInfo;

            if (!childPortInfos.TryGetValue(index, out childPortInfo))
            {
                InspectorPropertyInfo sourceChildInfo = base.GetChildInfo(index);

                string   portName = $"{nodePortInfo.BaseFieldName} {index}";
                Node     node     = nodePortInfo.Node;
                NodePort port     = node.GetPort(portName);

                childPortInfo = InspectorPropertyInfo.CreateValue(
                    $"{CollectionResolverUtilities.DefaultIndexToChildName( index )}:port",
                    0,
                    Property.ValueEntry.SerializationBackend,
                    new GetterSetter <TList, NodePort>(
                        ( ref TList owner ) => port,
                        (ref TList owner, NodePort value) => { }
                        )
                    , new HideInInspector()
                    );

                var childNodePortInfo = new NodePortInfo(
                    childPortInfo,
                    sourceChildInfo,
                    portName,
                    typeof(TElement),
                    node,                     // Needed?
                    nodePortInfo.ShowBackingValue,
                    nodePortInfo.ConnectionType,
                    nodePortInfo.TypeConstraint,
                    nodePortInfo.IsDynamicPortList,
                    true,
                    nodePortInfo.IsInput,
                    noDataResolver == null
                    );

                propertyToNodeProperty[sourceChildInfo.PropertyName] = childPortInfo.PropertyName;
                nameToNodePropertyInfo[childPortInfo.PropertyName]   = childNodePortInfo;

                childPortInfos[index] = childPortInfo;
            }
            return(childPortInfo);
        }
        protected virtual bool TryValidateMemberRecursivelyAsCollection(object collection, MemberInfo collectionMember, IMemberSelector selector, UnityEngine.Object root, List <ValidationPathStep> pathSoFar, HashSet <object> seenReferences, HashSet <MemberInfo> seenMembers, List <ValidationResult> results, int scanDepth)
        {
            scanDepth++;

            if (collection is Array)
            {
                int index       = 0;
                var elementType = collection.GetType().GetElementType();

                if (elementType.IsPrimitive)
                {
                    if (!this.ValidatorLocator.PotentiallyHasValidatorsFor(collectionMember, elementType, true))
                    {
                        // Don't actually validate the items in the array
                        return(true);
                    }
                }

                foreach (var item in collection as Array)
                {
                    var path = pathSoFar.ToList();

                    path.Add(new ValidationPathStep()
                    {
                        Member     = collectionMember,
                        Value      = collection,
                        StepString = CollectionResolverUtilities.DefaultIndexToChildName(index)
                    });

                    var itemType = item == null ? elementType : item.GetType();

                    ValidateMemberRecursive(collection, collectionMember, item, itemType, selector, root, path, seenReferences, seenMembers, results, scanDepth, true);
                    index++;
                }

                return(true);
            }
            else if (collection is IList)
            {
                var elementType = collection.GetType().ImplementsOpenGenericInterface(typeof(IList <>)) ? collection.GetType().GetArgumentsOfInheritedOpenGenericInterface(typeof(IList <>))[0] : typeof(object);

                if (elementType.IsPrimitive)
                {
                    if (!this.ValidatorLocator.PotentiallyHasValidatorsFor(collectionMember, elementType, true))
                    {
                        // Don't actually validate the items in the list
                        return(true);
                    }
                }

                int index = 0;
                foreach (var item in collection as IList)
                {
                    var path = pathSoFar.ToList();

                    path.Add(new ValidationPathStep()
                    {
                        Member     = collectionMember,
                        Value      = collection,
                        StepString = CollectionResolverUtilities.DefaultIndexToChildName(index)
                    });

                    var itemType = item == null ? elementType : item.GetType();

                    ValidateMemberRecursive(collection, collectionMember, item, itemType, selector, root, path, seenReferences, seenMembers, results, scanDepth, true);
                    index++;
                }

                return(true);
            }
            else if (collection is IDictionary)
            {
                Type baseKeyType, baseValueType;

                if (collection.GetType().ImplementsOpenGenericInterface(typeof(IDictionary <,>)))
                {
                    var args = collection.GetType().GetArgumentsOfInheritedOpenGenericInterface(typeof(IDictionary <,>));
                    baseKeyType   = args[0];
                    baseValueType = args[1];
                }
                else
                {
                    baseKeyType   = typeof(object);
                    baseValueType = typeof(object);
                }

                foreach (DictionaryEntry entry in collection as IDictionary)
                {
                    var keyPath   = pathSoFar.ToList();
                    var valuePath = pathSoFar.ToList();

                    var keyStr = DictionaryKeyUtility.GetDictionaryKeyString(entry.Key);

                    keyPath.Add(new ValidationPathStep()
                    {
                        Member     = collectionMember,
                        Value      = entry.Key,
                        StepString = keyStr + "#key"
                    });

                    valuePath.Add(new ValidationPathStep()
                    {
                        Member     = collectionMember,
                        Value      = entry.Value,
                        StepString = keyStr
                    });

                    var keyType   = entry.Key == null ? baseKeyType : entry.Key.GetType();
                    var valueType = entry.Value == null ? baseValueType : entry.Value.GetType();

                    ValidateMemberRecursive(collection, collectionMember, entry.Key, keyType, selector, root, keyPath, seenReferences, seenMembers, results, scanDepth, true);
                    ValidateMemberRecursive(collection, collectionMember, entry.Value, valueType, selector, root, valuePath, seenReferences, seenMembers, results, scanDepth, true);
                }

                return(true);
            }
            else if (collection is IEnumerable && collection is ISerializable && collection is IDeserializationCallback && collection.GetType().ImplementsOpenGenericClass(typeof(HashSet <>)))
            {
                var elementType = collection.GetType().GetArgumentsOfInheritedOpenGenericType(typeof(HashSet <>))[0];

                foreach (var item in collection as IEnumerable)
                {
                    var path = pathSoFar.ToList();

                    path.Add(new ValidationPathStep()
                    {
                        Member     = collectionMember,
                        Value      = collection,
                        StepString = DictionaryKeyUtility.GetDictionaryKeyString(item)
                    });

                    var itemType = item == null ? elementType : item.GetType();
                    ValidateMemberRecursive(collection, collectionMember, item, itemType, selector, root, path, seenReferences, seenMembers, results, scanDepth, true);
                }

                return(true);
            }
            else if (collection is ICollection || collection.GetType().ImplementsOpenGenericInterface(typeof(ICollection <>)))
            {
                Type elementType;

                if (collection.GetType().ImplementsOpenGenericInterface(typeof(ICollection <>)))
                {
                    elementType = collection.GetType().GetArgumentsOfInheritedOpenGenericType(typeof(ICollection <>))[0];
                }
                else
                {
                    elementType = typeof(object);
                }

                int index = 0;
                foreach (var item in collection as IEnumerable)
                {
                    var path = pathSoFar.ToList();

                    path.Add(new ValidationPathStep()
                    {
                        Member     = collectionMember,
                        Value      = collection,
                        StepString = CollectionResolverUtilities.DefaultIndexToChildName(index)
                    });

                    var itemType = item == null ? elementType : item.GetType();
                    ValidateMemberRecursive(collection, collectionMember, item, itemType, selector, root, path, seenReferences, seenMembers, results, scanDepth, true);
                    index++;
                }

                return(true);
            }

            return(false);
        }