public static bool CompareQueryArray(ComponentType[] filter, int *typeArray, byte *accessModeArray, int typeArrayCount)
        {
            int filterLength = filter != null ? filter.Length : 0;

            if (typeArrayCount != filterLength)
            {
                return(false);
            }

            var sortedTypes = stackalloc ComponentType[filterLength];

            for (var i = 0; i < filterLength; ++i)
            {
                SortingUtilities.InsertSorted(sortedTypes, i, filter[i]);
            }

            for (var i = 0; i < filterLength; ++i)
            {
                if (typeArray[i] != sortedTypes[i].TypeIndex || accessModeArray[i] != (byte)sortedTypes[i].AccessModeType)
                {
                    return(false);
                }
            }

            return(true);
        }
        void ConstructTypeArray(ref UnsafeScratchAllocator unsafeScratchAllocator, ComponentType[] types, out int *outTypes, out byte *outAccessModes, out int outLength)
        {
            if (types == null || types.Length == 0)
            {
                outTypes       = null;
                outAccessModes = null;
                outLength      = 0;
            }
            else
            {
                outLength      = types.Length;
                outTypes       = (int *)unsafeScratchAllocator.Allocate <int>(types.Length);
                outAccessModes = (byte *)unsafeScratchAllocator.Allocate <byte>(types.Length);

                var sortedTypes = stackalloc ComponentType[types.Length];
                for (var i = 0; i < types.Length; ++i)
                {
                    SortingUtilities.InsertSorted(sortedTypes, i, types[i]);
                }

                for (int i = 0; i != types.Length; i++)
                {
                    outTypes[i]       = sortedTypes[i].TypeIndex;
                    outAccessModes[i] = (byte)sortedTypes[i].AccessModeType;
                }
            }
        }
        public static bool CompareComponents(ComponentType *componentTypes, int componentTypesCount, EntityQueryData *queryData)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            for (var k = 0; k < componentTypesCount; ++k)
            {
                if (componentTypes[k].TypeIndex == TypeManager.GetTypeIndex <Entity>())
                {
                    throw new ArgumentException(
                              "EntityQuery.CompareComponents may not include typeof(Entity), it is implicit");
                }
            }
