Example #1
0
            public void Execute(int index)
            {
                var chunk     = Chunks[index].m_Chunk;
                var archetype = chunk->Archetype;

                for (var typeIndexInArchetype = 0; typeIndexInArchetype < archetype->TypesCount; typeIndexInArchetype++)
                {
                    var componentTypeInArchetype = archetype->Types[typeIndexInArchetype];
                    var typeInfo = TypeInfoStream.GetTypeInfo(componentTypeInArchetype.TypeIndex);

                    if (typeInfo.EntityOffsetCount == 0)
                    {
                        // This type has no entity references. Skip.
                        continue;
                    }

                    if (componentTypeInArchetype.IsSharedComponent || componentTypeInArchetype.IsZeroSized)
                    {
                        // @TODO Should we handle shared components with references? Is that a thing?
                        continue;
                    }

                    var typeInChunkPtr = ChunkUtility.GetBuffer(chunk) + archetype->Offsets[typeIndexInArchetype];
                    var typeSizeOf     = archetype->SizeOfs[typeIndexInArchetype];

                    var changed = false;

                    for (var entityIndexInChunk = 0; entityIndexInChunk < Chunks[index].Count; entityIndexInChunk++)
                    {
                        var componentDataPtr = typeInChunkPtr + typeSizeOf * entityIndexInChunk;

                        if (componentTypeInArchetype.IsBuffer)
                        {
                            var bufferHeader = (BufferHeader *)componentDataPtr;
                            var bufferLength = bufferHeader->Length;
                            var bufferPtr    = BufferHeader.GetElementPointer(bufferHeader);
                            changed |= SetMissingEntityReferencesToNullForComponent(typeInfo, bufferPtr, bufferLength);
                        }
                        else
                        {
                            changed |= SetMissingEntityReferencesToNullForComponent(typeInfo, componentDataPtr, 1);
                        }
                    }

                    if (changed)
                    {
                        chunk->SetChangeVersion(typeIndexInArchetype, GlobalSystemVersion);
                    }
                }
            }
            public void Execute()
            {
                for (var i = 0; i < CreatedEntities.Length; ++i)
                {
                    var entityGuid      = CreatedEntities[i].Component;
                    var afterEntity     = CreatedEntities[i].EntityInChunk;
                    var afterChunk      = afterEntity.Chunk;
                    var afterArchetype  = afterChunk->Archetype;
                    var afterTypesCount = afterArchetype->TypesCount;

                    for (var afterIndexInTypeArray = 1; afterIndexInTypeArray < afterTypesCount; afterIndexInTypeArray++)
                    {
                        var typeInArchetype = afterArchetype->Types[afterIndexInTypeArray];

                        if (typeInArchetype.IsSystemStateComponent)
                        {
                            continue;
                        }

                        var typeIndex = typeInArchetype.TypeIndex;
                        var typeInfo  = TypeInfoStream.GetTypeInfo(typeIndex);

                        AddComponentData(
                            afterChunk,
                            afterArchetype,
                            typeInArchetype,
                            afterIndexInTypeArray,
                            afterEntity.IndexInChunk,
                            entityGuid,
                            typeIndex,
                            typeInfo
                            );
                    }
                }

                for (var i = 0; i < ModifiedEntities.Length; ++i)
                {
                    var modification = ModifiedEntities[i];
                    var entityGuid   = modification.EntityGuid;

                    var afterEntity     = modification.After;
                    var afterChunk      = afterEntity.Chunk;
                    var afterArchetype  = afterChunk->Archetype;
                    var afterTypesCount = afterArchetype->TypesCount;

                    var beforeEntity     = modification.Before;
                    var beforeChunk      = beforeEntity.Chunk;
                    var beforeArchetype  = beforeChunk->Archetype;
                    var beforeTypesCount = beforeArchetype->TypesCount;

                    for (var afterIndexInTypeArray = 1; afterIndexInTypeArray < afterTypesCount; afterIndexInTypeArray++)
                    {
                        var afterTypeInArchetype = afterArchetype->Types[afterIndexInTypeArray];

                        if (afterTypeInArchetype.IsSystemStateComponent || afterTypeInArchetype.IsChunkComponent)
                        {
                            continue;
                        }

                        var typeIndex = afterTypeInArchetype.TypeIndex;
                        var beforeIndexInTypeArray = ChunkDataUtility.GetIndexInTypeArray(beforeArchetype, typeIndex);
                        var typeInfo = TypeInfoStream.GetTypeInfo(typeIndex);

                        // This type is missing in the before entity.
                        // This means we are dealing with a newly added component.
                        if (-1 == beforeIndexInTypeArray)
                        {
                            // This type does not exist on the before world. This was a newly added component.
                            AddComponentData(
                                afterChunk,
                                afterArchetype,
                                afterTypeInArchetype,
                                afterIndexInTypeArray,
                                afterEntity.IndexInChunk,
                                entityGuid,
                                typeIndex,
                                typeInfo
                                );

                            continue;
                        }

                        if (modification.CanCompareChunkVersions && afterChunk->GetChangeVersion(afterIndexInTypeArray) == beforeChunk->GetChangeVersion(beforeIndexInTypeArray))
                        {
                            continue;
                        }

                        SetComponentData(
                            afterChunk,
                            afterArchetype,
                            afterTypeInArchetype,
                            afterIndexInTypeArray,
                            afterEntity.IndexInChunk,
                            beforeChunk,
                            beforeArchetype,
                            beforeIndexInTypeArray,
                            beforeEntity.IndexInChunk,
                            entityGuid,
                            typeIndex,
                            typeInfo);
                    }

                    for (var beforeTypeIndexInArchetype = 1; beforeTypeIndexInArchetype < beforeTypesCount; beforeTypeIndexInArchetype++)
                    {
                        var beforeComponentTypeInArchetype = beforeArchetype->Types[beforeTypeIndexInArchetype];

                        if (beforeComponentTypeInArchetype.IsSystemStateComponent)
                        {
                            continue;
                        }

                        var beforeTypeIndex = beforeComponentTypeInArchetype.TypeIndex;

                        if (-1 == ChunkDataUtility.GetIndexInTypeArray(afterArchetype, beforeTypeIndex))
                        {
                            var packedComponent = PackComponent(entityGuid, beforeTypeIndex, TypeInfoStream.GetTypeInfo(beforeTypeIndex).StableTypeHash);
                            RemoveComponents.Add(packedComponent);
                        }
                    }
                }
            }