internal static global::MagicSpritz.IAction Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver formatterResolver, out int readSize) { int startOffset = offset; int typeId = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); if (typeId == -1) { MessagePackBinary.ReadNil(bytes, offset, out readSize); return(null); } offset += readSize; var result = (global::MagicSpritz.IAction)null; switch (typeId) { case 0: result = formatterResolver.GetFormatterWithVerify <global::RM.Hotel.NewGameAction>().Deserialize(bytes, offset, formatterResolver, out readSize); break; case 1: result = formatterResolver.GetFormatterWithVerify <global::RM.Hotel.BuyDecoAction>().Deserialize(bytes, offset, formatterResolver, out readSize); break; case 2: result = formatterResolver.GetFormatterWithVerify <global::RM.Hotel.UpgradeRoomAction>().Deserialize(bytes, offset, formatterResolver, out readSize); break; default: result = null; MessagePackBinary.ReadNil(bytes, offset, out readSize); break; } offset += readSize; readSize = offset - startOffset; return(result); }
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); }
public Nil ReadNil() { Nil value = MessagePackBinary.ReadNil(Buffer, Position, out int readSize); Position += readSize; return(value); }
public override void Read(ref object instance, byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) { InlineFormatter.SetInlineGetter(Getter, instance); var settings = GetSubSettings(); var filePath = settings.DistributedFolder + ".sav"; T value = Serializer.Instance.DeserializeFromFile <T>(filePath, settings); Setter(instance, value); MessagePackBinary.ReadNil(bytes, offset, out readSize); }
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; }
public static object ReadObject(byte[] input, ref int offset) { var type = MessagePackBinary.GetMessagePackType(input, offset); int size; switch (type) { case MessagePackType.Integer: var intValue = MessagePackBinary.ReadInt64(input, offset, out size); offset += size; return(intValue); case MessagePackType.Nil: MessagePackBinary.ReadNil(input, offset, out size); offset += size; return(null); case MessagePackType.Boolean: var boolValue = MessagePackBinary.ReadBoolean(input, offset, out size); offset += size; return(boolValue); case MessagePackType.Float: var doubleValue = MessagePackBinary.ReadDouble(input, offset, out size); offset += size; return(doubleValue); case MessagePackType.String: var textValue = MessagePackBinary.ReadString(input, offset, out size); offset += size; return(textValue); case MessagePackType.Binary: var binaryValue = MessagePackBinary.ReadBytes(input, offset, out size); offset += size; return(binaryValue); case MessagePackType.Array: var argumentCount = ReadArrayLength(input, ref offset, "arguments"); var array = new object[argumentCount]; for (int i = 0; i < argumentCount; i++) { array[i] = ReadObject(input, ref offset); } return(array); case MessagePackType.Map: var propertyCount = MessagePackBinary.ReadMapHeader(input, offset, out size); offset += size; var map = new Dictionary <string, object>(); for (int i = 0; i < propertyCount; i++) { textValue = MessagePackBinary.ReadString(input, offset, out size); offset += size; var value = ReadObject(input, ref offset); map[textValue] = value; } return(map); case MessagePackType.Extension: case MessagePackType.Unknown: default: return(null); } }
public Nil?Deserialize(byte[] bytes, int offset, IFormatterResolver typeResolver, out int readSize) { return(MessagePackBinary.ReadNil(bytes, offset, out readSize)); }
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); } }
public Product Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) { int startOffset = offset; int size = 0; Product p = new Product(); _ = MessagePackBinary.ReadMapHeader(bytes, offset, out size); offset += size; string fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Id = MessagePackBinary.ReadInt64(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Name = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Description = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Seller = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; formatterResolver.GetFormatter <Category>().Deserialize(bytes, offset, formatterResolver, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Rating = formatterResolver.GetFormatter <Rate>().Deserialize(bytes, offset, formatterResolver, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; if (bytes[offset] != 0xc0) { string price = MessagePackBinary.ReadString(bytes, offset, out size); decimal priceResult; if (Decimal.TryParse(price, out priceResult)) { p.Price = priceResult; } ; } else { MessagePackBinary.ReadNil(bytes, offset, out size); } offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Currency = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Created = MessagePackBinary.ReadDateTime(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; if (bytes[offset] != 0xc0) { p.Updated = MessagePackBinary.ReadDateTime(bytes, offset, out size); } else { MessagePackBinary.ReadNil(bytes, offset, out size); } offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Labels = formatterResolver.GetFormatter <ICollection <string> >().Deserialize(bytes, offset, formatterResolver, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; Uri uriStr = formatterResolver.GetFormatter <Uri>().Deserialize(bytes, offset, formatterResolver, out size); p.Location = uriStr; offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Image = MessagePackBinary.ReadBytes(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Details = formatterResolver.GetFormatter <Dictionary <string, string> >().Deserialize(bytes, offset, formatterResolver, out size); offset += size; readSize = offset - startOffset; return(p); }
public Unit Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) { MessagePackBinary.ReadNil(bytes, offset, out readSize); return(null); }