int ITypeSerialize.CalculateSize(object value)
        {
            if (value == null)
            {
                return(0);
            }

            int total = 0;

            for (int i = 0; i < fieldInfos.Count; ++i)
            {
                var fieldInfo = fieldInfos[i];
                var cv        = fieldInfo.GetValue(value);
                if (cv == null)
                {
                    continue;
                }

                var fieldType = fieldInfo.FieldType;
                total += WRStream.ComputeStringSize(fieldInfo.Name);
                int size = MonoSerialize.GetByType(fieldInfo).CalculateSize(cv);
                total += WRStream.ComputeLengthSize(size);
                total += size;
            }

            return(total);
        }
        // 写入流
        public static IStream WriteTo(object obj)
        {
            var stream = new WRStream(512);

            GetByInstance(obj).WriteTo(obj, stream);
            return(stream);
        }
Exemple #3
0
        public static void MergeFrom(object obj, byte[] bytes, List <Object> objs)
        {
            if (bytes == null || bytes.Length == 0)
            {
                return;
            }

            var ts = GetByInstance(obj);

            if (ts == null)
            {
                return;
            }

            try
            {
                WRStream stream = new WRStream(bytes);
                stream.WritePos = bytes.Length;
                var ms = new MonoStream(stream, objs);
                ts.MergeFrom(ref obj, ms);
            }
            catch (System.Exception ex)
            {
                wxb.L.LogException(ex);
            }
        }
Exemple #4
0
        public static IStream CreateReaderStream(System.IO.BinaryReader reader)
        {
            var cnt = (int)(reader.BaseStream.Length - reader.BaseStream.Position);

            byte[] bytes = new byte[cnt];
            reader.Read(bytes, 0, cnt);
            wxb.WRStream stream = new WRStream(bytes);
            stream.WritePos = cnt;
            return(stream);
        }
Exemple #5
0
        int ITypeSerialize.CalculateSize(object value)
        {
            Object[] objs = value as Object[];
            if (objs == null)
            {
                return(0);
            }

            return(WRStream.ComputeLengthSize(objs.Length) + objs.Length * 2);
        }
Exemple #6
0
        int ITypeSerialize.CalculateSize(object value)
        {
            var array = value as IList;

            if (array != null)
            {
                // 长度+各个元素内容
                return(WRStream.ComputeLengthSize(array.Count) + (array.Count * GetElementSize()));
            }
            else
            {
                return(0);
            }
        }
Exemple #7
0
        int ITypeSerialize.CalculateSize(object value)
        {
            var array = value as IList;

            if (array != null)
            {
                int total = WRStream.ComputeLengthSize(array.Count); // 长度

                // 长度+各个元素内容
                for (int i = 0; i < array.Count; ++i)
                {
                    total += WRStream.ComputeStringSize((string)array[i]);
                }
                return(total);
            }

            return(0);
        }
Exemple #8
0
        int ITypeSerialize.CalculateSize(object value)
        {
            if (value == null)
            {
                return(0);
            }

            IList array = value as IList;
            int   total = WRStream.ComputeLengthSize(array.Count);

            // 长度+各个元素内容
            for (int i = 0; i < array.Count; ++i)
            {
                int s = elementTypeSerialize.CalculateSize(array[i]);
                total += WRStream.ComputeLengthSize(s);
                total += s;
            }

            return(total);
        }
 protected override uint Read(WRStream stream)
 {
     return(stream.ReadUInt32());
 }
 protected override void Write(WRStream stream, uint value)
 {
     stream.WriteInt32(value);
 }
 protected abstract T Read(WRStream stream);
 protected override string Read(WRStream stream)
 {
     return(stream.ReadString());
 }
 protected override void Write(WRStream stream, string value)
 {
     stream.WriteString(value);
 }
 protected abstract void Write(WRStream stream, T value);
 protected override char Read(WRStream stream)
 {
     return((char)stream.ReadInt16());
 }
 protected override void Write(WRStream stream, double value)
 {
     stream.WriteDouble(value);
 }
        public void MergeFrom(ref object value, MonoStream ms)
        {
            if (value == null)
            {
                value = IL.Help.Create(type);
            }

            WRStream stream = ms.Stream;

            while (stream.ReadSize != 0)
            {
                var fieldName = stream.ReadString();
                var length    = stream.ReadLength();
                if (length == 0)
                {
                }
                else
                {
                    int endPos = stream.WritePos;
                    stream.WritePos = stream.ReadPos + length;
                    try
                    {
                        var fieldInfo = fieldInfos.Find((field) => { return(fieldName == field.Name); });
                        if (fieldInfo == null)
                        {
                            stream.ReadPos += length;
                        }
                        else
                        {
                            object cv = fieldInfo.GetValue(value);
                            //bool isSet = false;
                            //if (cv == null)
                            //{
                            //    cv = IL.Help.Create(fieldInfo.FieldType);
                            //    //isSet = true;
                            //}

                            MonoSerialize.GetByType(fieldInfo).MergeFrom(ref cv, ms);
                            //if (isSet || !cv.GetType().IsClass || cv.GetType().FullName == "System.String")
                            {
                                fieldInfo.SetValue(value, cv);
                            }
                        }
                    }
                    catch (System.Exception ex)
                    {
                        UnityEngine.Debug.LogException(ex);
                    }
                    finally
                    {
#if UNITY_EDITOR
                        if (stream.ReadSize != 0)
                        {
                            UnityEngine.Debug.LogErrorFormat("type:{0} fieldName:{1} length:{2}", type.Name, fieldName, length);
                        }
#endif
                        stream.WritePos = endPos;
                    }
                }
            }
        }
 protected override float Read(WRStream stream)
 {
     return(stream.ReadFloat());
 }
 protected override void Write(WRStream stream, float value)
 {
     stream.WriteFloat(value);
 }
 protected override ushort Read(WRStream stream)
 {
     return(stream.ReadUInt16());
 }
 protected override void Write(WRStream stream, sbyte value)
 {
     stream.WriteSByte(value);
 }
 protected override ulong Read(WRStream stream)
 {
     return(stream.ReadUInt64());
 }
 protected override sbyte Read(WRStream stream)
 {
     return(stream.ReadSByte());
 }
 protected override int CalculateSize(string value)
 {
     return(WRStream.ComputeStringSize(value));
 }
 protected override double Read(WRStream stream)
 {
     return(stream.ReadDouble());
 }
 protected override void Write(WRStream stream, ushort value)
 {
     stream.WriteInt16(value);
 }
 protected override void Write(WRStream stream, ulong value)
 {
     stream.WriteInt64(value);
 }
Exemple #28
0
 public MonoStream(WRStream Stream, List <Object> objs)
 {
     this.Stream = Stream;
     this.objs   = objs;
 }