A game about forced loneliness, made by TACStudios
at master 91 lines 3.3 kB view raw
1using System; 2 3namespace Unity.Collections.LowLevel.Unsafe.NotBurstCompatible 4{ 5 /// <summary> 6 /// Provides some extension methods for various collections. 7 /// </summary> 8 public static class Extensions 9 { 10 /// <summary> 11 /// Returns a new managed array with all the elements copied from a set. 12 /// </summary> 13 /// <typeparam name="T">The type of elements.</typeparam> 14 /// <param name="set">The set whose elements are copied to the array.</param> 15 /// <returns>A new managed array with all the elements copied from a set.</returns> 16 public static T[] ToArray<T>(this UnsafeParallelHashSet<T> set) 17 where T : unmanaged, IEquatable<T> 18 { 19 var array = set.ToNativeArray(Allocator.TempJob); 20 var managed = array.ToArray(); 21 array.Dispose(); 22 return managed; 23 } 24 25 /// <summary> 26 /// Adds the content of a string to this append buffer. 27 /// </summary> 28 /// <remarks>The length of the string is written as an int to the buffer before the characters are written.</remarks> 29 /// <param name="buffer">The buffer to which to add the string.</param> 30 /// <param name="value">The string to copy.</param> 31 [ExcludeFromBurstCompatTesting("Takes managed string")] 32 public static unsafe void AddNBC(ref this UnsafeAppendBuffer buffer, string value) 33 { 34 if (value != null) 35 { 36 buffer.Add(value.Length); 37 fixed (char* ptr = value) 38 { 39 buffer.Add(ptr, sizeof(char) * value.Length); 40 } 41 } 42 else 43 { 44 buffer.Add(-1); 45 } 46 } 47 48 /// <summary> 49 /// Returns an unmanaged byte array with a copy of this buffer's contents. 50 /// </summary> 51 /// <param name="buffer">This buffer.</param> 52 /// <returns>An unmanaged byte array with a copy of this buffer's contents.</returns> 53 [ExcludeFromBurstCompatTesting("Returns managed array")] 54 public static unsafe byte[] ToBytesNBC(ref this UnsafeAppendBuffer buffer) 55 { 56 var dst = new byte[buffer.Length]; 57 fixed (byte* dstPtr = dst) 58 { 59 UnsafeUtility.MemCpy(dstPtr, buffer.Ptr, buffer.Length); 60 } 61 return dst; 62 } 63 64 /// <summary> 65 /// Reads a string from this buffer reader. 66 /// </summary> 67 /// <param name="value">Outputs the string.</param> 68 /// <param name="reader">This reader.</param> 69 [ExcludeFromBurstCompatTesting("Managed string out argument")] 70 public static unsafe void ReadNextNBC(ref this UnsafeAppendBuffer.Reader reader, out string value) 71 { 72 int length; 73 reader.ReadNext(out length); 74 75 if (length != -1) 76 { 77 value = new string('0', length); 78 79 fixed (char* buf = value) 80 { 81 int bufLen = length * sizeof(char); 82 UnsafeUtility.MemCpy(buf, reader.ReadNext(bufLen), bufLen); 83 } 84 } 85 else 86 { 87 value = null; 88 } 89 } 90 } 91}