Пример #1
0
        public IEnumerable <string> DumpRows()
        {
            if (rawArrayBytes == null)
            {
                yield break;
            }

            byte[] buffer = new byte[10];

            var offset = 0;
            int readSize;

            var length = MessagePackBinary.ReadArrayHeader(rawArrayBytes, 0, out readSize);

            offset += readSize;

            for (int i = 0; i < length; i++)
            {
                readSize = MessagePackBinary.ReadNextBlock(rawArrayBytes, offset);

                MessagePackBinary.EnsureCapacity(ref buffer, 0, readSize);
                Buffer.BlockCopy(rawArrayBytes, offset, buffer, 0, readSize);

                yield return(MessagePackSerializer.ToJson(buffer)); // already decoded so use standard MessagePackSerialzier

                offset += readSize;
            }
        }
Пример #2
0
        public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }

            var startOffset   = offset;
            var elementsCount = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);

            offset += readSize;

            object instance;

            if (MessagePackBinary.IsNil(bytes, offset))
            {
                // reference:
                MessagePackBinary.ReadNil(bytes, offset, out readSize);
                offset += readSize;
                var reference = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
                offset += readSize;

                instance = Serializer.Instance.State.GetInstance(reference);
            }
            else
            {
                // full deserialization:
                var meta = DarkMeta <T> .Value;
                if (!meta.Flags.IsStatic())
                {
                    var typeIndex = (TypeIndex)MessagePackBinary.ReadInt32(bytes, offset, out readSize);
                    offset += readSize;
                    if (typeIndex != meta.TypeIndex)
                    {
                        meta = DarkMeta.Get(typeIndex);
                        if (meta == null)
                        {
                            Logger.Instance.Warning(String.Format("Can't find typeIndex {0}", typeIndex));
                            for (int i = 0; i < elementsCount - 1; i++)
                            {
                                offset += MessagePackBinary.ReadNextBlock(bytes, offset);
                            }
                            readSize = offset - startOffset;
                            return(null);
                        }
                    }
                }

                instance = DeserializeBody(meta.Type, elementsCount - 1, bytes, offset, formatterResolver, out readSize);

                offset += readSize;
                meta.OnDeserialized(instance);
            }

            readSize = offset - startOffset;

            return(instance as T);
        }
Пример #3
0
        public void ReadMembers(SerializationFlags flags, ref object instance, byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize, bool recursive = true)
        {
            var startOffset = offset;

            if (BaseMeta != null && recursive)
            {
                BaseMeta.ReadMembers(flags, ref instance, bytes, offset, formatterResolver, out readSize);
                offset += readSize;
            }

            if (Flags.IsSerializable() && !Flags.IsSkipReading())
            {
                if (MessagePackBinary.IsNil(bytes, offset))
                {
                    offset += 1;
                }
                else
                {
                    var memberCount = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                    offset += readSize;

                    for (int i = 0; i < memberCount; i++)
                    {
                        var formatter = GetFormatter(i, flags);
                        if (formatter != null)
                        {
                            formatter.Read(ref instance, bytes, offset, formatterResolver, out readSize);
                            offset += readSize;
                        }
                        else
                        {
                            if (MessagePackBinary.IsNil(bytes, offset))
                            {
                                MessagePackBinary.ReadNil(bytes, offset, out readSize);
                                offset += readSize;
                            }
                            else
                            {
                                var message = String.Format("unknown serializaion data: {0}:{1}", Type.Name, i);
                                Logger.Instance.Warning(message);
                                offset += MessagePackBinary.ReadNextBlock(bytes, offset);
                            }
                        }
                    }
                }
            }

            readSize = offset - startOffset;
        }
Пример #4
0
        public global::IMessagePackUnion Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }

            var startOffset = offset;

            if (MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize) != 2)
            {
                throw new InvalidOperationException("Invalid Union data was detected. Type:global::IMessagePackUnion");
            }
            offset += readSize;

            var key = MessagePackBinary.ReadInt32(bytes, offset, out readSize);

            offset += readSize;

            if (!this.keyToJumpMap.TryGetValue(key, out key))
            {
                key = -1;
            }

            global::IMessagePackUnion result = null;

            switch (key)
            {
            case 0:
                result  = (global::IMessagePackUnion)formatterResolver.GetFormatterWithVerify <global::PackableARWorldMap>().Deserialize(bytes, offset, formatterResolver, out readSize);
                offset += readSize;
                break;

            case 1:
                result  = (global::IMessagePackUnion)formatterResolver.GetFormatterWithVerify <global::PackableARUserAnchor>().Deserialize(bytes, offset, formatterResolver, out readSize);
                offset += readSize;
                break;

            default:
                offset += MessagePackBinary.ReadNextBlock(bytes, offset);
                break;
            }

            readSize = offset - startOffset;

            return(result);
        }
