mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-05-18 11:21:18 +03:00
39 lines
877 B
C#
39 lines
877 B
C#
![]() |
using System.Buffers.Binary;
|
|||
|
using System.Text;
|
|||
|
|
|||
|
namespace MissionTmaLib.Parsing;
|
|||
|
|
|||
|
public static class Extensions
|
|||
|
{
|
|||
|
public static int ReadInt32LittleEndian(this FileStream fs)
|
|||
|
{
|
|||
|
Span<byte> buf = stackalloc byte[4];
|
|||
|
fs.ReadExactly(buf);
|
|||
|
|
|||
|
return BinaryPrimitives.ReadInt32LittleEndian(buf);
|
|||
|
}
|
|||
|
|
|||
|
public static float ReadFloatLittleEndian(this FileStream fs)
|
|||
|
{
|
|||
|
Span<byte> buf = stackalloc byte[4];
|
|||
|
fs.ReadExactly(buf);
|
|||
|
|
|||
|
return BinaryPrimitives.ReadSingleLittleEndian(buf);
|
|||
|
}
|
|||
|
|
|||
|
public static string ReadLengthPrefixedString(this FileStream fs)
|
|||
|
{
|
|||
|
var len = fs.ReadInt32LittleEndian();
|
|||
|
|
|||
|
if (len == 0)
|
|||
|
{
|
|||
|
return "";
|
|||
|
}
|
|||
|
|
|||
|
var buffer = new byte[len];
|
|||
|
|
|||
|
fs.ReadExactly(buffer, 0, len);
|
|||
|
|
|||
|
return Encoding.ASCII.GetString(buffer, 0, len);
|
|||
|
}
|
|||
|
}
|