Ejemplo n.º 1
0
        public object Unpack(MsgPackReader reader)
        {
            if (!reader.Read ())
                throw new FormatException ();

            switch (reader.Type) {
                case TypePrefixes.PositiveFixNum:
                case TypePrefixes.NegativeFixNum:
                case TypePrefixes.Int8:
                case TypePrefixes.Int16:
                case TypePrefixes.Int32:
                    return reader.ValueSigned;
                case TypePrefixes.Int64:
                    return reader.ValueSigned64;
                case TypePrefixes.UInt8:
                case TypePrefixes.UInt16:
                case TypePrefixes.UInt32:
                    return reader.ValueUnsigned;
                case TypePrefixes.UInt64:
                    return reader.ValueUnsigned64;
                case TypePrefixes.True:
                    return true;
                case TypePrefixes.False:
                    return false;
                case TypePrefixes.Float:
                    return reader.ValueFloat;
                case TypePrefixes.Double:
                    return reader.ValueDouble;
                case TypePrefixes.Nil:
                    return null;
                case TypePrefixes.FixRaw:
                case TypePrefixes.Raw16:
                case TypePrefixes.Raw32:
                    byte[] raw = new byte[reader.Length];
                    reader.ReadValueRaw (raw, 0, raw.Length);
                    return raw;
                case TypePrefixes.FixArray:
                case TypePrefixes.Array16:
                case TypePrefixes.Array32:
                    object[] ary = new object[reader.Length];
                    for (int i = 0; i < ary.Length; i ++)
                        ary[i] = Unpack (reader);
                    return ary;
                case TypePrefixes.FixMap:
                case TypePrefixes.Map16:
                case TypePrefixes.Map32:
                    IDictionary<object, object> dic = new Dictionary<object, object> ((int)reader.Length);
                    int count = (int)reader.Length;
                    for (int i = 0; i < count; i ++) {
                        object k = Unpack (reader);
                        object v = Unpack (reader);
                        dic.Add (k, v);
                    }
                    return dic;
                default:
                    throw new FormatException ();
            }
        }
 static object StringUnpacker(CustomPacker packer, MsgPackReader reader)
 {
     if (!reader.Read())
     {
         throw new FormatException();
     }
     if (reader.Type == TypePrefixes.Nil)
     {
         return(null);
     }
     if (!reader.IsRaw())
     {
         throw new FormatException();
     }
     packer.CheckBufferSize((int)reader.Length);
     reader.ReadValueRaw(packer._buf, 0, (int)reader.Length);
     return(Encoding.UTF8.GetString(packer._buf, 0, (int)reader.Length));
 }
Ejemplo n.º 3
0
        public object Unpack(Type type, Stream strm)
        {
            MsgPackReader reader = new MsgPackReader(strm);

            return(Unpack(reader, type));
        }
Ejemplo n.º 4
0
        private object Unpack(MsgPackReader reader, Type t)
        {
            if (t.IsPrimitive)
            {
                if (!reader.Read())
                {
                    throw new FormatException();
                }
                if (t.Equals(typeof(int)) && reader.IsSigned())
                {
                    return(reader.ValueSigned);
                }
                if (t.Equals(typeof(uint)) && reader.IsUnsigned())
                {
                    return(reader.ValueUnsigned);
                }
                if (t.Equals(typeof(float)) && reader.Type == TypePrefixes.Float)
                {
                    return(reader.ValueFloat);
                }
                if (t.Equals(typeof(double)) && reader.Type == TypePrefixes.Double)
                {
                    return(reader.ValueDouble);
                }
                if (t.Equals(typeof(long)))
                {
                    if (reader.IsSigned64())
                    {
                        return(reader.ValueSigned64);
                    }
                    if (reader.IsSigned())
                    {
                        return((long)reader.ValueSigned);
                    }
                }
                else
                {
                    if (!t.Equals(typeof(ulong)))
                    {
                        if (t.Equals(typeof(bool)) && reader.IsBoolean())
                        {
                            return(reader.Type == TypePrefixes.True);
                        }
                        if (t.Equals(typeof(byte)) && reader.IsUnsigned())
                        {
                            return((byte)reader.ValueUnsigned);
                        }
                        if (t.Equals(typeof(sbyte)) && reader.IsSigned())
                        {
                            return((sbyte)reader.ValueSigned);
                        }
                        if (t.Equals(typeof(short)) && reader.IsSigned())
                        {
                            return((short)reader.ValueSigned);
                        }
                        if (t.Equals(typeof(ushort)) && reader.IsUnsigned())
                        {
                            return((ushort)reader.ValueUnsigned);
                        }
                        if (t.Equals(typeof(char)) && reader.IsUnsigned())
                        {
                            return((char)reader.ValueUnsigned);
                        }
                        throw new NotSupportedException();
                    }
                    if (reader.IsUnsigned64())
                    {
                        return(reader.ValueUnsigned64);
                    }
                    if (reader.IsUnsigned())
                    {
                        return((ulong)reader.ValueUnsigned);
                    }
                }
            }
            if (UnpackerMapping.TryGetValue(t, out UnpackDelegate value))
            {
                return(value(this, reader));
            }
            if (t.IsArray)
            {
                if (!reader.Read() || (!reader.IsArray() && reader.Type != TypePrefixes.Nil))
                {
                    throw new FormatException();
                }
                if (reader.Type == TypePrefixes.Nil)
                {
                    return(null);
                }
                Type  elementType = t.GetElementType();
                Array array       = Array.CreateInstance(elementType, (int)reader.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    array.SetValue(Unpack(reader, elementType), i);
                }
                return(array);
            }
            if (!reader.Read())
            {
                throw new FormatException();
            }
            if (reader.Type == TypePrefixes.Nil)
            {
                return(null);
            }
            if (t.IsInterface)
            {
                if (reader.Type != TypePrefixes.FixArray && reader.Length != 2)
                {
                    throw new FormatException();
                }
                if (!reader.Read() || !reader.IsRaw())
                {
                    throw new FormatException();
                }
                CheckBufferSize((int)reader.Length);
                reader.ReadValueRaw(_buf, 0, (int)reader.Length);
                t = Type.GetType(Encoding.UTF8.GetString(_buf, 0, (int)reader.Length));
                if (!reader.Read() || reader.Type == TypePrefixes.Nil)
                {
                    throw new FormatException();
                }
            }
            if (!reader.IsMap())
            {
                throw new FormatException();
            }
            object uninitializedObject = FormatterServices.GetUninitializedObject(t);
            ReflectionCacheEntry reflectionCacheEntry = ReflectionCache.Lookup(t);
            int length = (int)reader.Length;

            for (int j = 0; j < length; j++)
            {
                if (!reader.Read() || !reader.IsRaw())
                {
                    throw new FormatException();
                }
                CheckBufferSize((int)reader.Length);
                reader.ReadValueRaw(_buf, 0, (int)reader.Length);
                string @string = Encoding.UTF8.GetString(_buf, 0, (int)reader.Length);
                if (!reflectionCacheEntry.FieldMap.TryGetValue(@string, out FieldInfo value2))
                {
                    throw new FormatException();
                }
                value2.SetValue(uninitializedObject, Unpack(reader, value2.FieldType));
            }
            (uninitializedObject as IDeserializationCallback)?.OnDeserialization(this);
            return(uninitializedObject);
        }
