public static object GetValue(this NetDataReader reader, Type type)
        {
            #region Generic Values
            if (type.IsEnum)
            {
                type = type.GetEnumUnderlyingType();
            }

            if (type == typeof(bool))
            {
                return(reader.GetBool());
            }

            if (type == typeof(byte))
            {
                return(reader.GetByte());
            }

            if (type == typeof(char))
            {
                return(reader.GetChar());
            }

            if (type == typeof(double))
            {
                return(reader.GetDouble());
            }

            if (type == typeof(float))
            {
                return(reader.GetFloat());
            }

            if (type == typeof(int))
            {
                return(reader.GetPackedInt());
            }

            if (type == typeof(long))
            {
                return(reader.GetPackedLong());
            }

            if (type == typeof(sbyte))
            {
                return(reader.GetSByte());
            }

            if (type == typeof(short))
            {
                return(reader.GetPackedShort());
            }

            if (type == typeof(string))
            {
                return(reader.GetString());
            }

            if (type == typeof(uint))
            {
                return(reader.GetPackedUInt());
            }

            if (type == typeof(ulong))
            {
                return(reader.GetPackedULong());
            }

            if (type == typeof(ushort))
            {
                return(reader.GetPackedUShort());
            }
            #endregion

            #region Unity Values
            if (type == typeof(Color))
            {
                return(reader.GetColor());
            }

            if (type == typeof(Quaternion))
            {
                return(reader.GetQuaternion());
            }

            if (type == typeof(Vector2))
            {
                return(reader.GetVector2());
            }

            if (type == typeof(Vector2Int))
            {
                return(reader.GetVector2Int());
            }

            if (type == typeof(Vector3))
            {
                return(reader.GetVector3());
            }

            if (type == typeof(Vector3Int))
            {
                return(reader.GetVector3Int());
            }

            if (type == typeof(Vector4))
            {
                return(reader.GetVector4());
            }
            #endregion

            if (typeof(INetSerializable).IsAssignableFrom(type))
            {
                object instance = Activator.CreateInstance(type);
                (instance as INetSerializable).Deserialize(reader);
                return(instance);
            }

            throw new ArgumentException("NetDataReader cannot read type " + type.Name);
        }
Ejemplo n.º 2
0
        public static object GetValue(this NetDataReader reader, Type type)
        {
            #region Generic Values

            if (type.IsPrimitive)
            {
                if (type == typeof(bool))
                {
                    return(reader.GetBool());
                }

                if (type == typeof(byte))
                {
                    return(reader.GetByte());
                }

                if (type == typeof(ushort))
                {
                    return(reader.GetUShort());
                }

                if (type == typeof(int))
                {
                    return(reader.GetInt());
                }

                if (type == typeof(float))
                {
                    return(reader.GetFloat());
                }

                if (type == typeof(short))
                {
                    return(reader.GetShort());
                }

                if (type == typeof(string))
                {
                    return(reader.GetString());
                }

                if (type == typeof(uint))
                {
                    return(reader.GetUInt());
                }

                if (type == typeof(long))
                {
                    return(reader.GetLong());
                }

                if (type == typeof(char))
                {
                    return(reader.GetChar());
                }

                if (type == typeof(double))
                {
                    return(reader.GetDouble());
                }

                if (type == typeof(sbyte))
                {
                    return(reader.GetSByte());
                }


                if (type == typeof(ulong))
                {
                    return(reader.GetULong());
                }

                throw new ArgumentException("NetDataReader cannot read type " + type.Name);
            }
            else if (type.IsArray)
            {
                if (type == typeof(bool[]))
                {
                    return(reader.GetBoolArray());
                }

                if (type == typeof(byte[]))
                {
                    return(reader.GetBytesWithLength());
                }

                if (type == typeof(double[]))
                {
                    return(reader.GetDoubleArray());
                }

                if (type == typeof(float[]))
                {
                    return(reader.GetFloatArray());
                }

                if (type == typeof(int[]))
                {
                    return(reader.GetIntArray());
                }

                if (type == typeof(long[]))
                {
                    return(reader.GetLongArray());
                }

                if (type == typeof(short[]))
                {
                    return(reader.GetShortArray());
                }

                if (type == typeof(uint[]))
                {
                    return(reader.GetUIntArray());
                }

                if (type == typeof(ulong[]))
                {
                    return(reader.GetULongArray());
                }

                if (type == typeof(ushort[]))
                {
                    return(reader.GetUShortArray());
                }

                throw new ArgumentException("NetDataReader cannot read type " + type.Name);
            }
            else if (typeof(INetSerializable).IsAssignableFrom(type))
            {
                object instance = Activator.CreateInstance(type);
                ((INetSerializable)instance).Deserialize(reader);
                return(instance);
            }
            else
            {
                #region Unity Values
#if UNITY_2019
                if (type == typeof(Color))
                {
                    return(reader.GetColor());
                }

                if (type == typeof(Quaternion))
                {
                    return(reader.GetQuaternion());
                }

                if (type == typeof(Vector2))
                {
                    return(reader.GetVector2());
                }

                if (type == typeof(Vector2Int))
                {
                    return(reader.GetVector2Int());
                }

                if (type == typeof(Vector3))
                {
                    return(reader.GetVector3());
                }

                if (type == typeof(Vector3Int))
                {
                    return(reader.GetVector3Int());
                }

                if (type == typeof(Vector4))
                {
                    return(reader.GetVector4());
                }
#endif
                #endregion

                if (type == typeof(string))
                {
                    return(reader.GetString());
                }

                throw new ArgumentException("NetDataReader cannot read type " + type.Name);
            }
            #endregion
        }