TG9six 03a642d635 first push
first push
2025-09-06 17:17:39 +04:00

127 lines
2.5 KiB
C#

using UnityEngine;
using System.Collections;
using System;
public class Packet
{
//create a new packet
public Packet(byte[] data)
{
payload = data;
position = 0;
}
public Packet(int size)
{
position = 0;
payload = new byte[size];
}
//get the packet type
public int getType()
{
//position = 0;
return readByte();
}
public int size()
{
return payload.Length - 1;
}
public bool hasNext()
{
return position < size();
}
//read a byte from a packet buffer
public int readSignedByte()
{
//convert the byte to unsigned
return (payload[position++]);
}
//read a byte from a packet buffer
public int readByte()
{
//convert the byte to unsigned
position += 1;
return (0x00000FF & payload[position - 1]);
}
//read a short from a pakcet buffer
public int readShort()
{
int si0 = 0x00000FF & payload[position + 1];
int si1 = 0x000FF00 & (payload[position + 0] << 8);
position += 2;
return si0 + si1;
}
public int readInt()
{
int si0 = 0x00000FF & payload[position + 3];
int si1 = 0x000FF00 & (payload[position + 2] << 8);
int si2 = 0x0FF0000 & (payload[position + 1] << 16);
int si3 = (0x00000FF & payload[position]) << 24;
position += 4;
return si0 + si1 + si2 + si3;
}
public string readString()
{
int length = readByte();
string data = "";
for (int i = position; i < position + length; i++)
{
if (i < payload.Length)
{
data += (char)payload[i];
}
}
position += length;
return data;
}
public string readLongString()
{
int length = readInt();
string data = "";
for (int i = position; i < position + length; i++)
{
if (i < payload.Length)
{
data += (char)payload[i];
}
}
position += length;
return data;
}
public float readFloat()
{
int value = readInt();
return (float)value / 100;
}
public float readFloat2()
{
float value = BitConverter.ToSingle(payload, position);
position += 4;
return value;
}
public int position;
//the payload of the packet
protected byte[] payload;
}