Exemple #1
0
        internal static bool TryWriteMessage(NetStream stream, NetMessage message)
        {
            int pos = stream.Pos;

            if (!stream.CanWrite(32))
            {
                return(false);
            }
            stream.WriteUShort(message.MessageId, 11);
            stream.WriteBool(message.ViewId != 0);
            if (message.ViewId != 0)
            {
                stream.WriteUInt(message.ViewId, 20);
            }
            foreach (object param in message.Parameters)
            {
                if (TryWriteParam(stream, param))
                {
                    continue;
                }
                stream.Pos = pos;
                return(false);
            }
            return(true);
        }
Exemple #2
0
 internal static void WriteNetMessage(NetStream stream, NetMessage message)
 {
     stream.WriteUShort(message.MessageId, 11);
     stream.WriteBool(message.ViewId != 0);
     if (message.ViewId != 0)
     {
         stream.WriteUInt(message.ViewId, 20);
     }
     foreach (object param in message.Parameters)
     {
         WriteParam(stream, param);
     }
 }
Exemple #3
0
        /// <summary> Prepares the outgoing reliable stream: Writes the reliable bit & reliable header,
        /// sets stream parameters, and updates send stats. </summary>
        private NetHeader WriteHeader()
        {
            sendStream.Connection = Connection;
            sendStream.Socket     = Connection.Socket;

            sendStream.WriteBool(true);
            var header = NetHeader.Create(this, NetTime.Milliseconds());

            header.ToStream(sendStream);

            ReceivedSinceLastSend = 0;
            Sent++;

            return(header);
        }
 private void WriteInstantiateData(NetStream stream) {
     stream.WriteBool(set);
     if (!set) return;
     stream.WriteVector3(transform.position);
     ExampleItems.ItemSerializer(stream, item);
 }
Exemple #5
0
 public void WriteAdditional(NetStream stream) {
     stream.WriteBool(Equipped);
 }
Exemple #6
0
 private void WriteProxyData(NetStream stream) {
     stream.WriteBool(Dead);
     stream.WriteFloat(transform.position.x);
     stream.WriteFloat(transform.position.z);
 }
Exemple #7
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);
            }
        }
 void WriteInstantiateData(NetStream stream) {
     stream.WriteBool(Dead);
     stream.WriteString(PlayerName);
     stream.WriteVector3(transform.position);
     inventory.WriteMatchesToStream<MeleeWeapon>(stream, (byte)MeleeWeapon.Flag.Equipped);
 }