Beispiel #1
0
        /// <summary>
        /// Deserializes data at index <paramref name="index"/>.
        /// </summary>
        /// <param name="index">Data index.</param>
        /// <exception cref="InvalidOperationException">
        /// Thrown when there's no data at index <paramref name="index"/>.
        /// </exception>
        /// <remarks>
        /// The method automatically deserializes children.
        /// </remarks>
        private void Deserialize(int index)
        {
#if DEBUG
            if (index < 0 || index >= m_SerializedBehaviorData.Length)
            {
                m_treeBuilder = null;
                throw new InvalidOperationException($"Failed to get a serialized behavior at index {index}.");
            }
#endif

            SerializedBehaviorData data = m_SerializedBehaviorData[index];
            data.serializedBehavior.AddBehavior(m_treeBuilder);

            int[] children = data.childrenIndices;
            for (int i = 0, count = children.Length; i < count; ++i)
            {
                int child = children[i];
                if (child >= 0)
                {
                    Deserialize(child);
                }
            }

            m_treeBuilder.Complete();
        }
Beispiel #2
0
        private void ValidateNulls()
        {
            for (int i = 0; i < m_SerializedBehaviorData.Length; ++i)
            {
                SerializedBehaviorData  serializedBehaviorData = m_SerializedBehaviorData[i];
                SerializedBehavior_Base serializedBehavior     = serializedBehaviorData.serializedBehavior;

                if (serializedBehavior == null)
                {
                    RemoveBehavior(i);
                    --i;
                }
            }
        }
Beispiel #3
0
        private void ValidateChildrenCounts()
        {
            for (int i = 0, count = m_SerializedBehaviorData.Length; i < count; ++i)
            {
                SerializedBehaviorData serializedBehaviorData = m_SerializedBehaviorData[i];
                int[] children       = serializedBehaviorData.childrenIndices;
                Type  serializedType = serializedBehaviorData.serializedBehavior.serializedBehaviorType;

                if (serializedType.IsSubclassOf(typeof(Leaf)))
                {
                    if (children.Length > 0)
                    {
                        serializedBehaviorData.childrenIndices = new int[0];
                    }
                }
                else if (serializedType.IsSubclassOf(typeof(Decorator)))
                {
                    if (children.Length > 1)
                    {
                        int child = children[0];
                        serializedBehaviorData.childrenIndices = new[] { child };
                    }
                    else if (children.Length < 1)
                    {
                        serializedBehaviorData.childrenIndices = new[] { -1 };
                    }
                }
                else if (serializedType.IsSubclassOf(typeof(Composite)))
                {
                    if (children.Length < 2)
                    {
                        int[] newChildren = { -1, -1 };

                        if (children.Length == 1)
                        {
                            newChildren[0] = children[0];
                        }

                        serializedBehaviorData.childrenIndices = newChildren;
                    }
                }

                m_SerializedBehaviorData[i] = serializedBehaviorData;
            }
        }
Beispiel #4
0
        private void ValidateCopies()
        {
            for (int i = 0; i < m_SerializedBehaviorData.Length; ++i)
            {
                SerializedBehaviorData  serializedBehaviorData = m_SerializedBehaviorData[i];
                SerializedBehavior_Base serializedBehavior     = serializedBehaviorData.serializedBehavior;

                bool copy = false;

                for (int behaviorIndex = 0; behaviorIndex < i & !copy; ++behaviorIndex)
                {
                    copy = serializedBehavior == m_SerializedBehaviorData[behaviorIndex].serializedBehavior;
                }

                if (copy)
                {
                    RemoveBehavior(i);
                    --i;
                }
            }
        }
Beispiel #5
0
        private void ValidateChildren()
        {
            // Self and out of bounds validation
            for (int i = 0, count = m_SerializedBehaviorData.Length; i < count; ++i)
            {
                SerializedBehaviorData serializedBehaviorData = m_SerializedBehaviorData[i];
                int[] children = serializedBehaviorData.childrenIndices;

                for (int childIndex = 0, childCount = children.Length; childIndex < childCount; ++childIndex)
                {
                    int child = children[childIndex];

                    if (child == i || child >= count)
                    {
                        children[childIndex] = -1;
                    }
                }
            }

            // Multiple links to the same node validation
            for (int i = 0, count = m_SerializedBehaviorData.Length; i < count; ++i)
            {
                int parentIndex = -1;
                int parentPort  = -1;

                for (int nodeIndex = 0; nodeIndex < count & parentIndex < 0; ++nodeIndex)
                {
                    int[] children = m_SerializedBehaviorData[nodeIndex].childrenIndices;

                    for (int childIndex = 0, childCount = children.Length;
                         childIndex < childCount & parentIndex < 0;
                         ++childIndex)
                    {
                        if (children[childIndex] == i)
                        {
                            parentIndex = nodeIndex;
                            parentPort  = childIndex;
                        }
                    }
                }

                if (parentIndex < 0)
                {
                    continue;
                }

                int[] parentChildren = m_SerializedBehaviorData[parentIndex].childrenIndices;

                for (int parentPortIndex = parentPort + 1, parentPortCount = parentChildren.Length;
                     parentPortIndex < parentPortCount;
                     ++parentPortIndex)
                {
                    if (parentChildren[parentPortIndex] == i)
                    {
                        parentChildren[parentPortIndex] = -1;
                    }
                }

                for (int nodeIndex = parentIndex + 1; nodeIndex < count; ++nodeIndex)
                {
                    int[] children = m_SerializedBehaviorData[nodeIndex].childrenIndices;

                    for (int childIndex = 0, childCount = children.Length; childIndex < childCount; ++childIndex)
                    {
                        if (children[childIndex] == i)
                        {
                            children[childIndex] = -1;
                        }
                    }
                }
            }

            // Root out of bounds validation
            if (m_RootNode >= m_SerializedBehaviorData.Length)
            {
                m_RootNode = -1;
            }

            if (m_RootNode < 0)
            {
                return;
            }

            // Multiple links with root validation
            for (int i = 0, count = m_SerializedBehaviorData.Length; i < count; ++i)
            {
                int[] children = m_SerializedBehaviorData[i].childrenIndices;

                for (int childIndex = 0, childCount = children.Length; childIndex < childCount; ++childIndex)
                {
                    if (children[childIndex] == m_RootNode)
                    {
                        children[childIndex] = -1;
                    }
                }
            }
        }