public void WriteAndReadDecimal(decimal testValue)
    {
        writer.Write(testValue);
        var reader = new NetworkReader(writer);

        Assert.AreEqual(testValue, reader.ReadDecimal(), "Writer and Reader have different values for 'decimal' type");
    }
        public void TestWritingAndReading()
        {
            // write all simple types once
            NetworkWriter writer = new NetworkWriter();

            writer.Write((char)1);
            writer.Write((byte)2);
            writer.Write((sbyte)3);
            writer.Write((bool)true);
            writer.Write((short)4);
            writer.Write((ushort)5);
            writer.Write((int)6);
            writer.Write((uint)7);
            writer.Write((long)8L);
            writer.Write((ulong)9L);
            writer.Write((float)10);
            writer.Write((double)11);
            writer.Write((decimal)12);
            writer.Write((string)null);
            writer.Write((string)"");
            writer.Write((string)"13");
            writer.Write(new byte[] { 14, 15 }, 0, 2);                 // just the byte array, no size info etc.
            writer.WriteBytesAndSize((byte[])null);                    // [SyncVar] struct values can have uninitialized byte arrays, null needs to be supported
            writer.WriteBytesAndSize(new byte[] { 17, 18 }, 0, 2);     // buffer, no-offset, count
            writer.WriteBytesAndSize(new byte[] { 19, 20, 21 }, 1, 2); // buffer, offset, count
            writer.WriteBytesAndSize(new byte[] { 22, 23 }, 0, 2);     // size, buffer

            byte[] data = writer.ToArray();


            // read them
            NetworkReader reader = new NetworkReader(writer.ToArray());

            Assert.That(reader.ReadChar(), Is.EqualTo(1));
            Assert.That(reader.ReadByte(), Is.EqualTo(2));
            Assert.That(reader.ReadSByte(), Is.EqualTo(3));
            Assert.That(reader.ReadBoolean(), Is.True);
            Assert.That(reader.ReadInt16(), Is.EqualTo(4));
            Assert.That(reader.ReadUInt16(), Is.EqualTo(5));
            Assert.That(reader.ReadInt32(), Is.EqualTo(6));
            Assert.That(reader.ReadUInt32(), Is.EqualTo(7));
            Assert.That(reader.ReadInt64(), Is.EqualTo(8));
            Assert.That(reader.ReadUInt64(), Is.EqualTo(9));
            Assert.That(reader.ReadSingle(), Is.EqualTo(10));
            Assert.That(reader.ReadDouble(), Is.EqualTo(11));
            Assert.That(reader.ReadDecimal(), Is.EqualTo(12));
            Assert.That(reader.ReadString(), Is.Null); // writing null string should write null in HLAPI Pro ("" in original HLAPI)
            Assert.That(reader.ReadString(), Is.EqualTo(""));
            Assert.That(reader.ReadString(), Is.EqualTo("13"));

            Assert.That(reader.ReadBytes(2), Is.EqualTo(new byte[] { 14, 15 }));

            Assert.That(reader.ReadBytesAndSize(), Is.Null);

            Assert.That(reader.ReadBytesAndSize(), Is.EqualTo(new byte[] { 17, 18 }));

            Assert.That(reader.ReadBytesAndSize(), Is.EqualTo(new byte[] { 20, 21 }));

            Assert.That(reader.ReadBytesAndSize(), Is.EqualTo(new byte[] { 22, 23 }));
        }
