DotRecastNetSim/src/DotRecast.Core/ByteBuffer.cs

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