public static void DeleteChunk(Chunk *chunk, EntityComponentStore *entityComponentStore, ManagedComponentStore managedComponentStore) { var entityCount = chunk->Count; entityComponentStore->DeallocateDataEntitiesInChunk((Entity *)chunk->Buffer, chunk, 0, chunk->Count); managedComponentStore.IncrementComponentOrderVersion(chunk->Archetype, chunk->SharedComponentValues); entityComponentStore->IncrementComponentTypeOrderVersion(chunk->Archetype); chunk->Archetype->EntityCount -= entityCount; SetChunkCount(chunk, 0, entityComponentStore, managedComponentStore); }
static int InstantiateEntitiesOne(Entity srcEntity, Entity *outputEntities, int instanceCount, InstantiateRemapChunk *remapChunks, int remapChunksCount, EntityComponentStore *entityComponentStore, ManagedComponentStore managedComponentStore) { var src = entityComponentStore->GetEntityInChunk(srcEntity); var srcArchetype = src.Chunk->Archetype; var dstArchetype = srcArchetype->InstantiableArchetype; var temp = stackalloc int[dstArchetype->NumSharedComponents]; if (RequiresBuildingResidueSharedComponentIndices(srcArchetype, dstArchetype)) { BuildResidueSharedComponentIndices(srcArchetype, dstArchetype, src.Chunk->SharedComponentValues, temp); } else { // Always copy shared component indices since GetChunkWithEmptySlots might reallocate the storage of SharedComponentValues src.Chunk->SharedComponentValues.CopyTo(temp, 0, dstArchetype->NumSharedComponents); } SharedComponentValues sharedComponentValues = temp; Chunk *chunk = null; int instanceBeginIndex = 0; while (instanceBeginIndex != instanceCount) { chunk = GetChunkWithEmptySlots(dstArchetype, sharedComponentValues, entityComponentStore, managedComponentStore); int indexInChunk; var allocatedCount = AllocateIntoChunk(chunk, instanceCount - instanceBeginIndex, out indexInChunk, entityComponentStore, managedComponentStore); ChunkDataUtility.ReplicateComponents(src.Chunk, src.IndexInChunk, chunk, indexInChunk, allocatedCount); entityComponentStore->AllocateEntities(dstArchetype, chunk, indexInChunk, allocatedCount, outputEntities + instanceBeginIndex); chunk->SetAllChangeVersions(entityComponentStore->GlobalSystemVersion); #if UNITY_EDITOR for (var i = 0; i < allocatedCount; ++i) { entityComponentStore->SetName(outputEntities[i + instanceBeginIndex], entityComponentStore->GetName(srcEntity)); } #endif if (remapChunks != null) { remapChunks[remapChunksCount].Chunk = chunk; remapChunks[remapChunksCount].IndexInChunk = indexInChunk; remapChunks[remapChunksCount].AllocatedCount = allocatedCount; remapChunks[remapChunksCount].InstanceBeginIndex = instanceBeginIndex; remapChunksCount++; } instanceBeginIndex += allocatedCount; } if (chunk != null) { managedComponentStore.IncrementComponentOrderVersion(dstArchetype, chunk->SharedComponentValues); entityComponentStore->IncrementComponentTypeOrderVersion(dstArchetype); } return(remapChunksCount); }
static void DestroyBatch(Entity *entities, Chunk *chunk, int indexInChunk, int batchCount, EntityComponentStore *entityComponentStore, ManagedComponentStore managedComponentStore) { var archetype = chunk->Archetype; if (!archetype->SystemStateCleanupNeeded) { entityComponentStore->DeallocateDataEntitiesInChunk(entities, chunk, indexInChunk, batchCount); managedComponentStore.IncrementComponentOrderVersion(archetype, chunk->SharedComponentValues); entityComponentStore->IncrementComponentTypeOrderVersion(archetype); if (chunk->ManagedArrayIndex >= 0) { // We can just chop-off the end, no need to copy anything if (chunk->Count != indexInChunk + batchCount) { managedComponentStore.CopyManagedObjects(chunk, chunk->Count - batchCount, chunk, indexInChunk, batchCount); } managedComponentStore.ClearManagedObjects(chunk, chunk->Count - batchCount, batchCount); } chunk->Archetype->EntityCount -= batchCount; SetChunkCount(chunk, chunk->Count - batchCount, entityComponentStore, managedComponentStore); } else { var newType = archetype->SystemStateResidueArchetype; var sharedComponentValues = chunk->SharedComponentValues; if (RequiresBuildingResidueSharedComponentIndices(archetype, newType)) { var tempAlloc = stackalloc int[newType->NumSharedComponents]; BuildResidueSharedComponentIndices(archetype, newType, sharedComponentValues, tempAlloc); sharedComponentValues = tempAlloc; } // See: https://github.com/Unity-Technologies/dots/issues/1387 // For Locked Order Chunks specfically, need to make sure that structural changes are always done per-chunk. // If trying to muutate structure in a way that is not per chunk, will hit an exception in the else clause anyway. // This ultimately needs to be replaced by entity batch interface. if (batchCount == chunk->Count) { managedComponentStore.IncrementComponentOrderVersion(archetype, chunk->SharedComponentValues); entityComponentStore->IncrementComponentTypeOrderVersion(archetype); EntityManagerChangeArchetypeUtility.SetArchetype(chunk, newType, sharedComponentValues, entityComponentStore, managedComponentStore); } else { for (var i = 0; i < batchCount; i++) { var entity = entities[i]; managedComponentStore.IncrementComponentOrderVersion(archetype, entityComponentStore->GetChunk(entity)->SharedComponentValues); entityComponentStore->IncrementComponentTypeOrderVersion(archetype); EntityManagerChangeArchetypeUtility.SetArchetype(entity, newType, sharedComponentValues, entityComponentStore, managedComponentStore); } } } }