Beispiel #3
0
        public void TestDecimals(decimal weird)
        {
            writer.WriteDecimal(weird);
            var     reader      = new NetworkReader(writer.ToArray());
            decimal readDecimal = reader.ReadDecimal();

            Assert.That(readDecimal, Is.EqualTo(weird));
        }
    private static object ReadData(NetworkReader reader)
    {
        object resultObj = null;

        byte dataType = reader.ReadByte();

        if (dataType == BYTE)
        {
            resultObj = reader.ReadByte();
        }
        else if (dataType == SBYTE)
        {
            resultObj = reader.ReadSByte();
        }
        else if (dataType == CHAR)
        {
            resultObj = reader.ReadChar();
        }
        else if (dataType == BOOL)
        {
            resultObj = reader.ReadBoolean();
        }
        else if (dataType == SHORT)
        {
            resultObj = reader.ReadInt16();
        }
        else if (dataType == USHORT)
        {
            resultObj = reader.ReadUInt16();
        }
        else if (dataType == INT)
        {
            resultObj = reader.ReadInt32();
        }
        else if (dataType == UINT)
        {
            resultObj = reader.ReadPackedUInt32();
        }
        else if (dataType == LONG)
        {
            resultObj = reader.ReadInt64();
        }
        else if (dataType == ULONG)
        {
            resultObj = reader.ReadPackedUInt64();
        }
        else if (dataType == FLOAT)
        {
            resultObj = reader.ReadSingle();
        }
        else if (dataType == DOUBLE)
        {
            resultObj = reader.ReadDouble();
        }
        else if (dataType == DECIMAL)
        {
            resultObj = reader.ReadDecimal();
        }
        else if (dataType == STRING)
        {
            resultObj = reader.ReadString();
        }
        else if (dataType == VECTOR2)
        {
            resultObj = reader.ReadVector2();
        }
        else if (dataType == VECTOR3)
        {
            resultObj = reader.ReadVector3();
        }
        else if (dataType == VECTOR4)
        {
            resultObj = reader.ReadVector4();
        }
        else if (dataType == COLOR)
        {
            resultObj = reader.ReadColor();
        }
        else if (dataType == COLOR32)
        {
            resultObj = reader.ReadColor32();
        }
        else if (dataType == QUATERNION)
        {
            resultObj = reader.ReadQuaternion();
        }
        else if (dataType == TRANSFORM)
        {
            resultObj = reader.ReadTransform();
        }
        else if (dataType == RECT)
        {
            resultObj = reader.ReadRect();
        }
        else if (dataType == PLANE)
        {
            resultObj = reader.ReadPlane();
        }
        else if (dataType == GAMEOBJECT)
        {
            resultObj = reader.ReadGameObject();
        }
        else if (dataType == RAY)
        {
            resultObj = reader.ReadRay();
        }
        else if (dataType == MATRIX4X4)
        {
            resultObj = reader.ReadMatrix4x4();
        }
        else if (dataType == NETWORKHASH128)
        {
            resultObj = reader.ReadNetworkHash128();
        }
        else if (dataType == NETWORKIDENTITY)
        {
            resultObj = reader.ReadNetworkIdentity();
        }
        else if (dataType == MESSAGEBASE)
        {
            //resultObj = reader.ReadMessage<MessageBase>();
            resultObj = null;
        }
        else if (dataType == NETWORKINSTANCEID)
        {
            resultObj = reader.ReadNetworkId();
        }
        else if (dataType == NETWORKSCENEID)
        {
            resultObj = reader.ReadSceneId();
        }
        //Debug.Log(resultObj);
        return(resultObj);
    }
