Initial Commit (broken due to version update)
This commit is contained in:
parent
74003df84b
commit
3f7b3b7440
3132 changed files with 1103305 additions and 0 deletions
118
Assets/Scripts/LiteNetLib/Utils/FastBitConverter.cs
Normal file
118
Assets/Scripts/LiteNetLib/Utils/FastBitConverter.cs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace LiteNetLib.Utils
|
||||
{
|
||||
public static class FastBitConverter
|
||||
{
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
private struct ConverterHelperDouble
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public ulong Along;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public double Adouble;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
private struct ConverterHelperFloat
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public int Aint;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public float Afloat;
|
||||
}
|
||||
|
||||
private static void WriteLittleEndian(byte[] buffer, int offset, ulong data)
|
||||
{
|
||||
#if BIGENDIAN
|
||||
buffer[offset + 7] = (byte)(data);
|
||||
buffer[offset + 6] = (byte)(data >> 8);
|
||||
buffer[offset + 5] = (byte)(data >> 16);
|
||||
buffer[offset + 4] = (byte)(data >> 24);
|
||||
buffer[offset + 3] = (byte)(data >> 32);
|
||||
buffer[offset + 2] = (byte)(data >> 40);
|
||||
buffer[offset + 1] = (byte)(data >> 48);
|
||||
buffer[offset ] = (byte)(data >> 56);
|
||||
#else
|
||||
buffer[offset] = (byte)(data);
|
||||
buffer[offset + 1] = (byte)(data >> 8);
|
||||
buffer[offset + 2] = (byte)(data >> 16);
|
||||
buffer[offset + 3] = (byte)(data >> 24);
|
||||
buffer[offset + 4] = (byte)(data >> 32);
|
||||
buffer[offset + 5] = (byte)(data >> 40);
|
||||
buffer[offset + 6] = (byte)(data >> 48);
|
||||
buffer[offset + 7] = (byte)(data >> 56);
|
||||
#endif
|
||||
}
|
||||
|
||||
private static void WriteLittleEndian(byte[] buffer, int offset, int data)
|
||||
{
|
||||
#if BIGENDIAN
|
||||
buffer[offset + 3] = (byte)(data);
|
||||
buffer[offset + 2] = (byte)(data >> 8);
|
||||
buffer[offset + 1] = (byte)(data >> 16);
|
||||
buffer[offset ] = (byte)(data >> 24);
|
||||
#else
|
||||
buffer[offset] = (byte)(data);
|
||||
buffer[offset + 1] = (byte)(data >> 8);
|
||||
buffer[offset + 2] = (byte)(data >> 16);
|
||||
buffer[offset + 3] = (byte)(data >> 24);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void WriteLittleEndian(byte[] buffer, int offset, short data)
|
||||
{
|
||||
#if BIGENDIAN
|
||||
buffer[offset + 1] = (byte)(data);
|
||||
buffer[offset ] = (byte)(data >> 8);
|
||||
#else
|
||||
buffer[offset] = (byte)(data);
|
||||
buffer[offset + 1] = (byte)(data >> 8);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void GetBytes(byte[] bytes, int startIndex, double value)
|
||||
{
|
||||
ConverterHelperDouble ch = new ConverterHelperDouble { Adouble = value };
|
||||
WriteLittleEndian(bytes, startIndex, ch.Along);
|
||||
}
|
||||
|
||||
public static void GetBytes(byte[] bytes, int startIndex, float value)
|
||||
{
|
||||
ConverterHelperFloat ch = new ConverterHelperFloat { Afloat = value };
|
||||
WriteLittleEndian(bytes, startIndex, ch.Aint);
|
||||
}
|
||||
|
||||
public static void GetBytes(byte[] bytes, int startIndex, short value)
|
||||
{
|
||||
WriteLittleEndian(bytes, startIndex, value);
|
||||
}
|
||||
|
||||
public static void GetBytes(byte[] bytes, int startIndex, ushort value)
|
||||
{
|
||||
WriteLittleEndian(bytes, startIndex, (short)value);
|
||||
}
|
||||
|
||||
public static void GetBytes(byte[] bytes, int startIndex, int value)
|
||||
{
|
||||
WriteLittleEndian(bytes, startIndex, value);
|
||||
}
|
||||
|
||||
public static void GetBytes(byte[] bytes, int startIndex, uint value)
|
||||
{
|
||||
WriteLittleEndian(bytes, startIndex, (int)value);
|
||||
}
|
||||
|
||||
public static void GetBytes(byte[] bytes, int startIndex, long value)
|
||||
{
|
||||
WriteLittleEndian(bytes, startIndex, (ulong)value);
|
||||
}
|
||||
|
||||
public static void GetBytes(byte[] bytes, int startIndex, ulong value)
|
||||
{
|
||||
WriteLittleEndian(bytes, startIndex, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/LiteNetLib/Utils/FastBitConverter.cs.meta
Normal file
11
Assets/Scripts/LiteNetLib/Utils/FastBitConverter.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a15ef88c10c7a2d489ee33c39672425d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
464
Assets/Scripts/LiteNetLib/Utils/NetDataReader.cs
Normal file
464
Assets/Scripts/LiteNetLib/Utils/NetDataReader.cs
Normal file
|
|
@ -0,0 +1,464 @@
|
|||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clone NetDataReader without data copy (usable for OnReceive)
|
||||
/// </summary>
|
||||
/// <returns>new NetDataReader instance</returns>
|
||||
public NetDataReader Clone()
|
||||
{
|
||||
return new NetDataReader(_data, _position, _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 char GetChar()
|
||||
{
|
||||
char result = BitConverter.ToChar(_data, _position);
|
||||
_position += 2;
|
||||
return result;
|
||||
}
|
||||
|
||||
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 char PeekChar()
|
||||
{
|
||||
return BitConverter.ToChar(_data, _position);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
Assets/Scripts/LiteNetLib/Utils/NetDataReader.cs.meta
Normal file
11
Assets/Scripts/LiteNetLib/Utils/NetDataReader.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ac504f3a3ec9ad24298d741165b4610a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
381
Assets/Scripts/LiteNetLib/Utils/NetDataWriter.cs
Normal file
381
Assets/Scripts/LiteNetLib/Utils/NetDataWriter.cs
Normal file
|
|
@ -0,0 +1,381 @@
|
|||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace LiteNetLib.Utils
|
||||
{
|
||||
public class NetDataWriter
|
||||
{
|
||||
protected byte[] _data;
|
||||
protected int _position;
|
||||
|
||||
private int _maxLength;
|
||||
private readonly bool _autoResize;
|
||||
|
||||
public NetDataWriter()
|
||||
{
|
||||
_maxLength = 64;
|
||||
_data = new byte[_maxLength];
|
||||
_autoResize = true;
|
||||
}
|
||||
|
||||
public NetDataWriter(bool autoResize)
|
||||
{
|
||||
_maxLength = 64;
|
||||
_data = new byte[_maxLength];
|
||||
_autoResize = autoResize;
|
||||
}
|
||||
|
||||
public NetDataWriter(bool autoResize, int initialSize)
|
||||
{
|
||||
_maxLength = initialSize;
|
||||
_data = new byte[_maxLength];
|
||||
_autoResize = autoResize;
|
||||
}
|
||||
|
||||
public void ResizeIfNeed(int newSize)
|
||||
{
|
||||
if (_maxLength < newSize)
|
||||
{
|
||||
while (_maxLength < newSize)
|
||||
{
|
||||
_maxLength *= 2;
|
||||
}
|
||||
Array.Resize(ref _data, _maxLength);
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset(int size)
|
||||
{
|
||||
ResizeIfNeed(size);
|
||||
_position = 0;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_position = 0;
|
||||
}
|
||||
|
||||
public byte[] CopyData()
|
||||
{
|
||||
byte[] resultData = new byte[_position];
|
||||
Buffer.BlockCopy(_data, 0, resultData, 0, _position);
|
||||
return resultData;
|
||||
}
|
||||
|
||||
public byte[] Data
|
||||
{
|
||||
get { return _data; }
|
||||
}
|
||||
|
||||
public int Length
|
||||
{
|
||||
get { return _position; }
|
||||
}
|
||||
|
||||
public void Put(float value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 4);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 4;
|
||||
}
|
||||
|
||||
public void Put(double value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 8);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 8;
|
||||
}
|
||||
|
||||
public void Put(long value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 8);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 8;
|
||||
}
|
||||
|
||||
public void Put(ulong value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 8);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 8;
|
||||
}
|
||||
|
||||
public void Put(int value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 4);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 4;
|
||||
}
|
||||
|
||||
public void Put(uint value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 4);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 4;
|
||||
}
|
||||
|
||||
public void Put(char value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 2);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 2;
|
||||
}
|
||||
|
||||
public void Put(ushort value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 2);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 2;
|
||||
}
|
||||
|
||||
public void Put(short value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 2);
|
||||
FastBitConverter.GetBytes(_data, _position, value);
|
||||
_position += 2;
|
||||
}
|
||||
|
||||
public void Put(sbyte value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 1);
|
||||
_data[_position] = (byte)value;
|
||||
_position++;
|
||||
}
|
||||
|
||||
public void Put(byte value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 1);
|
||||
_data[_position] = value;
|
||||
_position++;
|
||||
}
|
||||
|
||||
public void Put(byte[] data, int offset, int length)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + length);
|
||||
Buffer.BlockCopy(data, offset, _data, _position, length);
|
||||
_position += length;
|
||||
}
|
||||
|
||||
public void Put(byte[] data)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + data.Length);
|
||||
Buffer.BlockCopy(data, 0, _data, _position, data.Length);
|
||||
_position += data.Length;
|
||||
}
|
||||
|
||||
public void PutBytesWithLength(byte[] data, int offset, int length)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + length + 4);
|
||||
FastBitConverter.GetBytes(_data, _position, length);
|
||||
Buffer.BlockCopy(data, offset, _data, _position + 4, length);
|
||||
_position += length + 4;
|
||||
}
|
||||
|
||||
public void PutBytesWithLength(byte[] data)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + data.Length + 4);
|
||||
FastBitConverter.GetBytes(_data, _position, data.Length);
|
||||
Buffer.BlockCopy(data, 0, _data, _position + 4, data.Length);
|
||||
_position += data.Length + 4;
|
||||
}
|
||||
|
||||
public void Put(bool value)
|
||||
{
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + 1);
|
||||
_data[_position] = (byte)(value ? 1 : 0);
|
||||
_position++;
|
||||
}
|
||||
|
||||
public void PutArray(float[] value)
|
||||
{
|
||||
ushort len = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + len * 4 + 2);
|
||||
Put(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Put(value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void PutArray(double[] value)
|
||||
{
|
||||
ushort len = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + len * 8 + 2);
|
||||
Put(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Put(value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void PutArray(long[] value)
|
||||
{
|
||||
ushort len = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + len * 8 + 2);
|
||||
Put(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Put(value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void PutArray(ulong[] value)
|
||||
{
|
||||
ushort len = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + len * 8 + 2);
|
||||
Put(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Put(value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void PutArray(int[] value)
|
||||
{
|
||||
ushort len = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + len * 4 + 2);
|
||||
Put(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Put(value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void PutArray(uint[] value)
|
||||
{
|
||||
ushort len = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + len * 4 + 2);
|
||||
Put(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Put(value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void PutArray(ushort[] value)
|
||||
{
|
||||
ushort len = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + len * 2 + 2);
|
||||
Put(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Put(value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void PutArray(short[] value)
|
||||
{
|
||||
ushort len = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + len * 2 + 2);
|
||||
Put(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Put(value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void PutArray(bool[] value)
|
||||
{
|
||||
ushort len = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + len + 2);
|
||||
Put(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Put(value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void PutArray(string[] value)
|
||||
{
|
||||
ushort len = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
Put(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Put(value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void PutArray(string[] value, int maxLength)
|
||||
{
|
||||
ushort len = value == null ? (ushort)0 : (ushort)value.Length;
|
||||
Put(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Put(value[i], maxLength);
|
||||
}
|
||||
}
|
||||
|
||||
public void Put(NetEndPoint endPoint)
|
||||
{
|
||||
Put(endPoint.Host);
|
||||
Put(endPoint.Port);
|
||||
}
|
||||
|
||||
public void Put(string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
Put(0);
|
||||
return;
|
||||
}
|
||||
|
||||
//put bytes count
|
||||
int bytesCount = Encoding.UTF8.GetByteCount(value);
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + bytesCount + 4);
|
||||
Put(bytesCount);
|
||||
|
||||
//put string
|
||||
Encoding.UTF8.GetBytes(value, 0, value.Length, _data, _position);
|
||||
_position += bytesCount;
|
||||
}
|
||||
|
||||
public void Put(string value, int maxLength)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
Put(0);
|
||||
return;
|
||||
}
|
||||
|
||||
int length = value.Length > maxLength ? maxLength : value.Length;
|
||||
//calculate max count
|
||||
int bytesCount = Encoding.UTF8.GetByteCount(value);
|
||||
if (_autoResize)
|
||||
ResizeIfNeed(_position + bytesCount + 4);
|
||||
|
||||
//put bytes count
|
||||
Put(bytesCount);
|
||||
|
||||
//put string
|
||||
Encoding.UTF8.GetBytes(value, 0, length, _data, _position);
|
||||
|
||||
_position += bytesCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/LiteNetLib/Utils/NetDataWriter.cs.meta
Normal file
11
Assets/Scripts/LiteNetLib/Utils/NetDataWriter.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6a8c25cfd47d69d43b138800c50e6348
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
725
Assets/Scripts/LiteNetLib/Utils/NetSerializer.cs
Normal file
725
Assets/Scripts/LiteNetLib/Utils/NetSerializer.cs
Normal file
|
|
@ -0,0 +1,725 @@
|
|||
#if (WINRT && !UNITY_EDITOR) || NETCORE
|
||||
#define USE_WINRT
|
||||
#endif
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
#if USE_WINRT
|
||||
using System.Linq;
|
||||
#endif
|
||||
|
||||
namespace LiteNetLib.Utils
|
||||
{
|
||||
public interface INetSerializable
|
||||
{
|
||||
void Serialize(NetDataWriter writer);
|
||||
void Deserialize(NetDataReader reader);
|
||||
}
|
||||
|
||||
public abstract class NetSerializerHasher
|
||||
{
|
||||
public abstract ulong GetHash(string type);
|
||||
public abstract void WriteHash(ulong hash, NetDataWriter writer);
|
||||
public abstract ulong ReadHash(NetDataReader reader);
|
||||
}
|
||||
|
||||
public sealed class FNVHasher : NetSerializerHasher
|
||||
{
|
||||
private readonly Dictionary<string, ulong> _hashCache = new Dictionary<string, ulong>();
|
||||
private readonly char[] _hashBuffer = new char[1024];
|
||||
|
||||
public override ulong GetHash(string type)
|
||||
{
|
||||
ulong hash;
|
||||
if (_hashCache.TryGetValue(type, out hash))
|
||||
{
|
||||
return hash;
|
||||
}
|
||||
hash = 14695981039346656037UL; //offset
|
||||
int len = type.Length;
|
||||
type.CopyTo(0, _hashBuffer, 0, len);
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
hash = hash ^ _hashBuffer[i];
|
||||
hash *= 1099511628211UL; //prime
|
||||
}
|
||||
_hashCache.Add(type, hash);
|
||||
return hash;
|
||||
}
|
||||
|
||||
public override ulong ReadHash(NetDataReader reader)
|
||||
{
|
||||
return reader.GetULong();
|
||||
}
|
||||
|
||||
public override void WriteHash(ulong hash, NetDataWriter writer)
|
||||
{
|
||||
writer.Put(hash);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NetSerializer
|
||||
{
|
||||
private sealed class CustomType
|
||||
{
|
||||
public readonly CustomTypeWrite WriteDelegate;
|
||||
public readonly CustomTypeRead ReadDelegate;
|
||||
|
||||
public CustomType(CustomTypeWrite writeDelegate, CustomTypeRead readDelegate)
|
||||
{
|
||||
WriteDelegate = writeDelegate;
|
||||
ReadDelegate = readDelegate;
|
||||
}
|
||||
}
|
||||
|
||||
private delegate void CustomTypeWrite(NetDataWriter writer, object customObj);
|
||||
private delegate object CustomTypeRead(NetDataReader reader);
|
||||
|
||||
private sealed class StructInfo
|
||||
{
|
||||
public readonly Action<NetDataWriter>[] WriteDelegate;
|
||||
public readonly Action<NetDataReader>[] ReadDelegate;
|
||||
public readonly Type[] FieldTypes;
|
||||
public object Reference;
|
||||
public Func<object> CreatorFunc;
|
||||
public Action<object, object> OnReceive;
|
||||
public readonly ulong Hash;
|
||||
public readonly int MembersCount;
|
||||
|
||||
public StructInfo(ulong hash, int membersCount)
|
||||
{
|
||||
Hash = hash;
|
||||
MembersCount = membersCount;
|
||||
WriteDelegate = new Action<NetDataWriter>[membersCount];
|
||||
ReadDelegate = new Action<NetDataReader>[membersCount];
|
||||
FieldTypes = new Type[membersCount];
|
||||
}
|
||||
|
||||
public void Write(NetDataWriter writer, object obj)
|
||||
{
|
||||
Reference = obj;
|
||||
for (int i = 0; i < MembersCount; i++)
|
||||
{
|
||||
WriteDelegate[i](writer);
|
||||
}
|
||||
}
|
||||
|
||||
public void Read(NetDataReader reader)
|
||||
{
|
||||
for (int i = 0; i < MembersCount; i++)
|
||||
{
|
||||
ReadDelegate[i](reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private readonly Dictionary<ulong, StructInfo> _cache;
|
||||
private readonly Dictionary<Type, CustomType> _registeredCustomTypes;
|
||||
|
||||
private static readonly HashSet<Type> BasicTypes = new HashSet<Type>
|
||||
{
|
||||
typeof(int),
|
||||
typeof(uint),
|
||||
typeof(byte),
|
||||
typeof(sbyte),
|
||||
typeof(short),
|
||||
typeof(char),
|
||||
typeof(ushort),
|
||||
typeof(long),
|
||||
typeof(ulong),
|
||||
typeof(string),
|
||||
typeof(float),
|
||||
typeof(double),
|
||||
typeof(bool)
|
||||
};
|
||||
|
||||
private readonly NetDataWriter _writer;
|
||||
private readonly NetSerializerHasher _hasher;
|
||||
private const int MaxStringLenght = 1024;
|
||||
|
||||
public NetSerializer() : this(new FNVHasher())
|
||||
{
|
||||
}
|
||||
|
||||
public NetSerializer(NetSerializerHasher hasher)
|
||||
{
|
||||
_hasher = hasher;
|
||||
_cache = new Dictionary<ulong, StructInfo>();
|
||||
_registeredCustomTypes = new Dictionary<Type, CustomType>();
|
||||
_writer = new NetDataWriter();
|
||||
}
|
||||
|
||||
private bool RegisterCustomTypeInternal<T>(Func<T> constructor) where T : INetSerializable
|
||||
{
|
||||
var t = typeof(T);
|
||||
if (_registeredCustomTypes.ContainsKey(t))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var rwDelegates = new CustomType(
|
||||
(writer, obj) =>
|
||||
{
|
||||
((T)obj).Serialize(writer);
|
||||
},
|
||||
reader =>
|
||||
{
|
||||
var instance = constructor();
|
||||
instance.Deserialize(reader);
|
||||
return instance;
|
||||
});
|
||||
_registeredCustomTypes.Add(t, rwDelegates);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register custom property type
|
||||
/// </summary>
|
||||
/// <typeparam name="T">INetSerializable structure</typeparam>
|
||||
/// <returns>True - if register successful, false - if type already registered</returns>
|
||||
public bool RegisterCustomType<T>() where T : struct, INetSerializable
|
||||
{
|
||||
return RegisterCustomTypeInternal(() => new T());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register custom property type
|
||||
/// </summary>
|
||||
/// <typeparam name="T">INetSerializable class</typeparam>
|
||||
/// <returns>True - if register successful, false - if type already registered</returns>
|
||||
public bool RegisterCustomType<T>(Func<T> constructor) where T : class, INetSerializable
|
||||
{
|
||||
return RegisterCustomTypeInternal(constructor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register custom property type
|
||||
/// </summary>
|
||||
/// <param name="writeDelegate"></param>
|
||||
/// <param name="readDelegate"></param>
|
||||
/// <returns>True - if register successful, false - if type already registered</returns>
|
||||
public bool RegisterCustomType<T>(Action<NetDataWriter, T> writeDelegate, Func<NetDataReader, T> readDelegate)
|
||||
{
|
||||
var t = typeof(T);
|
||||
if (BasicTypes.Contains(t) || _registeredCustomTypes.ContainsKey(t))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var rwDelegates = new CustomType(
|
||||
(writer, obj) => writeDelegate(writer, (T)obj),
|
||||
reader => readDelegate(reader));
|
||||
|
||||
_registeredCustomTypes.Add(t, rwDelegates);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Delegate CreateDelegate(Type type, MethodInfo info)
|
||||
{
|
||||
#if USE_WINRT
|
||||
return info.CreateDelegate(type);
|
||||
#else
|
||||
return Delegate.CreateDelegate(type, info);
|
||||
#endif
|
||||
}
|
||||
|
||||
private static Func<TClass, TProperty> ExtractGetDelegate<TClass, TProperty>(MethodInfo info)
|
||||
{
|
||||
return (Func<TClass, TProperty>)CreateDelegate(typeof(Func<TClass, TProperty>), info);
|
||||
}
|
||||
|
||||
private static Action<TClass, TProperty> ExtractSetDelegate<TClass, TProperty>(MethodInfo info)
|
||||
{
|
||||
return (Action<TClass, TProperty>)CreateDelegate(typeof(Action<TClass, TProperty>), info);
|
||||
}
|
||||
|
||||
private StructInfo RegisterInternal<T>() where T : class
|
||||
{
|
||||
Type t = typeof(T);
|
||||
ulong nameHash = _hasher.GetHash(t.Name);
|
||||
|
||||
StructInfo info;
|
||||
if (_cache.TryGetValue(nameHash, out info))
|
||||
{
|
||||
return info;
|
||||
}
|
||||
|
||||
#if USE_WINRT
|
||||
var props = t.GetRuntimeProperties().ToArray();
|
||||
int propsCount = props.Count();
|
||||
#else
|
||||
var props = t.GetProperties(
|
||||
BindingFlags.Instance |
|
||||
BindingFlags.Public |
|
||||
BindingFlags.GetProperty |
|
||||
BindingFlags.SetProperty);
|
||||
int propsCount = props.Length;
|
||||
#endif
|
||||
if (props == null || propsCount < 0)
|
||||
{
|
||||
throw new ArgumentException("Type does not contain acceptable fields");
|
||||
}
|
||||
|
||||
info = new StructInfo(nameHash, propsCount);
|
||||
for (int i = 0; i < props.Length; i++)
|
||||
{
|
||||
var property = props[i];
|
||||
var propertyType = property.PropertyType;
|
||||
|
||||
//Set field type
|
||||
info.FieldTypes[i] = propertyType.IsArray ? propertyType.GetElementType() : propertyType;
|
||||
#if USE_WINRT
|
||||
bool isEnum = propertyType.GetTypeInfo().IsEnum;
|
||||
var getMethod = property.GetMethod;
|
||||
var setMethod = property.SetMethod;
|
||||
#else
|
||||
bool isEnum = propertyType.IsEnum;
|
||||
var getMethod = property.GetGetMethod();
|
||||
var setMethod = property.GetSetMethod();
|
||||
#endif
|
||||
if (isEnum)
|
||||
{
|
||||
var underlyingType = Enum.GetUnderlyingType(propertyType);
|
||||
if (underlyingType == typeof(byte))
|
||||
{
|
||||
info.ReadDelegate[i] = reader =>
|
||||
{
|
||||
property.SetValue(info.Reference, Enum.ToObject(propertyType, reader.GetByte()), null);
|
||||
};
|
||||
info.WriteDelegate[i] = writer =>
|
||||
{
|
||||
writer.Put((byte)property.GetValue(info.Reference, null));
|
||||
};
|
||||
}
|
||||
else if (underlyingType == typeof(int))
|
||||
{
|
||||
info.ReadDelegate[i] = reader =>
|
||||
{
|
||||
property.SetValue(info.Reference, Enum.ToObject(propertyType, reader.GetInt()), null);
|
||||
};
|
||||
info.WriteDelegate[i] = writer =>
|
||||
{
|
||||
writer.Put((int)property.GetValue(info.Reference, null));
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Not supported enum underlying type: " + underlyingType.Name);
|
||||
}
|
||||
}
|
||||
else if (propertyType == typeof(string))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, string>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, string>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetString(MaxStringLenght));
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference), MaxStringLenght);
|
||||
}
|
||||
else if (propertyType == typeof(bool))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, bool>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, bool>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetBool());
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(byte))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, byte>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, byte>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetByte());
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(sbyte))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, sbyte>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, sbyte>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetSByte());
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(short))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, short>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, short>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetShort());
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(char))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, char>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, char>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetChar());
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(ushort))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, ushort>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, ushort>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetUShort());
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(int))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, int>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, int>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetInt());
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(uint))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, uint>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, uint>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetUInt());
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(long))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, long>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, long>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetLong());
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(ulong))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, ulong>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, ulong>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetULong());
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(float))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, float>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, float>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetFloat());
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(double))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, double>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, double>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetDouble());
|
||||
info.WriteDelegate[i] = writer => writer.Put(getDelegate((T)info.Reference));
|
||||
}
|
||||
// Array types
|
||||
else if (propertyType == typeof(string[]))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, string[]>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, string[]>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetStringArray(MaxStringLenght));
|
||||
info.WriteDelegate[i] = writer => writer.PutArray(getDelegate((T)info.Reference), MaxStringLenght);
|
||||
}
|
||||
else if (propertyType == typeof(bool[]))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, bool[]>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, bool[]>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetBoolArray());
|
||||
info.WriteDelegate[i] = writer => writer.PutArray(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(byte[]))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, byte[]>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, byte[]>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetBytesWithLength());
|
||||
info.WriteDelegate[i] = writer => writer.PutBytesWithLength(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(short[]))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, short[]>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, short[]>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetShortArray());
|
||||
info.WriteDelegate[i] = writer => writer.PutArray(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(ushort[]))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, ushort[]>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, ushort[]>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetUShortArray());
|
||||
info.WriteDelegate[i] = writer => writer.PutArray(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(int[]))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, int[]>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, int[]>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetIntArray());
|
||||
info.WriteDelegate[i] = writer => writer.PutArray(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(uint[]))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, uint[]>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, uint[]>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetUIntArray());
|
||||
info.WriteDelegate[i] = writer => writer.PutArray(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(long[]))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, long[]>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, long[]>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetLongArray());
|
||||
info.WriteDelegate[i] = writer => writer.PutArray(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(ulong[]))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, ulong[]>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, ulong[]>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetULongArray());
|
||||
info.WriteDelegate[i] = writer => writer.PutArray(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(float[]))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, float[]>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, float[]>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetFloatArray());
|
||||
info.WriteDelegate[i] = writer => writer.PutArray(getDelegate((T)info.Reference));
|
||||
}
|
||||
else if (propertyType == typeof(double[]))
|
||||
{
|
||||
var setDelegate = ExtractSetDelegate<T, double[]>(setMethod);
|
||||
var getDelegate = ExtractGetDelegate<T, double[]>(getMethod);
|
||||
info.ReadDelegate[i] = reader => setDelegate((T)info.Reference, reader.GetDoubleArray());
|
||||
info.WriteDelegate[i] = writer => writer.PutArray(getDelegate((T)info.Reference));
|
||||
}
|
||||
else
|
||||
{
|
||||
CustomType registeredCustomType;
|
||||
bool array = false;
|
||||
|
||||
if (propertyType.IsArray)
|
||||
{
|
||||
array = true;
|
||||
propertyType = propertyType.GetElementType();
|
||||
}
|
||||
|
||||
if (_registeredCustomTypes.TryGetValue(propertyType, out registeredCustomType))
|
||||
{
|
||||
if (array) //Array type serialize/deserialize
|
||||
{
|
||||
info.ReadDelegate[i] = reader =>
|
||||
{
|
||||
ushort arrLength = reader.GetUShort();
|
||||
Array arr = Array.CreateInstance(propertyType, arrLength);
|
||||
for (int k = 0; k < arrLength; k++)
|
||||
{
|
||||
arr.SetValue(registeredCustomType.ReadDelegate(reader), k);
|
||||
}
|
||||
|
||||
property.SetValue(info.Reference, arr, null);
|
||||
};
|
||||
|
||||
info.WriteDelegate[i] = writer =>
|
||||
{
|
||||
Array arr = (Array)property.GetValue(info.Reference, null);
|
||||
writer.Put((ushort)arr.Length);
|
||||
for (int k = 0; k < arr.Length; k++)
|
||||
{
|
||||
registeredCustomType.WriteDelegate(writer, arr.GetValue(k));
|
||||
}
|
||||
};
|
||||
}
|
||||
else //Simple
|
||||
{
|
||||
info.ReadDelegate[i] = reader =>
|
||||
{
|
||||
property.SetValue(info.Reference, registeredCustomType.ReadDelegate(reader), null);
|
||||
};
|
||||
|
||||
info.WriteDelegate[i] = writer =>
|
||||
{
|
||||
registeredCustomType.WriteDelegate(writer, property.GetValue(info.Reference, null));
|
||||
};
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Unknown property type: " + propertyType.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
_cache.Add(nameHash, info);
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads all available data from NetDataReader and calls OnReceive delegates
|
||||
/// </summary>
|
||||
/// <param name="reader">NetDataReader with packets data</param>
|
||||
public void ReadAllPackets(NetDataReader reader)
|
||||
{
|
||||
while (reader.AvailableBytes > 0)
|
||||
{
|
||||
ReadPacket(reader);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads all available data from NetDataReader and calls OnReceive delegates
|
||||
/// </summary>
|
||||
/// <param name="reader">NetDataReader with packets data</param>
|
||||
/// <param name="userData">Argument that passed to OnReceivedEvent</param>
|
||||
public void ReadAllPackets<T>(NetDataReader reader, T userData)
|
||||
{
|
||||
while (reader.AvailableBytes > 0)
|
||||
{
|
||||
ReadPacket(reader, userData);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads one packet from NetDataReader and calls OnReceive delegate
|
||||
/// </summary>
|
||||
/// <param name="reader">NetDataReader with packet</param>
|
||||
public void ReadPacket(NetDataReader reader)
|
||||
{
|
||||
ReadPacket(reader, null);
|
||||
}
|
||||
|
||||
private StructInfo ReadInfo(NetDataReader reader)
|
||||
{
|
||||
ulong hash = _hasher.ReadHash(reader);
|
||||
StructInfo info;
|
||||
if (!_cache.TryGetValue(hash, out info))
|
||||
{
|
||||
throw new Exception("Undefined packet received");
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads packet with known type
|
||||
/// </summary>
|
||||
/// <param name="reader">NetDataReader with packet</param>
|
||||
/// <returns>Returns packet if packet in reader is matched type</returns>
|
||||
public T ReadKnownPacket<T>(NetDataReader reader) where T : class, new()
|
||||
{
|
||||
var info = ReadInfo(reader);
|
||||
ulong typeHash = _hasher.GetHash(typeof(T).Name);
|
||||
if (typeHash != info.Hash)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
info.Reference = info.CreatorFunc != null ? info.CreatorFunc() : Activator.CreateInstance<T>();
|
||||
info.Read(reader);
|
||||
return (T)info.Reference;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads packet with known type (non alloc variant)
|
||||
/// </summary>
|
||||
/// <param name="reader">NetDataReader with packet</param>
|
||||
/// <param name="target">Deserialization target</param>
|
||||
/// <returns>Returns true if packet in reader is matched type</returns>
|
||||
public bool ReadKnownPacket<T>(NetDataReader reader, T target) where T : class, new()
|
||||
{
|
||||
var info = ReadInfo(reader);
|
||||
ulong typeHash = _hasher.GetHash(typeof(T).Name);
|
||||
if (typeHash != info.Hash)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
info.Reference = target;
|
||||
info.Read(reader);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads one packet from NetDataReader and calls OnReceive delegate
|
||||
/// </summary>
|
||||
/// <param name="reader">NetDataReader with packet</param>
|
||||
/// <param name="userData">Argument that passed to OnReceivedEvent</param>
|
||||
public void ReadPacket(NetDataReader reader, object userData)
|
||||
{
|
||||
var info = ReadInfo(reader);
|
||||
if (info.CreatorFunc != null)
|
||||
{
|
||||
info.Reference = info.CreatorFunc();
|
||||
}
|
||||
info.Read(reader);
|
||||
info.OnReceive(info.Reference, userData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register and subscribe to packet receive event
|
||||
/// </summary>
|
||||
/// <param name="onReceive">event that will be called when packet deserialized with ReadPacket method</param>
|
||||
/// <param name="packetConstructor">Method that constructs packet intead of slow Activator.CreateInstance</param>
|
||||
public void Subscribe<T>(Action<T> onReceive, Func<T> packetConstructor) where T : class, new()
|
||||
{
|
||||
var info = RegisterInternal<T>();
|
||||
info.CreatorFunc = () => packetConstructor();
|
||||
info.OnReceive = (o, userData) => { onReceive((T)o); };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register packet type for direct reading (ReadKnownPacket)
|
||||
/// </summary>
|
||||
/// <param name="packetConstructor">Method that constructs packet intead of slow Activator.CreateInstance</param>
|
||||
public void Register<T>(Func<T> packetConstructor = null) where T : class, new()
|
||||
{
|
||||
var info = RegisterInternal<T>();
|
||||
if (packetConstructor != null)
|
||||
{
|
||||
info.CreatorFunc = () => packetConstructor();
|
||||
}
|
||||
info.OnReceive = (o, userData) => { };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register and subscribe to packet receive event (with userData)
|
||||
/// </summary>
|
||||
/// <param name="onReceive">event that will be called when packet deserialized with ReadPacket method</param>
|
||||
/// <param name="packetConstructor">Method that constructs packet intead of slow Activator.CreateInstance</param>
|
||||
public void Subscribe<T, TUserData>(Action<T, TUserData> onReceive, Func<T> packetConstructor) where T : class, new()
|
||||
{
|
||||
var info = RegisterInternal<T>();
|
||||
info.CreatorFunc = () => packetConstructor();
|
||||
info.OnReceive = (o, userData) => { onReceive((T)o, (TUserData)userData); };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register and subscribe to packet receive event
|
||||
/// This metod will overwrite last received packet class on receive (less garbage)
|
||||
/// </summary>
|
||||
/// <param name="onReceive">event that will be called when packet deserialized with ReadPacket method</param>
|
||||
public void SubscribeReusable<T>(Action<T> onReceive) where T : class, new()
|
||||
{
|
||||
var info = RegisterInternal<T>();
|
||||
info.Reference = new T();
|
||||
info.OnReceive = (o, userData) => { onReceive((T)o); };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register and subscribe to packet receive event
|
||||
/// This metod will overwrite last received packet class on receive (less garbage)
|
||||
/// </summary>
|
||||
/// <param name="onReceive">event that will be called when packet deserialized with ReadPacket method</param>
|
||||
public void SubscribeReusable<T, TUserData>(Action<T, TUserData> onReceive) where T : class, new()
|
||||
{
|
||||
var info = RegisterInternal<T>();
|
||||
info.Reference = new T();
|
||||
info.OnReceive = (o, userData) => { onReceive((T)o, (TUserData)userData); };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize struct to NetDataWriter (fast)
|
||||
/// </summary>
|
||||
/// <param name="writer">Serialization target NetDataWriter</param>
|
||||
/// <param name="obj">Struct to serialize</param>
|
||||
public void Serialize<T>(NetDataWriter writer, T obj) where T : class, new()
|
||||
{
|
||||
var info = RegisterInternal<T>();
|
||||
_hasher.WriteHash(info.Hash, writer);
|
||||
info.Write(writer, obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize struct to byte array
|
||||
/// </summary>
|
||||
/// <param name="obj">Struct to serialize</param>
|
||||
/// <returns>byte array with serialized data</returns>
|
||||
public byte[] Serialize<T>(T obj) where T : class, new()
|
||||
{
|
||||
_writer.Reset();
|
||||
Serialize(_writer, obj);
|
||||
return _writer.CopyData();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/LiteNetLib/Utils/NetSerializer.cs.meta
Normal file
11
Assets/Scripts/LiteNetLib/Utils/NetSerializer.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c9b01a33d0d635d4381a5174f7e3806d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue