2023-11-12 01:59:17 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Buffers;
|
2023-11-14 23:16:59 +08:00
|
|
|
|
using System.Buffers.Binary;
|
2023-11-12 01:59:17 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics.Contracts;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace CodeWalker.Core.Utils
|
|
|
|
|
{
|
|
|
|
|
public static class StreamingExtensions
|
|
|
|
|
{
|
|
|
|
|
public static Task<int> ReadAsync(this BinaryReader br, byte[] buffer, int index, int count)
|
|
|
|
|
{
|
|
|
|
|
return br.BaseStream.ReadAsync(buffer, index, count);
|
|
|
|
|
}
|
2023-11-14 23:16:59 +08:00
|
|
|
|
}
|
2023-11-12 01:59:17 +08:00
|
|
|
|
|
2023-11-14 23:16:59 +08:00
|
|
|
|
public ref struct SpanStream
|
|
|
|
|
{
|
|
|
|
|
public Span<byte> Buffer { get; private set; }
|
|
|
|
|
private int _position;
|
2023-11-12 01:59:17 +08:00
|
|
|
|
|
2023-11-14 23:16:59 +08:00
|
|
|
|
public SpanStream(Span<byte> buffer)
|
2023-11-12 01:59:17 +08:00
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
Buffer = buffer;
|
|
|
|
|
_position = 0;
|
2023-11-12 01:59:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-14 23:16:59 +08:00
|
|
|
|
private ReadOnlySpan<byte> InternalRead(int count)
|
2023-11-12 01:59:17 +08:00
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
int origPos = _position;
|
|
|
|
|
int newPos = origPos + count;
|
2023-11-12 01:59:17 +08:00
|
|
|
|
|
2023-11-14 23:16:59 +08:00
|
|
|
|
if ((uint)newPos > (uint)Buffer.Length)
|
2023-11-12 01:59:17 +08:00
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
_position = Buffer.Length;
|
|
|
|
|
ThrowHelper.ThrowEndOfFileException();
|
2023-11-12 01:59:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-14 23:16:59 +08:00
|
|
|
|
var span = Buffer.Slice(origPos, count);
|
|
|
|
|
_position = newPos;
|
|
|
|
|
return span;
|
2023-11-12 01:59:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-14 23:16:59 +08:00
|
|
|
|
public short ReadInt16() => BinaryPrimitives.ReadInt16LittleEndian(InternalRead(sizeof(short)));
|
2023-11-12 01:59:17 +08:00
|
|
|
|
|
2023-11-14 23:16:59 +08:00
|
|
|
|
public ushort ReadUInt16() => BinaryPrimitives.ReadUInt16LittleEndian(InternalRead(sizeof(ushort)));
|
2023-11-12 01:59:17 +08:00
|
|
|
|
|
2023-11-14 23:16:59 +08:00
|
|
|
|
public int ReadInt32() => BinaryPrimitives.ReadInt32LittleEndian(InternalRead(sizeof(int)));
|
|
|
|
|
public uint ReadUInt32() => BinaryPrimitives.ReadUInt32LittleEndian(InternalRead(sizeof(uint)));
|
|
|
|
|
public long ReadInt64() => BinaryPrimitives.ReadInt64LittleEndian(InternalRead(sizeof(long)));
|
|
|
|
|
public ulong ReadUInt64() => BinaryPrimitives.ReadUInt64LittleEndian(InternalRead(sizeof(ulong)));
|
|
|
|
|
public unsafe Half ReadHalf() => BinaryPrimitives.ReadHalfLittleEndian(InternalRead(sizeof(Half)));
|
|
|
|
|
public unsafe float ReadSingle() => BinaryPrimitives.ReadSingleLittleEndian(InternalRead(sizeof(float)));
|
|
|
|
|
public unsafe double ReadDouble() => BinaryPrimitives.ReadDoubleLittleEndian(InternalRead(sizeof(double)));
|
2023-11-12 01:59:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|