private unsafe int FindNonDefaultSharedComponentIndex(int typeIndex, int hashCode, void *newData, FastEquality.TypeInfo typeInfo) { int itemIndex; NativeMultiHashMapIterator <int> iter; if (!m_HashLookup.TryGetFirstValue(hashCode, out itemIndex, out iter)) { return(-1); } do { var data = m_SharedComponentData[itemIndex]; if (data != null && m_SharedComponentType[itemIndex] == typeIndex) { ulong handle; var value = PinGCObjectAndGetAddress(data, out handle); var res = FastEquality.Equals(newData, value, typeInfo); UnsafeUtility.ReleaseGCObject(handle); if (res) { return(itemIndex); } } } while (m_HashLookup.TryGetNextValue(out itemIndex, ref iter)); return(-1); }
public TypeInfo(Type type, int typeIndex, int size, TypeCategory category, FastEquality.TypeInfo typeInfo, EntityOffsetInfo[] entityOffsets, EntityOffsetInfo[] blobAssetRefOffsets, ulong memoryOrdering, int bufferCapacity, int elementSize, int alignmentInBytes, ulong stableTypeHash, int *writeGroups, int writeGroupCount, int maximumChunkCapacity) { Type = type; TypeIndex = typeIndex; SizeInChunk = size; Category = category; FastEqualityTypeInfo = typeInfo; EntityOffsetCount = entityOffsets?.Length ?? 0; EntityOffsets = entityOffsets; BlobAssetRefOffsetCount = blobAssetRefOffsets?.Length ?? 0; BlobAssetRefOffsets = blobAssetRefOffsets; MemoryOrdering = memoryOrdering; BufferCapacity = bufferCapacity; ElementSize = elementSize; AlignmentInBytes = alignmentInBytes; StableTypeHash = stableTypeHash; WriteGroups = writeGroups; WriteGroupCount = writeGroupCount; MaximumChunkCapacity = maximumChunkCapacity; // System state shared components are also considered system state components bool isSystemStateSharedComponent = typeof(ISystemStateSharedComponentData).IsAssignableFrom(type); bool isSystemStateBufferElement = typeof(ISystemStateBufferElementData).IsAssignableFrom(type); bool isSystemStateComponent = isSystemStateSharedComponent || isSystemStateBufferElement || typeof(ISystemStateComponentData).IsAssignableFrom(type); if (typeIndex != 0) { if (SizeInChunk == 0) { TypeIndex |= ZeroSizeTypeFlag; } if (Category == TypeCategory.ISharedComponentData) { TypeIndex |= SharedComponentTypeFlag; } if (isSystemStateComponent) { TypeIndex |= SystemStateTypeFlag; } if (isSystemStateSharedComponent) { TypeIndex |= SystemStateSharedComponentTypeFlag; } if (BufferCapacity >= 0) { TypeIndex |= BufferComponentTypeFlag; } if (EntityOffsetCount == 0) { TypeIndex |= HasNoEntityReferencesFlag; } } }
private static unsafe int GetHashCodeFast(object target, FastEquality.TypeInfo typeInfo) { ulong handle; var ptr = PinGCObjectAndGetAddress(target, out handle); var hashCode = FastEquality.GetHashCode(ptr, typeInfo); UnsafeUtility.ReleaseGCObject(handle); return(hashCode); }
public ComponentType(Type type, int size, TypeCategory category, FastEquality.TypeInfo typeInfo, EntityOffsetInfo[] entityOffsets, UInt64 memoryOrdering, int bufferCapacity, int elementSize) { Type = type; SizeInChunk = size; Category = category; FastEqualityTypeInfo = typeInfo; EntityOffsets = entityOffsets; MemoryOrdering = memoryOrdering; BufferCapacity = bufferCapacity; ElementSize = elementSize; }
public TypeInfo(Type type, int size, TypeCategory category, FastEquality.TypeInfo typeInfo, EntityOffsetInfo[] entityOffsets, UInt64 memoryOrdering, int bufferCapacity, int elementSize) { Type = type; SizeInChunk = size; Category = category; FastEqualityTypeInfo = typeInfo; EntityOffsets = entityOffsets; MemoryOrdering = memoryOrdering; BufferCapacity = bufferCapacity; ElementSize = elementSize; IsSystemStateSharedComponent = typeof(ISystemStateSharedComponentData).IsAssignableFrom(type); IsSystemStateComponent = typeof(ISystemStateComponentData).IsAssignableFrom(type); }
public TypeInfo(System.Type type, int size, TypeManager.TypeCategory category, FastEquality.TypeInfo typeInfo, TypeManager.EntityOffsetInfo[] entityOffsets, ulong memoryOrdering, int bufferCapacity, int elementSize) { this.Type = type; this.SizeInChunk = size; this.Category = category; this.FastEqualityTypeInfo = typeInfo; this.EntityOffsets = entityOffsets; this.MemoryOrdering = memoryOrdering; this.BufferCapacity = bufferCapacity; this.ElementSize = elementSize; this.IsSystemStateSharedComponent = typeof(ISystemStateSharedComponentData).IsAssignableFrom(type); this.IsSystemStateComponent = typeof(ISystemStateComponentData).IsAssignableFrom(type); }
public TypeInfo(Type type, int size, TypeCategory category, FastEquality.TypeInfo typeInfo, EntityOffsetInfo[] entityOffsets, ulong memoryOrdering, int bufferCapacity, int elementSize, int alignmentInBytes, ulong stableTypeHash) { Type = type; SizeInChunk = size; Category = category; FastEqualityTypeInfo = typeInfo; EntityOffsetCount = entityOffsets != null ? entityOffsets.Length : 0; EntityOffsets = entityOffsets; MemoryOrdering = memoryOrdering; BufferCapacity = bufferCapacity; ElementSize = elementSize; AlignmentInBytes = alignmentInBytes; IsSystemStateSharedComponent = typeof(ISystemStateSharedComponentData).IsAssignableFrom(type); IsSystemStateComponent = typeof(ISystemStateComponentData).IsAssignableFrom(type); StableTypeHash = stableTypeHash; }
internal unsafe int InsertSharedComponentAssumeNonDefault(int typeIndex, int hashCode, object newData, FastEquality.TypeInfo typeInfo) { ulong handle; var newDataPtr = PinGCObjectAndGetAddress(newData, out handle); var index = FindNonDefaultSharedComponentIndex(typeIndex, hashCode, newDataPtr, typeInfo); UnsafeUtility.ReleaseGCObject(handle); if (-1 == index) { index = Add(typeIndex, hashCode, newData); } else { m_SharedComponentRefCount[index] += 1; } return(index); }
public static TypeInfo BuildComponentType(Type type) { TypeCategory componentData; int size = 0; FastEquality.TypeInfo @null = FastEquality.TypeInfo.Null; EntityOffsetInfo[] entityOffsets = null; int bufferCapacity = -1; ulong memoryOrdering = CalculateMemoryOrdering(type); int num4 = 0; if (type.IsInterface) { throw new ArgumentException($"{type} is an interface. It must be a concrete type."); } if (typeof(IComponentData).IsAssignableFrom(type)) { if (!type.IsValueType) { throw new ArgumentException($"{type} is an IComponentData, and thus must be a struct."); } if (!UnsafeUtility.IsBlittable(type)) { throw new ArgumentException($"{type} is an IComponentData, and thus must be blittable (No managed object is allowed on the struct)."); } componentData = TypeCategory.ComponentData; if (IsZeroSizeStruct(type)) { size = 0; } else { size = UnsafeUtility.SizeOf(type); } @null = FastEquality.CreateTypeInfo(type); entityOffsets = EntityRemapUtility.CalculateEntityOffsets(type); } else if (typeof(IBufferElementData).IsAssignableFrom(type)) { if (!type.IsValueType) { throw new ArgumentException($"{type} is an IBufferElementData, and thus must be a struct."); } if (!UnsafeUtility.IsBlittable(type)) { throw new ArgumentException($"{type} is an IBufferElementData, and thus must be blittable (No managed object is allowed on the struct)."); } componentData = TypeCategory.BufferData; num4 = UnsafeUtility.SizeOf(type); InternalBufferCapacityAttribute customAttribute = (InternalBufferCapacityAttribute)type.GetCustomAttribute(typeof(InternalBufferCapacityAttribute)); if (customAttribute != null) { bufferCapacity = customAttribute.Capacity; } else { bufferCapacity = 0x80 / num4; } size = sizeof(BufferHeader) + (bufferCapacity * num4); @null = FastEquality.CreateTypeInfo(type); entityOffsets = EntityRemapUtility.CalculateEntityOffsets(type); } else if (typeof(ISharedComponentData).IsAssignableFrom(type)) { if (!type.IsValueType) { throw new ArgumentException($"{type} is an ISharedComponentData, and thus must be a struct."); } componentData = TypeCategory.ISharedComponentData; @null = FastEquality.CreateTypeInfo(type); } else { if (!type.IsClass) { throw new ArgumentException($"{type} is not a valid component."); } componentData = TypeCategory.Class; if (type.FullName == "Unity.Entities.GameObjectEntity") { throw new ArgumentException("GameObjectEntity cannot be used from EntityManager. The component is ignored when creating entities for a GameObject."); } if (UnityEngineComponentType == null) { throw new ArgumentException($"{type} cannot be used from EntityManager. If it inherits UnityEngine.Component, you must first register {typeof(TypeManager)}.{"UnityEngineComponentType"} or include the Unity.Entities.Hybrid assembly in your build."); } if (!UnityEngineComponentType.IsAssignableFrom(type)) { throw new ArgumentException($"{type} must inherit {UnityEngineComponentType}."); } } int num5 = 0; foreach (Type type2 in s_SingularInterfaces) { if (type2.IsAssignableFrom(type)) { num5++; } } if (num5 > 1) { throw new ArgumentException($"Component {type} can only implement one of IComponentData, ISharedComponentData and IBufferElementData"); } return(new TypeInfo(type, size, componentData, @null, entityOffsets, memoryOrdering, bufferCapacity, (num4 > 0) ? num4 : size)); }