DotRecastNetSim/src/DotRecast.Core/RcByteBuffer.cs

143 lines
3.2 KiB
C#
Raw Normal View History

2023-04-29 06:41:07 +03:00
using System;
2023-03-14 08:02:43 +03:00
using System.Buffers.Binary;
2023-03-16 19:09:10 +03:00
namespace DotRecast.Core
{
2023-05-10 16:44:51 +03:00
public class RcByteBuffer
2023-03-14 08:02:43 +03:00
{
2023-05-10 16:44:51 +03:00
private RcByteOrder _order;
2023-03-16 19:48:49 +03:00
private byte[] _bytes;
private int _position;
2023-03-14 08:02:43 +03:00
2023-05-10 16:44:51 +03:00
public RcByteBuffer(byte[] bytes)
2023-03-16 19:48:49 +03:00
{
_order = BitConverter.IsLittleEndian
2023-05-10 16:44:51 +03:00
? RcByteOrder.LITTLE_ENDIAN
: RcByteOrder.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-05-10 16:44:51 +03:00
public RcByteOrder Order()
2023-03-16 19:48:49 +03:00
{
return _order;
}
2023-03-14 08:02:43 +03:00
2023-05-10 16:44:51 +03:00
public void Order(RcByteOrder order)
2023-03-16 19:48:49 +03:00
{
_order = order;
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public int Limit()
2023-03-16 19:48:49 +03:00
{
return _bytes.Length - _position;
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +03:00
public int Remaining()
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
int rem = Limit();
2023-03-16 19:48:49 +03:00
return rem > 0 ? rem : 0;
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +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
2023-05-05 02:44:48 +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
2023-05-05 02:44:48 +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-05-05 02:44:48 +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);
2023-05-10 16:44:51 +03:00
if (_order == RcByteOrder.BIG_ENDIAN)
2023-03-16 19:48:49 +03:00
{
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
2023-05-05 02:44:48 +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);
2023-05-10 16:44:51 +03:00
if (_order == RcByteOrder.BIG_ENDIAN)
2023-03-16 19:48:49 +03:00
{
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-05-05 02:44:48 +03:00
public float GetFloat()
2023-03-16 19:48:49 +03:00
{
var span = ReadBytes(4);
2023-05-10 16:44:51 +03:00
if (_order == RcByteOrder.BIG_ENDIAN && BitConverter.IsLittleEndian)
2023-03-16 19:48:49 +03:00
{
span.Reverse();
}
2023-05-10 16:44:51 +03:00
else if (_order == RcByteOrder.LITTLE_ENDIAN && !BitConverter.IsLittleEndian)
2023-03-16 19:48:49 +03:00
{
span.Reverse();
}
return BitConverter.ToSingle(span);
}
2023-03-14 08:02:43 +03:00
2023-05-05 02:44:48 +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);
2023-05-10 16:44:51 +03:00
if (_order == RcByteOrder.BIG_ENDIAN)
2023-03-16 19:48:49 +03:00
{
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
2023-05-05 02:44:48 +03:00
public void PutFloat(float v)
2023-03-14 08:02:43 +03:00
{
2023-03-18 18:09:36 +03:00
// if (_order == ByteOrder.BIG_ENDIAN)
// {
// BinaryPrimitives.WriteInt32BigEndian(_bytes[_position]);
// }
// else
// {
// BinaryPrimitives.ReadInt64LittleEndian(span);
// }
2023-03-16 19:48:49 +03:00
// ?
2023-03-14 08:02:43 +03:00
}
2023-05-05 02:44:48 +03:00
public void PutInt(int v)
2023-03-16 19:48:49 +03:00
{
// ?
}
2023-03-14 08:02:43 +03:00
}
2023-04-29 06:41:07 +03:00
}