#endif

            var sortedTypes = stackalloc ComponentType[componentTypesCount];
            for (var i = 0; i < componentTypesCount; ++i)
            {
                SortingUtilities.InsertSorted(sortedTypes, i, componentTypes[i]);
            }

            // EntityQueries are constructed including the Entity ID
            if (componentTypesCount + 1 != queryData->RequiredComponentsCount)
            {
                return(false);
            }

            for (var i = 0; i < componentTypesCount; ++i)
            {
                if (queryData->RequiredComponents[i + 1] != sortedTypes[i])
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
 public unsafe ComponentTypes(ComponentType a)
 {
     this.m_sorted        = new ResizableArray64Byte <int>();
     this.m_masks         = new Masks();
     this.m_sorted.Length = 1;
     SortingUtilities.InsertSorted((int *)this.m_sorted.GetUnsafePointer(), 0, a.TypeIndex);
     this.ComputeMasks();
 }
Ejemplo n.º 5
0
 private int PopulatedCachedTypeInArchetypeArray(ComponentType *requiredComponents, int count)
 {
     m_CachedComponentTypeInArchetypeArray[0] = new ComponentTypeInArchetype(ComponentType.Create <Entity>());
     for (var i = 0; i < count; ++i)
     {
         SortingUtilities.InsertSorted(m_CachedComponentTypeInArchetypeArray, i + 1, requiredComponents[i]);
     }
     return(count + 1);
 }
Ejemplo n.º 6
0
 int PopulatedCachedTypeInArchetypeArray(ComponentType[] requiredComponents)
 {
     m_CachedComponentTypeInArchetypeArray[0] = new ComponentTypeInArchetype(ComponentType.Create <Entity>());
     for (var i = 0; i < requiredComponents.Length; ++i)
     {
         SortingUtilities.InsertSorted(m_CachedComponentTypeInArchetypeArray, i + 1, requiredComponents[i]);
     }
     return(requiredComponents.Length + 1);
 }
Ejemplo n.º 7
0
        public static bool CompareComponents(ComponentType *componentTypes, int componentTypesCount, EntityQueryData *queryData)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            for (var k = 0; k < componentTypesCount; ++k)
            {
                if (componentTypes[k].TypeIndex == TypeManager.GetTypeIndex <Entity>())
                {
                    throw new ArgumentException(
                              "EntityQuery.CompareComponents may not include typeof(Entity), it is implicit");
                }
            }
#endif
            if ((queryData->ArchetypeQueryCount != 1) ||                              // This code path only matches one archetypequery
                (queryData->ArchetypeQuery[0].Options != EntityQueryOptions.Default)) // This code path does not allow query options
            {
                return(false);
            }

            var sortedTypes = stackalloc ComponentType[componentTypesCount];
            for (var i = 0; i < componentTypesCount; ++i)
            {
                SortingUtilities.InsertSorted(sortedTypes, i, componentTypes[i]);
            }

            ConvertComponentListToSortedIntListsNoAlloc(sortedTypes, componentTypesCount, out var componentCompareQuery);

            var includeCount   = componentCompareQuery.includeTypeIndices.Length;
            var excludeCount   = componentCompareQuery.excludeTypeIndices.Length;
            var archetypeQuery = queryData->ArchetypeQuery[0];

            if ((includeCount != archetypeQuery.AllCount) ||
                (excludeCount != archetypeQuery.NoneCount))
            {
                return(false);
            }

            for (int i = 0; i < includeCount; ++i)
            {
                if (componentCompareQuery.includeTypeIndices[i] != archetypeQuery.All[i] ||
                    componentCompareQuery.includeAccessModes[i] != archetypeQuery.AllAccessMode[i])
                {
                    return(false);
                }
            }

            for (int i = 0; i < excludeCount; ++i)
            {
                if (componentCompareQuery.excludeTypeIndices[i] != archetypeQuery.None[i] ||
                    componentCompareQuery.excludeAccessModes[i] != archetypeQuery.NoneAccessMode[i])
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 8
0
        public unsafe ComponentTypes(ComponentType a, ComponentType b)
        {
            this.m_sorted        = new ResizableArray64Byte <int>();
            this.m_masks         = new Masks();
            this.m_sorted.Length = 2;
            int *data = (int *)ref this.m_sorted.GetUnsafePointer();

            SortingUtilities.InsertSorted(data, 0, a.TypeIndex);
            SortingUtilities.InsertSorted(data, 1, b.TypeIndex);
            this.ComputeMasks();
        }
Ejemplo n.º 9
0
 private unsafe int PopulatedCachedTypeInArchetypeArray(ComponentType *requiredComponents, int count)
 {
     if ((count + 1) > 0x400)
     {
         throw new ArgumentException($"Archetypes can't hold more than {0x400}");
     }
     this.m_CachedComponentTypeInArchetypeArray[0] = new ComponentTypeInArchetype(ComponentType.Create <Entity>());
     for (int i = 0; i < count; i++)
     {
         SortingUtilities.InsertSorted(this.m_CachedComponentTypeInArchetypeArray, i + 1, requiredComponents[i]);
     }
     return(count + 1);
 }
Ejemplo n.º 10
0
        public EntityQuery CreateEntityQuery(EntityComponentStore *entityComponentStore, ManagedComponentStore managedComponentStore, ComponentType *inRequiredComponents, int inRequiredComponentsCount)
        {
            var buffer                = stackalloc byte[1024];
            var scratchAllocator      = new UnsafeScratchAllocator(buffer, 1024);
            var archetypeQuery        = CreateQuery(ref scratchAllocator, inRequiredComponents, inRequiredComponentsCount);
            var outRequiredComponents = (ComponentType *)scratchAllocator.Allocate <ComponentType>(inRequiredComponentsCount + 1);

            outRequiredComponents[0] = ComponentType.ReadWrite <Entity>();
            for (int i = 0; i != inRequiredComponentsCount; i++)
            {
                SortingUtilities.InsertSorted(outRequiredComponents + 1, i, inRequiredComponents[i]);
            }
            var outRequiredComponentsCount = inRequiredComponentsCount + 1;

            return(CreateEntityQuery(entityComponentStore, managedComponentStore, archetypeQuery, 1, outRequiredComponents, outRequiredComponentsCount));
        }
Ejemplo n.º 11
0
        private int PopulatedCachedTypeInArchetypeArray(ComponentType *requiredComponents, int count)
        {
            #if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (count + 1 > m_CachedComponentTypeInArchetypeArrayLength)
            {
                throw new System.ArgumentException($"Archetypes can't hold more than {m_CachedComponentTypeInArchetypeArrayLength}");
            }
            #endif

            m_CachedComponentTypeInArchetypeArray[0] = new ComponentTypeInArchetype(ComponentType.Create <Entity>());
            for (var i = 0; i < count; ++i)
            {
                SortingUtilities.InsertSorted(m_CachedComponentTypeInArchetypeArray, i + 1, requiredComponents[i]);
            }
            return(count + 1);
        }
Ejemplo n.º 12
0
        public unsafe ComponentTypes(ComponentType[] componentType)
        {
            this.m_sorted        = new ResizableArray64Byte <int>();
            this.m_masks         = new Masks();
            this.m_sorted.Length = componentType.Length;
            int *data   = (int *)ref this.m_sorted.GetUnsafePointer();
            int  length = 0;

            while (true)
            {
                if (length >= componentType.Length)
                {
                    this.ComputeMasks();
                    return;
                }
                SortingUtilities.InsertSorted(data, length, componentType[length].TypeIndex);
                length++;
            }
        }
        // ----------------------------------------------------------------------------------------------------------
        // PUBLIC
        // ----------------------------------------------------------------------------------------------------------

        public static Archetype *GetOrCreateArchetype(ComponentTypeInArchetype *inTypesSorted, int count,
                                                      EntityComponentStore *entityComponentStore)
        {
            var srcArchetype = entityComponentStore->GetExistingArchetype(inTypesSorted, count);

            if (srcArchetype != null)
            {
                return(srcArchetype);
            }

            srcArchetype = entityComponentStore->CreateArchetype(inTypesSorted, count);
            var types = stackalloc ComponentTypeInArchetype[count + 1];

            // Setup Instantiable archetype
            {
                UnsafeUtility.MemCpy(types, inTypesSorted, sizeof(ComponentTypeInArchetype) * count);

                var hasCleanup       = false;
                var removedTypes     = 0;
                var prefabTypeIndex  = TypeManager.GetTypeIndex <Prefab>();
                var cleanupTypeIndex = TypeManager.GetTypeIndex <CleanupEntity>();
                for (var t = 0; t < srcArchetype->TypesCount; ++t)
                {
                    var type = srcArchetype->Types[t];

                    hasCleanup |= type.TypeIndex == cleanupTypeIndex;

                    var skip = type.IsSystemStateComponent || type.TypeIndex == prefabTypeIndex;
                    if (skip)
                    {
                        ++removedTypes;
                    }
                    else
                    {
                        types[t - removedTypes] = srcArchetype->Types[t];
                    }
                }

                // Entity has already been destroyed, so it shouldn't be instantiated anymore
                if (hasCleanup)
                {
                    srcArchetype->InstantiableArchetype = null;
                }
                else if (removedTypes > 0)
                {
                    var instantiableArchetype = GetOrCreateArchetype(types, count - removedTypes, entityComponentStore);

                    srcArchetype->InstantiableArchetype = instantiableArchetype;
                    Assert.IsTrue(instantiableArchetype->InstantiableArchetype == instantiableArchetype);
                    Assert.IsTrue(instantiableArchetype->SystemStateResidueArchetype == null);
                }
                else
                {
                    srcArchetype->InstantiableArchetype = srcArchetype;
                }
            }


            // Setup System state cleanup archetype
            if (srcArchetype->SystemStateCleanupNeeded)
            {
                var  cleanupEntityType = new ComponentTypeInArchetype(ComponentType.ReadWrite <CleanupEntity>());
                bool cleanupAdded      = false;

                types[0] = inTypesSorted[0];
                var newTypeCount = 1;

                for (var t = 1; t < srcArchetype->TypesCount; ++t)
                {
                    var type = srcArchetype->Types[t];

                    if (type.IsSystemStateComponent)
                    {
                        if (!cleanupAdded && (cleanupEntityType < srcArchetype->Types[t]))
                        {
                            types[newTypeCount++] = cleanupEntityType;
                            cleanupAdded          = true;
                        }

                        types[newTypeCount++] = srcArchetype->Types[t];
                    }
                }

                if (!cleanupAdded)
                {
                    types[newTypeCount++] = cleanupEntityType;
                }

                var systemStateResidueArchetype = GetOrCreateArchetype(types, newTypeCount, entityComponentStore);
                srcArchetype->SystemStateResidueArchetype = systemStateResidueArchetype;

                Assert.IsTrue(systemStateResidueArchetype->SystemStateResidueArchetype == systemStateResidueArchetype);
                Assert.IsTrue(systemStateResidueArchetype->InstantiableArchetype == null);
            }

            // Setup meta chunk archetype
            if (count > 1)
            {
                types[0] = new ComponentTypeInArchetype(typeof(Entity));
                int metaArchetypeTypeCount = 1;
                for (int i = 1; i < count; ++i)
                {
                    var           t = inTypesSorted[i];
                    ComponentType typeToInsert;
                    if (inTypesSorted[i].IsChunkComponent)
                    {
                        typeToInsert = new ComponentType
                        {
                            TypeIndex = TypeManager.ChunkComponentToNormalTypeIndex(t.TypeIndex)
                        };
                        SortingUtilities.InsertSorted(types, metaArchetypeTypeCount++, typeToInsert);
                    }
                }

                if (metaArchetypeTypeCount > 1)
                {
                    SortingUtilities.InsertSorted(types, metaArchetypeTypeCount++, new ComponentType(typeof(ChunkHeader)));
                    srcArchetype->MetaChunkArchetype = GetOrCreateArchetype(types, metaArchetypeTypeCount, entityComponentStore);
                }
            }

            return(srcArchetype);
        }
Ejemplo n.º 14
0
        // ----------------------------------------------------------------------------------------------------------
        // PUBLIC
        // ----------------------------------------------------------------------------------------------------------
        public Archetype *GetOrCreateArchetype(ComponentTypeInArchetype *inTypesSorted, int count)
        {
            var srcArchetype = GetExistingArchetype(inTypesSorted, count);

            if (srcArchetype != null)
            {
                return(srcArchetype);
            }

            srcArchetype = CreateArchetype(inTypesSorted, count);

            var types = stackalloc ComponentTypeInArchetype[count + 1];

            srcArchetype->InstantiateArchetype = CreateInstanceArchetype(inTypesSorted, count, types, srcArchetype, true);
            srcArchetype->CopyArchetype        = CreateInstanceArchetype(inTypesSorted, count, types, srcArchetype, false);

            if (srcArchetype->InstantiateArchetype != null)
            {
                Assert.IsTrue(srcArchetype->InstantiateArchetype->InstantiateArchetype == srcArchetype->InstantiateArchetype);
                Assert.IsTrue(srcArchetype->InstantiateArchetype->SystemStateResidueArchetype == null);
            }

            if (srcArchetype->CopyArchetype != null)
            {
                Assert.IsTrue(srcArchetype->CopyArchetype->CopyArchetype == srcArchetype->CopyArchetype);
                Assert.IsTrue(srcArchetype->CopyArchetype->SystemStateResidueArchetype == null);
            }


            // Setup System state cleanup archetype
            if (srcArchetype->SystemStateCleanupNeeded)
            {
                var  cleanupEntityType = new ComponentTypeInArchetype(ComponentType.FromTypeIndex(m_CleanupEntityType));
                bool cleanupAdded      = false;

                types[0] = inTypesSorted[0];
                var newTypeCount = 1;

                for (var t = 1; t < srcArchetype->TypesCount; ++t)
                {
                    var type = srcArchetype->Types[t];

                    if (type.IsSystemStateComponent)
                    {
                        if (!cleanupAdded && (cleanupEntityType < srcArchetype->Types[t]))
                        {
                            types[newTypeCount++] = cleanupEntityType;
                            cleanupAdded          = true;
                        }

                        types[newTypeCount++] = srcArchetype->Types[t];
                    }
                }

                if (!cleanupAdded)
                {
                    types[newTypeCount++] = cleanupEntityType;
                }

                var systemStateResidueArchetype = GetOrCreateArchetype(types, newTypeCount);
                srcArchetype->SystemStateResidueArchetype = systemStateResidueArchetype;

                Assert.IsTrue(systemStateResidueArchetype->SystemStateResidueArchetype == systemStateResidueArchetype);
                Assert.IsTrue(systemStateResidueArchetype->InstantiateArchetype == null);
                Assert.IsTrue(systemStateResidueArchetype->CopyArchetype == null);
            }

            // Setup meta chunk archetype
            if (count > 1)
            {
                types[0] = new ComponentTypeInArchetype(m_EntityComponentType);
                int metaArchetypeTypeCount = 1;
                for (int i = 1; i < count; ++i)
                {
                    var           t = inTypesSorted[i];
                    ComponentType typeToInsert;
                    if (inTypesSorted[i].IsChunkComponent)
                    {
                        typeToInsert = new ComponentType
                        {
                            TypeIndex = ChunkComponentToNormalTypeIndex(t.TypeIndex)
                        };
                        SortingUtilities.InsertSorted(types, metaArchetypeTypeCount++, typeToInsert);
                    }
                }

                if (metaArchetypeTypeCount > 1)
                {
                    SortingUtilities.InsertSorted(types, metaArchetypeTypeCount++, m_ChunkHeaderComponentType);
                    srcArchetype->MetaChunkArchetype = GetOrCreateArchetype(types, metaArchetypeTypeCount);
                }
            }

            return(srcArchetype);
        }