mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
source code checkin
This commit is contained in:
parent
5f8fb2c825
commit
7e57b72e35
675 changed files with 168433 additions and 0 deletions
98
src/QueryMaster/Parser.cs
Normal file
98
src/QueryMaster/Parser.cs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace QueryMaster
|
||||
{
|
||||
class Parser
|
||||
{
|
||||
private byte[] Data = null;
|
||||
private int CurrentPosition = -1;
|
||||
private int LastPosition;
|
||||
|
||||
internal bool HasUnParsedBytes
|
||||
{
|
||||
get { return CurrentPosition < LastPosition; }
|
||||
}
|
||||
|
||||
internal Parser(byte[] data)
|
||||
{
|
||||
Data = data;
|
||||
CurrentPosition = -1;
|
||||
LastPosition = Data.Length - 1;
|
||||
}
|
||||
|
||||
internal byte ReadByte()
|
||||
{
|
||||
CurrentPosition++;
|
||||
if (CurrentPosition > LastPosition)
|
||||
throw new ParseException("Index was outside the bounds of the byte array.");
|
||||
return Data[CurrentPosition];
|
||||
|
||||
}
|
||||
|
||||
internal short ReadShort()
|
||||
{
|
||||
CurrentPosition++;
|
||||
if (CurrentPosition + 3 > LastPosition)
|
||||
throw new ParseException("Unable to parse bytes to short.");
|
||||
short num;
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
Array.Reverse(Data, CurrentPosition, 2);
|
||||
num = BitConverter.ToInt16(Data, CurrentPosition);
|
||||
CurrentPosition++;
|
||||
return num;
|
||||
}
|
||||
|
||||
internal int ReadInt()
|
||||
{
|
||||
CurrentPosition++;
|
||||
if (CurrentPosition + 3 > LastPosition)
|
||||
throw new ParseException("Unable to parse bytes to int.");
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
Array.Reverse(Data, CurrentPosition, 4);
|
||||
int num = BitConverter.ToInt32(Data, CurrentPosition);
|
||||
CurrentPosition += 3;
|
||||
return num;
|
||||
}
|
||||
|
||||
internal float ReadFloat()
|
||||
{
|
||||
CurrentPosition++;
|
||||
if (CurrentPosition + 3 > LastPosition)
|
||||
throw new ParseException("Unable to parse bytes to float.");
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
Array.Reverse(Data, CurrentPosition, 4);
|
||||
float Num = BitConverter.ToSingle(Data, CurrentPosition);
|
||||
CurrentPosition += 3;
|
||||
return Num;
|
||||
|
||||
}
|
||||
|
||||
internal string ReadString()
|
||||
{
|
||||
CurrentPosition++;
|
||||
int temp = CurrentPosition;
|
||||
while (Data[CurrentPosition] != 0x00)
|
||||
{
|
||||
CurrentPosition++;
|
||||
if (CurrentPosition > LastPosition)
|
||||
throw new ParseException("Unable to parse bytes to string.");
|
||||
}
|
||||
return Encoding.UTF8.GetString(Data, temp, CurrentPosition - temp);
|
||||
}
|
||||
|
||||
internal void Skip(byte count)
|
||||
{
|
||||
CurrentPosition += count;
|
||||
if (CurrentPosition > LastPosition)
|
||||
throw new ParseException("skip count was outside the bounds of the byte array.");
|
||||
}
|
||||
|
||||
internal byte[] GetUnParsedData()
|
||||
{
|
||||
return Data.Skip(CurrentPosition + 1).ToArray();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue