예제 #1
0
        void SerializeObject(TeraBinaryWriter writer, object source)
        {
            var fields  = GetPacketFields <ReflectionPacketFieldInfo>(source.GetType());
            var offsets = new List <(ReflectionPacketFieldInfo, int)>();

            foreach (var info in fields)
            {
                if (info.IsByteArray)
                {
                    offsets.Add((info, writer.Position));
                    writer.WriteUInt16(0);
                    writer.WriteUInt16((ushort)((List <byte>)info.Property
                                                .GetValue(source)).Count);
                }
                else if (info.IsArray)
                {
                    writer.WriteUInt16(
                        (ushort)((IList)info.Property.GetValue(source)).Count);
                    offsets.Add((info, writer.Position));
                    writer.WriteUInt16(0);
                }
                else if (info.IsString)
                {
                    offsets.Add((info, writer.Position));
                    writer.WriteUInt16(0);
                }
                else
                {
                    info.PrimitiveSerializer(writer, info.Property.GetValue(source));
                }
            }

            foreach (var(info, offset) in offsets)
            {
                if (info.IsByteArray)
                {
                    var list = (List <byte>)info.Property.GetValue(source);

                    if (list.Count == 0)
                    {
                        continue;
                    }

                    writer.Seek(offset, (w, op) => w.WriteOffset(op));
                    writer.WriteBytes(list.ToArray());
                }
                else if (info.IsArray)
                {
                    var list = (IList)info.Property.GetValue(source);

                    if (list.Count == 0)
                    {
                        continue;
                    }

                    writer.Seek(offset, (w, op) => w.WriteOffset(op));

                    for (var i = 0; i < list.Count; i++)
                    {
                        var pos = writer.Position;

                        writer.WriteOffset(pos);
                        writer.WriteUInt16(0);

                        SerializeObject(writer, list[i]);

                        if (i != list.Count - 1)
                        {
                            writer.Seek(pos + sizeof(ushort), (w, op) => w.WriteOffset(op));
                        }
                    }
                }
                else
                {
                    writer.Seek(offset, (w, op) => w.WriteOffset(op));
                    writer.WriteString((string)info.Property.GetValue(source) ?? string.Empty);
                }
            }
        }
예제 #2
0
        void SerializeObject(TeraBinaryWriter writer, object source)
        {
            var fields  = GetPacketFields(source.GetType());
            var counts  = new Dictionary <string, int>();
            var offsets = new Dictionary <string, int>();

            foreach (var info in fields.Where(x => x.IsPrimitive))
            {
                if (info.IsCount)
                {
                    counts.Add(info.Property.Name, writer.Position);
                }
                else if (info.IsOffset)
                {
                    offsets.Add(info.Property.Name, writer.Position);
                }

                if (info.IsCount || info.IsOffset)
                {
                    writer.WriteUInt16(0);
                }
                else
                {
                    SerializePrimitive(writer, info.Property.GetValue(source));
                }
            }

            foreach (var info in fields.Where(x => !x.IsPrimitive))
            {
                var type     = info.Property.PropertyType;
                var value    = info.Property.GetValue(source);
                var array    = value as Array;
                var noOffset = array != null && array.Length == 0;

                writer.Seek(offsets[info.Property.Name + OffsetNameSuffix],
                            (w, op) => w.WriteUInt16((ushort)(noOffset ?
                                                              0 : op + PacketHeader.HeaderSize)));

                if (type.IsArray)
                {
                    writer.Seek(counts[info.Property.Name + CountNameSuffix],
                                (w, op) => w.WriteUInt16((ushort)array.Length));

                    var elemType = type.GetElementType();

                    if (elemType.IsEnum)
                    {
                        elemType = elemType.GetEnumUnderlyingType();
                    }

                    var markers = new Stack <int>();

                    for (var i = 0; i < array.Length; i++)
                    {
                        var isLast = i == array.Length - 1;

                        if (!IsByte(elemType))
                        {
                            writer.WriteUInt16((ushort)(writer.Position +
                                                        PacketHeader.HeaderSize));

                            if (!isLast)
                            {
                                markers.Push(writer.Position);
                            }

                            writer.WriteUInt16(0);
                        }

                        var elem = array.GetValue(i);

                        if (elemType.IsPrimitive)
                        {
                            SerializePrimitive(writer, elem);
                        }
                        else
                        {
                            SerializeObject(writer, elem);
                        }

                        if (!IsByte(elemType) && !isLast)
                        {
                            markers.Push(writer.Position);
                        }
                    }

                    if (!IsByte(elemType))
                    {
                        for (var i = 0; i < markers.Count / 2; i++)
                        {
                            var afterElemPos = markers.Pop();
                            var nextPos      = markers.Pop();

                            writer.Seek(nextPos, (w, op) =>
                                        w.WriteUInt16((ushort)(afterElemPos +
                                                               PacketHeader.HeaderSize)));
                        }
                    }
                }
                else
                {
                    writer.WriteString((string)value);
                }
            }
        }