private static unsafe void ApplySetComponents( EntityManager entityManager, NativeArray <PackedComponentDataChange> changes, NativeArray <byte> payload, NativeArray <EntityGuid> packedEntityGuids, NativeMultiHashMap <int, Entity> packedEntities, NativeArray <ComponentType> packedTypes, NativeMultiHashMap <EntityGuid, Entity> entityGuidToEntity, NativeHashMap <Entity, EntityGuid> entityToEntityGuid) { var entityGuidTypeIndex = TypeManager.GetTypeIndex <EntityGuid>(); var offset = 0L; for (var i = 0; i < changes.Length; i++) { var packedComponentDataChange = changes[i]; var packedComponent = packedComponentDataChange.Component; var component = packedTypes[packedComponent.PackedTypeIndex]; var size = packedComponentDataChange.Size; var data = (byte *)payload.GetUnsafeReadOnlyPtr() + offset; var componentTypeInArchetype = new ComponentTypeInArchetype(component); if (packedEntities.TryGetFirstValue(packedComponent.PackedEntityIndex, out var entity, out var iterator)) { do { if (!entityManager.Exists(entity)) { Debug.LogWarning($"SetComponent<{component}>({packedEntityGuids[packedComponent.PackedEntityIndex]}) but entity does not exist."); } else if (!entityManager.HasComponent(entity, component)) { Debug.LogWarning($"SetComponent<{component}>({packedEntityGuids[packedComponent.PackedEntityIndex]}) but component does not exist."); } else { if (componentTypeInArchetype.IsZeroSized) { // Nothing to set. } else if (componentTypeInArchetype.IsBuffer) { var typeInfo = TypeManager.GetTypeInfo(componentTypeInArchetype.TypeIndex); var elementSize = typeInfo.ElementSize; var lengthInElements = size / elementSize; var header = (BufferHeader *)entityManager.GetComponentDataRawRW(entity, component.TypeIndex); BufferHeader.Assign(header, data, lengthInElements, elementSize, 16); } else { var target = (byte *)entityManager.GetComponentDataRawRW(entity, component.TypeIndex); // Perform incremental updates on the entityGuidToEntity map to avoid a full rebuild. if (componentTypeInArchetype.TypeIndex == entityGuidTypeIndex) { EntityGuid entityGuid; UnsafeUtility.MemCpy(&entityGuid, target, sizeof(EntityGuid)); if (!entityGuid.Equals(default))
/// <summary> /// Gets the dynamic buffer of an entity. /// </summary> /// <param name="entity">The entity.</param> /// <typeparam name="T">The type of the buffer's elements.</typeparam> /// <returns>The DynamicBuffer object for accessing the buffer contents.</returns> /// <exception cref="ArgumentException">Thrown if T is an unsupported type.</exception> public DynamicBuffer <T> GetBuffer <T>(Entity entity) where T : struct, IBufferElementData { var typeIndex = TypeManager.GetTypeIndex <T>(); #if ENABLE_UNITY_COLLECTIONS_CHECKS EntityComponentStore->AssertEntityHasComponent(entity, typeIndex); if (!TypeManager.IsBuffer(typeIndex)) { throw new ArgumentException( $"GetBuffer<{typeof(T)}> may not be IComponentData or ISharedComponentData; currently {TypeManager.GetTypeInfo<T>().Category}"); } #endif ComponentJobSafetyManager->CompleteReadAndWriteDependency(typeIndex); BufferHeader *header = (BufferHeader *)EntityComponentStore->GetComponentDataWithTypeRW(entity, typeIndex, EntityComponentStore->GlobalSystemVersion); int internalCapacity = TypeManager.GetTypeInfo(typeIndex).BufferCapacity; #if ENABLE_UNITY_COLLECTIONS_CHECKS return(new DynamicBuffer <T>(header, ComponentJobSafetyManager->GetSafetyHandle(typeIndex, false), ComponentJobSafetyManager->GetBufferSafetyHandle(typeIndex), false, internalCapacity)); #else return(new DynamicBuffer <T>(header, internalCapacity)); #endif }
public ComponentType(Type type, AccessMode accessModeType = AccessMode.ReadWrite) { TypeIndex = TypeManager.GetTypeIndex(type); var ct = TypeManager.GetTypeInfo(TypeIndex); AccessModeType = accessModeType; }
private unsafe void ComputeMasks() { for (int i = 0; i < this.m_sorted.Length; i++) { int typeIndex = this.m_sorted[i]; TypeManager.TypeInfo typeInfo = TypeManager.GetTypeInfo(typeIndex); ushort num3 = (ushort)(1 << (i & 0x1f)); if (typeInfo.BufferCapacity >= 0) { ushort *numPtr1 = (ushort *)ref this.m_masks.m_BufferMask; numPtr1[0] = (ushort)(numPtr1[0] | num3); } if (typeInfo.IsSystemStateComponent) { ushort *numPtr2 = (ushort *)ref this.m_masks.m_SystemStateComponentMask; numPtr2[0] = (ushort)(numPtr2[0] | num3); } if (typeInfo.IsSystemStateSharedComponent) { ushort *numPtr3 = (ushort *)ref this.m_masks.m_SystemStateSharedComponentMask; numPtr3[0] = (ushort)(numPtr3[0] | num3); } if (TypeManager.TypeCategory.ISharedComponentData == typeInfo.Category) { ushort *numPtr4 = (ushort *)ref this.m_masks.m_SharedComponentMask; numPtr4[0] = (ushort)(numPtr4[0] | num3); } if (typeInfo.IsZeroSized) { ushort *numPtr5 = (ushort *)ref this.m_masks.m_ZeroSizedMask; numPtr5[0] = (ushort)(numPtr5[0] | num3); } } }
// After cloning two worlds have access to the same malloc'ed buffer pointer leading to double deallocate etc. // So after cloning, just allocate all malloc based buffers and copy the data. public static void PatchAfterCloningChunkForDiff(Chunk *chunk) { for (int i = 0; i < chunk->Archetype->TypesCount; ++i) { var type = chunk->Archetype->Types[i]; if (!type.IsBuffer) { continue; } var ti = TypeManager.GetTypeInfo(type.TypeIndex); var sizeOf = chunk->Archetype->SizeOfs[i]; var offset = chunk->Archetype->Offsets[i]; for (var j = 0; j < chunk->Count; ++j) { var offsetOfBuffer = offset + sizeOf * j; var header = (BufferHeader *)(chunk->Buffer + offsetOfBuffer); if (header->Pointer != null) // hoo boy, it's a malloc { BufferHeader newHeader = *header; var bytesToAllocate = header->Capacity * ti.ElementSize; var bytesToCopy = header->Length * ti.ElementSize; newHeader.Pointer = (byte *)UnsafeUtility.Malloc(bytesToAllocate, 16, Allocator.Persistent); UnsafeUtility.MemCpy(newHeader.Pointer, header->Pointer, bytesToCopy); *header = newHeader; } } } }
public DynamicBuffer <T> GetBuffer <T>(Entity entity) where T : struct, IBufferElementData { CheckAccess(); var typeIndex = TypeManager.GetTypeIndex <T>(); m_EntityComponentStore->AssertEntityHasComponent(entity, typeIndex); #if ENABLE_UNITY_COLLECTIONS_CHECKS if (!TypeManager.IsBuffer(typeIndex)) { throw new ArgumentException( $"GetBuffer<{typeof(T)}> may not be IComponentData or ISharedComponentData; currently {TypeManager.GetTypeInfo<T>().Category}"); } #endif BufferHeader *header = (BufferHeader *)m_EntityComponentStore->GetComponentDataWithTypeRW(entity, typeIndex, m_EntityComponentStore->GlobalSystemVersion); int internalCapacity = TypeManager.GetTypeInfo(typeIndex).BufferCapacity; #if ENABLE_UNITY_COLLECTIONS_CHECKS return(new DynamicBuffer <T>(header, m_Safety, m_Safety, false, internalCapacity)); #else return(new DynamicBuffer <T>(header, internalCapacity)); #endif }
static void AssignHybridComponentsToCompanionGameObjectsDelegate(EntityManager entityManager, NativeArray <Entity> entities) { for (int i = 0; i < entities.Length; ++i) { var entity = entities[i]; var companionGameObject = entityManager.GetComponentData <CompanionLink>(entity).Companion; var archetypeChunk = entityManager.GetChunk(entities[i]); var archetype = archetypeChunk.Archetype.Archetype; var types = archetype->Types; var firstIndex = archetype->FirstManagedComponent; var lastIndex = archetype->ManagedComponentsEnd; for (int t = firstIndex; t < lastIndex; ++t) { var type = TypeManager.GetTypeInfo(types[t].TypeIndex); if (type.Category != TypeManager.TypeCategory.Class) { continue; } var hybridComponent = companionGameObject.GetComponent(type.Type); entityManager.SetComponentObject(entity, ComponentType.FromTypeIndex(type.TypeIndex), hybridComponent); } } entityManager.RemoveComponent <CompanionGameObjectActiveSystemState>(entities); entityManager.RemoveComponent <EditorCompanionInPreviewSceneTag>(entities); }
public override string ToString() { #if UNITY_CSHARP_TINY var name = TypeManager.GetTypeInfo(TypeIndex).StableTypeHash.ToString(); #else var name = GetManagedType().Name; if (IsBuffer) { return($"{name}[B]"); } if (AccessModeType == AccessMode.Subtractive) { return($"{name} [S]"); } if (AccessModeType == AccessMode.ReadOnly) { return($"{name} [RO]"); } if (TypeIndex == 0) { return("None"); } #endif return(name); }
//cm internal void AddBufferRaw(Entity entity, int componentTypeIndex, BufferHeader *tempBuffer) { EntityComponentStore->AssertEntityHasComponent(entity, componentTypeIndex); ComponentJobSafetyManager->CompleteReadAndWriteDependency(componentTypeIndex); var bufferHeader = (BufferHeader *)EntityComponentStore->GetComponentDataWithTypeRW(entity, componentTypeIndex, EntityComponentStore->GlobalSystemVersion); var typeInfo = TypeManager.GetTypeInfo(componentTypeIndex); var elementSize = typeInfo.ElementSize; var alignment = typeInfo.AlignmentInBytes; var total = bufferHeader->Length + tempBuffer->Length; if (total > bufferHeader->Capacity) { BufferHeader.EnsureCapacity(bufferHeader, total, elementSize, alignment, BufferHeader.TrashMode.RetainOldData); } UnsafeUtility.MemCpy( BufferHeader.GetElementPointer(bufferHeader) + bufferHeader->Length * elementSize, BufferHeader.GetElementPointer(tempBuffer), tempBuffer->Length * elementSize); bufferHeader->Length = total; }
public ComponentType(Type type, AccessMode accessModeType = 0) { this.TypeIndex = TypeManager.GetTypeIndex(type); TypeManager.TypeInfo typeInfo = TypeManager.GetTypeInfo(this.TypeIndex); this.BufferCapacity = typeInfo.BufferCapacity; this.AccessModeType = accessModeType; }
public object GetComponentBoxed(Entity entity, ComponentType type) { m_Manager.EntityComponentStore->AssertEntityHasComponent(entity, type); var typeInfo = TypeManager.GetTypeInfo(type.TypeIndex); if (typeInfo.Category == TypeManager.TypeCategory.ComponentData) { var obj = Activator.CreateInstance(TypeManager.GetType(type.TypeIndex)); if (!typeInfo.IsZeroSized) { ulong handle; var ptr = (byte *)UnsafeUtility.PinGCObjectAndGetAddress(obj, out handle); ptr += TypeManager.ObjectOffset; var src = m_Manager.EntityComponentStore->GetComponentDataWithTypeRO(entity, type.TypeIndex); UnsafeUtility.MemCpy(ptr, src, TypeManager.GetTypeInfo(type.TypeIndex).SizeInChunk); UnsafeUtility.ReleaseGCObject(handle); } return(obj); } else if (typeInfo.Category == TypeManager.TypeCategory.ISharedComponentData) { return(m_Manager.GetSharedComponentData(entity, type.TypeIndex)); } else { throw new System.NotImplementedException(); } }
public override string ToString() { #if NET_DOTS var name = TypeManager.GetTypeInfo(TypeIndex).StableTypeHash.ToString(); #else var name = GetManagedType().Name; if (IsBuffer) { return($"{name} [B]"); } if (AccessModeType == AccessMode.Exclude) { return($"{name} [S]"); } if (AccessModeType == AccessMode.ReadOnly) { return($"{name} [RO]"); } if (TypeIndex == 0) { return("None"); } #endif return(name); }
internal void AssertZeroSizedComponent(int typeIndex) { if (TypeManager.GetTypeInfo(typeIndex).IsZeroSized) { throw new System.ArgumentException( "Get component data can not be called with a zero sized component."); } }
internal static void AssertComponentSizeMatches(int typeIndex, int size) { if (TypeManager.GetTypeInfo(typeIndex).SizeInChunk != size) { throw new System.ArgumentException( "SetComponentData can not be called with a zero sized component and must have same size as sizeof(T)."); } }
public unsafe TValue GetValue(ref EntityContainer container) { if (!TypeManager.GetTypeInfo(m_TypeIndex).IsZeroSized) { return(Unsafe.AsRef <TValue>(container.EntityManager.GetComponentDataRawRO(container.Entity, m_TypeIndex))); } return(default);
public static void ReplicateComponents(Chunk *srcChunk, int srcIndex, Chunk *dstChunk, int dstBaseIndex, int count) { var srcArchetype = srcChunk->Archetype; var srcBuffer = srcChunk->Buffer; var dstBuffer = dstChunk->Buffer; var dstArchetype = dstChunk->Archetype; var srcOffsets = srcArchetype->Offsets; var srcSizeOfs = srcArchetype->SizeOfs; var srcBufferCapacities = srcArchetype->BufferCapacities; var srcTypesCount = srcArchetype->TypesCount; var srcTypes = srcArchetype->Types; var dstTypes = dstArchetype->Types; var dstOffsets = dstArchetype->Offsets; var dstTypeIndex = 1; // type[0] is always Entity, and will be patched up later, so just skip for (var srcTypeIndex = 1; srcTypeIndex != srcTypesCount; srcTypeIndex++) { var srcType = srcTypes[srcTypeIndex]; var dstType = dstTypes[dstTypeIndex]; // Type does not exist in destination. Skip it. if (srcType.TypeIndex != dstType.TypeIndex) { continue; } var srcOffset = srcOffsets[srcTypeIndex]; var srcSizeOf = srcSizeOfs[srcTypeIndex]; var dstOffset = dstOffsets[dstTypeIndex]; var src = srcBuffer + (srcOffset + srcSizeOf * srcIndex); var dst = dstBuffer + (dstOffset + srcSizeOf * dstBaseIndex); if (!srcType.IsBuffer) { UnsafeUtility.MemCpyReplicate(dst, src, srcSizeOf, count); } else { var srcBufferCapacity = srcBufferCapacities[srcTypeIndex]; var alignment = 8; // TODO: Need a way to compute proper alignment for arbitrary non-generic types in TypeManager var elementSize = TypeManager.GetTypeInfo(srcType.TypeIndex).ElementSize; for (int i = 0; i < count; ++i) { BufferHeader *srcHdr = (BufferHeader *)src; BufferHeader *dstHdr = (BufferHeader *)dst; BufferHeader.Initialize(dstHdr, srcBufferCapacity); BufferHeader.Assign(dstHdr, BufferHeader.GetElementPointer(srcHdr), srcHdr->Length, elementSize, alignment); dst += srcSizeOf; } } dstTypeIndex++; } }
public static bool Equals <T>(ref T left, ref T right) where T : struct { #if !UNITY_CSHARP_TINY var typeInfo = TypeManager.GetTypeInfo <T>().FastEqualityTypeInfo; return(FastEquality.Equals(ref left, ref right, typeInfo)); #else return(EqualityHelper <T> .Equals(left, right)); #endif }
public unsafe void SetValue(ref EntityContainer container, TValue value) { if (TypeManager.GetTypeInfo(m_TypeIndex).IsZeroSized) { throw new InvalidOperationException("Property is ReadOnly"); } Unsafe.Copy(container.EntityManager.GetComponentDataRawRW(container.Entity, m_TypeIndex), ref value); }
internal BufferFromEntity(int typeIndex, EntityComponentStore *entityComponentStoreComponentStore, bool isReadOnly) { m_TypeIndex = typeIndex; m_EntityComponentStore = entityComponentStoreComponentStore; m_IsReadOnly = isReadOnly; m_TypeLookupCache = 0; m_GlobalSystemVersion = entityComponentStoreComponentStore->GlobalSystemVersion; m_InternalCapacity = TypeManager.GetTypeInfo <T>().BufferCapacity; }
public static bool Equals(void *left, void *right, int typeIndex) { #if !UNITY_CSHARP_TINY var typeInfo = TypeManager.GetTypeInfo(typeIndex).FastEqualityTypeInfo; return(FastEquality.Equals(left, right, typeInfo)); #else return(StaticTypeRegistry.StaticTypeRegistry.Equals(left, right, typeIndex & ClearFlagsMask)); #endif }
public static int GetHashCode(void *val, int typeIndex) { #if !UNITY_CSHARP_TINY var typeInfo = TypeManager.GetTypeInfo(typeIndex).FastEqualityTypeInfo; return(FastEquality.GetHashCode(val, typeInfo)); #else return(StaticTypeRegistry.StaticTypeRegistry.GetHashCode(val, typeIndex & ClearFlagsMask)); #endif }
public static int GetHashCode <T>(ref T val) where T : struct { #if !UNITY_CSHARP_TINY var typeInfo = TypeManager.GetTypeInfo <T>().FastEqualityTypeInfo; return(FastEquality.GetHashCode(ref val, typeInfo)); #else return(EqualityHelper <T> .Hash(val)); #endif }
internal BufferFromEntity(int typeIndex, EntityDataManager *entityData, bool isReadOnly) { m_TypeIndex = typeIndex; m_Entities = entityData; m_IsReadOnly = isReadOnly; m_TypeLookupCache = 0; m_GlobalSystemVersion = entityData->GlobalSystemVersion; m_InternalCapacity = TypeManager.GetTypeInfo <T>().BufferCapacity; }
private static ComponentType GetWriteGroupReadOnlyComponentType(NativeArray <int> writeGroupTypes, int i) { // Need to get "Clean" TypeIndex from Type. Since TypeInfo.TypeIndex is not actually the index of the // type. (It includes other flags.) What is stored in WriteGroups is the actual index of the type. var excludedType = TypeManager.GetTypeInfo(writeGroupTypes[i]); var excludedComponentType = ComponentType.ReadOnly(excludedType.TypeIndex); return(excludedComponentType); }
public static ComponentType FromTypeIndex(int typeIndex) { ComponentType type; type.TypeIndex = typeIndex; type.AccessModeType = AccessMode.ReadWrite; type.BufferCapacity = TypeManager.GetTypeInfo(typeIndex).BufferCapacity; return(type); }
private static PackedSharedComponentDataChange[] GetChangedSharedComponents( PackedCollection <EntityGuid> packedEntityCollection, PackedCollection <ComponentTypeHash> packedStableTypeHashCollection, NativeList <DeferredPackedSharedComponentDataChange> changes, ManagedComponentStore beforeManagedComponentStore, ManagedComponentStore afterManagedComponentStore) { if (changes.Length == 0) { return(s_EmptySetSharedComponentDiff); } var result = new List <PackedSharedComponentDataChange>(); for (var i = 0; i < changes.Length; i++) { var change = changes[i]; object afterValue = null; if (change.AfterSharedComponentIndex != 0) { afterValue = afterManagedComponentStore.GetSharedComponentDataBoxed(change.AfterSharedComponentIndex, change.TypeIndex); } if (change.BeforeSharedComponentIndex > -1 && change.AfterSharedComponentIndex != 0) { var beforeValue = beforeManagedComponentStore.GetSharedComponentDataBoxed(change.BeforeSharedComponentIndex, change.TypeIndex); if (TypeManager.Equals(beforeValue, afterValue, change.TypeIndex)) { continue; } } var packedEntityIndex = packedEntityCollection.GetOrAdd(change.EntityGuid); var packedTypeIndex = packedStableTypeHashCollection.GetOrAdd(new ComponentTypeHash { StableTypeHash = TypeManager.GetTypeInfo(change.TypeIndex).StableTypeHash }); var packedComponent = new PackedComponent { PackedEntityIndex = packedEntityIndex, PackedTypeIndex = packedTypeIndex }; result.Add(new PackedSharedComponentDataChange { Component = packedComponent, BoxedSharedValue = afterValue }); } return(result.ToArray()); }
internal unsafe void *GetComponentDataRawRW(Entity entity, int typeIndex) { this.Entities.AssertEntityHasComponent(entity, typeIndex); this.ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex); if (TypeManager.GetTypeInfo(typeIndex).IsZeroSized) { throw new ArgumentException($"GetComponentDataRaw<{TypeManager.GetType(typeIndex)}> can not be called with a zero sized component."); } return((void *)ref this.Entities.GetComponentDataWithTypeRW(entity, typeIndex, this.Entities.GlobalSystemVersion)); }
internal unsafe void SetComponentDataRaw(Entity entity, int typeIndex, void *data, int size) { this.Entities.AssertEntityHasComponent(entity, typeIndex); this.ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex); if (TypeManager.GetTypeInfo(typeIndex).SizeInChunk != size) { throw new ArgumentException($"SetComponentDataRaw<{TypeManager.GetType(typeIndex)}> can not be called with a zero sized component and must have same size as sizeof(T)."); } UnsafeUtility.MemCpy((void *)this.Entities.GetComponentDataWithTypeRW(entity, typeIndex, this.Entities.GlobalSystemVersion), data, (long)size); }
internal ArchetypeChunkComponentType(AtomicSafetyHandle safety, bool isReadOnly, uint globalSystemVersion) { this.m_Length = 1; this.m_TypeIndex = TypeManager.GetTypeIndex <T>(); this.m_IsZeroSized = TypeManager.GetTypeInfo(this.m_TypeIndex).IsZeroSized; this.m_GlobalSystemVersion = globalSystemVersion; this.m_IsReadOnly = isReadOnly; this.m_MinIndex = 0; this.m_MaxIndex = 0; this.m_Safety = safety; }
private unsafe int FindSharedComponentIndex <T>(int typeIndex, T newData) where T : struct { var defaultVal = default(T); var typeInfo = TypeManager.GetTypeInfo(typeIndex).FastEqualityTypeInfo; if (FastEquality.Equals(ref defaultVal, ref newData, typeInfo)) { return(0); } return(FindNonDefaultSharedComponentIndex(typeIndex, FastEquality.GetHashCode(ref newData, typeInfo), UnsafeUtility.AddressOf(ref newData), typeInfo)); }