示例#1
0
    public void WriteStruct <T>(T str)
    {
        var strSize = Marshal.SizeOf(typeof(T));

        Marshal.StructureToPtr(str, BytesPtr, true);
        BytesPtr = BytesPtr.Offset(strSize);
    }
示例#2
0
    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);
    }
示例#3
0
    public int ReadInt32()
    {
        var result = Marshal.ReadInt32(BytesPtr);

        BytesPtr = BytesPtr.Offset(4);
        return(result);
    }
示例#4
0
    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);
    }
示例#5
0
    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));
    }
示例#6
0
    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);
    }
示例#7
0
    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));
    }
示例#8
0
    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);
    }
示例#9
0
    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);
    }
示例#10
0
    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);
        }
    }
示例#11
0
    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);
        }
    }
示例#12
0
 public void WriteInt(int value)
 {
     Marshal.WriteInt32(BytesPtr, value);
     BytesPtr = BytesPtr.Offset(4);
 }