mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/PCK-Studio.git
synced 2026-07-13 06:28:17 +00:00
Add StreamData{Reader/Writer}
This commit is contained in:
58
MinecraftUSkinEditor/Classes/Utils/StreamDataReader.cs
Normal file
58
MinecraftUSkinEditor/Classes/Utils/StreamDataReader.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PckStudio.Classes.Utils
|
||||
{
|
||||
public class StreamDataReader
|
||||
{
|
||||
private static bool isLittleEndian;
|
||||
protected StreamDataReader(bool useLittleEndian)
|
||||
{
|
||||
isLittleEndian = useLittleEndian;
|
||||
}
|
||||
|
||||
protected static string ReadString(Stream stream, int length, Encoding encoding)
|
||||
{
|
||||
byte[] buffer = ReadBytes(stream, length);
|
||||
return encoding.GetString(buffer);
|
||||
}
|
||||
|
||||
protected static byte[] ReadBytes(Stream stream, int count)
|
||||
{
|
||||
byte[] buffer = new byte[count];
|
||||
stream.Read(buffer, 0, count);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
protected static ushort ReadUShort(Stream stream) => (ushort)ReadShort(stream);
|
||||
protected static short ReadShort(Stream stream)
|
||||
{
|
||||
byte[] bytes = ReadBytes(stream, 2);
|
||||
if (BitConverter.IsLittleEndian && !isLittleEndian)
|
||||
Array.Reverse(bytes);
|
||||
return BitConverter.ToInt16(bytes, 0);
|
||||
}
|
||||
|
||||
protected static uint ReadUInt(Stream stream) => (uint)ReadInt(stream);
|
||||
protected static int ReadInt(Stream stream)
|
||||
{
|
||||
byte[] buffer = ReadBytes(stream, 4);
|
||||
if (BitConverter.IsLittleEndian && !isLittleEndian)
|
||||
Array.Reverse(buffer);
|
||||
return BitConverter.ToInt32(buffer, 0);
|
||||
}
|
||||
|
||||
protected static ulong ReadULong(Stream stream) => (ulong)ReadLong(stream);
|
||||
protected static long ReadLong(Stream stream)
|
||||
{
|
||||
byte[] buffer = ReadBytes(stream, 8);
|
||||
if (BitConverter.IsLittleEndian && !isLittleEndian)
|
||||
Array.Reverse(buffer);
|
||||
return BitConverter.ToInt64(buffer, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user