Ejemplo n.º 5
0
        public object Unpack(Stream strm)
        {
            MsgPackReader reader = new MsgPackReader(strm);

            return(Unpack(reader));
        }
Ejemplo n.º 6
0
 private static object XorUIntUnpacker(ObjectPacker packer, MsgPackReader reader)
 {
     return(new XorUInt((uint)packer.Unpack(reader, typeof(uint))));
 }
Ejemplo n.º 7
0
 static object DateTimeOffsetUnpacker(ObjectPacker packer, MsgPackReader reader)
 {
     return(DateTimeOffset.Parse(reader.ReadRawString()));
 }
Ejemplo n.º 8
0
		public object Unpack (Stream strm)
		{
			MsgPackReader reader = new MsgPackReader (strm);
			return Unpack (reader);
		}
Ejemplo n.º 9
0
 object ValueAsPrimitive(MsgPackReader reader, Type type)
 {
     if (type.Equals(typeof(int)) && reader.IsSigned())
     {
         return(reader.ValueSigned);
     }
     if (type.Equals(typeof(int)) && reader.IsUnsigned() && reader.ValueUnsigned <= int.MaxValue)
     {
         return((int)reader.ValueUnsigned);
     }
     else if (type.Equals(typeof(uint)) && reader.IsUnsigned())
     {
         return(reader.ValueUnsigned);
     }
     else if (type.Equals(typeof(float)) && reader.Type == TypePrefixes.Float)
     {
         return(reader.ValueFloat);
     }
     else if (type.Equals(typeof(double)) && reader.Type == TypePrefixes.Double)
     {
         return(reader.ValueDouble);
     }
     else if (type.Equals(typeof(long)))
     {
         if (reader.IsSigned64())
         {
             return(reader.ValueSigned64);
         }
         if (reader.IsSigned())
         {
             return((long)reader.ValueSigned);
         }
     }
     else if (type.Equals(typeof(ulong)))
     {
         if (reader.IsUnsigned64())
         {
             return(reader.ValueUnsigned64);
         }
         if (reader.IsUnsigned())
         {
             return((ulong)reader.ValueUnsigned);
         }
     }
     else if (type.Equals(typeof(bool)) && reader.IsBoolean())
     {
         return(reader.Type == TypePrefixes.True);
     }
     else if (type.Equals(typeof(byte)) && reader.IsUnsigned())
     {
         return((byte)reader.ValueUnsigned);
     }
     else if (type.Equals(typeof(sbyte)) && reader.IsSigned())
     {
         return((sbyte)reader.ValueSigned);
     }
     else if (type.Equals(typeof(short)) && reader.IsSigned())
     {
         return((short)reader.ValueSigned);
     }
     else if (type.Equals(typeof(ushort)) && reader.IsUnsigned())
     {
         return((ushort)reader.ValueUnsigned);
     }
     else if (type.Equals(typeof(char)) && reader.IsUnsigned())
     {
         return((char)reader.ValueUnsigned);
     }
     throw new NotSupportedException(type.FullName + " is not supported");
 }
Ejemplo n.º 10
0
 static object StringUnpacker(ObjectPacker packer, MsgPackReader reader)
 {
     return((object)reader.ReadRawString());
 }