Beispiel #5
0
        public void TestWritingAndReading()
        {
            writer.WriteChar((char)1);
            writer.WriteByte(2);
            writer.WriteSByte(3);
            writer.WriteBoolean(true);
            writer.WriteInt16(4);
            writer.WriteUInt16(5);
            writer.WriteInt32(6);
            writer.WriteUInt32(7U);
            writer.WriteInt64(8L);
            writer.WriteUInt64(9UL);
            writer.WriteSingle(10.0F);
            writer.WriteDouble(11.0D);
            writer.WriteDecimal(12);
            writer.WriteString(null);
            writer.WriteString("");
            writer.WriteString("13");
            // just the byte array, no size info etc.
            writer.WriteBytes(new byte[] { 14, 15 }, 0, 2);
            // [SyncVar] struct values can have uninitialized byte arrays, null needs to be supported
            writer.WriteBytesAndSize(null);

            // buffer, no-offset, count
            writer.WriteBytesAndSize(new byte[] { 17, 18 }, 0, 2);
            // buffer, offset, count
            writer.WriteBytesAndSize(new byte[] { 19, 20, 21 }, 1, 2);
            // size, buffer
            writer.WriteBytesAndSize(new byte[] { 22, 23 }, 0, 2);

            // read them
            var reader = new NetworkReader(writer.ToArray());

            Assert.That(reader.ReadChar(), Is.EqualTo(1));
            Assert.That(reader.ReadByte(), Is.EqualTo(2));
            Assert.That(reader.ReadSByte(), Is.EqualTo(3));
            Assert.That(reader.ReadBoolean(), Is.True);
            Assert.That(reader.ReadInt16(), Is.EqualTo(4));
            Assert.That(reader.ReadUInt16(), Is.EqualTo(5));
            Assert.That(reader.ReadInt32(), Is.EqualTo(6));
            Assert.That(reader.ReadUInt32(), Is.EqualTo(7));
            Assert.That(reader.ReadInt64(), Is.EqualTo(8));
            Assert.That(reader.ReadUInt64(), Is.EqualTo(9));
            Assert.That(reader.ReadSingle(), Is.EqualTo(10));
            Assert.That(reader.ReadDouble(), Is.EqualTo(11));
            Assert.That(reader.ReadDecimal(), Is.EqualTo(12));
            // writing null string should write null in Mirage ("" in original HLAPI)
            Assert.That(reader.ReadString(), Is.Null);
            Assert.That(reader.ReadString(), Is.EqualTo(""));
            Assert.That(reader.ReadString(), Is.EqualTo("13"));

            Assert.That(reader.ReadBytes(2), Is.EqualTo(new byte[] { 14, 15 }));

            Assert.That(reader.ReadBytesAndSize(), Is.Null);

            Assert.That(reader.ReadBytesAndSize(), Is.EqualTo(new byte[] { 17, 18 }));

            Assert.That(reader.ReadBytesAndSize(), Is.EqualTo(new byte[] { 20, 21 }));

            Assert.That(reader.ReadBytesAndSize(), Is.EqualTo(new byte[] { 22, 23 }));
        }
Beispiel #6
0
 public override void Deserialize(NetworkReader reader, out decimal value)
 {
     value = reader.ReadDecimal();
 }
