Example #1
0
        private static ArchetypeOffsetsFromChunk GetChunkOffsets(EntityManager em, EntityArchetype archetype, ComponentType metaType, ComponentType componentType, ComponentType bufferType = default, ComponentType linkType = default)
        {
            ArchetypeOffsetsFromChunk schema = default;

            var   tmpEntity = em.CreateEntity(archetype);
            var   chunk     = em.GetChunk(tmpEntity);
            byte *chunkPtr  = *(byte **)&chunk;
            var   tmp       = em.GetArchetypeChunkComponentType <ChunkHeader>(false);
            var   types     = archetype.GetComponentTypes();

            UnsafeUtility.CopyStructureToPtr(ref metaType.TypeIndex, UnsafeUtility.AddressOf(ref tmp));
            schema.MetaOffset = (byte *)chunk.GetNativeArray(tmp).GetUnsafeReadOnlyPtr() - chunkPtr;

            UnsafeUtility.CopyStructureToPtr(ref componentType.TypeIndex, UnsafeUtility.AddressOf(ref tmp));
            schema.ComponentOffset = (byte *)chunk.GetNativeArray(tmp).GetUnsafeReadOnlyPtr() - chunkPtr;

            if (bufferType != default)
            {
                UnsafeUtility.CopyStructureToPtr(ref linkType.TypeIndex, UnsafeUtility.AddressOf(ref tmp));
                schema.BufferLinkOffset = (byte *)chunk.GetNativeArray(tmp).GetUnsafeReadOnlyPtr() - chunkPtr;

                UnsafeUtility.CopyStructureToPtr(ref bufferType.TypeIndex, UnsafeUtility.AddressOf(ref tmp));
                schema.BufferOffset = (byte *)chunk.GetNativeArray(tmp).GetUnsafeReadOnlyPtr() - chunkPtr;
            }

            em.DestroyEntity(tmpEntity);
            return(schema);
        }
Example #2
0
        public EventBatch(EntityManager em, EventDefinition definition, Allocator allocator = Allocator.Persistent) : this()
        {
            var componentType  = ComponentType.FromTypeIndex(definition.ComponentTypeInfo.TypeIndex);
            var bufferType     = ComponentType.FromTypeIndex(definition.BufferTypeInfo.TypeIndex);
            var bufferLinkType = ComponentType.ReadWrite <BufferLink>();

            HasBuffer    = definition.BufferTypeInfo.TypeIndex != 0;
            HasComponent = definition.ComponentTypeInfo.TypeIndex != 0;

            var components = new List <ComponentType>()
            {
                definition.MetaType,
            };

            if (HasComponent)
            {
                components.AddRange(new[] {
                    componentType,
                });
            }

            if (HasBuffer)
            {
                components.AddRange(new[] {
                    bufferType,
                    bufferLinkType,
                });
            }

            Archetype = em.CreateArchetype(components.ToArray());

            var inactiveComponents = new List <ComponentType>(components)
            {
                ComponentType.ReadWrite <Disabled>()
            };

            InactiveArchetype = em.CreateArchetype(inactiveComponents.ToArray());


            Allocator          = allocator;
            ComponentType      = componentType;
            ComponentTypeIndex = definition.ComponentTypeInfo.TypeIndex;
            ComponentTypeSize  = definition.ComponentTypeInfo.SizeInChunk;

            BufferType             = bufferType;
            BufferTypeIndex        = definition.BufferTypeInfo.TypeIndex;
            BufferElementSize      = definition.BufferTypeInfo.ElementSize;
            BufferSizeInChunk      = definition.BufferTypeInfo.SizeInChunk;
            BufferAlignmentInBytes = definition.BufferTypeInfo.AlignmentInBytes;
            BufferLinkTypeIndex    = TypeManager.GetTypeIndex <BufferLink>();

            StartingPoolSize = definition.StartingPoolSize;

            ComponentQueue = new EventQueue(ComponentTypeIndex, ComponentTypeSize, BufferTypeIndex, BufferElementSize, allocator);
            Offsets        = GetChunkOffsets(em, Archetype, definition.MetaType, componentType, bufferType, bufferLinkType);

            ActiveChunks   = new ArchetypeView(Archetype);
            InactiveChunks = new ArchetypeView(InactiveArchetype);

            ActiveArchetypeChunks         = new UnsafeList(Allocator.Persistent);
            ActiveFullArchetypeChunks     = new UnsafeList(Allocator.Persistent);
            ActivePartialArchetypeChunk   = new UnsafeList(Allocator.Persistent);
            InactiveFullArchetypeChunks   = new UnsafeList(Allocator.Persistent);
            InactivePartialArchetypeChunk = new UnsafeList(Allocator.Persistent);

            if (definition.StartingPoolSize > 0)
            {
                CreateInactiveEntities(em, definition.StartingPoolSize);
                UpdateChunkCollections();
            }
        }