Ejemplo n.º 11
0
        object Unpack(MsgPackReader reader, string name, Type type)
        {
            if (!reader.Read())
            {
                throw GenerateFormatException(type, name);
            }

            if (reader.Type == TypePrefixes.Nil)
            {
                return(null);
            }

            if (type.IsPrimitive)
            {
                return(ValueAsPrimitive(reader, type));
            }

            if (type.IsEnum)
            {
                return(ValueAsPrimitive(reader, typeof(int)));
            }

            Type nullableType = Nullable.GetUnderlyingType(type);

            if (nullableType != null && nullableType.IsPrimitive)
            {
                return(ValueAsPrimitive(reader, nullableType));
            }

            if (type.IsArray)
            {
                if ((!reader.IsArray()))
                {
                    throw GenerateFormatException(type, name);
                }

                Type  elementType = type.GetElementType();
                Array array       = Array.CreateInstance(elementType, (int)reader.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    array.SetValue(Unpack(reader, name, elementType), i);
                }
                return(array);
            }

            if (type.IsInterface)
            {
                if (reader.Type != TypePrefixes.FixArray && reader.Length != 2)
                {
                    throw GenerateFormatException(type, name);
                }
                if (!reader.Read() || !reader.IsRaw())
                {
                    throw GenerateFormatException(type, name);
                }
                var nextType = Type.GetType(reader.ReadRawString());
                return(Unpack(reader, name, nextType));
            }

            if (reader.IsMap())
            {
                object obj = FormatterServices.GetUninitializedObject(type);
                ReflectionCacheEntry entry = ReflectionCache.Lookup(type);
                int members = (int)reader.Length;
                for (int i = 0; i < members; i++)
                {
                    if (!reader.Read() || !reader.IsRaw())
                    {
                        throw GenerateFormatException(type, name);
                    }
                    FieldInfo fieldInfo;
                    string    nextFieldName = reader.ReadRawString();
                    if (!entry.FieldMap.TryGetValue(nextFieldName, out fieldInfo))
                    {
                        reader.Skip();                         // skip to next if field name is missing.
                        continue;
                    }
                    fieldInfo.SetValue(obj, Unpack(reader, nextFieldName, fieldInfo.FieldType));
                }
                return(obj);
            }

            UnpackDelegate unpacker;

            if (UnpackerMapping.TryGetValue(type, out unpacker))
            {
                return(unpacker(this, reader));
            }

            throw GenerateFormatException(type, name);
        }
Ejemplo n.º 12
0
 object Unpack(MsgPackReader reader, Type Type)
 {
     return(Unpack(reader, "<root>", Type));
 }
Ejemplo n.º 13
0
 private static object XorFloatUnpacker(ObjectPacker packer, MsgPackReader reader)
 {
     return(new XorFloat((float)packer.Unpack(reader, typeof(float))));
 }
Ejemplo n.º 14
0
        private object Unpack(MsgPackReader reader)
        {
            if (!reader.Read())
            {
                throw new FormatException();
            }
            switch (reader.Type)
            {
            case TypePrefixes.PositiveFixNum:
            case TypePrefixes.Int8:
            case TypePrefixes.Int16:
            case TypePrefixes.Int32:
            case TypePrefixes.NegativeFixNum:
                return(reader.ValueSigned);

            case TypePrefixes.Int64:
                return(reader.ValueSigned64);

            case TypePrefixes.UInt8:
            case TypePrefixes.UInt16:
            case TypePrefixes.UInt32:
                return(reader.ValueUnsigned);

            case TypePrefixes.UInt64:
                return(reader.ValueUnsigned64);

            case TypePrefixes.True:
                return(true);

            case TypePrefixes.False:
                return(false);

            case TypePrefixes.Float:
                return(reader.ValueFloat);

            case TypePrefixes.Double:
                return(reader.ValueDouble);

            case TypePrefixes.Nil:
                return(null);

            case TypePrefixes.FixRaw:
            case TypePrefixes.Raw8:
            case TypePrefixes.Raw16:
            case TypePrefixes.Raw32:
            {
                byte[] array2 = new byte[reader.Length];
                reader.ReadValueRaw(array2, 0, array2.Length);
                return(array2);
            }

            case TypePrefixes.FixArray:
            case TypePrefixes.Array16:
            case TypePrefixes.Array32:
            {
                object[] array = new object[reader.Length];
                for (int j = 0; j < array.Length; j++)
                {
                    array[j] = Unpack(reader);
                }
                return(array);
            }

            case TypePrefixes.FixMap:
            case TypePrefixes.Map16:
            case TypePrefixes.Map32:
            {
                IDictionary <object, object> dictionary = new Dictionary <object, object>((int)reader.Length);
                int length = (int)reader.Length;
                for (int i = 0; i < length; i++)
                {
                    object key   = Unpack(reader) ?? "null";
                    object value = Unpack(reader);

                    if (key is byte[] ba)
                    {
                        key = Encoding.UTF8.GetString(ba);
                    }
                    if (value is byte[] ba2)
                    {
                        value = Encoding.UTF8.GetString(ba2);
                    }

                    dictionary.Add(key, value);
                }
                return(dictionary);
            }

            default:
                throw new FormatException();
            }
        }
Ejemplo n.º 15
0
        public IEnumerator Unpack(Stream strm)
        {
            MsgPackReader reader = new MsgPackReader(strm);

            return(Unpack(reader));
        }
Ejemplo n.º 16
0
        object Unpack(MsgPackReader reader)
        {
            if (!reader.Read())
            {
                throw new FormatException();
            }

            switch (reader.Type)
            {
            case TypePrefixes.PositiveFixNum:
            case TypePrefixes.NegativeFixNum:
            case TypePrefixes.Int8:
            case TypePrefixes.Int16:
            case TypePrefixes.Int32:
                return(reader.ValueSigned);

            case TypePrefixes.Int64:
                return(reader.ValueSigned64);

            case TypePrefixes.UInt8:
            case TypePrefixes.UInt16:
            case TypePrefixes.UInt32:
                return(reader.ValueUnsigned);

            case TypePrefixes.UInt64:
                return(reader.ValueUnsigned64);

            case TypePrefixes.True:
                return(true);

            case TypePrefixes.False:
                return(false);

            case TypePrefixes.Float:
                return(reader.ValueFloat);

            case TypePrefixes.Double:
                return(reader.ValueDouble);

            case TypePrefixes.Nil:
                return(null);

            case TypePrefixes.FixRaw:
            case TypePrefixes.Raw16:
            case TypePrefixes.Raw32:
                byte[] raw = new byte[reader.Length];
                reader.ReadValueRaw(raw, 0, raw.Length);
                return(raw);

            case TypePrefixes.FixArray:
            case TypePrefixes.Array16:
            case TypePrefixes.Array32:
                object[] ary = new object[reader.Length];
                for (int i = 0; i < ary.Length; i++)
                {
                    ary[i] = Unpack(reader);
                }
                return(ary);

            case TypePrefixes.FixMap:
            case TypePrefixes.Map16:
            case TypePrefixes.Map32:
                IDictionary <object, object> dic = new Dictionary <object, object> ((int)reader.Length);
                int count = (int)reader.Length;
                for (int i = 0; i < count; i++)
                {
                    object k = Unpack(reader);
                    object v = Unpack(reader);
                    dic.Add(k, v);
                }
                return(dic);

            default:
                throw new FormatException();
            }
        }