Пример #5
0
        public DynamicArgumentTuple <T1, T2, T3, T4, T5, T6, T7, T8, T9> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var startOffset = offset;

            var length = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);

            offset += readSize;

            var item1 = default1;
            var item2 = default2;
            var item3 = default3;
            var item4 = default4;
            var item5 = default5;
            var item6 = default6;
            var item7 = default7;
            var item8 = default8;
            var item9 = default9;

            for (var i = 0; i < length; i++)
            {
                switch (i)
                {
                case 0:
                    item1 = formatterResolver.GetFormatterWithVerify <T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 1:
                    item2 = formatterResolver.GetFormatterWithVerify <T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 2:
                    item3 = formatterResolver.GetFormatterWithVerify <T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 3:
                    item4 = formatterResolver.GetFormatterWithVerify <T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 4:
                    item5 = formatterResolver.GetFormatterWithVerify <T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 5:
                    item6 = formatterResolver.GetFormatterWithVerify <T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 6:
                    item7 = formatterResolver.GetFormatterWithVerify <T7>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 7:
                    item8 = formatterResolver.GetFormatterWithVerify <T8>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 8:
                    item9 = formatterResolver.GetFormatterWithVerify <T9>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                default:
                    readSize = MessagePackBinary.ReadNextBlock(bytes, offset);
                    break;
                }

                offset += readSize;
            }

            readSize = offset - startOffset;
            return(new DynamicArgumentTuple <T1, T2, T3, T4, T5, T6, T7, T8, T9>(item1, item2, item3, item4, item5, item6, item7, item8, item9));
        }
