예제 #1
0
        public virtual void AddToMessageStream(Stream stream, Type type, object val)
        {
            if (null == val)
            {
                UnityEngine.Debug.Log("NULL VALUE of Type = " + type.ToString());
                return;
            }
            if (type == typeof(bool))
            {
                stream.WriteByte((byte)((bool)val ? 1 : 0));
                return;
            }
            if (type == typeof(byte))
            {
                stream.WriteByte((byte)val);
                return;
            }
            if (type == typeof(Int16))
            {
                var buf = BitConverter.GetBytes((Int16)val);
                stream.Write(buf, 0, buf.Length);
                return;
            }
            if (type == typeof(UInt16))
            {
                var buf = BitConverter.GetBytes((UInt16)val);
                stream.Write(buf, 0, buf.Length);
                return;
            }
            if (type == typeof(Int32))
            {
                var buf = BitConverter.GetBytes((Int32)val);
                stream.Write(buf, 0, buf.Length);
                return;
            }
            if (type == typeof(UInt32))
            {
                var buf = BitConverter.GetBytes((UInt32)val);
                stream.Write(buf, 0, buf.Length);
                return;
            }
            if (type == typeof(Int64))
            {
                var buf = BitConverter.GetBytes((Int64)val);
                stream.Write(buf, 0, buf.Length);
                return;
            }
            if (type == typeof(UInt64))
            {
                var buf = BitConverter.GetBytes((UInt64)val);
                stream.Write(buf, 0, buf.Length);
                return;
            }
            if (type == typeof(float))
            {
                var buf = BitConverter.GetBytes((float)val);
                stream.Write(buf, 0, buf.Length);
                return;
            }
            if (type == typeof(double))
            {
                var buf = BitConverter.GetBytes((double)val);
                stream.Write(buf, 0, buf.Length);
                return;
            }
            if (type == typeof(DateTime))
            {
                var buf = BitConverter.GetBytes((Int32)(((DateTime)val).Ticks / 10000000 - 62135596800)); // 01.01.1970
                stream.Write(buf, 0, buf.Length);
                return;
            }
            if (type == typeof(Byte[]))
            {
                var buf = (byte[])val;
                stream.Write(buf, 0, buf.Length);
                return;
            }
            if (type == typeof(String) || val.GetType() == typeof(string))
            {
                var typed = (string)val;
                if (string.IsNullOrEmpty(typed))
                {
                    stream.WriteByte(0);
                    return;
                }
                var buf    = Encoding.UTF8.GetBytes(typed);
                var buflen = VarInt(buf.Length);
                stream.Write(buflen, 0, buflen.Length);
                stream.Write(buf, 0, buf.Length);
                return;
            }

            ICustomSerializer customSerializer = val as ICustomSerializer;

            if (null != customSerializer)
            {
                customSerializer.Serializer(stream, this);
                return;
            }

            if (type.IsEnum)
            {
                stream.WriteByte((byte)val);
                return;
            }
            if (type.IsArray)
            {
                var typed = (ICollection)val;
                if (typed == null)
                {
                    return;
                }
                var buf = VarInt(typed.Count);
                stream.Write(buf, 0, buf.Length);
                foreach (var value in typed)
                {
                    AddToMessageStream(stream, value.GetType(), value);
                }
                return;
            }
            if (type.IsClass)
            {
                var chType     = val.GetType();
                var properties = GetPropertiesForMessage(chType);
                foreach (var prop in properties)
                {
                    AddToMessageStream(stream, prop, val);
                }
                return;
            }

            throw new NotImplementedException();
        }
예제 #2
0
 public void Serializer(Source sourceData)
 {
     SerializerData = customDataContractSerializer.Serializer <Source, string>(sourceData);
 }