Ejemplo n.º 1
0
        internal static void WriteParam(NetStream stream, object param)
        {
            if (param == null)
            {
                // TODO: Explicit nullable params should be properly supported.
                stream.WriteBool(false);
                return;
            }

            // Get the object type so we can compare it:
            Type type = param.GetType();

            // Built-in types:
            if (type == typeof(bool))
            {
                stream.WriteBool((bool)param);
            }
            else if (type == typeof(byte))
            {
                stream.WriteByte((byte)param);
            }
            else if (type == typeof(short))
            {
                stream.WriteShort((short)param);
            }
            else if (type == typeof(ushort))
            {
                stream.WriteUShort((ushort)param);
            }
            else if (type == typeof(int))
            {
                stream.WriteInt((int)param);
            }
            else if (type == typeof(uint))
            {
                stream.WriteUInt((uint)param);
            }
            else if (type == typeof(float))
            {
                stream.WriteFloat((float)param);
            }
            else if (type == typeof(long))
            {
                stream.WriteLong((long)param);
            }
            else if (type == typeof(ulong))
            {
                stream.WriteULong((ulong)param);
            }
            else if (type == typeof(double))
            {
                stream.WriteDouble((double)param);
            }
            else if (type == typeof(string))
            {
                stream.WriteString((string)param);
            }
            else if (type == typeof(Vector2))
            {
                stream.WriteVector2((Vector2)param);
            }
            else if (type == typeof(Vector3))
            {
                stream.WriteVector3((Vector3)param);
            }
            else if (type == typeof(Quaternion))
            {
                stream.WriteQuaternion((Quaternion)param);
            }

            else if (type == typeof(bool[]))
            {
                bool[] arr = (bool[])param;
                stream.WriteUShort((ushort)arr.Length);
                for (int i = 0; i < arr.Length; i++)
                {
                    stream.WriteBool(arr[i]);
                }
            }
            else if (type == typeof(byte[]))
            {
                byte[] arr = (byte[])param;
                stream.WriteUShort((ushort)arr.Length);
                stream.WriteByteArray(arr, arr.Length);
            }
            else if (type == typeof(short[]))
            {
                short[] arr = (short[])param;
                stream.WriteUShort((ushort)arr.Length);
                for (int i = 0; i < arr.Length; i++)
                {
                    stream.WriteShort(arr[i]);
                }
            }
            else if (type == typeof(ushort[]))
            {
                ushort[] arr = (ushort[])param;
                stream.WriteUShort((ushort)arr.Length);
                for (int i = 0; i < arr.Length; i++)
                {
                    stream.WriteUShort(arr[i]);
                }
            }
            else if (type == typeof(int[]))
            {
                int[] arr = (int[])param;
                stream.WriteUShort((ushort)arr.Length);
                for (int i = 0; i < arr.Length; i++)
                {
                    stream.WriteInt(arr[i]);
                }
            }
            else if (type == typeof(uint[]))
            {
                uint[] arr = (uint[])param;
                stream.WriteUShort((ushort)arr.Length);
                for (int i = 0; i < arr.Length; i++)
                {
                    stream.WriteUInt(arr[i]);
                }
            }
            else if (type == typeof(float[]))
            {
                float[] arr = (float[])param;
                stream.WriteUShort((ushort)arr.Length);
                for (int i = 0; i < arr.Length; i++)
                {
                    stream.WriteFloat(arr[i]);
                }
            }
            else if (type == typeof(string[]))
            {
                string[] arr = (string[])param;
                stream.WriteUShort((ushort)arr.Length);
                for (int i = 0; i < arr.Length; i++)
                {
                    stream.WriteString(arr[i]);
                }
            }
            else if (type == typeof(char[]))
            {
                char[] arr = (char[])param;
                stream.WriteUShort((ushort)arr.Length);
                for (int i = 0; i < arr.Length; i++)
                {
                    stream.WriteChar(arr[i]);
                }
            }
            else if (type == typeof(NetMessage))
            {
                WriteNetMessage(stream, (NetMessage)param);
            }
            else if (type == typeof(NetMessage[]))
            {
                NetMessage[] arr = (NetMessage[])param;
                stream.WriteByte((byte)arr.Length);
                for (int i = 0; i < arr.Length; i++)
                {
                    WriteNetMessage(stream, arr[i]);
                }
            }
            else if (type == typeof(IPAddress))
            {
                IPAddress address = (IPAddress)param;
                stream.WriteByteArray(address.GetAddressBytes());
            }
            else if (type == typeof(IPEndPoint))
            {
                IPEndPoint ep = (IPEndPoint)param;
                stream.WriteBool(true); // non-null
                WriteParam(stream, ep.Address);
                stream.WriteUShort((ushort)ep.Port);
            }
            else if (type == typeof(NetZone))
            {
                var zone = (NetZone)param;
                stream.WriteUInt(zone.Id);
                bool serializeEndpoint = zone.ServerEndpoint != null;
                stream.WriteBool(serializeEndpoint);
                if (serializeEndpoint)
                {
                    WriteParam(stream, zone.ServerEndpoint);
                }
                stream.WriteVector3(zone.Position);
                stream.WriteInt(zone.ViewIdMin);
                stream.WriteInt(zone.ViewIdMax);
            }
            else if (type == typeof(NetStream))
            {
                NetStream netStream = (NetStream)param;
                netStream.CopyTo(stream);
            }
            else if (HasType(type))
            {
                Write(stream, type, param);
            }
            else
            {
                NetLog.Error("Failed to serialize, no serializer found: " + type);
                throw new Exception(
                          "Serializer not implemented for type! You must add your own type check and serialization logic to serialize this type: " +
                          type);
            }
        }