コード例 #1
0
ファイル: NetSerializer.cs プロジェクト: ejasan0502/Timeless
        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);
        }
コード例 #2
0
ファイル: NetSerializer.cs プロジェクト: ejasan0502/Timeless
 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);
     }
 }
コード例 #3
0
ファイル: ExampleItems.cs プロジェクト: RainsSoft/MassiveNet
 public static void ItemSerializer(NetStream stream, object instance) {
     IInvItem item = (IInvItem) instance;
     stream.WriteUInt(item.DbId);
     stream.WriteInt(item.Quantity);
     item.WriteAdditional(stream);
 }
コード例 #4
0
ファイル: NetSerializer.cs プロジェクト: ejasan0502/Timeless
        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);
            }
        }