RAGECOOP-V/RageCoop.Core/BitReader.cs

147 lines
3.6 KiB
C#
Raw Normal View History

using System;
using System.Text;
using System.Linq;
using GTA.Math;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Core
{
2022-07-01 14:39:43 +08:00
internal class BitReader
{
public int CurrentIndex { get; set; }
private byte[] ResultArray;
public BitReader(byte[] array)
{
CurrentIndex = 0;
ResultArray = array;
}
~BitReader()
{
ResultArray = null;
}
public bool CanRead(int bytes)
{
return ResultArray.Length >= CurrentIndex + bytes;
}
public bool ReadBool()
{
bool value = BitConverter.ToBoolean(ResultArray, CurrentIndex);
CurrentIndex += 1;
return value;
}
public float ReadFloat()
{
float value = BitConverter.ToSingle(ResultArray, CurrentIndex);
CurrentIndex += 4;
return value;
}
public byte ReadByte()
{
byte value = ResultArray[CurrentIndex];
CurrentIndex += 1;
return value;
}
public byte[] ReadByteArray(int length)
{
2022-07-09 14:04:39 +08:00
byte[] value = new byte[length];
Array.Copy(ResultArray, CurrentIndex,value,0,length);
CurrentIndex += length;
return value;
}
2022-06-24 10:33:36 +08:00
public byte[] ReadByteArray()
{
return ReadByteArray(ReadInt());
}
public short ReadShort()
{
short value = BitConverter.ToInt16(ResultArray, CurrentIndex);
CurrentIndex += 2;
return value;
}
public ushort ReadUShort()
{
ushort value = BitConverter.ToUInt16(ResultArray, CurrentIndex);
CurrentIndex += 2;
return value;
}
public int ReadInt()
{
int value = BitConverter.ToInt32(ResultArray, CurrentIndex);
CurrentIndex += 4;
return value;
}
public uint ReadUInt()
{
uint value = BitConverter.ToUInt32(ResultArray, CurrentIndex);
CurrentIndex += 4;
return value;
}
public long ReadLong()
{
long value = BitConverter.ToInt64(ResultArray, CurrentIndex);
CurrentIndex += 8;
return value;
}
public ulong ReadULong()
{
ulong value = BitConverter.ToUInt64(ResultArray, CurrentIndex);
CurrentIndex += 8;
return value;
}
public string ReadString(int index)
{
string value = Encoding.UTF8.GetString(ResultArray.Skip(CurrentIndex).Take(index).ToArray());
CurrentIndex += index;
return value;
}
2022-06-19 11:12:20 +08:00
public string ReadString()
{
var len = ReadInt();
string value = Encoding.UTF8.GetString(ResultArray.Skip(CurrentIndex).Take(len).ToArray());
CurrentIndex += len;
return value;
}
2022-05-22 15:55:26 +08:00
public Vector3 ReadVector3()
2022-05-22 15:55:26 +08:00
{
return new Vector3()
2022-05-22 15:55:26 +08:00
{
X = ReadFloat(),
Y = ReadFloat(),
Z = ReadFloat()
};
}
2022-07-03 15:28:28 +08:00
public Vector2 ReadVector2()
{
return new Vector2()
{
X = ReadFloat(),
Y = ReadFloat()
};
}
2022-06-03 14:40:41 +08:00
public Quaternion ReadQuaternion()
2022-05-22 15:55:26 +08:00
{
return new Quaternion()
2022-05-22 15:55:26 +08:00
{
X = ReadFloat(),
Y = ReadFloat(),
Z = ReadFloat(),
W = ReadFloat()
};
}
}
}