| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- #if DEBUG && !UNITY_WP_8_1 && !UNITY_WSA_8_1
- using System;
- using System.Text;
- namespace FlyingWormConsole3.LiteNetLib.Utils
- {
- public class NetDataReader
- {
- protected byte[] _data;
- protected int _position;
- protected int _dataSize;
- public byte[] Data
- {
- get { return _data; }
- }
- public int Position
- {
- get { return _position; }
- }
- public bool EndOfData
- {
- get { return _position == _dataSize; }
- }
- public int AvailableBytes
- {
- get { return _dataSize - _position; }
- }
- public void SetSource(NetDataWriter dataWriter)
- {
- _data = dataWriter.Data;
- _position = 0;
- _dataSize = dataWriter.Length;
- }
- public void SetSource(byte[] source)
- {
- _data = source;
- _position = 0;
- _dataSize = source.Length;
- }
- public void SetSource(byte[] source, int offset)
- {
- _data = source;
- _position = offset;
- _dataSize = source.Length;
- }
- public void SetSource(byte[] source, int offset, int dataSize)
- {
- _data = source;
- _position = offset;
- _dataSize = dataSize;
- }
- public NetDataReader()
- {
- }
- public NetDataReader(byte[] source)
- {
- SetSource(source);
- }
- public NetDataReader(byte[] source, int offset)
- {
- SetSource(source, offset);
- }
- public NetDataReader(byte[] source, int offset, int maxSize)
- {
- SetSource(source, offset, maxSize);
- }
- #region GetMethods
- public NetEndPoint GetNetEndPoint()
- {
- string host = GetString(1000);
- int port = GetInt();
- return new NetEndPoint(host, port);
- }
- public byte GetByte()
- {
- byte res = _data[_position];
- _position += 1;
- return res;
- }
- public sbyte GetSByte()
- {
- var b = (sbyte)_data[_position];
- _position++;
- return b;
- }
- public bool[] GetBoolArray()
- {
- ushort size = BitConverter.ToUInt16(_data, _position);
- _position += 2;
- var arr = new bool[size];
- for (int i = 0; i < size; i++)
- {
- arr[i] = GetBool();
- }
- return arr;
- }
- public ushort[] GetUShortArray()
- {
- ushort size = BitConverter.ToUInt16(_data, _position);
- _position += 2;
- var arr = new ushort[size];
- for (int i = 0; i < size; i++)
- {
- arr[i] = GetUShort();
- }
- return arr;
- }
- public short[] GetShortArray()
- {
- ushort size = BitConverter.ToUInt16(_data, _position);
- _position += 2;
- var arr = new short[size];
- for (int i = 0; i < size; i++)
- {
- arr[i] = GetShort();
- }
- return arr;
- }
- public long[] GetLongArray()
- {
- ushort size = BitConverter.ToUInt16(_data, _position);
- _position += 2;
- var arr = new long[size];
- for (int i = 0; i < size; i++)
- {
- arr[i] = GetLong();
- }
- return arr;
- }
- public ulong[] GetULongArray()
- {
- ushort size = BitConverter.ToUInt16(_data, _position);
- _position += 2;
- var arr = new ulong[size];
- for (int i = 0; i < size; i++)
- {
- arr[i] = GetULong();
- }
- return arr;
- }
- public int[] GetIntArray()
- {
- ushort size = BitConverter.ToUInt16(_data, _position);
- _position += 2;
- var arr = new int[size];
- for (int i = 0; i < size; i++)
- {
- arr[i] = GetInt();
- }
- return arr;
- }
- public uint[] GetUIntArray()
- {
- ushort size = BitConverter.ToUInt16(_data, _position);
- _position += 2;
- var arr = new uint[size];
- for (int i = 0; i < size; i++)
- {
- arr[i] = GetUInt();
- }
- return arr;
- }
- public float[] GetFloatArray()
- {
- ushort size = BitConverter.ToUInt16(_data, _position);
- _position += 2;
- var arr = new float[size];
- for (int i = 0; i < size; i++)
- {
- arr[i] = GetFloat();
- }
- return arr;
- }
- public double[] GetDoubleArray()
- {
- ushort size = BitConverter.ToUInt16(_data, _position);
- _position += 2;
- var arr = new double[size];
- for (int i = 0; i < size; i++)
- {
- arr[i] = GetDouble();
- }
- return arr;
- }
- public string[] GetStringArray(int maxLength)
- {
- ushort size = BitConverter.ToUInt16(_data, _position);
- _position += 2;
- var arr = new string[size];
- for (int i = 0; i < size; i++)
- {
- arr[i] = GetString(maxLength);
- }
- return arr;
- }
- public bool GetBool()
- {
- bool res = _data[_position] > 0;
- _position += 1;
- return res;
- }
- public ushort GetUShort()
- {
- ushort result = BitConverter.ToUInt16(_data, _position);
- _position += 2;
- return result;
- }
- public short GetShort()
- {
- short result = BitConverter.ToInt16(_data, _position);
- _position += 2;
- return result;
- }
- public long GetLong()
- {
- long result = BitConverter.ToInt64(_data, _position);
- _position += 8;
- return result;
- }
- public ulong GetULong()
- {
- ulong result = BitConverter.ToUInt64(_data, _position);
- _position += 8;
- return result;
- }
- public int GetInt()
- {
- int result = BitConverter.ToInt32(_data, _position);
- _position += 4;
- return result;
- }
- public uint GetUInt()
- {
- uint result = BitConverter.ToUInt32(_data, _position);
- _position += 4;
- return result;
- }
- public float GetFloat()
- {
- float result = BitConverter.ToSingle(_data, _position);
- _position += 4;
- return result;
- }
- public double GetDouble()
- {
- double result = BitConverter.ToDouble(_data, _position);
- _position += 8;
- return result;
- }
- public string GetString(int maxLength)
- {
- int bytesCount = GetInt();
- if (bytesCount <= 0 || bytesCount > maxLength*2)
- {
- return string.Empty;
- }
- int charCount = Encoding.UTF8.GetCharCount(_data, _position, bytesCount);
- if (charCount > maxLength)
- {
- return string.Empty;
- }
- string result = Encoding.UTF8.GetString(_data, _position, bytesCount);
- _position += bytesCount;
- return result;
- }
- public string GetString()
- {
- int bytesCount = GetInt();
- if (bytesCount <= 0)
- {
- return string.Empty;
- }
- string result = Encoding.UTF8.GetString(_data, _position, bytesCount);
- _position += bytesCount;
- return result;
- }
- public byte[] GetRemainingBytes()
- {
- byte[] outgoingData = new byte[AvailableBytes];
- Buffer.BlockCopy(_data, _position, outgoingData, 0, AvailableBytes);
- _position = _data.Length;
- return outgoingData;
- }
- public void GetRemainingBytes(byte[] destination)
- {
- Buffer.BlockCopy(_data, _position, destination, 0, AvailableBytes);
- _position = _data.Length;
- }
- public void GetBytes(byte[] destination, int lenght)
- {
- Buffer.BlockCopy(_data, _position, destination, 0, lenght);
- _position += lenght;
- }
- public byte[] GetBytesWithLength()
- {
- int length = GetInt();
- byte[] outgoingData = new byte[length];
- Buffer.BlockCopy(_data, _position, outgoingData, 0, length);
- _position += length;
- return outgoingData;
- }
- #endregion
- #region PeekMethods
- public byte PeekByte()
- {
- return _data[_position];
- }
- public sbyte PeekSByte()
- {
- return (sbyte)_data[_position];
- }
- public bool PeekBool()
- {
- return _data[_position] > 0;
- }
- public ushort PeekUShort()
- {
- return BitConverter.ToUInt16(_data, _position);
- }
- public short PeekShort()
- {
- return BitConverter.ToInt16(_data, _position);
- }
- public long PeekLong()
- {
- return BitConverter.ToInt64(_data, _position);
- }
- public ulong PeekULong()
- {
- return BitConverter.ToUInt64(_data, _position);
- }
- public int PeekInt()
- {
- return BitConverter.ToInt32(_data, _position);
- }
- public uint PeekUInt()
- {
- return BitConverter.ToUInt32(_data, _position);
- }
- public float PeekFloat()
- {
- return BitConverter.ToSingle(_data, _position);
- }
- public double PeekDouble()
- {
- return BitConverter.ToDouble(_data, _position);
- }
- public string PeekString(int maxLength)
- {
- int bytesCount = BitConverter.ToInt32(_data, _position);
- if (bytesCount <= 0 || bytesCount > maxLength * 2)
- {
- return string.Empty;
- }
- int charCount = Encoding.UTF8.GetCharCount(_data, _position + 4, bytesCount);
- if (charCount > maxLength)
- {
- return string.Empty;
- }
- string result = Encoding.UTF8.GetString(_data, _position + 4, bytesCount);
- return result;
- }
- public string PeekString()
- {
- int bytesCount = BitConverter.ToInt32(_data, _position);
- if (bytesCount <= 0)
- {
- return string.Empty;
- }
- string result = Encoding.UTF8.GetString(_data, _position + 4, bytesCount);
- return result;
- }
- #endregion
- public void Clear()
- {
- _position = 0;
- _dataSize = 0;
- _data = null;
- }
- }
- }
- #endif
|