Beispiel #7
0
        public bool Deserialize(byte[] _bytes)
        {
            using (MemoryStream ms = new MemoryStream(_bytes))
            {
                using (NetworkReader nr = new NetworkReader(ms))
                {
                    Type type = GetType();

                    FieldInfo[] allFields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

                    PropertyInfo[]          allProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    IEnumerable <FieldInfo> fields        = allFields
                                                            .Where(o => o.GetCustomAttributes(typeof(SyncVar)).Count() > 0)
                                                            .OrderBy(o => o.Name);
                    IEnumerable <PropertyInfo> properties = allProperties
                                                            .Where(o => o.GetCustomAttributes(typeof(SyncVar)).Count() > 0)
                                                            .OrderBy(o => o.Name);

                    Type fieldType;

                    foreach (FieldInfo info in fields)
                    {
                        fieldType = info.FieldType;

                        string typeString = fieldType.Name;
                        object tmpValue   = null;

                        bool switchOnType = true;

                        if (fieldType.IsEnum)
                        {
                            tmpValue     = nr.ReadInt32();
                            switchOnType = false;
                        }
                        if (switchOnType)
                        {
                            switch (typeString)
                            {
                            case "Boolean":
                                tmpValue = nr.ReadBoolean();
                                break;

                            case "Byte":
                                tmpValue = nr.ReadByte();
                                break;

                            case "SByte":
                                tmpValue = nr.ReadSByte();
                                break;

                            case "Int16":
                                tmpValue = nr.ReadInt16();
                                break;

                            case "Int32":
                                tmpValue = nr.ReadInt32();
                                break;

                            case "Int64":
                                tmpValue = nr.ReadInt64();
                                break;

                            case "UInt16":
                                tmpValue = nr.ReadUInt16();
                                break;

                            case "UInt32":
                                tmpValue = nr.ReadUInt32();
                                break;

                            case "UInt64":
                                tmpValue = nr.ReadUInt64();
                                break;

                            case "Single":
                                tmpValue = nr.ReadSingle();
                                break;

                            case "Double":
                                tmpValue = nr.ReadDouble();
                                break;

                            case "Decimal":
                                tmpValue = nr.ReadDecimal();
                                break;

                            case "Char":
                                tmpValue = nr.ReadChar();
                                break;

                            case "String":
                                tmpValue = nr.ReadString();
                                break;

                            case "Byte[]":
                                int length = nr.ReadInt32();
                                tmpValue = nr.ReadBytes(length);
                                break;

                            case "Vector2":
                                tmpValue = nr.ReadVector2();
                                break;

                            case "Vector3":
                                tmpValue = nr.ReadVector3();
                                break;

                            case "Vector4":
                                tmpValue = nr.ReadVector4();
                                break;

                            case "GameObject":
                                tmpValue = nr.ReadGameObject();
                                break;

                            case "Transform":
                                tmpValue = nr.ReadTransform();
                                break;

                            case "Color":
                                tmpValue = nr.ReadColor();
                                break;

                            case "Color32":
                                tmpValue = nr.ReadColor32();
                                break;

                            default:
                                Debug.LogWarning("Could not serialize field! " + info.Name + "(" + info.FieldType.Name + ")", this);
                                break;
                            }
                        }
                        info.SetValue(this, tmpValue);
                        SyncVar syncVar = info.GetCustomAttribute <SyncVar>();
                        if (syncVar is object && syncVar.Hook is object)
                        {
                            SendMessage(syncVar.Hook, tmpValue, SendMessageOptions.RequireReceiver);
                        }
                    }

                    foreach (PropertyInfo info in properties)
                    {
                        fieldType = info.PropertyType;

                        string typeString = fieldType.Name;
                        object tmpValue   = null;

                        bool switchOnType = true;

                        if (fieldType.IsEnum)
                        {
                            tmpValue     = nr.ReadInt32();
                            switchOnType = false;
                        }
                        if (switchOnType)
                        {
                            switch (typeString)
                            {
                            case "Boolean":
                                tmpValue = nr.ReadBoolean();
                                break;

                            case "Byte":
                                tmpValue = nr.ReadByte();
                                break;

                            case "SByte":
                                tmpValue = nr.ReadSByte();
                                break;

                            case "Int16":
                                tmpValue = nr.ReadInt16();
                                break;

                            case "Int32":
                                tmpValue = nr.ReadInt32();
                                break;

                            case "Int64":
                                tmpValue = nr.ReadInt64();
                                break;

                            case "UInt16":
                                tmpValue = nr.ReadUInt16();
                                break;

                            case "UInt32":
                                tmpValue = nr.ReadUInt32();
                                break;

                            case "UInt64":
                                tmpValue = nr.ReadUInt64();
                                break;

                            case "Single":
                                tmpValue = nr.ReadSingle();
                                break;

                            case "Double":
                                tmpValue = nr.ReadDouble();
                                break;

                            case "Decimal":
                                tmpValue = nr.ReadDecimal();
                                break;

                            case "Char":
                                tmpValue = nr.ReadChar();
                                break;

                            case "String":
                                tmpValue = nr.ReadString();
                                break;

                            case "Byte[]":
                                int length = nr.ReadInt32();
                                tmpValue = nr.ReadBytes(length);
                                break;

                            case "Vector2":
                                tmpValue = nr.ReadVector2();
                                break;

                            case "Vector3":
                                tmpValue = nr.ReadVector3();
                                break;

                            case "Vector4":
                                tmpValue = nr.ReadVector4();
                                break;

                            case "GameObject":
                                tmpValue = nr.ReadGameObject();
                                break;

                            case "Transform":
                                tmpValue = nr.ReadTransform();
                                break;

                            case "Color":
                                tmpValue = nr.ReadColor();
                                break;

                            case "Color32":
                                tmpValue = nr.ReadColor32();
                                break;

                            default:
                                Debug.LogWarning("Could not serialize field! " + info.Name + "(" + info.PropertyType.Name + ")", this);
                                break;
                            }
                        }
                        info.SetValue(this, tmpValue);
                        SyncVar syncVar = info.GetCustomAttribute <SyncVar>();
                        if (syncVar is object && syncVar.Hook is object)
                        {
                            SendMessage(syncVar.Hook, tmpValue, SendMessageOptions.RequireReceiver);
                        }
                    }
                }

                return(true);
            }
        }