Пример #6
0
        public unsafe Dup Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }

            var startOffset = offset;

            var len = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);

            offset += readSize;


            int aBCDEFGH     = 0;
            int aBCDEFGHIJKL = 0;
            int aBCDEFGHIJKO = 0;

            // ---isStringKey

            ulong key;
            ArraySegment <byte> arraySegment;
            byte *p;
            int   rest;

            fixed(byte *buffer = &bytes[0])
            {
                for (int i = 0; i < len; i++)
                {
                    arraySegment = MessagePackBinary.ReadStringSegment(bytes, offset, out readSize);
                    offset      += readSize;

                    p    = buffer + arraySegment.Offset;
                    rest = arraySegment.Count;

                    if (rest == 0)
                    {
                        goto LOOP_END;
                    }

                    key = AutomataKeyGen.GetKey(ref p, ref rest);
                    if (rest == 0)
                    {
                        if (key == 5208208757389214273L)
                        {
                            aBCDEFGH = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
                            goto LOOP_END;
                        }

                        goto READ_NEXT;
                    }

                    if (key == 5208208757389214273L)
                    {
                        key = AutomataKeyGen.GetKey(ref p, ref rest);
                        if (rest == 0)
                        {
                            if (key == 1280002633L)
                            {
                                aBCDEFGHIJKL = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
                                goto LOOP_END;
                            }

                            if (key == 1330334281L)
                            {
                                aBCDEFGHIJKO = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
                                goto LOOP_END;
                            }
                        }
                    }

READ_NEXT:
                    readSize = MessagePackBinary.ReadNextBlock(bytes, offset);

LOOP_END:
                    offset += readSize;
                    continue;
                }
            }

            // --- end

            return(new Dup
            {
                ABCDEFGH = aBCDEFGH,
                ABCDEFGHIJKL = aBCDEFGHIJKL,
                ABCDEFGHIJKO = aBCDEFGHIJKO
            });
        }
        public void Read()
        {
            void Check1 <T, T2>(T data, T2 result, Func <Stream, T2> streamRead)
            {
                const int CheckOffset = 10;

                byte[] bytes = null;
                var    len   = MessagePack.Resolvers.StandardResolver.Instance.GetFormatter <T>().Serialize(ref bytes, CheckOffset, data, MessagePack.Resolvers.StandardResolver.Instance);

                MessagePackBinary.FastResize(ref bytes, CheckOffset + len);


                var ms = new MemoryStream(bytes);

                ms.Position = CheckOffset;

                streamRead(ms).Is(result);
            }

            void Check2 <T>(T data, Func <Stream, T> streamRead)
            {
                const int CheckOffset = 10;

                byte[] bytes = null;
                var    len   = MessagePack.Resolvers.StandardResolver.Instance.GetFormatter <T>().Serialize(ref bytes, CheckOffset, data, MessagePack.Resolvers.StandardResolver.Instance);

                MessagePackBinary.FastResize(ref bytes, CheckOffset + len);


                var ms = new MemoryStream(bytes);

                ms.Position = CheckOffset;

                streamRead(ms).Is(data);
            }

            Check1(new[] { 1, 10, 100, 1000, 10000, short.MaxValue, int.MaxValue }, 7, x => MessagePackBinary.ReadArrayHeader(x));
            Check1(new[] { 1, 10, 100, 1000, 10000, short.MaxValue, int.MaxValue }, (uint)7, x => MessagePackBinary.ReadArrayHeaderRaw(x));
            Check1(Nil.Default, true, x => MessagePackBinary.IsNil((x)));
            Check2(true, x => MessagePackBinary.ReadBoolean(x));
            Check2((byte)100, x => MessagePackBinary.ReadByte(x));
            Check2(new byte[] { 1, 10, 100, 245 }, x => MessagePackBinary.ReadBytes(x));
            Check2('あ', x => MessagePackBinary.ReadChar(x));
            Check2(DateTime.UtcNow, x => MessagePackBinary.ReadDateTime(x));
            Check2(132, x => MessagePackBinary.ReadInt16(x));
            Check2(423, x => MessagePackBinary.ReadInt32(x));
            Check2(64332, x => MessagePackBinary.ReadInt64(x));
            Check2(Nil.Default, x => MessagePackBinary.ReadNil(x));
            Check2(11, x => MessagePackBinary.ReadSByte(x));
            Check2(10.31231f, x => MessagePackBinary.ReadSingle(x));
            Check2("foobar", x => MessagePackBinary.ReadString(x));
            Check2(124, x => MessagePackBinary.ReadUInt16(x));
            Check2((uint)432, x => MessagePackBinary.ReadUInt32(x));
            Check2((ulong)432, x => MessagePackBinary.ReadUInt64(x));


            Check1(new Dictionary <int, int>()
            {
                { 1, 2 }
            }, 1, x => MessagePackBinary.ReadMapHeader(x));
            Check1(new Dictionary <int, int>()
            {
                { 1, 2 }
            }, (uint)1, x => MessagePackBinary.ReadMapHeaderRaw(x));

            {
                var block  = new object[] { 1, new[] { 1, 10, 100 }, 100 };
                var bytes  = MessagePackSerializer.Serialize(block);
                var stream = new MemoryStream(bytes);
                MessagePackBinary.ReadNext(stream);      // array(first)
                MessagePackBinary.ReadNext(stream);      // int
                MessagePackBinary.ReadNextBlock(stream); // skip array
                MessagePackBinary.ReadInt32(stream).Is(100);
            }
            {
                var block = new object[] { 1, new Dictionary <int, int> {
                                               { 1, 10 }, { 111, 200 }
                                           }, 100 };
                var bytes  = MessagePackSerializer.Serialize(block);
                var stream = new MemoryStream(bytes);
                MessagePackBinary.ReadNext(stream);
                MessagePackBinary.ReadNext(stream);
                MessagePackBinary.ReadNextBlock(stream);
                MessagePackBinary.ReadInt32(stream).Is(100);
            }
        }
Пример #8
0
 public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
 {
     readSize = MessagePackBinary.ReadNextBlock(bytes, offset);
     return(default(T));
 }