Ejemplo n.º 17
0
        IEnumerator Unpack(MsgPackReader reader, uint depth = 0)
        {
Retry:
            object obj;
            IEnumerator it;

            it = reader.Read(); while (it.MoveNext())
            {
                yield return(it.Current);
            }
            ;
            //Debug.Log("type = " + reader.Type);
            switch (reader.Type)
            {
            case TypePrefixes.PositiveFixNum:
            case TypePrefixes.NegativeFixNum:
            case TypePrefixes.Int32:
                obj = reader.ValueSigned;
                break;

            case TypePrefixes.Int8:
                obj = (sbyte)reader.ValueSigned;
                break;

            case TypePrefixes.Int16:
                obj = (Int16)reader.ValueSigned;
                break;

            case TypePrefixes.Int64:
                obj = reader.ValueSigned64;
                break;

            case TypePrefixes.UInt8:
                obj = (byte)reader.ValueUnsigned;
                break;

            case TypePrefixes.UInt16:
                obj = (UInt16)reader.ValueUnsigned;
                break;

            case TypePrefixes.UInt32:
                obj = reader.ValueUnsigned;
                break;

            case TypePrefixes.UInt64:
                obj = reader.ValueUnsigned64;
                break;

            case TypePrefixes.True:
                obj = true;
                break;

            case TypePrefixes.False:
                obj = false;
                break;

            case TypePrefixes.Float:
                obj = reader.ValueFloat;
                break;

            case TypePrefixes.Double:
                obj = reader.ValueDouble;
                break;

            case TypePrefixes.Nil:
                obj = null;
                break;

            case TypePrefixes.FixStr:
            case TypePrefixes.Str8:
            case TypePrefixes.Str16:
            case TypePrefixes.Str32:
                byte[] str = new byte[reader.Length];
                it = reader.ReadStream(str, reader.Length); while (it.MoveNext())
                {
                    yield return(it.Current);
                }
                ;
                obj = (object)reader.StringifyBytes(str);
                break;

            case TypePrefixes.Bin8:
            case TypePrefixes.Bin16:
            case TypePrefixes.Bin32:
                byte[] tmp = new byte[reader.Length];
                it = reader.ReadStream(tmp, reader.Length); while (it.MoveNext())
                {
                    yield return(it.Current);
                }
                ;
                obj = (object)tmp;
                break;

            case TypePrefixes.FixArray:
            case TypePrefixes.Array16:
            case TypePrefixes.Array32:
                object[] ary = new object[reader.Length];
                for (int i = 0; i < ary.Length; i++)
                {
                    it = Unpack(reader, depth + 1); while (it.MoveNext())
                    {
                        yield return(it.Current);
                    }
                    ;
                    ary[i] = _stack.Pop();
                }
                obj = (object)ary;
                break;

            case TypePrefixes.FixMap:
            case TypePrefixes.Map16:
            case TypePrefixes.Map32:
                Dictionary <object, object> dic = new Dictionary <object, object> ((int)reader.Length);
                int    count = (int)reader.Length;
                object k, v;
                for (int i = 0; i < count; i++)
                {
                    it = Unpack(reader, depth + 1); while (it.MoveNext())
                    {
                        yield return(it.Current);
                    }
                    ;
                    k  = _stack.Pop();
                    it = Unpack(reader, depth + 1); while (it.MoveNext())
                    {
                        yield return(it.Current);
                    }
                    ;
                    v = _stack.Pop();
                    dic.Add(k, v);
                }
                obj = (object)dic;
                break;

            case TypePrefixes.Ext8:
            case TypePrefixes.Ext16:
            case TypePrefixes.Ext32:
                byte[] et   = new byte[1];
                byte[] ftmp = new byte[4];
                it = reader.ReadExtType(et); while (it.MoveNext())
                {
                    yield return(it.Current);
                }
                ;
                switch (et[0])
                {
                case 0x51:
                    if (reader.Length == 16)
                    {
                        float a, b, c, d;
                        it = reader.ReadStream(ftmp, 4); while (it.MoveNext())
                        {
                            yield return(it.Current);
                        }
                        ;
                        a  = reader.ReadSingle(ftmp);
                        it = reader.ReadStream(ftmp, 4); while (it.MoveNext())
                        {
                            yield return(it.Current);
                        }
                        ;
                        b  = reader.ReadSingle(ftmp);
                        it = reader.ReadStream(ftmp, 4); while (it.MoveNext())
                        {
                            yield return(it.Current);
                        }
                        ;
                        c  = reader.ReadSingle(ftmp);
                        it = reader.ReadStream(ftmp, 4); while (it.MoveNext())
                        {
                            yield return(it.Current);
                        }
                        ;
                        d   = reader.ReadSingle(ftmp);
                        obj = (object)new UnityEngine.Quaternion(b, c, d, a);
                    }
                    break;

                case 0x56:
                    if (reader.Length == 12)
                    {
                        float a, b, c;
                        it = reader.ReadStream(ftmp, 4); while (it.MoveNext())
                        {
                            yield return(it.Current);
                        }
                        ;
                        a  = reader.ReadSingle(ftmp);
                        it = reader.ReadStream(ftmp, 4); while (it.MoveNext())
                        {
                            yield return(it.Current);
                        }
                        ;
                        b  = reader.ReadSingle(ftmp);
                        it = reader.ReadStream(ftmp, 4); while (it.MoveNext())
                        {
                            yield return(it.Current);
                        }
                        ;
                        c   = reader.ReadSingle(ftmp);
                        obj = (object)new UnityEngine.Vector3(a, b, c);
                    }
                    break;

                case 0x57:
                    if (reader.Length == 8)
                    {
                        float a, b;
                        it = reader.ReadStream(ftmp, 4); while (it.MoveNext())
                        {
                            yield return(it.Current);
                        }
                        ;
                        a  = reader.ReadSingle(ftmp);
                        it = reader.ReadStream(ftmp, 4); while (it.MoveNext())
                        {
                            yield return(it.Current);
                        }
                        ;
                        b   = reader.ReadSingle(ftmp);
                        obj = (object)new UnityEngine.Vector2(a, b);
                    }
                    break;
                }
                var data = new byte[reader.Length];
                it = reader.ReadStream(data, reader.Length); while (it.MoveNext())
                {
                    yield return(it.Current);
                }
                ;
                obj = (object)new Ext(et[0], data);
                break;

            default:
                throw new FormatException();
            }
            if (depth == 0)
            {
                yield return(obj);

                goto Retry;
            }
            else
            {
                _stack.Push(obj);
            }
        }
