예제 #1
0
        public BufferAccessor <T> GetBufferAccessor <T>(ArchetypeChunkBufferType <T> bufferComponentType)
            where T : struct, IBufferElementData
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.CheckReadAndThrow(bufferComponentType.m_Safety0);
#endif
            var archetype            = m_Chunk->Archetype;
            var typeIndex            = bufferComponentType.m_TypeIndex;
            var typeIndexInArchetype = ChunkDataUtility.GetIndexInTypeArray(archetype, typeIndex);
            if (typeIndexInArchetype == -1)
            {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                return(new BufferAccessor <T>(null, 0, 0, true, bufferComponentType.m_Safety0, bufferComponentType.m_Safety1));
#else
                return(new BufferAccessor <T>(null, 0, 0));
#endif
            }

            if (!bufferComponentType.IsReadOnly)
            {
                m_Chunk->ChangeVersion[typeIndexInArchetype] = bufferComponentType.GlobalSystemVersion;
            }

            var buffer      = m_Chunk->Buffer;
            var length      = m_Chunk->Count;
            var startOffset = archetype->Offsets[typeIndexInArchetype];
            int stride      = archetype->SizeOfs[typeIndexInArchetype];
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            return(new BufferAccessor <T>(buffer + startOffset, length, stride, bufferComponentType.IsReadOnly, bufferComponentType.m_Safety0, bufferComponentType.m_Safety1));
#else
            return(new BufferAccessor <T>(buffer + startOffset, length, stride));
#endif
        }
예제 #2
0
        public bool Has <T>(ArchetypeChunkBufferType <T> chunkBufferType)
            where T : struct, IBufferElementData
        {
            var typeIndexInArchetype = ChunkDataUtility.GetIndexInTypeArray(m_Chunk->Archetype, chunkBufferType.m_TypeIndex);

            return(typeIndexInArchetype != -1);
        }
예제 #3
0
        public void AssertWillDestroyAllInLinkedEntityGroup(NativeArray <ArchetypeChunk> chunkArray,
                                                            ArchetypeChunkBufferType <LinkedEntityGroup> linkedGroupType,
                                                            ref Entity errorEntity,
                                                            ref Entity errorReferencedEntity)
        {
            var chunks      = (ArchetypeChunk *)chunkArray.GetUnsafeReadOnlyPtr();
            var chunksCount = chunkArray.Length;

            var tempChunkStateFlag = (uint)ChunkFlags.TempAssertWillDestroyAllInLinkedEntityGroup;

            for (int i = 0; i != chunksCount; i++)
            {
                var chunk = chunks[i].m_Chunk;
                Assert.IsTrue((chunk->Flags & tempChunkStateFlag) == 0);
                chunk->Flags |= tempChunkStateFlag;
            }

            errorEntity           = default;
            errorReferencedEntity = default;

            for (int i = 0; i < chunkArray.Length; ++i)
            {
                if (!chunks[i].Has(linkedGroupType))
                {
                    continue;
                }

                var chunk   = chunks[i];
                var buffers = chunk.GetBufferAccessor(linkedGroupType);

                for (int b = 0; b != buffers.Length; b++)
                {
                    var buffer      = buffers[b];
                    int entityCount = buffer.Length;
                    var entities    = (Entity *)buffer.GetUnsafeReadOnlyPtr();
                    for (int e = 0; e != entityCount; e++)
                    {
                        var referencedEntity = entities[e];
                        if (Exists(referencedEntity))
                        {
                            var referencedChunk = GetChunk(referencedEntity);

                            if ((referencedChunk->Flags & tempChunkStateFlag) == 0)
                            {
                                errorEntity           = entities[0];
                                errorReferencedEntity = referencedEntity;
                            }
                        }
                    }
                }
            }

            for (int i = 0; i != chunksCount; i++)
            {
                var chunk = chunks[i].m_Chunk;
                Assert.IsTrue((chunk->Flags & tempChunkStateFlag) != 0);
                chunk->Flags &= ~tempChunkStateFlag;
            }
        }
