public void WriteStruct <T>(T str) { var strSize = Marshal.SizeOf(typeof(T)); Marshal.StructureToPtr(str, BytesPtr, true); BytesPtr = BytesPtr.Offset(strSize); }
public Color32[] ReadColor32Array() { var elementsCount = ReadInt32(); if (elementsCount == 0) { return(new Color32[0]); } var result = new Color32[elementsCount]; var arrByteSize = elementsCount * 4 * sizeof(byte); var handleVs = GCHandle.Alloc(result, GCHandleType.Pinned); try { CopyMemory(handleVs.AddrOfPinnedObject(), BytesPtr, arrByteSize); } finally { handleVs.Free(); BytesPtr = BytesPtr.Offset(arrByteSize); } BytesPtr = BytesPtr.Offset(arrByteSize); return(result); }
public int ReadInt32() { var result = Marshal.ReadInt32(BytesPtr); BytesPtr = BytesPtr.Offset(4); return(result); }
public T ReadStruct <T>() where T : struct { var strSize = Marshal.SizeOf(typeof(T)); var result = (T)Marshal.PtrToStructure(BytesPtr, typeof(T)); BytesPtr = BytesPtr.Offset(strSize); return(result); }
public void WriteString(string s) { var elementsCount = s.Length; WriteInt(elementsCount); if (s.Length == 0) { return; } Marshal.Copy(s.ToCharArray(), 0, BytesPtr, elementsCount); BytesPtr = BytesPtr.Offset(elementsCount * sizeof(char)); }
public void WriteArray(byte[] array) { WriteInt(array.Length); if (array.Length == 0) { return; } var arrByteSize = array.Length; Marshal.Copy(array, 0, BytesPtr, array.Length); BytesPtr = BytesPtr.Offset(arrByteSize); }
public string ReadString() { var elementsCount = ReadInt32(); if (elementsCount == 0) { return(string.Empty); } var result = new char[elementsCount]; Marshal.Copy(BytesPtr, result, 0, elementsCount); BytesPtr = BytesPtr.Offset(elementsCount * sizeof(char)); return(new string(result)); }
public byte[] ReadByteArray() { var elementsCount = ReadInt32(); if (elementsCount == 0) { return(new byte[0]); } var result = new byte[elementsCount]; Marshal.Copy(BytesPtr, result, 0, elementsCount); BytesPtr = BytesPtr.Offset(elementsCount); return(result); }
public int[] ReadInt32Array() { var elementsCount = ReadInt32(); if (elementsCount == 0) { return(new int[0]); } var result = new int[elementsCount * sizeof(int)]; Marshal.Copy(BytesPtr, result, 0, elementsCount); BytesPtr = BytesPtr.Offset(elementsCount * sizeof(int)); return(result); }
public void WriteArray(Color32[] array) { WriteInt(array.Length); if (array.Length == 0) { return; } var arrByteSize = array.Length * 4 * sizeof(byte); var handleVs = GCHandle.Alloc(array, GCHandleType.Pinned); try { CopyMemory(BytesPtr, handleVs.AddrOfPinnedObject(), arrByteSize); } finally { handleVs.Free(); BytesPtr = BytesPtr.Offset(arrByteSize); } }
public void WriteArray(Vector3[] array) { WriteInt(array.Length); if (array.Length == 0) { return; } var arrByteSize = array.Length * 3 * sizeof(float); var handleVs = GCHandle.Alloc(array, GCHandleType.Pinned); var copyLenBytes = array.Length * sizeof(float) * 3; try { CopyMemory(BytesPtr, handleVs.AddrOfPinnedObject(), copyLenBytes); } finally { handleVs.Free(); BytesPtr = BytesPtr.Offset(arrByteSize); } }
public void WriteInt(int value) { Marshal.WriteInt32(BytesPtr, value); BytesPtr = BytesPtr.Offset(4); }