using FishNet.Connection; using FishNet.Documenting; using FishNet.Managing; using FishNet.Managing.Logging; using FishNet.Object; using FishNet.Serializing.Helping; using FishNet.Transporting; using FishNet.Utility.Constant; using FishNet.Utility.Extension; using FishNet.Utility.Performance; using System; using System.Collections.Generic; using System.IO; using System.Runtime.CompilerServices; using System.Text; using UnityEngine; [assembly: InternalsVisibleTo(UtilityConstants.GENERATED_ASSEMBLY_NAME)] //Required for internal tests. [assembly: InternalsVisibleTo(UtilityConstants.TEST_ASSEMBLY_NAME)] namespace FishNet.Serializing { /// /// Used for read references to generic types. /// /// [APIExclude] public static class GenericReader { public static Func Read { internal get; set; } public static Func ReadAutoPack { internal get; set; } } /// /// Reads data from a buffer. /// public class Reader { #region Public. /// /// NetworkManager for this reader. Used to lookup objects. /// public NetworkManager NetworkManager; /// /// Offset within the buffer when the reader was created. /// public int Offset { get; private set; } /// /// Position for the next read. /// public int Position; /// /// Total number of bytes available within the buffer. /// public int Length { get; private set; } /// /// Bytes remaining to be read. This value is Length - Position. /// public int Remaining => ((Length + Offset) - Position); #endregion #region Internal. #if UNITY_EDITOR || DEVELOPMENT_BUILD /// /// Last NetworkObject parsed. /// public static NetworkObject LastNetworkObject { get; private set; } /// /// Last NetworkBehaviour parsed. /// public static NetworkBehaviour LastNetworkBehaviour { get; private set; } #endif #endregion #region Private. /// /// Data being read. /// private byte[] _buffer; /// /// Buffer to copy Guids into. /// private byte[] _guidBuffer = new byte[16]; /// /// Used to encode strings. /// private readonly UTF8Encoding _encoding = new UTF8Encoding(false, true); #endregion [MethodImpl(MethodImplOptions.AggressiveInlining)] public Reader(byte[] bytes, NetworkManager networkManager) { Initialize(bytes, networkManager); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Reader(ArraySegment segment, NetworkManager networkManager) { Initialize(segment, networkManager); } /// /// Outputs reader to string. /// /// public override string ToString() { return $"Position: {Position}, Length: {Length}, Buffer: {BitConverter.ToString(_buffer, Offset, Length)}."; } /// /// Initializes this reader with data. /// /// /// internal void Initialize(ArraySegment bytes, NetworkManager networkManager) { if (bytes.Array == null) { if (_buffer == null) _buffer = new byte[0]; } else { _buffer = bytes.Array; } Position = bytes.Offset; Offset = bytes.Offset; Length = bytes.Count; NetworkManager = networkManager; } /// /// Initializes this reader with data. /// /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void Initialize(byte[] bytes, NetworkManager networkManager) { Initialize(new ArraySegment(bytes), networkManager); } /// /// Writes a dictionary. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Dictionary ReadDictionary() { bool isNull = ReadBoolean(); if (isNull) return null; int count = ReadInt32(); Dictionary result = new Dictionary(count); for (int i = 0; i < count; i++) { TKey key = Read(); TValue value = Read(); result.Add(key, value); } return result; } /// /// Reads length. This method is used to make debugging easier. /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal int ReadLength() { return ReadInt32(); } /// /// Reads a packetId. /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal PacketId ReadPacketId() { return (PacketId)ReadUInt16(); } /// /// Returns a ushort without advancing the reader. /// /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal PacketId PeekPacketId() { int currentPosition = Position; PacketId result = ReadPacketId(); Position = currentPosition; return result; } /// /// Skips a number of bytes in the reader. /// /// [CodegenExclude] public void Skip(int value) { if (value < 1 || Remaining < value) return; Position += value; } /// /// Returns the buffer as an ArraySegment. /// /// public ArraySegment GetArraySegmentBuffer() { return new ArraySegment(_buffer, Offset, Length); } /// /// Returns the buffer as bytes. This does not trim excessive bytes. /// /// public byte[] GetByteBuffer() { return _buffer; } /// /// Returns the buffer as bytes and allocates into a new array. /// /// public byte[] GetByteBufferAllocated() { byte[] result = new byte[Length]; Buffer.BlockCopy(_buffer, Offset, result, 0, Length); return result; } /// /// BlockCopies data from the reader to target and advances reader. /// /// /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void BlockCopy(ref byte[] target, int targetOffset, int count) { Buffer.BlockCopy(_buffer, Position, target, targetOffset, count); Position += count; } /// /// Reads a byte. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public byte ReadByte() { byte r = _buffer[Position]; Position += 1; return r; } /// /// Read bytes from position into target. /// /// [CodegenExclude] //[MethodImpl(MethodImplOptions.AggressiveInlining)] public void ReadBytes(ref byte[] target, int count) { if (target == null) throw new EndOfStreamException($"Target is null."); //Target isn't large enough. if (count > target.Length) throw new EndOfStreamException($"Count of {count} exceeds target length of {target.Length}."); BlockCopy(ref target, 0, count); } /// /// Creates an ArraySegment by reading a number of bytes from position. /// /// /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] public ArraySegment ReadArraySegment(int count) { ArraySegment result = new ArraySegment(_buffer, Position, count); Position += count; return result; } /// /// Reads a sbyte. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public sbyte ReadSByte() { return (sbyte)ReadByte(); } /// /// Reads a char. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public char ReadChar() => (char)ReadUInt16(); /// /// Reads a boolean. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool ReadBoolean() { byte result = ReadByte(); return (result == 1) ? true : false; } /// /// Reads an int16. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public ushort ReadUInt16() { ushort result = 0; result |= _buffer[Position++]; result |= (ushort)(_buffer[Position++] << 8); return result; } /// /// Reads a uint16. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public short ReadInt16() => (short)ReadUInt16(); /// /// Reads an int32. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public uint ReadUInt32(AutoPackType packType = AutoPackType.Packed) { if (packType == AutoPackType.Packed) return (uint)ReadPackedWhole(); uint result = 0; result |= _buffer[Position++]; result |= (uint)_buffer[Position++] << 8; result |= (uint)_buffer[Position++] << 16; result |= (uint)_buffer[Position++] << 24; return result; } /// /// Reads a uint32. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public int ReadInt32(AutoPackType packType = AutoPackType.Packed) { if (packType == AutoPackType.Packed) return (int)(long)ZigZagDecode(ReadPackedWhole()); return (int)ReadUInt32(packType); } /// /// Reads a uint64. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public long ReadInt64(AutoPackType packType = AutoPackType.Packed) { if (packType == AutoPackType.Packed) return (long)ZigZagDecode(ReadPackedWhole()); return (long)ReadUInt64(packType); } /// /// Reads an int64. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong ReadUInt64(AutoPackType packType = AutoPackType.Packed) { if (packType == AutoPackType.Packed) return (ulong)ReadPackedWhole(); ulong result = 0; result |= _buffer[Position++]; result |= (ulong)_buffer[Position++] << 8; result |= (ulong)_buffer[Position++] << 16; result |= (ulong)_buffer[Position++] << 24; result |= (ulong)_buffer[Position++] << 32; result |= (ulong)_buffer[Position++] << 40; result |= (ulong)_buffer[Position++] << 48; result |= (ulong)_buffer[Position++] << 56; return result; } /// /// Reads a single. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public float ReadSingle(AutoPackType packType = AutoPackType.Unpacked) { if (packType == AutoPackType.Unpacked) { UIntFloat converter = new UIntFloat(); converter.UIntValue = ReadUInt32(AutoPackType.Unpacked); return converter.FloatValue; } else { long converter = (long)ReadPackedWhole(); return (float)(converter / 100f); } } /// /// Reads a double. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public double ReadDouble() { UIntDouble converter = new UIntDouble(); converter.LongValue = ReadUInt64(AutoPackType.Unpacked); return converter.DoubleValue; } /// /// Reads a decimal. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public decimal ReadDecimal() { UIntDecimal converter = new UIntDecimal(); converter.LongValue1 = ReadUInt64(AutoPackType.Unpacked); converter.LongValue2 = ReadUInt64(AutoPackType.Unpacked); return converter.DecimalValue; } /// /// Reads a string. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public string ReadString() { int size = ReadInt32(); //Null string. if (size == -1) return null; else if (size == 0) return string.Empty; if (!CheckAllocationAttack(size)) return string.Empty; ArraySegment data = ReadArraySegment(size); return _encoding.GetString(data.Array, data.Offset, data.Count); } /// /// Creates a byte array and reads bytes and size into it. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public byte[] ReadBytesAndSizeAllocated() { int size = ReadInt32(); if (size == -1) return null; else return ReadBytesAllocated(size); } /// /// Reads bytes and size and copies results into target. Returns -1 if null was written. /// /// Bytes read. [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] public int ReadBytesAndSize(ref byte[] target) { int size = ReadInt32(); if (size > 0) ReadBytes(ref target, size); return size; } /// /// Reads bytes and size and returns as an ArraySegment. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public ArraySegment ReadArraySegmentAndSize() { int size = ReadInt32(); /* -1 would be written for null. But since * ArraySegments cannot be null return default if * length is 0 or less. */ if (size <= 0) return default; return ReadArraySegment(size); } /// /// Reads a Vector2. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector2 ReadVector2() { return new Vector2(ReadSingle(), ReadSingle()); } /// /// Reads a Vector3. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector3 ReadVector3() { return new Vector3(ReadSingle(), ReadSingle(), ReadSingle()); } /// /// Reads a Vector4. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector4 ReadVector4() { return new Vector4(ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle()); } /// /// Reads a Vector2Int. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector2Int ReadVector2Int(AutoPackType packType = AutoPackType.Packed) { return new Vector2Int(ReadInt32(packType), ReadInt32(packType)); } /// /// Reads a Vector3Int. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector3Int ReadVector3Int(AutoPackType packType = AutoPackType.Packed) { return new Vector3Int(ReadInt32(packType), ReadInt32(packType), ReadInt32(packType)); } /// /// Reads a color. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color ReadColor(AutoPackType packType = AutoPackType.Packed) { float r, g, b, a; if (packType == AutoPackType.Unpacked) { r = ReadSingle(); g = ReadSingle(); b = ReadSingle(); a = ReadSingle(); } else { r = (float)(ReadByte() / 100f); g = (float)(ReadByte() / 100f); b = (float)(ReadByte() / 100f); a = (float)(ReadByte() / 100f); } return new Color(r, g, b, a); } /// /// Reads a Color32. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Color32 ReadColor32() { return new Color32(ReadByte(), ReadByte(), ReadByte(), ReadByte()); } /// /// Reads a Quaternion. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Quaternion ReadQuaternion(AutoPackType packType = AutoPackType.Packed) { if (packType == AutoPackType.Packed) { uint result = ReadUInt32(AutoPackType.Unpacked); return Quaternion32Compression.Decompress(result); } else if (packType == AutoPackType.PackedLess) { ulong result = ReadUInt64(AutoPackType.Unpacked); return Quaternion64Compression.Decompress(result); } else { return new Quaternion( ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle() ); } } /// /// Reads a Rect. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Rect ReadRect() { return new Rect(ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle()); } /// /// Plane. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Plane ReadPlane() { return new Plane(ReadVector3(), ReadSingle()); } /// /// Reads a Ray. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Ray ReadRay() { Vector3 position = ReadVector3(); Vector3 direction = ReadVector3(); return new Ray(position, direction); } /// /// Reads a Ray. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Ray2D ReadRay2D() { Vector3 position = ReadVector2(); Vector2 direction = ReadVector2(); return new Ray2D(position, direction); } /// /// Reads a Matrix4x4. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Matrix4x4 ReadMatrix4x4() { Matrix4x4 result = new Matrix4x4 { 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() }; return result; } /// /// Creates a new byte array and reads bytes into it. /// /// /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] public byte[] ReadBytesAllocated(int count) { byte[] bytes = ByteArrayPool.Retrieve(count); ReadBytes(ref bytes, count); return bytes; } /// /// Reads a Guid. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public System.Guid ReadGuid() { ReadBytes(ref _guidBuffer, 16); return new System.Guid(_guidBuffer); } /// /// Reads a GameObject. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public GameObject ReadGameObject() { NetworkObject nob = ReadNetworkObject(); return (nob == null) ? null : nob.gameObject; } /// /// Reads a Transform. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Transform ReadTransform() { NetworkObject nob = ReadNetworkObject(); return (nob == null) ? null : nob.transform; } /// /// Reads a NetworkObject. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public NetworkObject ReadNetworkObject() { return ReadNetworkObject(out _); } /// /// Reads a NetworkObject. /// /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] public NetworkObject ReadNetworkObject(out int objectId) { #if UNITY_EDITOR || DEVELOPMENT_BUILD LastNetworkBehaviour = null; #endif bool isSpawned = ReadBoolean(); objectId = ReadInt16(); /* -1 indicates that the object * is null or no PrefabId is set. * PrefabIds are set in Awake within * the NetworkManager so that should * never happen so long as nob isn't null. */ if (objectId == -1) return null; bool isServer = NetworkManager.ServerManager.Started; bool isClient = NetworkManager.ClientManager.Started; NetworkObject result; //Is spawned. if (isSpawned) { result = null; /* Try to get the object client side first if client * is running. When acting as a host generally the object * will be available in the server and client list * but there can be occasions where the server side * deinitializes the object, making it unavailable, while * it is still available in the client side. Since FishNet doesn't * use a fake host connection like some lesser solutions the client * has to always be treated as it's own entity. */ if (isClient) NetworkManager.ClientManager.Objects.Spawned.TryGetValueIL2CPP(objectId, out result); //If not found on client and server is running then try server. if (result == null && isServer) NetworkManager.ServerManager.Objects.Spawned.TryGetValueIL2CPP(objectId, out result); } //Not spawned. else { //Only look up asServer if not client, otherwise use client. bool asServer = !isClient; //Look up prefab. result = NetworkManager.SpawnablePrefabs.GetObject(asServer, objectId); } #if UNITY_EDITOR || DEVELOPMENT_BUILD LastNetworkObject = result; #endif return result; } /// /// Reads a NetworkBehaviour. /// /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] public NetworkBehaviour ReadNetworkBehaviour(out int objectId, out byte componentIndex) { NetworkObject nob = ReadNetworkObject(out objectId); componentIndex = ReadByte(); NetworkBehaviour result; if (nob == null) { result = null; } else { if (componentIndex >= nob.NetworkBehaviours.Length) { if (NetworkManager.CanLog(LoggingType.Error)) Debug.LogError($"ComponentIndex of {componentIndex} is out of bounds on {nob.gameObject.name} [id {nob.ObjectId}]. This may occur if you have modified your gameObject/prefab without saving it, or the scene."); result = null; } else { result = nob.NetworkBehaviours[componentIndex]; } } #if UNITY_EDITOR || DEVELOPMENT_BUILD LastNetworkBehaviour = result; #endif return result; } /// /// Reads a NetworkBehaviour. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public NetworkBehaviour ReadNetworkBehaviour() { return ReadNetworkBehaviour(out _, out _); } /// /// Writes a transport channel. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Channel ReadChannel() { return (Channel)ReadByte(); } /// /// Reads the Id for a NetworkConnection. /// /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] public int ReadNetworkConnectionId() { return ReadInt16(); } /// /// Reads the Id for a NetworkObject. /// /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] public int ReadNetworkObjectId() { //Clear spawned. ReadBoolean(); return ReadInt16(); } /// /// Writes a NetworkConnection. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public NetworkConnection ReadNetworkConnection() { int value = ReadInt16(); if (value == -1) { return FishNet.Managing.NetworkManager.EmptyConnection; } else { //Prefer server. if (NetworkManager.IsServer) { NetworkConnection result; if (NetworkManager.ServerManager.Clients.TryGetValueIL2CPP(value, out result)) { return result; } //If also client then try client side data. else if (NetworkManager.IsClient) { //If found in client collection then return. if (NetworkManager.ClientManager.Clients.TryGetValueIL2CPP(value, out result)) return result; //Otherwise make a new instance. else return new NetworkConnection(NetworkManager, value); } //Only server and not found. else { if (NetworkManager.CanLog(LoggingType.Warning)) Debug.LogWarning($"Unable to find connection for read Id {value}. An empty connection will be returned."); return FishNet.Managing.NetworkManager.EmptyConnection; } } //Try client side, will only be able to fetch against local connection. else { //If value is self then return self. if (value == NetworkManager.ClientManager.Connection.ClientId) return NetworkManager.ClientManager.Connection; //Try client side dictionary. else if (NetworkManager.ClientManager.Clients.TryGetValueIL2CPP(value, out NetworkConnection result)) return result; //Otherwise return a new connection. else return new NetworkConnection(NetworkManager, value); //todo make and use NC cache. } } } /// /// Checks if the size could possibly be an allocation attack. /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] private bool CheckAllocationAttack(int size) { /* Possible attacks. Impossible size, or size indicates * more elements in collection or more bytes needed * than what bytes are available. */ if (size < -1) { if (NetworkManager.CanLog(LoggingType.Error)) Debug.LogError($"Size of {size} is invalid."); return false; } if (size > Remaining) { if (NetworkManager.CanLog(LoggingType.Error)) Debug.LogError($"Read size of {size} is larger than remaining data of {Remaining}."); return false; } //Checks pass. return true; } #region Packed readers. /// /// ZigZag decode an integer. Move the sign bit back to the left. /// public ulong ZigZagDecode(ulong value) { ulong sign = value << 63; if (sign > 0) return ~(value >> 1) | sign; return value >> 1; } /// /// Reads a packed whole number. /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong ReadPackedWhole() { byte data = ReadByte(); ulong result = (ulong)(data & 0x7F); if ((data & 0x80) == 0) return result; data = ReadByte(); result |= (ulong)(data & 0x7F) << 7; if ((data & 0x80) == 0) return result; data = ReadByte(); result |= (ulong)(data & 0x7F) << 14; if ((data & 0x80) == 0) return result; data = ReadByte(); result |= (ulong)(data & 0x7F) << 21; if ((data & 0x80) == 0) return result; data = ReadByte(); result |= (ulong)(data & 0x0F) << 28; int extraBytes = data >> 4; switch (extraBytes) { case 0: break; case 1: result |= (ulong)ReadByte() << 32; break; case 2: result |= (ulong)ReadByte() << 32; result |= (ulong)ReadByte() << 40; break; case 3: result |= (ulong)ReadByte() << 32; result |= (ulong)ReadByte() << 40; result |= (ulong)ReadByte() << 48; break; case 4: result |= (ulong)ReadByte() << 32; result |= (ulong)ReadByte() << 40; result |= (ulong)ReadByte() << 48; result |= (ulong)ReadByte() << 56; break; } return result; } #endregion #region Generators. /// /// Reads a list. /// /// /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] public List ReadListAllocated() { List result = null; ReadList(ref result); return result; } /// /// Reads into collection and returns amount read. /// /// /// /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] public int ReadList(ref List collection) { int count = ReadInt32(); if (count == -1) { return 0; } else if (count == 0) { if (collection == null) collection = new List(); return 0; } else { //Initialize buffer if not already done. if (collection == null) collection = new List(count); else if (collection.Count < count) collection.Capacity = count; for (int i = 0; i < count; i++) collection[i] = Read(); return count; } } /// /// Reads an array. /// /// /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] public T[] ReadArrayAllocated() { T[] result = null; ReadArray(ref result); return result; } /// /// Reads into collection and returns amount read. /// /// /// /// [CodegenExclude] [MethodImpl(MethodImplOptions.AggressiveInlining)] public int ReadArray(ref T[] collection) { int count = ReadInt32(); if (count == -1) { return 0; } else if (count == 0) { if (collection == null) collection = new T[0]; return 0; } else { //Initialize buffer if not already done. if (collection == null) collection = new T[count]; else if (collection.Length < count) Array.Resize(ref collection, count); for (int i = 0; i < count; i++) collection[i] = Read(); return count; } } /// /// Reads any supported type. /// /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Read() { if (IsAutoPackType(out AutoPackType packType)) { Func del = GenericReader.ReadAutoPack; if (del == null) { if (NetworkManager.CanLog(LoggingType.Error)) Debug.LogError($"Read method not found for {typeof(T).Name}. Use a supported type or create a custom serializer."); return default; } else { return del.Invoke(this, packType); } } else { Func del = GenericReader.Read; if (del == null) { if (NetworkManager.CanLog(LoggingType.Error)) Debug.LogError($"Read method not found for {typeof(T).Name}. Use a supported type or create a custom serializer."); return default; } else { return del.Invoke(this); } } } /// /// Returns if T takes AutoPackType argument. /// /// Outputs the default pack type for T. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] internal bool IsAutoPackType(out AutoPackType packType) => Writer.IsAutoPackType(out packType); #endregion } }