DotRecastNetSim/src/DotRecast.Core/ByteBuffer.cs

133 lines
2.9 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
using System;
using System.Buffers.Binary;
2023-03-16 19:09:10 +03:00
namespace DotRecast.Core
{
2023-03-16 19:48:49 +03:00
public class ByteBuffer
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
private ByteOrder _order;
private byte[] _bytes;
private int _position;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
public ByteBuffer(byte[] bytes)
{
_order = BitConverter.IsLittleEndian
? ByteOrder.LITTLE_ENDIAN
: ByteOrder.BIG_ENDIAN;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
_bytes = bytes;
_position = 0;
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
public ByteOrder order()
{
return _order;
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
public void order(ByteOrder order)
{
_order = order;
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
public int limit()
{
return _bytes.Length - _position;
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
public int remaining()
{
int rem = limit();
return rem > 0 ? rem : 0;
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
public void position(int pos)
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
_position = pos;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
public int position()
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
return _position;
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
public Span<byte> ReadBytes(int length)
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
var nextPos = _position + length;
(nextPos, _position) = (_position, nextPos);
return _bytes.AsSpan(nextPos, length);
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
public byte get()
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
var span = ReadBytes(1);
return span[0];
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
public short getShort()
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
var span = ReadBytes(2);
if (_order == ByteOrder.BIG_ENDIAN)
{
return BinaryPrimitives.ReadInt16BigEndian(span);
}
else
{
return BinaryPrimitives.ReadInt16LittleEndian(span);
}
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
public int getInt()
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
var span = ReadBytes(4);
if (_order == ByteOrder.BIG_ENDIAN)
{
return BinaryPrimitives.ReadInt32BigEndian(span);
}
else
{
return BinaryPrimitives.ReadInt32LittleEndian(span);
}
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:09:10 +03:00
2023-03-16 19:48:49 +03:00
public float getFloat()
{
var span = ReadBytes(4);
if (_order == ByteOrder.BIG_ENDIAN && BitConverter.IsLittleEndian)
{
span.Reverse();
}
else if (_order == ByteOrder.LITTLE_ENDIAN && !BitConverter.IsLittleEndian)
{
span.Reverse();
}
return BitConverter.ToSingle(span);
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
public long getLong()
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
var span = ReadBytes(8);
if (_order == ByteOrder.BIG_ENDIAN)
{
return BinaryPrimitives.ReadInt64BigEndian(span);
}
else
{
return BinaryPrimitives.ReadInt64LittleEndian(span);
}
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
public void putFloat(float v)
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
// ?
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
public void putInt(int v)
{
// ?
}
2023-03-14 08:02:43 +03:00
}
}