public void AllowSecondaryWriting() { AtomicSafetyHandle handle = AtomicSafetyHandle.Create(); AtomicSafetyHandle.SetAllowSecondaryVersionWriting(handle, false); AtomicSafetyHandle.UseSecondaryVersion(ref handle); AtomicSafetyHandle.CheckReadAndThrow(handle); Assert.Throws <InvalidOperationException>(() => AtomicSafetyHandle.CheckWriteAndThrow(handle)); }
public void ReleaseShouldThrow() { AtomicSafetyHandle handle = AtomicSafetyHandle.Create(); AtomicSafetyHandle.Release(handle); Assert.Throws <InvalidOperationException>(() => AtomicSafetyHandle.CheckReadAndThrow(handle)); Assert.Throws <InvalidOperationException>(() => AtomicSafetyHandle.CheckWriteAndThrow(handle)); Assert.Throws <InvalidOperationException>(() => AtomicSafetyHandle.CheckExistsAndThrow(handle)); }
unsafe public bool TryGetValue(TKey key, out TValue item) { NativeMultiHashMapIterator <TKey> tempIt; #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(m_Safety); #endif return(NativeHashMapBase <TKey, TValue> .TryGetFirstValueAtomic((NativeHashMapData *)m_Buffer, key, out item, out tempIt)); }
public bool Exists(Entity entity) { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(m_Safety); #endif //@TODO: out of bounds index checks... return(m_EntityComponentStore->HasComponent(entity, m_TypeIndex)); }
/// <summary> /// Returns an enumerator for key that iterates through a container. /// </summary> /// <param name="key">Key to enumerate values for.</param> /// <returns>An IEnumerator object that can be used to iterate through the container.</returns> public Enumerator GetValuesForKey(TKey key) { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(m_Safety); #endif return(new Enumerator { hashmap = this, key = key, isFirst = true }); }
public void CompleteWriteDependency(int type) { CompleteWriteDependencyNoChecks(type); #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(m_ComponentSafetyHandles[type].SafetyHandle); AtomicSafetyHandle.CheckReadAndThrow(m_ComponentSafetyHandles[type].BufferHandle); #endif }
public T[] ToArray() { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(m_Safety); #endif var array = new T[Length]; Copy(this, array); return(array); }
public NativeArray <T> ToNativeArray() { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(m_Safety); #endif var array = new NativeArray <T>(Length, m_AllocatorLabel); Copy(this, array); return(array); }
public unsafe NativeArray <Entity> GetNativeArray(ArchetypeChunkEntityType archetypeChunkEntityType) { AtomicSafetyHandle.CheckReadAndThrow(archetypeChunkEntityType.m_Safety); int count = this.m_Chunk.Count; NativeArray <Entity> array = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <Entity>((void *)(&this.m_Chunk.Buffer.FixedElementField + this.m_Chunk.Archetype.Offsets[0]), count, Allocator.None); NativeArrayUnsafeUtility.SetAtomicSafetyHandle <Entity>(ref array, archetypeChunkEntityType.m_Safety); return(array); }
public void Query(Bounds obbBounds, quaternion rotation, NativeList <T> resultList) { Assert.IsTrue(resultList.IsCreated); #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(_safety); #endif var bounds = TransformBounds(obbBounds, rotation); bounds.Clamp(_data->WorldBounds); CalculStartEndIterationInternal(_data, bounds, out var start, out var end); var hashMapUnic = new NativeHashMap <int, byte>(64, Allocator.Temp); var hashPosition = new int3(0F); //add offset for simplify logic and allowed pruning obbBounds.Size += _data->CellSize * 1F; var inverseRotation = math.inverse(rotation); for (int x = start.x; x < end.x; ++x) { hashPosition.x = x; for (int y = start.y; y < end.y; ++y) { hashPosition.y = y; for (int z = start.z; z < end.z; ++z) { hashPosition.z = z; var pos = GetPositionVoxel(hashPosition, true); if (obbBounds.RayCastOBBFast(pos - new float3(_data->CellSize.x * 0.5F, 0F, 0F), _right, inverseRotation, _data->CellSize.x) || obbBounds.RayCastOBBFast(pos - new float3(0F, _data->CellSize.y * 0.5F, 0F), _up, inverseRotation, _data->CellSize.y) || obbBounds.RayCastOBBFast(pos - new float3(0F, 0F, _data->CellSize.z * 0.5F), _forward, inverseRotation, _data->CellSize.z)) { var hash = Hash(hashPosition); if (_buckets.TryGetFirstValue(hash, out var item, out var it)) { do { hashMapUnic.TryAdd(item, 0); }while (_buckets.TryGetNextValue(out item, ref it)); } } } } } ExtractValueFromHashMap(hashMapUnic, bounds, resultList); }
public void CopyTo(NativeArray <T> array) { AtomicSafetyHandle.CheckReadAndThrow(this.m_Safety); AtomicSafetyHandle.CheckWriteAndThrow(array.m_Safety); if (this.Length != array.Length) { throw new ArgumentException("array.Length does not match the length of this instance"); } UnsafeUtility.MemCpy(array.m_Buffer, this.m_Buffer, (ulong)((long)this.Length * (long)UnsafeUtility.SizeOf <T>())); }
private unsafe void CheckElementReadAccess(int index) { if (index < m_MinIndex || index > m_MaxIndex) { FailOutOfRangeError(index); } #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(m_Safety); #endif }
/// <summary> /// 拷贝数组元素 /// </summary> /// <param name="src">原数组</param> /// <param name="dst">目标Native堆</param> public static void Copy(T[] src, NativeHeap <T> dst) { //检查是否可以读取手柄。如果已经销毁或作业当前正在写入数据,则引发异常 AtomicSafetyHandle.CheckReadAndThrow(dst.m_Safety); if (src.Length != dst.Length) { throw new ArgumentException("source and destination length must be the same"); } Copy(src, 0, dst, 0, src.Length); }
private void CheckValidAndThrow() { if (!Valid) { throw new InvalidOperationException("Invalid SampleProvider being used."); } #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(Safety); #endif }
public void CompleteWriteDependency(int type) { CompleteWriteDependencyNoChecks(type); #if ENABLE_UNITY_COLLECTIONS_CHECKS var typeWithoutFlags = type & TypeManager.ClearFlagsMask; AtomicSafetyHandle.CheckReadAndThrow(m_ComponentSafetyHandles[typeWithoutFlags].SafetyHandle); AtomicSafetyHandle.CheckReadAndThrow(m_ComponentSafetyHandles[typeWithoutFlags].BufferHandle); #endif }
void CheckReadAccess(int index) { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(m_Safety); if ((uint)index >= (uint)m_Length) { throw new System.IndexOutOfRangeException(string.Format("Index {0} is out of range in NativeArrayFullSOA of '{1}' Length.", index, m_Length)); } #endif }
private int GetCellCountFromSize(float3 size) { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(_safety); #endif var deltaSize = size / _data->CellSize; return((int)math.ceil(deltaSize.x) * (int)math.ceil(deltaSize.y) * (int)math.ceil(deltaSize.z)); }
public int this[Entity entity] { get { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(m_Safety); #endif return(m_ComponentGroupData.EntityIndex(entity)); } }
public T[] ToArray() { AtomicSafetyHandle.CheckReadAndThrow(this.m_Safety); T[] array = new T[this.Length]; for (int i = 0; i < this.Length; i++) { array[i] = UnsafeUtility.ReadArrayElement <T>(this.m_Buffer, i * this.m_Stride); } return(array); }
public static void Copy(NativeArrayNoLeakDetection <T> src, NativeArrayNoLeakDetection <T> dst) { AtomicSafetyHandle.CheckReadAndThrow(src.m_Safety); AtomicSafetyHandle.CheckWriteAndThrow(dst.m_Safety); if (src.Length != dst.Length) { throw new ArgumentException("source and destination length must be the same"); } Copy(src, 0, dst, 0, src.Length); }
private T this[int index] { get { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(this.safety); #endif return(ReadArrayElement <T>(this.binaryHeapData->Ptr, index)); } }
public unsafe void MoveLocationsInSameAreas(NativeSlice <NavMeshLocation> locations, NativeSlice <Vector3> targets, int areaMask = NavMesh.AllAreas) { if (locations.Length != targets.Length) { throw new ArgumentException("locations.Length and targets.Length must be equal"); } AtomicSafetyHandle.CheckReadAndThrow(m_Safety); MoveLocationsInSameAreas(m_NavMeshQuery, locations.GetUnsafePtr(), targets.GetUnsafeReadOnlyPtr(), locations.Length, areaMask); }
/// <summary> /// /// </summary> /// <param name="chunkComponentType"></param> /// <param name="expectedTypeSize"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> /// <exception cref="ArgumentException"></exception> /// <exception cref="InvalidOperationException"></exception> public NativeArray <T> GetDynamicComponentDataArrayReinterpret <T>(ArchetypeChunkComponentTypeDynamic chunkComponentType, int expectedTypeSize) where T : struct { #if ENABLE_UNITY_COLLECTIONS_CHECKS if (chunkComponentType.m_IsZeroSized) { throw new ArgumentException($"ArchetypeChunk.GetDynamicComponentDataArrayReinterpret<{typeof(T)}> cannot be called on zero-sized IComponentData"); } AtomicSafetyHandle.CheckReadAndThrow(chunkComponentType.m_Safety); #endif var archetype = m_Chunk->Archetype; int typeIndexInArchetype = chunkComponentType.m_TypeLookupCache; ChunkDataUtility.GetIndexInTypeArray(m_Chunk->Archetype, chunkComponentType.m_TypeIndex, ref typeIndexInArchetype); chunkComponentType.m_TypeLookupCache = (short)typeIndexInArchetype; if (typeIndexInArchetype == -1) { var emptyResult = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <T>(null, 0, 0); #if ENABLE_UNITY_COLLECTIONS_CHECKS NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref emptyResult, chunkComponentType.m_Safety); #endif return(emptyResult); } var typeSize = archetype->SizeOfs[typeIndexInArchetype]; var length = m_Chunk->Count; var byteLen = length * typeSize; var outTypeSize = UnsafeUtility.SizeOf <T>(); var outLength = byteLen / outTypeSize; #if ENABLE_UNITY_COLLECTIONS_CHECKS if (typeSize != expectedTypeSize) { throw new InvalidOperationException($"Dynamic chunk component type {TypeManager.GetType(chunkComponentType.m_TypeIndex)} (size = {typeSize}) size does not equal {expectedTypeSize}. Component size must match with expectedTypeSize."); } if (outTypeSize * outLength != byteLen) { throw new InvalidOperationException($"Dynamic chunk component type {TypeManager.GetType(chunkComponentType.m_TypeIndex)} (array length {length}) and {typeof(T)} cannot be aliased due to size constraints. The size of the types and lengths involved must line up."); } #endif var buffer = m_Chunk->Buffer; var startOffset = archetype->Offsets[typeIndexInArchetype]; var result = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <T>(buffer + startOffset, outLength, Allocator.None); #if ENABLE_UNITY_COLLECTIONS_CHECKS NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref result, chunkComponentType.m_Safety); #endif if (!chunkComponentType.IsReadOnly) { m_Chunk->SetChangeVersion(typeIndexInArchetype, chunkComponentType.GlobalSystemVersion); } return(result); }
public bool IsAllocated(int index) { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(m_Safety); #endif CheckArgPositive(index); var positiveIndex = AssumePositive(index); CheckArgInRange(positiveIndex, m_Array->Length); var ptr = m_Array->Ptr[positiveIndex]; return(ptr != null && ptr->IsCreated); }
public Vector2 GetUV(int index) { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(m_Safety); if ((uint)index >= (uint)UVCount) { throw new IndexOutOfRangeException($"Index {index} is out of range in VertexArray of '{UVCount}' Length."); } #endif return(UnsafeUtility.ReadArrayElement <Vector2>(UV, index)); }
static void CopySafe(ReadOnly src, int srcIndex, NativeArray <T> dst, int dstIndex, int length) { AtomicSafetyHandle.CheckReadAndThrow(src.m_Safety); AtomicSafetyHandle.CheckWriteAndThrow(dst.m_Safety); CheckCopyArguments(src.Length, srcIndex, dst.Length, dstIndex, length); UnsafeUtility.MemCpy( (byte *)dst.m_Buffer + dstIndex * UnsafeUtility.SizeOf <T>(), (byte *)src.m_Buffer + srcIndex * UnsafeUtility.SizeOf <T>(), length * UnsafeUtility.SizeOf <T>()); }
public void MoveLocationsInSameAreas(NativeSlice <NavMeshLocation> locations, NativeSlice <Vector3> targets, int areaMask = -1) { bool flag = locations.Length != targets.Length; if (flag) { throw new ArgumentException("locations.Length and targets.Length must be equal"); } AtomicSafetyHandle.CheckReadAndThrow(this.m_Safety); NavMeshQuery.MoveLocationsInSameAreas(this.m_NavMeshQuery, locations.GetUnsafePtr <NavMeshLocation>(), targets.GetUnsafeReadOnlyPtr <Vector3>(), locations.Length, areaMask); }
/// <summary> /// Retrieve a member of the contaner by index. /// </summary> /// <param name="index">The zero-based index into the list.</param> /// <value>The list item at the specified index.</value> /// <exception cref="IndexOutOfRangeException">Thrown if index is negative or >= to <see cref="Length"/>.</exception> public float3 this[int index] { get { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(m_Safety); CheckIndexInRange(index, m_Vertices->Length); #endif return(UnsafeUtility.ReadArrayElement <float3>(m_Vertices->Ptr, index)); } }
void CheckReadIndex(int index) { #if ENABLE_UNITY_COLLECTIONS_CHECKS if (index < m_MinIndex || index > m_MaxIndex) { FailOutOfRangeError(index); } AtomicSafetyHandle.CheckReadAndThrow(m_Safety); #endif }
public int3 GetIndexVoxel(float3 position) { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(_safety); #endif position -= _data->WorldBoundsMin; position /= _data->CellSize; return(position.FloorToInt()); }