Ejemplo n.º 18
0
        object Unpack(MsgPackReader reader, Type t)
        {
            if (t.IsPrimitive)
            {
                if (!reader.Read())
                {
                    throw new FormatException();
                }
                if (t.Equals(typeof(int)) && reader.IsSigned())
                {
                    return(reader.ValueSigned);
                }
                else if (t.Equals(typeof(uint)) && reader.IsUnsigned())
                {
                    return(reader.ValueUnsigned);
                }
                else if (t.Equals(typeof(float)) && reader.Type == TypePrefixes.Float)
                {
                    return(reader.ValueFloat);
                }
                else if (t.Equals(typeof(double)) && reader.Type == TypePrefixes.Double)
                {
                    return(reader.ValueDouble);
                }
                else if (t.Equals(typeof(long)))
                {
                    if (reader.IsSigned64())
                    {
                        return(reader.ValueSigned64);
                    }
                    if (reader.IsSigned())
                    {
                        return((long)reader.ValueSigned);
                    }
                }
                else if (t.Equals(typeof(ulong)))
                {
                    if (reader.IsUnsigned64())
                    {
                        return(reader.ValueUnsigned64);
                    }
                    if (reader.IsUnsigned())
                    {
                        return((ulong)reader.ValueUnsigned);
                    }
                }
                else if (t.Equals(typeof(bool)) && reader.IsBoolean())
                {
                    return(reader.Type == TypePrefixes.True);
                }
                else if (t.Equals(typeof(byte)) && reader.IsUnsigned())
                {
                    return((byte)reader.ValueUnsigned);
                }
                else if (t.Equals(typeof(sbyte)) && reader.IsSigned())
                {
                    return((sbyte)reader.ValueSigned);
                }
                else if (t.Equals(typeof(short)) && reader.IsSigned())
                {
                    return((short)reader.ValueSigned);
                }
                else if (t.Equals(typeof(ushort)) && reader.IsUnsigned())
                {
                    return((ushort)reader.ValueUnsigned);
                }
                else if (t.Equals(typeof(char)) && reader.IsUnsigned())
                {
                    return((char)reader.ValueUnsigned);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }

            UnpackDelegate unpacker;

            if (UnpackerMapping.TryGetValue(t, out unpacker))
            {
                return(unpacker(this, reader));
            }

            if (t.IsArray)
            {
                if (!reader.Read() || (!reader.IsArray() && reader.Type != TypePrefixes.Nil))
                {
                    throw new FormatException();
                }
                if (reader.Type == TypePrefixes.Nil)
                {
                    return(null);
                }
                Type  et  = t.GetElementType();
                Array ary = Array.CreateInstance(et, (int)reader.Length);
                for (int i = 0; i < ary.Length; i++)
                {
                    ary.SetValue(Unpack(reader, et), i);
                }
                return(ary);
            }

            if (!reader.Read())
            {
                throw new FormatException();
            }
            if (reader.Type == TypePrefixes.Nil)
            {
                return(null);
            }
            if (t.IsInterface)
            {
                if (reader.Type != TypePrefixes.FixArray && reader.Length != 2)
                {
                    throw new FormatException();
                }
                if (!reader.Read() || !reader.IsRaw())
                {
                    throw new FormatException();
                }
                CheckBufferSize((int)reader.Length);
                reader.ReadValueRaw(_buf, 0, (int)reader.Length);
                t = Type.GetType(Encoding.UTF8.GetString(_buf, 0, (int)reader.Length));
                if (!reader.Read() || reader.Type == TypePrefixes.Nil)
                {
                    throw new FormatException();
                }
            }
            if (!reader.IsMap())
            {
                throw new FormatException();
            }

            object o = FormatterServices.GetUninitializedObject(t);
            ReflectionCacheEntry entry = ReflectionCache.Lookup(t);
            int members = (int)reader.Length;

            for (int i = 0; i < members; i++)
            {
                if (!reader.Read() || !reader.IsRaw())
                {
                    throw new FormatException();
                }
                CheckBufferSize((int)reader.Length);
                reader.ReadValueRaw(_buf, 0, (int)reader.Length);
                string    name = Encoding.UTF8.GetString(_buf, 0, (int)reader.Length);
                FieldInfo f;
                if (!entry.FieldMap.TryGetValue(name, out f))
                {
                    throw new FormatException();
                }
                f.SetValue(o, Unpack(reader, f.FieldType));
            }

            IDeserializationCallback callback = o as IDeserializationCallback;

            if (callback != null)
            {
                callback.OnDeserialization(this);
            }
            return(o);
        }
Ejemplo n.º 19
0
        object Unpack(MsgPackReader reader, Type t)
        {
            if (t.IsPrimitive)
            {
                if (!reader.Read())
                {
                    throw new FormatException();
                }
                if (t.Equals(typeof(int)) && reader.IsSigned())
                {
                    return(reader.ValueSigned);
                }
                else if (t.Equals(typeof(uint)) && reader.IsUnsigned())
                {
                    return(reader.ValueUnsigned);
                }
                else if (t.Equals(typeof(float)) && reader.Type == TypePrefixes.Float)
                {
                    return(reader.ValueFloat);
                }
                else if (t.Equals(typeof(double)) && reader.Type == TypePrefixes.Double)
                {
                    return(reader.ValueDouble);
                }
                else if (t.Equals(typeof(long)))
                {
                    if (reader.IsSigned64())
                    {
                        return(reader.ValueSigned64);
                    }
                    if (reader.IsSigned())
                    {
                        return((long)reader.ValueSigned);
                    }
                }
                else if (t.Equals(typeof(ulong)))
                {
                    if (reader.IsUnsigned64())
                    {
                        return(reader.ValueUnsigned64);
                    }
                    if (reader.IsUnsigned())
                    {
                        return((ulong)reader.ValueUnsigned);
                    }
                }
                else if (t.Equals(typeof(bool)) && reader.IsBoolean())
                {
                    return(reader.Type == TypePrefixes.True);
                }
                else if (t.Equals(typeof(byte)) && reader.IsUnsigned())
                {
                    return((byte)reader.ValueUnsigned);
                }
                else if (t.Equals(typeof(sbyte)) && reader.IsSigned())
                {
                    return((sbyte)reader.ValueSigned);
                }
                else if (t.Equals(typeof(short)) && reader.IsSigned())
                {
                    return((short)reader.ValueSigned);
                }
                else if (t.Equals(typeof(ushort)) && reader.IsUnsigned())
                {
                    return((ushort)reader.ValueUnsigned);
                }
                else if (t.Equals(typeof(char)) && reader.IsUnsigned())
                {
                    return((char)reader.ValueUnsigned);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }

            UnpackDelegate unpacker;

            if (UnpackerMapping.TryGetValue(t, out unpacker))
            {
                return(unpacker(this, reader));
            }

            if (typeof(Packable).IsAssignableFrom(t))
            {
                Packable p = Activator.CreateInstance(t) as Packable;
                return(p.Unpack(this, reader));
            }

            if (t.IsArray)
            {
                if (!reader.Read() || (!reader.IsArray() && reader.Type != TypePrefixes.Nil))
                {
                    throw new FormatException();
                }
                if (reader.Type == TypePrefixes.Nil)
                {
                    return(null);
                }
                Type  et  = t.GetElementType();
                Array ary = Array.CreateInstance(et, (int)reader.Length);
                for (int i = 0; i < ary.Length; i++)
                {
                    ary.SetValue(Unpack(reader, et), i);
                }
                return(ary);
            }

            // IEnumerable 型の場合
            // 現在は IList, ISet, IDictionary をサポート
            if (typeof(IEnumerable).IsAssignableFrom(t) && t.IsGenericType)
            {
                Type[] generics = t.GetGenericArguments();

                // IList 型の場合
                if (typeof(IList).IsAssignableFrom(t))
                {
                    if (!reader.Read() || (!reader.IsArray() && reader.Type != TypePrefixes.Nil))
                    {
                        throw new FormatException();
                    }
                    if (reader.Type == TypePrefixes.Nil)
                    {
                        return(null);
                    }

                    Type  et    = generics.Single();
                    int   count = (int)reader.Length;
                    Array ary   = Array.CreateInstance(et, count);

                    for (int i = 0; i < count; i++)
                    {
                        ary.SetValue(Unpack(reader, et), i);
                    }
                    return(Activator.CreateInstance(t, new object[] { ary }));
                }

                // ISet 型の場合
                var setType = typeof(ISet <>).MakeGenericType(generics[0]);
                if (setType.IsAssignableFrom(t))
                {
                    if (!reader.Read() || (!reader.IsArray() && reader.Type != TypePrefixes.Nil))
                    {
                        throw new FormatException();
                    }
                    if (reader.Type == TypePrefixes.Nil)
                    {
                        return(null);
                    }

                    Type et    = generics.Single();
                    int  count = (int)reader.Length;

                    Array ary = Array.CreateInstance(et, count);

                    for (int i = 0; i < count; i++)
                    {
                        ary.SetValue(Unpack(reader, et), i);
                    }
                    return(Activator.CreateInstance(t, new object[] { ary }));
                }

                if (typeof(IDictionary).IsAssignableFrom(t))
                {
                    if (!reader.Read() || (!reader.IsArray() && reader.Type != TypePrefixes.Nil))
                    {
                        throw new FormatException();
                    }
                    if (reader.Type == TypePrefixes.Nil)
                    {
                        return(null);
                    }

                    Type kt = generics[0];
                    Type vt = generics[1];

                    IDictionary dict  = (IDictionary)Activator.CreateInstance(t);
                    int         count = (int)reader.Length;

                    for (int i = 0; i < count; i++)
                    {
                        dict[Unpack(reader, kt)] = Unpack(reader, vt);
                    }
                    return(dict);
                }
            }

            if (!reader.Read())
            {
                throw new FormatException();
            }
            if (reader.Type == TypePrefixes.Nil)
            {
                return(null);
            }
            if (t.IsInterface)
            {
                if (reader.Type != TypePrefixes.FixArray && reader.Length != 2)
                {
                    throw new FormatException();
                }
                if (!reader.Read() || !reader.IsRaw())
                {
                    throw new FormatException();
                }
                CheckBufferSize((int)reader.Length);
                reader.ReadValueRaw(_buf, 0, (int)reader.Length);
                t = Type.GetType(Encoding.UTF8.GetString(_buf, 0, (int)reader.Length));
                if (!reader.Read() || reader.Type == TypePrefixes.Nil)
                {
                    throw new FormatException();
                }
            }
            if (!reader.IsMap())
            {
                throw new FormatException();
            }

            object o = FormatterServices.GetUninitializedObject(t);
            ReflectionCacheEntry entry = ReflectionCache.Lookup(t);
            int members = (int)reader.Length;

            for (int i = 0; i < members; i++)
            {
                if (!reader.Read() || !reader.IsRaw())
                {
                    throw new FormatException();
                }
                CheckBufferSize((int)reader.Length);
                reader.ReadValueRaw(_buf, 0, (int)reader.Length);
                string    name = Encoding.UTF8.GetString(_buf, 0, (int)reader.Length);
                FieldInfo f;

                if (!entry.FieldMap.TryGetValue(name, out f))
                {
                    throw new FormatException();
                }
                f.SetValue(o, Unpack(reader, f.FieldType));
            }

            IDeserializationCallback callback = o as IDeserializationCallback;

            if (callback != null)
            {
                callback.OnDeserialization(this);
            }
            return(o);
        }
Ejemplo n.º 20
0
        public object Unpack(MsgPackReader reader)
        {
            //IL_0260: Unknown result type (might be due to invalid IL or missing references)
            //IL_029b: Unknown result type (might be due to invalid IL or missing references)
            //IL_02cb: Unknown result type (might be due to invalid IL or missing references)
            if (reader.Read())
            {
                switch (reader.Type)
                {
                case TypePrefixes.PositiveFixNum:
                case TypePrefixes.Int32:
                case TypePrefixes.NegativeFixNum:
                    return(reader.ValueSigned);

                case TypePrefixes.Int8:
                    return((sbyte)reader.ValueSigned);

                case TypePrefixes.Int16:
                    return((short)reader.ValueSigned);

                case TypePrefixes.Int64:
                    return(reader.ValueSigned64);

                case TypePrefixes.UInt8:
                    return((byte)reader.ValueUnsigned);

                case TypePrefixes.UInt16:
                    return((ushort)reader.ValueUnsigned);

                case TypePrefixes.UInt32:
                    return(reader.ValueUnsigned);

                case TypePrefixes.UInt64:
                    return(reader.ValueUnsigned64);

                case TypePrefixes.True:
                    return(true);

                case TypePrefixes.False:
                    return(false);

                case TypePrefixes.Float:
                    return(reader.ValueFloat);

                case TypePrefixes.Double:
                    return(reader.ValueDouble);

                case TypePrefixes.Nil:
                    return(null);

                case TypePrefixes.FixRaw:
                case TypePrefixes.Raw8:
                case TypePrefixes.Raw16:
                case TypePrefixes.Raw32:
                    return(reader.ReadRawString());

                case TypePrefixes.Bin8:
                case TypePrefixes.Bin16:
                case TypePrefixes.Bin32:
                {
                    byte[] array3 = new byte[reader.Length];
                    reader.ReadValueRaw(array3, 0, array3.Length);
                    return(array3);
                }

                case TypePrefixes.FixArray:
                case TypePrefixes.Array16:
                case TypePrefixes.Array32:
                {
                    object[] array2 = new object[reader.Length];
                    for (int j = 0; j < array2.Length; j++)
                    {
                        array2[j] = Unpack(reader);
                    }
                    return(array2);
                }

                case TypePrefixes.FixMap:
                case TypePrefixes.Map16:
                case TypePrefixes.Map32:
                {
                    IDictionary <object, object> dictionary = new Dictionary <object, object>((int)reader.Length);
                    int length = (int)reader.Length;
                    for (int i = 0; i < length; i++)
                    {
                        object key   = Unpack(reader);
                        object value = Unpack(reader);
                        dictionary.Add(key, value);
                    }
                    return(dictionary);
                }

                case TypePrefixes.Ext8:
                case TypePrefixes.Ext16:
                case TypePrefixes.Ext32:
                {
                    sbyte b = reader.ReadExtType();
                    switch (b)
                    {
                    case 81:
                        if (reader.Length == 16)
                        {
                            float num3 = reader.ReadSingle();
                            float num4 = reader.ReadSingle();
                            float num5 = reader.ReadSingle();
                            float num6 = reader.ReadSingle();
                            return(new Quaternion(num3, num4, num5, num6));
                        }
                        break;

                    case 86:
                        if (reader.Length == 12)
                        {
                            float num7 = reader.ReadSingle();
                            float num8 = reader.ReadSingle();
                            float num9 = reader.ReadSingle();
                            return(new Vector3(num7, num8, num9));
                        }
                        break;

                    case 87:
                        if (reader.Length == 8)
                        {
                            float num  = reader.ReadSingle();
                            float num2 = reader.ReadSingle();
                            return(new Vector2(num, num2));
                        }
                        break;
                    }
                    byte[] array = new byte[reader.Length];
                    reader.ReadValueRaw(array, 0, (int)reader.Length);
                    return(new Ext(b, array));
                }

                default:
                    throw new FormatException();
                }
            }
            throw new FormatException();
        }
Ejemplo n.º 21
0
        private object Unpack(MsgPackReader reader, Type t)
        {
            //IL_0591: Unknown result type (might be due to invalid IL or missing references)
            //IL_0596: Expected O, but got Unknown
            if (t.IsPrimitive)
            {
                if (!reader.Read())
                {
                    throw new FormatException();
                }
                if (t.Equals(typeof(int)) && reader.IsSigned())
                {
                    return(reader.ValueSigned);
                }
                if (t.Equals(typeof(int)) && reader.IsUnsigned())
                {
                    return((int)reader.ValueUnsigned);
                }
                if (t.Equals(typeof(uint)) && reader.IsUnsigned())
                {
                    return(reader.ValueUnsigned);
                }
                if (t.Equals(typeof(float)))
                {
                    if (reader.Type == TypePrefixes.Float)
                    {
                        return(reader.ValueFloat);
                    }
                    if (reader.Type == TypePrefixes.Double)
                    {
                        return((float)reader.ValueDouble);
                    }
                    if (reader.IsUnsigned())
                    {
                        return((float)(double)reader.ValueUnsigned);
                    }
                    if (reader.IsSigned())
                    {
                        return((float)reader.ValueSigned);
                    }
                }
                else
                {
                    if (t.Equals(typeof(double)) && reader.Type == TypePrefixes.Double)
                    {
                        return(reader.ValueDouble);
                    }
                    if (t.Equals(typeof(long)))
                    {
                        if (reader.IsSigned64())
                        {
                            return(reader.ValueSigned64);
                        }
                        if (reader.IsSigned())
                        {
                            return((long)reader.ValueSigned);
                        }
                        if (reader.IsUnsigned64())
                        {
                            return((long)reader.ValueUnsigned64);
                        }
                        if (reader.IsUnsigned())
                        {
                            return((long)reader.ValueUnsigned);
                        }
                    }
                    else
                    {
                        if (!t.Equals(typeof(ulong)))
                        {
                            if (t.Equals(typeof(bool)) && reader.IsBoolean())
                            {
                                return(reader.Type == TypePrefixes.True);
                            }
                            if (t.Equals(typeof(byte)) && reader.IsUnsigned())
                            {
                                return((byte)reader.ValueUnsigned);
                            }
                            if (t.Equals(typeof(sbyte)) && reader.IsSigned())
                            {
                                return((sbyte)reader.ValueSigned);
                            }
                            if (t.Equals(typeof(short)) && reader.IsSigned())
                            {
                                return((short)reader.ValueSigned);
                            }
                            if (t.Equals(typeof(ushort)) && reader.IsUnsigned())
                            {
                                return((ushort)reader.ValueUnsigned);
                            }
                            if (t.Equals(typeof(char)) && reader.IsUnsigned())
                            {
                                return((char)reader.ValueUnsigned);
                            }
                            throw new NotSupportedException();
                        }
                        if (reader.IsUnsigned64())
                        {
                            return(reader.ValueUnsigned64);
                        }
                        if (reader.IsUnsigned())
                        {
                            return((ulong)reader.ValueUnsigned);
                        }
                    }
                }
            }
            if (UnpackerMapping.TryGetValue(t, out UnpackDelegate value))
            {
                return(value(this, reader));
            }
            if (t.IsArray)
            {
                if (!reader.Read() || (!reader.IsArray() && reader.Type != TypePrefixes.Nil))
                {
                    throw new FormatException();
                }
                if (reader.Type == TypePrefixes.Nil)
                {
                    return(null);
                }
                Type  elementType = t.GetElementType();
                Array array       = Array.CreateInstance(elementType, (int)reader.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    array.SetValue(Unpack(reader, elementType), i);
                }
                return(array);
            }
            if (t.IsEnum)
            {
                if (!reader.Read())
                {
                    throw new FormatException();
                }
                if (reader.IsSigned())
                {
                    return(Enum.ToObject(t, reader.ValueSigned));
                }
                if (reader.IsSigned64())
                {
                    return(Enum.ToObject(t, reader.ValueSigned64));
                }
                if (reader.IsUnsigned())
                {
                    return(Enum.ToObject(t, reader.ValueUnsigned));
                }
                if (reader.IsUnsigned64())
                {
                    return(Enum.ToObject(t, reader.ValueUnsigned64));
                }
                if (reader.IsRaw())
                {
                    CheckBufferSize((int)reader.Length);
                    reader.ReadValueRaw(_buf, 0, (int)reader.Length);
                    string @string = Encoding.UTF8.GetString(_buf, 0, (int)reader.Length);
                    return(Enum.Parse(t, @string));
                }
                throw new FormatException();
            }
            if (!reader.Read())
            {
                throw new FormatException();
            }
            if (reader.Type == TypePrefixes.Nil)
            {
                return(null);
            }
            if (t.IsInterface)
            {
                if (reader.Type != TypePrefixes.FixArray && reader.Length != 2)
                {
                    throw new FormatException();
                }
                if (!reader.Read() || !reader.IsRaw())
                {
                    throw new FormatException();
                }
                CheckBufferSize((int)reader.Length);
                reader.ReadValueRaw(_buf, 0, (int)reader.Length);
                t = Type.GetType(Encoding.UTF8.GetString(_buf, 0, (int)reader.Length));
                if (!reader.Read() || reader.Type == TypePrefixes.Nil)
                {
                    throw new FormatException();
                }
            }
            if (!reader.IsMap())
            {
                throw new FormatException();
            }
            object obj = (!typeof(ScriptableObject).IsAssignableFrom(t)) ? FormatterServices.GetUninitializedObject(t) : ((object)ScriptableObject.CreateInstance(t));
            ReflectionCacheEntry reflectionCacheEntry = ReflectionCache.Lookup(t);
            int length = (int)reader.Length;

            for (int j = 0; j < length; j++)
            {
                if (!reader.Read() || !reader.IsRaw())
                {
                    throw new FormatException();
                }
                CheckBufferSize((int)reader.Length);
                reader.ReadValueRaw(_buf, 0, (int)reader.Length);
                string string2 = Encoding.UTF8.GetString(_buf, 0, (int)reader.Length);
                if (!reflectionCacheEntry.FieldMap.TryGetValue(string2, out FieldInfo value2))
                {
                    new BoxingPacker().Unpack(reader);
                }
                else
                {
                    value2.SetValue(obj, Unpack(reader, value2.FieldType));
                }
            }
            (obj as IDeserializationCallback)?.OnDeserialization(this);
            return(obj);
        }