Пример #9
0
        /// <summary>
        /// Open the database.
        /// </summary>
        public static Database Open(byte[] bytes)
        {
            var offset = 0;
            int readSize;
            var memoryCount = MessagePackBinary.ReadArrayHeader(bytes, 0, out readSize);

            offset += readSize;

            var memories = new KeyValuePair <string, IInternalMemory> [memoryCount];

            for (int i = 0; i < memoryCount; i++)
            {
                var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset += readSize;

                if (len != 2)
                {
                    throw new InvalidOperationException("Invalid MsgPack Binary of Database.");
                }

                var keyName = MessagePackBinary.ReadString(bytes, offset, out readSize);
                offset += readSize;

                // is LZ4 or plain msgpack?
                byte[] rawData;
                var    type = MessagePackBinary.GetMessagePackType(bytes, offset);
                if (type == MessagePackType.Extension)
                {
                    var extensionOffset = offset;
                    var ext             = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
                    if (ext.TypeCode == LZ4MessagePackSerializer.ExtensionTypeCode)
                    {
                        offset += readSize;
                        offset += (int)ext.Length;

                        rawData = new byte[offset - extensionOffset];
                        Buffer.BlockCopy(bytes, extensionOffset, rawData, 0, rawData.Length);
                        goto END;
                    }
                }

                {
                    var beginOffset = offset;
                    var arrayLen    = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                    offset += readSize;
                    for (int j = 0; j < arrayLen; j++)
                    {
                        offset += MessagePackBinary.ReadNextBlock(bytes, offset);
                    }
                    readSize = offset - beginOffset;
                    rawData  = new byte[readSize];
                    Buffer.BlockCopy(bytes, beginOffset, rawData, 0, readSize);
                }

END:
                var memory  = new InternalRawMemory(rawData);
                memories[i] = new KeyValuePair <string, IInternalMemory>(keyName, memory);
            }

            return(new Database(memories));
        }
Пример #10
0
 public void ReadNextBlock()
 {
     Position += MessagePackBinary.ReadNextBlock(Buffer, Position);
 }
        internal static object Deserialize(ObjectSerializationInfo metaInfo, ObjectSerializationInfo.EmittableMember[] readMembers, int[] constructorParameterIndexes, AutomataDictionary mapMemberDictionary, byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var startOffset = offset;

            object[] parameters = null;

            var headerType = MessagePackBinary.GetMessagePackType(bytes, offset);

            if (headerType == MessagePackType.Nil)
            {
                readSize = 1;
                return(null);
            }
            else if (headerType == MessagePackType.Array)
            {
                var arraySize = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset += readSize;

                // ReadValues
                parameters = new object[arraySize];
                for (int i = 0; i < arraySize; i++)
                {
                    var info = readMembers[i];
                    if (info != null)
                    {
                        parameters[i] = MessagePackSerializer.NonGeneric.Deserialize(info.Type, bytes, offset, formatterResolver, out readSize);
                        offset       += readSize;
                    }
                    else
                    {
                        offset += MessagePackBinary.ReadNextBlock(bytes, offset);
                    }
                }
            }
            else if (headerType == MessagePackType.Map)
            {
                var mapSize = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
                offset += readSize;

                // ReadValues
                parameters = new object[mapSize];
                for (int i = 0; i < mapSize; i++)
                {
                    var rawPropName = MessagePackBinary.ReadStringSegment(bytes, offset, out readSize);
                    offset += readSize;

                    int index;
                    if (mapMemberDictionary.TryGetValue(rawPropName.Array, rawPropName.Offset, rawPropName.Count, out index))
                    {
                        var info = readMembers[index];
                        parameters[index] = MessagePackSerializer.NonGeneric.Deserialize(info.Type, bytes, offset, formatterResolver, out readSize);
                        offset           += readSize;
                    }
                    else
                    {
                        offset += MessagePackBinary.ReadNextBlock(bytes, offset);
                    }
                }
            }
            else
            {
                throw new InvalidOperationException("Invalid MessagePackType:" + MessagePackCode.ToFormatName(bytes[offset]));
            }

            // CreateObject
            object result = null;

            if (constructorParameterIndexes.Length == 0)
            {
                result = Activator.CreateInstance(metaInfo.Type);
            }
            else
            {
                var args = new object[constructorParameterIndexes.Length];
                for (int i = 0; i < constructorParameterIndexes.Length; i++)
                {
                    args[i] = parameters[constructorParameterIndexes[i]];
                }

                result = Activator.CreateInstance(metaInfo.Type, args);
            }

            // SetMembers
            for (int i = 0; i < readMembers.Length; i++)
            {
                var info = readMembers[i];
                if (info != null)
                {
                    info.ReflectionStoreValue(result, parameters[i]);
                }
            }

            readSize = offset - startOffset;
            return(result);
        }