quantum-space-buddies/QuantumUNET/QSBNetworkReader.cs

566 lines
11 KiB
C#
Raw Normal View History

2020-12-04 09:23:27 +00:00
using System;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
2020-12-04 22:14:53 +00:00
namespace QuantumUNET
2020-12-04 09:23:27 +00:00
{
public class QSBNetworkReader
{
public QSBNetworkReader()
{
2020-12-04 21:04:18 +00:00
m_buf = new QSBNetBuffer();
2020-12-04 22:15:41 +00:00
Initialize();
2020-12-04 09:23:27 +00:00
}
public QSBNetworkReader(QSBNetworkWriter writer)
{
2020-12-04 21:04:18 +00:00
m_buf = new QSBNetBuffer(writer.AsArray());
2020-12-04 22:15:41 +00:00
Initialize();
2020-12-04 09:23:27 +00:00
}
public QSBNetworkReader(byte[] buffer)
{
2020-12-04 21:04:18 +00:00
m_buf = new QSBNetBuffer(buffer);
2020-12-04 22:15:41 +00:00
Initialize();
2020-12-04 09:23:27 +00:00
}
private static void Initialize()
{
2020-12-04 21:04:18 +00:00
if (s_Encoding == null)
2020-12-04 09:23:27 +00:00
{
2020-12-04 22:15:41 +00:00
s_StringReaderBuffer = new byte[1024];
s_Encoding = new UTF8Encoding();
2020-12-04 09:23:27 +00:00
}
}
public uint Position
{
get
{
2020-12-04 21:04:18 +00:00
return m_buf.Position;
2020-12-04 09:23:27 +00:00
}
}
public int Length
{
get
{
2020-12-04 21:04:18 +00:00
return m_buf.Length;
2020-12-04 09:23:27 +00:00
}
}
public void SeekZero()
{
2020-12-04 21:04:18 +00:00
m_buf.SeekZero();
2020-12-04 09:23:27 +00:00
}
internal void Replace(byte[] buffer)
{
2020-12-04 21:04:18 +00:00
m_buf.Replace(buffer);
2020-12-04 09:23:27 +00:00
}
public uint ReadPackedUInt32()
{
2020-12-04 21:04:18 +00:00
byte b = ReadByte();
2020-12-04 09:23:27 +00:00
uint result;
if (b < 241)
{
result = (uint)b;
}
else
{
2020-12-04 21:04:18 +00:00
byte b2 = ReadByte();
2020-12-04 09:23:27 +00:00
if (b >= 241 && b <= 248)
{
result = 240U + 256U * (uint)(b - 241) + (uint)b2;
}
else
{
2020-12-04 21:04:18 +00:00
byte b3 = ReadByte();
2020-12-04 09:23:27 +00:00
if (b == 249)
{
result = 2288U + 256U * (uint)b2 + (uint)b3;
}
else
{
2020-12-04 21:04:18 +00:00
byte b4 = ReadByte();
2020-12-04 09:23:27 +00:00
if (b == 250)
{
result = (uint)((int)b2 + ((int)b3 << 8) + ((int)b4 << 16));
}
else
{
2020-12-04 21:04:18 +00:00
byte b5 = ReadByte();
2020-12-04 09:23:27 +00:00
if (b < 251)
{
throw new IndexOutOfRangeException("ReadPackedUInt32() failure: " + b);
}
result = (uint)((int)b2 + ((int)b3 << 8) + ((int)b4 << 16) + ((int)b5 << 24));
}
}
}
}
return result;
}
public ulong ReadPackedUInt64()
{
2020-12-04 21:04:18 +00:00
byte b = ReadByte();
2020-12-04 09:23:27 +00:00
ulong result;
if (b < 241)
{
result = (ulong)b;
}
else
{
2020-12-04 21:04:18 +00:00
byte b2 = ReadByte();
2020-12-04 09:23:27 +00:00
if (b >= 241 && b <= 248)
{
result = 240UL + 256UL * ((ulong)b - 241UL) + (ulong)b2;
}
else
{
2020-12-04 21:04:18 +00:00
byte b3 = ReadByte();
2020-12-04 09:23:27 +00:00
if (b == 249)
{
result = 2288UL + 256UL * (ulong)b2 + (ulong)b3;
}
else
{
2020-12-04 21:04:18 +00:00
byte b4 = ReadByte();
2020-12-04 09:23:27 +00:00
if (b == 250)
{
result = (ulong)b2 + ((ulong)b3 << 8) + ((ulong)b4 << 16);
}
else
{
2020-12-04 21:04:18 +00:00
byte b5 = ReadByte();
2020-12-04 09:23:27 +00:00
if (b == 251)
{
result = (ulong)b2 + ((ulong)b3 << 8) + ((ulong)b4 << 16) + ((ulong)b5 << 24);
}
else
{
2020-12-04 21:04:18 +00:00
byte b6 = ReadByte();
2020-12-04 09:23:27 +00:00
if (b == 252)
{
result = (ulong)b2 + ((ulong)b3 << 8) + ((ulong)b4 << 16) + ((ulong)b5 << 24) + ((ulong)b6 << 32);
}
else
{
2020-12-04 21:04:18 +00:00
byte b7 = ReadByte();
2020-12-04 09:23:27 +00:00
if (b == 253)
{
result = (ulong)b2 + ((ulong)b3 << 8) + ((ulong)b4 << 16) + ((ulong)b5 << 24) + ((ulong)b6 << 32) + ((ulong)b7 << 40);
}
else
{
2020-12-04 21:04:18 +00:00
byte b8 = ReadByte();
2020-12-04 09:23:27 +00:00
if (b == 254)
{
result = (ulong)b2 + ((ulong)b3 << 8) + ((ulong)b4 << 16) + ((ulong)b5 << 24) + ((ulong)b6 << 32) + ((ulong)b7 << 40) + ((ulong)b8 << 48);
}
else
{
2020-12-04 21:04:18 +00:00
byte b9 = ReadByte();
2020-12-04 09:23:27 +00:00
if (b != 255)
{
throw new IndexOutOfRangeException("ReadPackedUInt64() failure: " + b);
}
result = (ulong)b2 + ((ulong)b3 << 8) + ((ulong)b4 << 16) + ((ulong)b5 << 24) + ((ulong)b6 << 32) + ((ulong)b7 << 40) + ((ulong)b8 << 48) + ((ulong)b9 << 56);
}
}
}
}
}
}
}
}
return result;
}
public NetworkInstanceId ReadNetworkId()
{
2020-12-04 21:04:18 +00:00
return new NetworkInstanceId(ReadPackedUInt32());
2020-12-04 09:23:27 +00:00
}
public NetworkSceneId ReadSceneId()
{
2020-12-04 21:04:18 +00:00
return new NetworkSceneId(ReadPackedUInt32());
2020-12-04 09:23:27 +00:00
}
public byte ReadByte()
{
2020-12-04 21:04:18 +00:00
return m_buf.ReadByte();
2020-12-04 09:23:27 +00:00
}
public sbyte ReadSByte()
{
2020-12-04 21:04:18 +00:00
return (sbyte)m_buf.ReadByte();
2020-12-04 09:23:27 +00:00
}
public short ReadInt16()
{
ushort num = 0;
2020-12-04 21:04:18 +00:00
num |= (ushort)m_buf.ReadByte();
num |= (ushort)(m_buf.ReadByte() << 8);
2020-12-04 09:23:27 +00:00
return (short)num;
}
public ushort ReadUInt16()
{
2020-12-04 21:04:18 +00:00
return (ushort)((uint)(ushort)(0U | (uint)m_buf.ReadByte()) | (uint)(ushort)((uint)m_buf.ReadByte() << 8));
2020-12-04 09:23:27 +00:00
}
public int ReadInt32()
{
uint num = 0U;
2020-12-04 21:04:18 +00:00
num |= (uint)m_buf.ReadByte();
num |= (uint)((uint)m_buf.ReadByte() << 8);
num |= (uint)((uint)m_buf.ReadByte() << 16);
return (int)(num | (uint)((uint)m_buf.ReadByte() << 24));
2020-12-04 09:23:27 +00:00
}
public uint ReadUInt32()
{
uint num = 0U;
2020-12-04 21:04:18 +00:00
num |= (uint)m_buf.ReadByte();
num |= (uint)((uint)m_buf.ReadByte() << 8);
num |= (uint)((uint)m_buf.ReadByte() << 16);
return num | (uint)((uint)m_buf.ReadByte() << 24);
2020-12-04 09:23:27 +00:00
}
public long ReadInt64()
{
ulong num = 0UL;
2020-12-04 21:04:18 +00:00
ulong num2 = (ulong)m_buf.ReadByte();
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 8;
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 16;
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 24;
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 32;
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 40;
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 48;
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 56;
2020-12-04 09:23:27 +00:00
return (long)(num | num2);
}
public ulong ReadUInt64()
{
ulong num = 0UL;
2020-12-04 21:04:18 +00:00
ulong num2 = (ulong)m_buf.ReadByte();
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 8;
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 16;
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 24;
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 32;
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 40;
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 48;
2020-12-04 09:23:27 +00:00
num |= num2;
2020-12-04 21:04:18 +00:00
num2 = (ulong)m_buf.ReadByte() << 56;
2020-12-04 09:23:27 +00:00
return num | num2;
}
public decimal ReadDecimal()
{
return new decimal(new int[]
{
2020-12-04 21:04:18 +00:00
ReadInt32(),
ReadInt32(),
ReadInt32(),
ReadInt32()
2020-12-04 09:23:27 +00:00
});
}
public float ReadSingle()
{
2020-12-04 21:04:18 +00:00
var bytes = ReadBytes(4);
return BitConverter.ToSingle(bytes, 0);
2020-12-04 09:23:27 +00:00
}
public double ReadDouble()
{
2020-12-04 21:04:18 +00:00
var bytes = ReadBytes(8);
return BitConverter.ToSingle(bytes, 0);
2020-12-04 09:23:27 +00:00
}
public string ReadString()
{
2020-12-04 21:04:18 +00:00
ushort num = ReadUInt16();
2020-12-04 09:23:27 +00:00
string result;
if (num == 0)
{
result = "";
}
else
{
if (num >= 32768)
{
throw new IndexOutOfRangeException("ReadString() too long: " + num);
}
2020-12-04 21:04:18 +00:00
while ((int)num > s_StringReaderBuffer.Length)
2020-12-04 09:23:27 +00:00
{
2020-12-04 22:15:41 +00:00
s_StringReaderBuffer = new byte[s_StringReaderBuffer.Length * 2];
2020-12-04 09:23:27 +00:00
}
2020-12-04 21:04:18 +00:00
m_buf.ReadBytes(s_StringReaderBuffer, (uint)num);
char[] chars = s_Encoding.GetChars(s_StringReaderBuffer, 0, (int)num);
2020-12-04 09:23:27 +00:00
result = new string(chars);
}
return result;
}
public char ReadChar()
{
2020-12-04 21:04:18 +00:00
return (char)m_buf.ReadByte();
2020-12-04 09:23:27 +00:00
}
public bool ReadBoolean()
{
2020-12-04 21:04:18 +00:00
int num = (int)m_buf.ReadByte();
2020-12-04 09:23:27 +00:00
return num == 1;
}
public byte[] ReadBytes(int count)
{
if (count < 0)
{
throw new IndexOutOfRangeException("NetworkReader ReadBytes " + count);
}
byte[] array = new byte[count];
2020-12-04 21:04:18 +00:00
m_buf.ReadBytes(array, (uint)count);
2020-12-04 09:23:27 +00:00
return array;
}
public byte[] ReadBytesAndSize()
{
2020-12-04 21:04:18 +00:00
ushort num = ReadUInt16();
2020-12-04 09:23:27 +00:00
byte[] result;
if (num == 0)
{
result = new byte[0];
}
else
{
2020-12-04 21:04:18 +00:00
result = ReadBytes((int)num);
2020-12-04 09:23:27 +00:00
}
return result;
}
public Vector2 ReadVector2()
{
2020-12-04 21:04:18 +00:00
return new Vector2(ReadSingle(), ReadSingle());
2020-12-04 09:23:27 +00:00
}
public Vector3 ReadVector3()
{
2020-12-04 21:04:18 +00:00
return new Vector3(ReadSingle(), ReadSingle(), ReadSingle());
2020-12-04 09:23:27 +00:00
}
public Vector4 ReadVector4()
{
2020-12-04 21:04:18 +00:00
return new Vector4(ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle());
2020-12-04 09:23:27 +00:00
}
public Color ReadColor()
{
2020-12-04 21:04:18 +00:00
return new Color(ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle());
2020-12-04 09:23:27 +00:00
}
public Color32 ReadColor32()
{
2020-12-04 21:04:18 +00:00
return new Color32(ReadByte(), ReadByte(), ReadByte(), ReadByte());
2020-12-04 09:23:27 +00:00
}
public Quaternion ReadQuaternion()
{
2020-12-04 21:04:18 +00:00
return new Quaternion(ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle());
2020-12-04 09:23:27 +00:00
}
public Rect ReadRect()
{
2020-12-04 21:04:18 +00:00
return new Rect(ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle());
2020-12-04 09:23:27 +00:00
}
public Plane ReadPlane()
{
2020-12-04 21:04:18 +00:00
return new Plane(ReadVector3(), ReadSingle());
2020-12-04 09:23:27 +00:00
}
public Ray ReadRay()
{
2020-12-04 21:04:18 +00:00
return new Ray(ReadVector3(), ReadVector3());
2020-12-04 09:23:27 +00:00
}
public Matrix4x4 ReadMatrix4x4()
{
return new Matrix4x4
{
2020-12-04 21:04:18 +00:00
m00 = ReadSingle(),
m01 = ReadSingle(),
m02 = ReadSingle(),
m03 = ReadSingle(),
m10 = ReadSingle(),
m11 = ReadSingle(),
m12 = ReadSingle(),
m13 = ReadSingle(),
m20 = ReadSingle(),
m21 = ReadSingle(),
m22 = ReadSingle(),
m23 = ReadSingle(),
m30 = ReadSingle(),
m31 = ReadSingle(),
m32 = ReadSingle(),
m33 = ReadSingle()
2020-12-04 09:23:27 +00:00
};
}
public NetworkHash128 ReadNetworkHash128()
{
NetworkHash128 result;
2020-12-04 21:04:18 +00:00
result.i0 = ReadByte();
result.i1 = ReadByte();
result.i2 = ReadByte();
result.i3 = ReadByte();
result.i4 = ReadByte();
result.i5 = ReadByte();
result.i6 = ReadByte();
result.i7 = ReadByte();
result.i8 = ReadByte();
result.i9 = ReadByte();
result.i10 = ReadByte();
result.i11 = ReadByte();
result.i12 = ReadByte();
result.i13 = ReadByte();
result.i14 = ReadByte();
result.i15 = ReadByte();
2020-12-04 09:23:27 +00:00
return result;
}
public Transform ReadTransform()
{
2020-12-04 21:04:18 +00:00
NetworkInstanceId networkInstanceId = ReadNetworkId();
2020-12-04 09:23:27 +00:00
Transform result;
if (networkInstanceId.IsEmpty())
{
result = null;
}
else
{
GameObject gameObject = QSBClientScene.FindLocalObject(networkInstanceId);
2020-12-04 09:23:27 +00:00
if (gameObject == null)
{
if (LogFilter.logDebug)
{
Debug.Log("ReadTransform netId:" + networkInstanceId);
}
result = null;
}
else
{
result = gameObject.transform;
}
}
return result;
}
public GameObject ReadGameObject()
{
2020-12-04 21:04:18 +00:00
NetworkInstanceId networkInstanceId = ReadNetworkId();
2020-12-04 09:23:27 +00:00
GameObject result;
if (networkInstanceId.IsEmpty())
{
result = null;
}
else
{
GameObject gameObject;
if (QSBNetworkServer.active)
{
gameObject = QSBNetworkServer.FindLocalObject(networkInstanceId);
}
else
{
gameObject = QSBClientScene.FindLocalObject(networkInstanceId);
}
if (gameObject == null)
{
if (LogFilter.logDebug)
{
Debug.Log("ReadGameObject netId:" + networkInstanceId + "go: null");
}
}
result = gameObject;
}
return result;
}
public QSBNetworkIdentity ReadNetworkIdentity()
{
2020-12-04 21:04:18 +00:00
NetworkInstanceId networkInstanceId = ReadNetworkId();
2020-12-04 09:23:27 +00:00
QSBNetworkIdentity result;
if (networkInstanceId.IsEmpty())
{
result = null;
}
else
{
GameObject gameObject;
if (QSBNetworkServer.active)
{
gameObject = QSBNetworkServer.FindLocalObject(networkInstanceId);
}
else
{
gameObject = QSBClientScene.FindLocalObject(networkInstanceId);
}
if (gameObject == null)
{
if (LogFilter.logDebug)
{
Debug.Log("ReadNetworkIdentity netId:" + networkInstanceId + "go: null");
}
result = null;
}
else
{
result = gameObject.GetComponent<QSBNetworkIdentity>();
}
}
return result;
}
public override string ToString()
{
2020-12-04 21:04:18 +00:00
return m_buf.ToString();
2020-12-04 09:23:27 +00:00
}
public TMsg ReadMessage<TMsg>() where TMsg : QSBMessageBase, new()
{
TMsg result = Activator.CreateInstance<TMsg>();
result.Deserialize(this);
return result;
}
private QSBNetBuffer m_buf;
private const int k_MaxStringLength = 32768;
private const int k_InitialStringBufferSize = 1024;
private static byte[] s_StringReaderBuffer;
private static Encoding s_Encoding;
}
2020-12-04 09:29:23 +00:00
}