예제 #4
0
        public uint GetComponentVersion <T>(ArchetypeChunkBufferType <T> chunkBufferType)
            where T : struct, IBufferElementData
        {
            var typeIndexInArchetype = ChunkDataUtility.GetIndexInTypeArray(m_Chunk->Archetype, chunkBufferType.m_TypeIndex);

            if (typeIndexInArchetype == -1)
            {
                return(0);
            }
            return(m_Chunk->GetChangeVersion(typeIndexInArchetype));
        }
        public unsafe BufferAccessor <T> GetBufferAccessor <T>(ArchetypeChunkBufferType <T> bufferComponentType) where T : struct, IBufferElementData
        {
            BufferAccessor <T> accessor;

            AtomicSafetyHandle.CheckReadAndThrow(bufferComponentType.m_Safety);
            Unity.Entities.Archetype *archetype = this.m_Chunk.Archetype;
            int indexInTypeArray = ChunkDataUtility.GetIndexInTypeArray(archetype, bufferComponentType.m_TypeIndex);

            if (indexInTypeArray == -1)
            {
                accessor = new BufferAccessor <T>(null, 0, 0, true, bufferComponentType.m_Safety, bufferComponentType.m_ArrayInvalidationSafety);
            }
            else
            {
                if (!bufferComponentType.IsReadOnly)
                {
                    this.m_Chunk.ChangeVersion[indexInTypeArray] = bufferComponentType.GlobalSystemVersion;
                }
                int count = this.m_Chunk.Count;
                accessor = new BufferAccessor <T>(&this.m_Chunk.Buffer.FixedElementField + archetype->Offsets[indexInTypeArray], count, archetype->SizeOfs[indexInTypeArray], bufferComponentType.IsReadOnly, bufferComponentType.m_Safety, bufferComponentType.m_ArrayInvalidationSafety);
            }
            return(accessor);
        }
예제 #6
0
 public bool DidChange <T>(ArchetypeChunkBufferType <T> chunkBufferType, uint version) where T : struct, IBufferElementData
 {
     return(ChangeVersionUtility.DidChange(GetComponentVersion(chunkBufferType), version));
 }
예제 #7
0
        public void AssertWillDestroyAllInLinkedEntityGroup(NativeArray <ArchetypeChunk> chunkArray,
                                                            ArchetypeChunkBufferType <LinkedEntityGroup> linkedGroupType)
        {
            var chunks      = (ArchetypeChunk *)chunkArray.GetUnsafeReadOnlyPtr();
            var chunksCount = chunkArray.Length;

            var tempChunkStateFlag = (uint)ChunkFlags.TempAssertWillDestroyAllInLinkedEntityGroup;

            for (int i = 0; i != chunksCount; i++)
            {
                var chunk = chunks[i].m_Chunk;
                Assert.IsTrue((chunk->Flags & tempChunkStateFlag) == 0);
                chunk->Flags |= tempChunkStateFlag;
            }

            string error = null;

            for (int i = 0; i < chunkArray.Length; ++i)
            {
                if (!chunks[i].Has(linkedGroupType))
                {
                    continue;
                }

                var chunk   = chunks[i];
                var buffers = chunk.GetBufferAccessor(linkedGroupType);

                for (int b = 0; b != buffers.Length; b++)
                {
                    var buffer      = buffers[b];
                    int entityCount = buffer.Length;
                    var entities    = (Entity *)buffer.GetUnsafePtr();
                    for (int e = 0; e != entityCount; e++)
                    {
                        var referencedEntity = entities[e];
                        if (Exists(referencedEntity))
                        {
                            var referencedChunk = GetChunk(referencedEntity);

                            if ((referencedChunk->Flags & tempChunkStateFlag) == 0)
                            {
                                error =
                                    $"DestroyEntity(EntityQuery query) is destroying entity {entities[0]} which contains a LinkedEntityGroup and the entity {entities[e]} in that group is not included in the query. If you want to destroy entities using a query all linked entities must be contained in the query..";
                            }
                        }
                    }
                }
            }

            for (int i = 0; i != chunksCount; i++)
            {
                var chunk = chunks[i].m_Chunk;
                Assert.IsTrue((chunk->Flags & tempChunkStateFlag) != 0);
                chunk->Flags &= ~tempChunkStateFlag;
            }

            if (error != null)
            {
                throw new ArgumentException(error);
            }
        }