public static void AssertArchetypeComponents(ComponentTypeInArchetype *types, int count) { if (count < 1) { throw new ArgumentException($"Invalid component count"); } if (types[0].TypeIndex == 0) { throw new ArgumentException($"Component type may not be null"); } if (types[0].TypeIndex != TypeManager.GetTypeIndex <Entity>()) { throw new ArgumentException($"The Entity ID must always be the first component"); } for (var i = 1; i < count; i++) { if (!TypeManager.IsValidComponentTypeForArchetype(types[i].TypeIndex, types[i].IsFixedArray)) { throw new ArgumentException($"{types[i]} is not a valid component type."); } if (types[i - 1].TypeIndex == types[i].TypeIndex) { throw new ArgumentException( $"It is not allowed to have two components of the same type on the same entity. ({types[i - 1]} and {types[i]})"); } } }
public static Archetype *GetArchetypeWithAddedComponentType(Archetype *archetype, ComponentType addedComponentType, EntityComponentStore *entityComponentStore, int *indexInTypeArray = null) { var componentType = new ComponentTypeInArchetype(addedComponentType); ComponentTypeInArchetype *newTypes = stackalloc ComponentTypeInArchetype[archetype->TypesCount + 1]; var t = 0; while (t < archetype->TypesCount && archetype->Types[t] < componentType) { newTypes[t] = archetype->Types[t]; ++t; } if (indexInTypeArray != null) { *indexInTypeArray = t; } if (archetype->Types[t] == componentType) { Assert.IsTrue(addedComponentType.IgnoreDuplicateAdd, $"{addedComponentType} is already part of the archetype."); // Tag component type is already there, no new archetype required. return(null); } newTypes[t] = componentType; while (t < archetype->TypesCount) { newTypes[t + 1] = archetype->Types[t]; ++t; } return(GetOrCreateArchetype(newTypes, archetype->TypesCount + 1, entityComponentStore)); }
public Archetype *GetOrCreateArchetype(ComponentTypeInArchetype *types, int count, EntityGroupManager groupManager) { var srcArchetype = GetOrCreateArchetypeInternal(types, count, groupManager); var removedTypes = 0; var prefabTypeIndex = TypeManager.GetTypeIndex <Prefab>(); for (var t = 0; t < srcArchetype->TypesCount; ++t) { var type = srcArchetype->Types[t]; var skip = type.IsSystemStateComponent || type.IsSystemStateSharedComponent || (type.TypeIndex == prefabTypeIndex); if (skip) { ++removedTypes; } else { types[t - removedTypes] = srcArchetype->Types[t]; } } srcArchetype->InstantiableArchetype = srcArchetype; if (removedTypes > 0) { var instantiableArchetype = GetOrCreateArchetypeInternal(types, count - removedTypes, groupManager); srcArchetype->InstantiableArchetype = instantiableArchetype; instantiableArchetype->InstantiableArchetype = instantiableArchetype; } return(srcArchetype); }
public static unsafe int GetIndexInTypeArray(Archetype *archetype, int typeIndex) { ComponentTypeInArchetype *types = archetype.Types; int typesCount = archetype.TypesCount; int index = 0; while (true) { int num3; if (index == typesCount) { num3 = -1; } else { if (typeIndex != types[index].TypeIndex) { index++; continue; } num3 = index; } return(num3); } }
public static unsafe void InitializeComponents(Chunk *dstChunk, int dstIndex, int count) { Archetype *archetype = dstChunk.Archetype; int * offsets = archetype->Offsets; int * sizeOfs = archetype->SizeOfs; byte * numPtr3 = &dstChunk.Buffer.FixedElementField; int typesCount = archetype->TypesCount; ComponentTypeInArchetype *types = archetype->Types; for (int i = 1; i != typesCount; i++) { int num3 = offsets[i]; int num4 = sizeOfs[i]; byte *numPtr4 = numPtr3 + (num3 + (num4 * dstIndex)); if (!(types + i).IsBuffer) { UnsafeUtility.MemClear((void *)numPtr4, (long)(num4 * count)); } else { int num5 = 0; while (true) { if (num5 >= count) { break; } BufferHeader.Initialize((BufferHeader *)numPtr4, types[i].BufferCapacity); numPtr4 += num4; num5++; } } } }
protected override void OnDestroyManager() { EndExclusiveEntityTransaction(); ComponentJobSafetyManager.PreDisposeCheck(); // Clean up all entities. This is needed to free all internal buffer allocations so memory is not leaked. using (var allEntities = GetAllEntities()) { DestroyEntity(allEntities); } ComponentJobSafetyManager.Dispose(); ComponentJobSafetyManager = null; Entities->OnDestroy(); UnsafeUtility.Free(Entities, Allocator.Persistent); Entities = null; ArchetypeManager.Dispose(); ArchetypeManager = null; m_GroupManager.Dispose(); m_GroupManager = null; m_ExclusiveEntityTransaction.OnDestroyManager(); m_SharedComponentManager.Dispose(); UnsafeUtility.Free(m_CachedComponentTypeArray, Allocator.Persistent); m_CachedComponentTypeArray = null; UnsafeUtility.Free(m_CachedComponentTypeInArchetypeArray, Allocator.Persistent); m_CachedComponentTypeInArchetypeArray = null; }
public static unsafe bool CompareArray(ComponentTypeInArchetype *type1, int typeCount1, ComponentTypeInArchetype *type2, int typeCount2) { bool flag2; if (typeCount1 != typeCount2) { flag2 = false; } else { int index = 0; while (true) { if (index >= typeCount1) { flag2 = true; } else { if (!(type1[index] != type2[index])) { index++; continue; } flag2 = false; } break; } } return(flag2); }
// ---------------------------------------------------------------------------------------------------------- // INTERNAL // ---------------------------------------------------------------------------------------------------------- internal EntityArchetype CreateArchetype(ComponentType *types, int count) { ComponentTypeInArchetype *typesInArchetype = stackalloc ComponentTypeInArchetype[count + 1]; var cachedComponentCount = FillSortedArchetypeArray(typesInArchetype, types, count); // Lookup existing archetype (cheap) EntityArchetype entityArchetype; entityArchetype.Archetype = EntityComponentStore->GetExistingArchetype(typesInArchetype, cachedComponentCount); if (entityArchetype.Archetype != null) { return(entityArchetype); } // Creating an archetype invalidates all iterators / jobs etc // because it affects the live iteration linked lists... BeforeStructuralChange(); var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking(); entityArchetype.Archetype = EntityComponentStore->GetOrCreateArchetype(typesInArchetype, cachedComponentCount); var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges); EntityQueryManager.AddAdditionalArchetypes(changedArchetypes); return(entityArchetype); }
internal EntityArchetype CreateArchetype(ComponentType *types, int count) { ComponentTypeInArchetype *typesInArchetype = stackalloc ComponentTypeInArchetype[count + 1]; var cachedComponentCount = FillSortedArchetypeArray(typesInArchetype, types, count); // Lookup existing archetype (cheap) EntityArchetype entityArchetype; #if ENABLE_UNITY_COLLECTIONS_CHECKS entityArchetype._DebugComponentStore = EntityComponentStore; #endif entityArchetype.Archetype = EntityComponentStore->GetExistingArchetype(typesInArchetype, cachedComponentCount); if (entityArchetype.Archetype != null) { return(entityArchetype); } // Creating an archetype invalidates all iterators / jobs etc // because it affects the live iteration linked lists... EntityComponentStore.ArchetypeChanges archetypeChanges = default; if (m_IsMainThread) { BeforeStructuralChange(); } archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking(); entityArchetype.Archetype = EntityComponentStore->GetOrCreateArchetype(typesInArchetype, cachedComponentCount); EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges, EntityQueryManager); return(entityArchetype); }
private static uint GetHash(ComponentTypeInArchetype *types, int count) { var hash = HashUtility.Fletcher32((ushort *)types, count * sizeof(ComponentTypeInArchetype) / sizeof(ushort)); return(hash); }
private Archetype *GetOrCreateArchetypeInternal(ComponentTypeInArchetype *types, int count, EntityGroupManager groupManager) { var type = GetExistingArchetype(types, count); return(type != null ? type : CreateArchetypeInternal(types, count, groupManager)); }
public static Archetype *GetArchetypeWithRemovedComponentType(Archetype *archetype, ComponentType addedComponentType, EntityComponentStore *entityComponentStore, int *indexInOldTypeArray = null) { var componentType = new ComponentTypeInArchetype(addedComponentType); ComponentTypeInArchetype *newTypes = stackalloc ComponentTypeInArchetype[archetype->TypesCount]; var removedTypes = 0; for (var t = 0; t < archetype->TypesCount; ++t) { if (archetype->Types[t].TypeIndex == componentType.TypeIndex) { if (indexInOldTypeArray != null) { *indexInOldTypeArray = t; } ++removedTypes; } else { newTypes[t - removedTypes] = archetype->Types[t]; } } return(GetOrCreateArchetype(newTypes, archetype->TypesCount - removedTypes, entityComponentStore)); }
public static void Burst_Unity__Entities__EntityDataAccess_FillSortedArchetypeArray(IntPtr p) { ComponentTypeInArchetype *v0 = (ComponentTypeInArchetype *)((byte *)p + 0); ComponentType * v1 = (ComponentType *)((byte *)p + 1024); var v2 = default(int); EntityDataAccess.FillSortedArchetypeArray(v0, v1, v2); }
internal unsafe ExclusiveEntityTransaction(Unity.Entities.ArchetypeManager archetypes, EntityGroupManager entityGroupManager, Unity.Entities.SharedComponentDataManager sharedComponentDataManager, EntityDataManager *data) { this.m_Safety = new AtomicSafetyHandle(); this.m_Entities = data; this.m_ArchetypeManager = GCHandle.Alloc(archetypes, GCHandleType.Weak); this.m_EntityGroupManager = GCHandle.Alloc(entityGroupManager, GCHandleType.Weak); this.m_SharedComponentDataManager = GCHandle.Alloc(sharedComponentDataManager, GCHandleType.Weak); this.m_CachedComponentTypeInArchetypeArray = (ComponentTypeInArchetype *)UnsafeUtility.Malloc((long)((sizeof(ComponentTypeInArchetype) * 0x20) * 0x400), 0x10, Allocator.Persistent); }
private static unsafe bool TestMatchingArchetypeAll(Archetype *archetype, int *allTypes, int allCount) { ComponentTypeInArchetype *types = archetype.Types; int typesCount = archetype.TypesCount; int num2 = 0; int typeIndex = TypeManager.GetTypeIndex <Disabled>(); int num4 = TypeManager.GetTypeIndex <Prefab>(); bool flag = false; bool flag2 = false; int index = 0; while (true) { if (index >= typesCount) { bool flag9; if (archetype.Disabled && !flag) { flag9 = false; } else if (archetype.Prefab && !flag2) { flag9 = false; } else { flag9 = num2 == allCount; } return(flag9); } int num6 = types[index].TypeIndex; int num7 = 0; while (true) { if (num7 >= allCount) { index++; break; } int num8 = allTypes[num7]; if (num8 == typeIndex) { flag = true; } if (num8 == num4) { flag2 = true; } if (num6 == num8) { num2++; } num7++; } } }
private Archetype *GetEntityOnlyArchetype(ComponentTypeInArchetype *types, EntityGroupManager groupManager) { if (m_entityOnlyArchetype == null) { m_entityOnlyArchetype = GetOrCreateArchetypeInternal(types, 1, groupManager); m_entityOnlyArchetype->InstantiableArchetype = m_entityOnlyArchetype; m_entityOnlyArchetype->SystemStateResidueArchetype = null; } return(m_entityOnlyArchetype); }
static uint GetHashCode(ComponentTypeInArchetype *type, int types) { UInt32 result = math.hash(type, types * sizeof(ComponentTypeInArchetype)); if (result == 0 || result == kSkipCode) { result = kAValidHashCode; } return(result); }
public static unsafe void InsertSorted(ComponentTypeInArchetype *data, int length, ComponentType newValue) { var newVal = new ComponentTypeInArchetype(newValue); while (length > 0 && newVal < data[length - 1]) { data[length] = data[length - 1]; --length; } data[length] = newVal; }
protected override unsafe void OnCreateManager() { TypeManager.Initialize(); this.Entities = (EntityDataManager *)UnsafeUtility.Malloc((long)sizeof(EntityDataManager), 0x40, Allocator.Persistent); this.Entities.OnCreate(); this.m_SharedComponentManager = new SharedComponentDataManager(); this.ArchetypeManager = new Unity.Entities.ArchetypeManager(this.m_SharedComponentManager); this.ComponentJobSafetyManager = new Unity.Entities.ComponentJobSafetyManager(); this.m_GroupManager = new EntityGroupManager(this.ComponentJobSafetyManager); this.m_ExclusiveEntityTransaction = new ExclusiveEntityTransaction(this.ArchetypeManager, this.m_GroupManager, this.m_SharedComponentManager, this.Entities); this.m_CachedComponentTypeInArchetypeArray = (ComponentTypeInArchetype *)UnsafeUtility.Malloc((long)(sizeof(ComponentTypeInArchetype) * 0x400), 0x10, Allocator.Persistent); }
internal ExclusiveEntityTransaction(ArchetypeManager archetypes, EntityGroupManager entityGroupManager, SharedComponentDataManager sharedComponentDataManager, EntityDataManager *data) { #if ENABLE_UNITY_COLLECTIONS_CHECKS m_Safety = new AtomicSafetyHandle(); #endif m_Entities = data; m_ArchetypeManager = GCHandle.Alloc(archetypes, GCHandleType.Weak); m_EntityGroupManager = GCHandle.Alloc(entityGroupManager, GCHandleType.Weak); m_SharedComponentDataManager = GCHandle.Alloc(sharedComponentDataManager, GCHandleType.Weak); m_CachedComponentTypeInArchetypeArray = (ComponentTypeInArchetype *)UnsafeUtility.Malloc(sizeof(ComponentTypeInArchetype) * 32 * 1024, 16, Allocator.Persistent); }
internal EntityArchetype CreateArchetype(ComponentType *types, int count) { CheckAccess(); ComponentTypeInArchetype *typesInArchetype = stackalloc ComponentTypeInArchetype[count + 1]; var componentCount = EntityManager.FillSortedArchetypeArray(typesInArchetype, types, count); EntityArchetype type; type.Archetype = EntityManagerCreateArchetypeUtility.GetOrCreateArchetype(typesInArchetype, componentCount, EntityComponentStore); return(type); }
public static unsafe void ReplicateComponents(Chunk *srcChunk, int srcIndex, Chunk *dstChunk, int dstBaseIndex, int count) { Archetype *archetypePtr = srcChunk.Archetype; byte * numPtr = &srcChunk.Buffer.FixedElementField; byte * numPtr2 = &dstChunk.Buffer.FixedElementField; Archetype *archetypePtr2 = dstChunk.Archetype; int * offsets = archetypePtr->Offsets; int * sizeOfs = archetypePtr->SizeOfs; int typesCount = archetypePtr->TypesCount; ComponentTypeInArchetype *types = archetypePtr->Types; ComponentTypeInArchetype *archetypePtr4 = archetypePtr2->Types; int *numPtr5 = archetypePtr2->Offsets; int index = 1; for (int i = 1; i != typesCount; i++) { ComponentTypeInArchetype archetype = types[i]; ComponentTypeInArchetype archetype2 = archetypePtr4[index]; if (archetype.TypeIndex == archetype2.TypeIndex) { int size = sizeOfs[i]; byte *numPtr6 = numPtr + (offsets[i] + (size * srcIndex)); byte *numPtr7 = numPtr2 + (numPtr5[index] + (size * dstBaseIndex)); if (!archetype.IsBuffer) { UnsafeUtility.MemCpyReplicate((void *)numPtr7, (void *)numPtr6, size, count); } else { int alignment = 8; int elementSize = TypeManager.GetTypeInfo(archetype.TypeIndex).ElementSize; int num9 = 0; while (true) { if (num9 >= count) { break; } BufferHeader *header = (BufferHeader *)numPtr6; BufferHeader *headerPtr2 = (BufferHeader *)numPtr7; BufferHeader.Initialize(headerPtr2, archetype.BufferCapacity); BufferHeader.Assign(headerPtr2, BufferHeader.GetElementPointer(header), header->Length, elementSize, alignment); numPtr7 += size; num9++; } } index++; } } }
internal EntityArchetype CreateArchetype(ComponentType *types, int count) { CheckAccess(); var groupManager = (EntityGroupManager)m_EntityGroupManager.Target; ComponentTypeInArchetype *typesInArchetype = stackalloc ComponentTypeInArchetype[count + 1]; var componentCount = EntityManager.FillSortedArchetypeArray(typesInArchetype, types, count); EntityArchetype type; type.Archetype = ArchetypeManager.GetOrCreateArchetype(typesInArchetype, componentCount, groupManager); return(type); }
public static unsafe bool CompareArray(ComponentTypeInArchetype *type1, int typeCount1, ComponentTypeInArchetype *type2, int typeCount2) { if (typeCount1 != typeCount2) { return(false); } for (var i = 0; i < typeCount1; ++i) { if (type1[i] != type2[i]) { return(false); } } return(true); }
public static unsafe void InsertSorted(ComponentTypeInArchetype *data, int length, ComponentType newValue) { ComponentTypeInArchetype archetype = new ComponentTypeInArchetype(newValue); while (true) { if ((length <= 0) || (archetype >= data[length - 1])) { data[length] = archetype; return; } data[length] = data[length - 1]; length--; } }
internal static int FillSortedArchetypeArray(ComponentTypeInArchetype *dst, ComponentType *requiredComponents, int count) { #if ENABLE_UNITY_COLLECTIONS_CHECKS if (count + 1 > 1024) { throw new ArgumentException($"Archetypes can't hold more than 1024 components"); } #endif dst[0] = new ComponentTypeInArchetype(ComponentType.ReadWrite <Entity>()); for (var i = 0; i < count; ++i) { SortingUtilities.InsertSorted(dst, i + 1, requiredComponents[i]); } return(count + 1); }
public void AddComponent(Entity entity, ComponentType type, ArchetypeManager archetypeManager, SharedComponentDataManager sharedComponentDataManager, EntityGroupManager groupManager, ComponentTypeInArchetype *componentTypeInArchetypeArray) { var componentType = new ComponentTypeInArchetype(type); var archetype = GetArchetype(entity); var t = 0; while (t < archetype->TypesCount && archetype->Types[t] < componentType) { componentTypeInArchetypeArray[t] = archetype->Types[t]; ++t; } var indexInTypeArray = t; componentTypeInArchetypeArray[t] = componentType; while (t < archetype->TypesCount) { componentTypeInArchetypeArray[t + 1] = archetype->Types[t]; ++t; } var newType = archetypeManager.GetOrCreateArchetype(componentTypeInArchetypeArray, archetype->TypesCount + 1, groupManager); int *sharedComponentDataIndices = GetComponentChunk(entity)->SharedComponentValueArray; if ((newType->NumSharedComponents > 0) && (newType->NumSharedComponents != archetype->NumSharedComponents)) { var oldSharedComponentDataIndices = sharedComponentDataIndices; int *stackAlloced = stackalloc int[newType->NumSharedComponents]; sharedComponentDataIndices = stackAlloced; int indexOfNewSharedComponent = newType->SharedComponentOffset[indexInTypeArray]; UnsafeUtility.MemCpy(sharedComponentDataIndices, oldSharedComponentDataIndices, indexOfNewSharedComponent * sizeof(int)); sharedComponentDataIndices[indexOfNewSharedComponent] = 0; UnsafeUtility.MemCpy(sharedComponentDataIndices + indexOfNewSharedComponent + 1, oldSharedComponentDataIndices + indexOfNewSharedComponent, (archetype->NumSharedComponents - indexOfNewSharedComponent) * sizeof(int)); } SetArchetype(archetypeManager, entity, newType, sharedComponentDataIndices); IncrementComponentOrderVersion(newType, GetComponentChunk(entity), sharedComponentDataManager); }
protected override void OnCreateManager(int capacity) { TypeManager.Initialize(); m_Entities = (EntityDataManager *)UnsafeUtility.Malloc(sizeof(EntityDataManager), 64, Allocator.Persistent); m_Entities->OnCreate(capacity); m_SharedComponentManager = new SharedComponentDataManager(); m_ArchetypeManager = new ArchetypeManager(m_SharedComponentManager); ComponentJobSafetyManager = new ComponentJobSafetyManager(); m_GroupManager = new EntityGroupManager(ComponentJobSafetyManager); m_ExclusiveEntityTransaction = new ExclusiveEntityTransaction(m_ArchetypeManager, m_GroupManager, m_SharedComponentManager, m_Entities); m_CachedComponentTypeArray = (ComponentType *)UnsafeUtility.Malloc(sizeof(ComponentType) * 32 * 1024, 16, Allocator.Persistent); m_CachedComponentTypeInArchetypeArray = (ComponentTypeInArchetype *)UnsafeUtility.Malloc(sizeof(ComponentTypeInArchetype) * 32 * 1024, 16, Allocator.Persistent); }
public void RemoveComponent(Entity entity, ComponentType type, ArchetypeManager archetypeManager, SharedComponentDataManager sharedComponentDataManager, EntityGroupManager groupManager, ComponentTypeInArchetype *componentTypeInArchetypeArray) { var componentType = new ComponentTypeInArchetype(type); var archetype = GetArchetype(entity); var removedTypes = 0; int indexInOldTypeArray = -1; for (var t = 0; t < archetype->TypesCount; ++t) { if (archetype->Types[t].TypeIndex == componentType.TypeIndex) { indexInOldTypeArray = t; ++removedTypes; } else { componentTypeInArchetypeArray[t - removedTypes] = archetype->Types[t]; } } var newType = archetypeManager.GetOrCreateArchetype(componentTypeInArchetypeArray, archetype->TypesCount - removedTypes, groupManager); int *sharedComponentDataIndices = GetComponentChunk(entity)->SharedComponentValueArray; if ((newType->NumSharedComponents > 0) && (newType->NumSharedComponents != archetype->NumSharedComponents)) { var oldSharedComponentDataIndices = sharedComponentDataIndices; int *tempAlloc = stackalloc int[newType->NumSharedComponents]; sharedComponentDataIndices = tempAlloc; int indexOfRemovedSharedComponent = archetype->SharedComponentOffset[indexInOldTypeArray]; UnsafeUtility.MemCpy(sharedComponentDataIndices, oldSharedComponentDataIndices, indexOfRemovedSharedComponent * sizeof(int)); UnsafeUtility.MemCpy(sharedComponentDataIndices + indexOfRemovedSharedComponent, oldSharedComponentDataIndices + indexOfRemovedSharedComponent + 1, (newType->NumSharedComponents - indexOfRemovedSharedComponent) * sizeof(int)); } IncrementComponentOrderVersion(archetype, GetComponentChunk(entity), sharedComponentDataManager); SetArchetype(archetypeManager, entity, newType, sharedComponentDataIndices); }
private static unsafe bool TestMatchingArchetypeNone(Archetype *archetype, int *noneTypes, int noneCount) { bool flag2; ComponentTypeInArchetype *types = archetype.Types; int typesCount = archetype.TypesCount; int index = 0; while (true) { if (index < typesCount) { int typeIndex = types[index].TypeIndex; int num4 = 0; while (true) { if (num4 < noneCount) { int num5 = noneTypes[num4]; if (typeIndex != num5) { num4++; continue; } return(false); } else { index++; } break; } continue; } else { flag2 = true; } break; } return(flag2); }