예제 #1
0
 public static object ItemDeserializer(NetStream stream) {
     uint dbId = stream.ReadUInt();
     int quantity = stream.ReadInt();
     IInvItem item;
     if (!Inventory.TryCloneFromDb(dbId, quantity, stream, out item)) {
         Debug.LogError("Failed to deserialize IInvItem. Item with given ID not found in database: " + dbId);
     }
     return item;
 }
예제 #2
0
        internal static NetMessage ReadNetMessage(NetStream stream)
        {
            List <Type> paramTypes;
            ushort      messageId = stream.ReadUShort(11);
            uint        viewId    = 0;

            if (stream.ReadBool())
            {
                viewId = stream.ReadUInt(20);
            }

            if (messageId == (int)Cmd.RequestResponse)
            {
                return(CreateResponseMessage(stream, messageId, viewId));
            }

            if (messageId > 1800)
            {
                if (!stream.Socket.Command.Exists(messageId))
                {
                    NetLog.Error("Cannot deserialize message, Command ID not found: " + messageId);
                    return(null);
                }
                paramTypes = stream.Socket.Command.ParamTypes(messageId);
            }
            else
            {
                if (!stream.Socket.Rpc.Exists(messageId))
                {
                    NetLog.Error("Cannot deserialize message, RPC ID not found: " + messageId);
                    return(null);
                }
                paramTypes = stream.Socket.Rpc.ParamTypes(messageId);
            }

            NetMessage netMessage = NetMessage.Create(messageId, viewId, paramTypes.Count, false);

            if (stream.Socket.Rpc.TakesRequests(messageId))
            {
                return(CreateRequestMessage(stream, netMessage, paramTypes));
            }

            for (int i = 0; i < paramTypes.Count; i++)
            {
                netMessage.Parameters[i] = ReadParam(stream, paramTypes[i]);
            }

            return(netMessage);
        }
예제 #3
0
        private static object ReadParam(NetStream stream, Type type)
        {
            if (type == typeof(bool))
            {
                return(stream.ReadBool());
            }
            if (type == typeof(byte))
            {
                return(stream.ReadByte());
            }
            if (type == typeof(short))
            {
                return(stream.ReadShort());
            }
            if (type == typeof(ushort))
            {
                return(stream.ReadUShort());
            }
            if (type == typeof(int))
            {
                return(stream.ReadInt());
            }
            if (type == typeof(uint))
            {
                return(stream.ReadUInt());
            }
            if (type == typeof(float))
            {
                return(stream.ReadFloat());
            }
            if (type == typeof(long))
            {
                return(stream.ReadLong());
            }
            if (type == typeof(ulong))
            {
                return(stream.ReadULong());
            }
            if (type == typeof(double))
            {
                return(stream.ReadDouble());
            }
            if (type == typeof(string))
            {
                return(stream.ReadString());
            }
            if (type == typeof(Vector2))
            {
                return(stream.ReadVector2());
            }
            if (type == typeof(Vector3))
            {
                return(stream.ReadVector3());
            }
            if (type == typeof(Quaternion))
            {
                return(stream.ReadQuaternion());
            }

            if (type == typeof(bool[]))
            {
                ushort length = stream.ReadUShort();
                bool[] arr    = new bool[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = stream.ReadBool();
                }
                return(arr);
            }
            if (type == typeof(byte[]))
            {
                ushort length = stream.ReadUShort();
                byte[] arr    = new byte[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = stream.ReadByte();
                }
                return(arr);
            }
            if (type == typeof(ushort[]))
            {
                ushort   length = stream.ReadUShort();
                ushort[] arr    = new ushort[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = stream.ReadUShort();
                }
                return(arr);
            }
            if (type == typeof(int[]))
            {
                ushort length = stream.ReadUShort();
                int[]  arr    = new int[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = stream.ReadInt();
                }
                return(arr);
            }
            if (type == typeof(uint[]))
            {
                ushort length = stream.ReadUShort();
                uint[] arr    = new uint[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = stream.ReadUInt();
                }
                return(arr);
            }
            if (type == typeof(float[]))
            {
                ushort  length = stream.ReadUShort();
                float[] arr    = new float[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = stream.ReadFloat();
                }
                return(arr);
            }
            if (type == typeof(string[]))
            {
                ushort   length = stream.ReadUShort();
                string[] arr    = new string[length];
                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = stream.ReadString();
                }
                return(arr);
            }
            if (type == typeof(char[]))
            {
                ushort length = stream.ReadUShort();
                char[] arr    = new char[length];
                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = stream.ReadChar();
                }
                return(arr);
            }
            if (type == typeof(NetMessage))
            {
                return(ReadNetMessage(stream));
            }
            if (type == typeof(NetMessage[]))
            {
                byte         length = stream.ReadByte();
                NetMessage[] arr    = new NetMessage[length];
                for (int i = 0; i < length; i++)
                {
                    arr[i] = ReadNetMessage(stream);
                }
                return(arr);
            }
            if (type == typeof(NetConnection))
            {
                return(stream.Connection);
            }
            if (type == typeof(IPAddress))
            {
                byte[] array = new byte[4];
                stream.ReadByteArray(array);
                return(new IPAddress(array));
            }
            if (type == typeof(IPEndPoint))
            {
                if (!stream.ReadBool())
                {
                    return(null);
                }
                return(new IPEndPoint((IPAddress)ReadParam(stream, typeof(IPAddress)), stream.ReadUShort()));
            }
            if (type == typeof(NetStream))
            {
                return(stream.ReadNetStream());
            }
            if (type == typeof(NetZone))
            {
                return(new NetZone {
                    Id = stream.ReadUInt(),
                    ServerEndpoint =
                        stream.ReadBool()
                            ? (IPEndPoint)ReadParam(stream, typeof(IPEndPoint))
                            : null,
                    Position = stream.ReadVector3(),
                    ViewIdMin = stream.ReadInt(),
                    ViewIdMax = stream.ReadInt()
                });
            }
            if (HasType(type))
            {
                return(Read(stream, type));
            }

            // We don't know how to deserialize this type
            throw new Exception("Deserializer not implemented for type: " + type);
        }