Beispiel #8
0
        public static object ReadObject(this NetworkReader reader, Type type)
        {
            var @switch = new Dictionary <Type, Func <object> >
            {
                { typeof(Color), () => reader.ReadColor() },
                { typeof(Color32), () => reader.ReadInt32() },
                { typeof(GameObject), reader.ReadGameObject },
                { typeof(Matrix4x4), () => reader.ReadMatrix4x4() },
                { typeof(NetworkHash128), () => reader.ReadNetworkHash128() },
                { typeof(NetworkIdentity), reader.ReadNetworkIdentity },
                { typeof(NetworkInstanceId), () => reader.ReadNetworkId() },
                { typeof(NetworkSceneId), () => reader.ReadSceneId() },
                { typeof(Plane), () => reader.ReadPlane() },
                { typeof(Quaternion), () => reader.ReadQuaternion() },
                { typeof(Ray), () => reader.ReadRay() },
                { typeof(Rect), () => reader.ReadRect() },
                { typeof(Transform), reader.ReadTransform },
                { typeof(Vector2), () => reader.ReadVector2() },
                { typeof(Vector3), () => reader.ReadVector3() },
                { typeof(Vector4), () => reader.ReadVector4() },

                { typeof(bool), () => reader.ReadBoolean() },
                { typeof(byte[]), reader.ReadBytesAndSize },
                { typeof(char), () => reader.ReadChar() },
                { typeof(decimal), () => reader.ReadDecimal() },
                { typeof(double), () => reader.ReadDouble() },
                { typeof(float), () => reader.ReadSingle() },

                { typeof(sbyte), () => reader.ReadSByte() },
                { typeof(string), reader.ReadString },

                { typeof(short), () => reader.ReadInt16() },
                { typeof(int), () => reader.ReadInt32() },
                { typeof(long), () => reader.ReadInt64() },

                { typeof(ushort), () => reader.ReadUInt16() },
                { typeof(uint), () => reader.ReadUInt32() },
                { typeof(ulong), () => reader.ReadUInt64() },
            };

            if ([email protected](type))
            {
                if (typeof(MessageBase).IsAssignableFrom(type))
                {
                    MessageBase instance;
                    var         constructor = type.GetConstructor(Type.EmptyTypes);

                    if (constructor != null)
                    {
                        instance = (MessageBase)constructor.Invoke(null);
                    }
                    else
                    {
                        instance = (MessageBase)FormatterServices.GetUninitializedObject(type);
                    }

                    instance.Deserialize(reader);
                    return(instance);
                }

                throw new ArgumentException($"The type ({type}) passed to ReadObject is not a type supported by NetworkReader.", nameof(type));
            }

            return(@switch[type]());
        }