void HandleChunk(int idx, EntityComponentStore *dstEntityComponentStore, UnsafeHashMap <int, int> sharedComponentRemap) { var srcChunk = Chunks[idx].m_Chunk; var numSharedComponents = srcChunk->Archetype->NumSharedComponents; var dstSharedIndices = stackalloc int[numSharedComponents]; srcChunk->SharedComponentValues.CopyTo(dstSharedIndices, 0, numSharedComponents); for (int i = 0; i < numSharedComponents; i++) { dstSharedIndices[i] = sharedComponentRemap[dstSharedIndices[i]]; } var srcArchetype = srcChunk->Archetype; var dstArchetype = dstEntityComponentStore->GetOrCreateArchetype(srcArchetype->Types, srcArchetype->TypesCount); var dstChunk = dstEntityComponentStore->GetCleanChunkNoMetaChunk(dstArchetype, dstSharedIndices); dstChunk->metaChunkEntity = srcChunk->metaChunkEntity; ChunkDataUtility.SetChunkCountKeepMetaChunk(dstChunk, srcChunk->Count); dstChunk->Archetype->EntityCount += srcChunk->Count; dstChunk->SequenceNumber = srcChunk->SequenceNumber; ClonedChunks[idx] = new ArchetypeChunk { m_Chunk = dstChunk }; }
static void DestroyChunkForDiffing(EntityManager entityManager, Chunk *chunk) { var access = entityManager.GetCheckedEntityDataAccess(); var ecs = access->EntityComponentStore; var mcs = access->ManagedComponentStore; var count = chunk->Count; ChunkDataUtility.DeallocateBuffers(chunk); ecs->DeallocateManagedComponents(chunk, 0, count); chunk->Archetype->EntityCount -= chunk->Count; ecs->FreeEntities(chunk); ChunkDataUtility.SetChunkCountKeepMetaChunk(chunk, 0); mcs.Playback(ref ecs->ManagedChangesTracker); }
public void Execute() { var access = EntityManager.GetCheckedEntityDataAccess(); var ecs = access->EntityComponentStore; for (var i = 0; i < Chunks.Length; i++) { var chunk = Chunks[i].m_Chunk; var count = chunk->Count; ChunkDataUtility.DeallocateBuffers(chunk); ecs->DeallocateManagedComponents(chunk, 0, count); chunk->Archetype->EntityCount -= chunk->Count; ecs->FreeEntities(chunk); ChunkDataUtility.SetChunkCountKeepMetaChunk(chunk, 0); } }
static Chunk *CloneChunkWithoutAllocatingEntities(EntityManager dstEntityManager, Chunk *srcChunk, ManagedComponentStore srcManagedComponentStore) { var dstAccess = dstEntityManager.GetCheckedEntityDataAccess(); var dstEntityComponentStore = dstAccess->EntityComponentStore; var dstManagedComponentStore = dstAccess->ManagedComponentStore; // Copy shared component data var dstSharedIndices = stackalloc int[srcChunk->Archetype->NumSharedComponents]; srcChunk->SharedComponentValues.CopyTo(dstSharedIndices, 0, srcChunk->Archetype->NumSharedComponents); dstManagedComponentStore.CopySharedComponents(srcManagedComponentStore, dstSharedIndices, srcChunk->Archetype->NumSharedComponents); // @TODO: Why don't we memcpy the whole chunk. So we include all extra fields??? // Allocate a new chunk var srcArchetype = srcChunk->Archetype; var dstArchetype = dstEntityComponentStore->GetOrCreateArchetype(srcArchetype->Types, srcArchetype->TypesCount); var dstChunk = dstEntityComponentStore->GetCleanChunkNoMetaChunk(dstArchetype, dstSharedIndices); dstManagedComponentStore.Playback(ref dstEntityComponentStore->ManagedChangesTracker); dstChunk->metaChunkEntity = srcChunk->metaChunkEntity; // Release any references obtained by GetCleanChunk & CopySharedComponents for (var i = 0; i < srcChunk->Archetype->NumSharedComponents; i++) { dstManagedComponentStore.RemoveReference(dstSharedIndices[i]); } ChunkDataUtility.SetChunkCountKeepMetaChunk(dstChunk, srcChunk->Count); dstManagedComponentStore.Playback(ref dstEntityComponentStore->ManagedChangesTracker); dstChunk->Archetype->EntityCount += srcChunk->Count; var copySize = Chunk.GetChunkBufferSize(); UnsafeUtility.MemCpy((byte *)dstChunk + Chunk.kBufferOffset, (byte *)srcChunk + Chunk.kBufferOffset, copySize); var numManagedComponents = dstChunk->Archetype->NumManagedComponents; var hasHybridComponents = dstArchetype->HasHybridComponents; for (int t = 0; t < numManagedComponents; ++t) { int indexInArchetype = t + dstChunk->Archetype->FirstManagedComponent; if (hasHybridComponents) { // We consider hybrid components as always different, there's no reason to clone those at this point var typeCategory = TypeManager.GetTypeInfo(dstChunk->Archetype->Types[indexInArchetype].TypeIndex).Category; if (typeCategory == TypeManager.TypeCategory.Class) { continue; } } var offset = dstChunk->Archetype->Offsets[indexInArchetype]; var a = (int *)(dstChunk->Buffer + offset); dstManagedComponentStore.CloneManagedComponentsFromDifferentWorld(a, dstChunk->Count, srcManagedComponentStore, ref *dstAccess->EntityComponentStore); } BufferHeader.PatchAfterCloningChunk(dstChunk); dstChunk->SequenceNumber = srcChunk->SequenceNumber; return(dstChunk); }