private void WriteType(BinaryWriter writer, Type type)
        {
            SerializationTypeInfo serializationTypeInfo = Helper.EncodeSerializationType(type);

            writer.Write((byte)serializationTypeInfo);
            if (type.IsGenericType)
            {
                if (Helper.IsUserDefinedType(serializationTypeInfo))
                {
                    writer.WriteKleiString(type.GetKTypeString());
                }
                Type[] genericArguments = type.GetGenericArguments();
                writer.Write((byte)genericArguments.Length);
                for (int i = 0; i < genericArguments.Length; i++)
                {
                    WriteType(writer, genericArguments[i]);
                }
            }
            else if (Helper.IsArray(serializationTypeInfo))
            {
                Type elementType = type.GetElementType();
                WriteType(writer, elementType);
            }
            else if (type.IsEnum || Helper.IsUserDefinedType(serializationTypeInfo))
            {
                writer.WriteKleiString(type.GetKTypeString());
            }
        }
示例#2
0
        public bool TryGetInfo(Type type, out SerializationTypeInfo info)
        {
            _cacheLock.EnterReadLock();
            try {
                if (_infoByType.TryGetValue(type, out info))
                {
                    return(true);
                }
            } finally {
                _cacheLock.ExitReadLock();
            }

            _cacheLock.EnterWriteLock();
            try {
                EnsureAssembly(type.Assembly);
            } finally {
                _cacheLock.ExitWriteLock();
            }

            if (_infoByType.TryGetValue(type, out info))
            {
                return(true);
            }

            return(false);
        }
示例#3
0
 internal void Subtypes(SerializationTypeRegistry registry, SerializationTypeInfo info)
 {
     GIVEN["a registry with some types"] = ()
                                           => registry = new SerializationTypeRegistry(typeof(SerializationFeature).GetNestedTypes(BindingFlags.NonPublic));
     WHEN["getting the type info"]      = () => info = registry.GetInfo(typeof(ICommandMessage));
     THEN["it contains all subclasses"] = () => info.Subclasses.Should().BeEquivalentTo(
         registry.GetInfo(typeof(UpdateCertificationObjects)),
         registry.GetInfo(typeof(UpdateServices)));
 }
 public ISerializationTypeInfo GetSerializationTypeInfo(Type type)
 {
     SerializationTypeInfo result;
     lock (infos) {
         if (!infos.TryGetValue(type, out result)) {
             result = new SerializationTypeInfo(type, mappingProvider);
             infos.Add(type, result);
         }
     }
     return result;
 }
 public InnerObjectInfo(Type refType, SerializationTypeInfo typeInfo, long streamPos, uint metaSize, int version, int latestVersion)
 {
     RefType = refType;
     TypeInfo = typeInfo;
     Version = version;
     LatestVersion = latestVersion;
     StreamPosition = streamPos;
     MetaSize = metaSize;
     Caption = RefType == TypeInfo.Type
         ? RefType.PrettyName()
         : $"{RefType.PrettyName()} : {TypeInfo.Type.PrettyName()}";
 }
        /// <summary>
        /// Skips a value from the input stream, advancing the read pointer but storing no
        /// actual data.
        /// </summary>
        /// <param name="typeInfo">The type of the element to be skipped.</param>
        /// <param name="reader">The stream containing serialized data.</param>
        protected void SkipValue(SerializationTypeInfo typeInfo, IReader reader)
        {
            int length;

            switch (typeInfo)
            {
            case SerializationTypeInfo.SByte:
            case SerializationTypeInfo.Byte:
            case SerializationTypeInfo.Boolean:
                reader.SkipBytes(1);
                return;

            case SerializationTypeInfo.Int16:
            case SerializationTypeInfo.UInt16:
                reader.SkipBytes(2);
                return;

            case SerializationTypeInfo.Int32:
            case SerializationTypeInfo.UInt32:
            case SerializationTypeInfo.Single:
            case SerializationTypeInfo.Enumeration:
                reader.SkipBytes(4);
                return;

            case SerializationTypeInfo.Int64:
            case SerializationTypeInfo.UInt64:
            case SerializationTypeInfo.Double:
            case SerializationTypeInfo.Vector2I:
            case SerializationTypeInfo.Vector2:
                reader.SkipBytes(8);
                return;

            case SerializationTypeInfo.String:
                length = reader.ReadInt32();
                if (length > 0)
                {
                    reader.SkipBytes(length);
                }
                return;

            case SerializationTypeInfo.Vector3:
                reader.SkipBytes(12);
                return;

            case SerializationTypeInfo.Colour:
                reader.SkipBytes(4);
                return;
            }
            throw new ArgumentException("Unhandled type for skipping: " + typeInfo);
        }
示例#7
0
        private SerializationTypeInfo?Process(Type type)
        {
            if (_infoByType.TryGetValue(type, out SerializationTypeInfo result))
            {
                return(result);
            }

            ContractAttribute?attribute = type.GetCustomAttribute <ContractAttribute>(inherit: false);

            if (attribute != null)
            {
                string?moduleCode = GetModuleCode(type.Assembly);

                if (moduleCode != null)
                {
                    string typeName      = attribute.Name ?? RemoveGenericBackticks(type.Name);
                    string?containerName = GetContractContainerName(type);

                    var name = containerName != null ?
                               new SerializationTypeName(moduleCode, containerName, typeName) :
                               new SerializationTypeName(moduleCode, typeName);

                    ContractTypeInfo contractInfo = new ContractTypeInfo(type, name);

                    // HACK: This seems kind of hacky... Maybe rethink...
                    if (!type.IsGenericTypeDefinition)
                    {
                        Expect.That(_infoByDiscriminator.TryAdd(contractInfo.Discriminator, contractInfo));
                    }

                    result = contractInfo;
                }
                else
                {
                    result = new SerializationTypeInfo(type);
                }

                Expect.That(_infoByType.TryAdd(result.Type, result));

                bool hasBaseType = !type.IsValueType &&
                                   type.BaseType != typeof(object) &&
                                   type.BaseType != null;

                IEnumerable <Type> baseType = hasBaseType ? new[] { type.BaseType ! } : Enumerable.Empty <Type>();
        public static bool IsPOD(SerializationTypeInfo info)
        {
            switch (info)
            {
            case SerializationTypeInfo.SByte:
            case SerializationTypeInfo.Byte:
            case SerializationTypeInfo.Int16:
            case SerializationTypeInfo.UInt16:
            case SerializationTypeInfo.Int32:
            case SerializationTypeInfo.UInt32:
            case SerializationTypeInfo.Int64:
            case SerializationTypeInfo.UInt64:
            case SerializationTypeInfo.Single:
            case SerializationTypeInfo.Double:
                return(true);

            default:
                return(false);
            }
        }
 public static void ClearTypeInfoMask()
 {
     TYPE_INFO_MASK = (SerializationTypeInfo)255;
 }
 public static bool IsUserDefinedType(SerializationTypeInfo type_info)
 {
     return((type_info & SerializationTypeInfo.VALUE_MASK) == SerializationTypeInfo.UserDefined);
 }
示例#11
0
        private TypeInfo ReadType(IReader reader)
        {
            TypeInfo typeInfo = new TypeInfo();
            byte     b        = (byte)(typeInfo.info = (SerializationTypeInfo)reader.ReadByte());
            SerializationTypeInfo serializationTypeInfo = typeInfo.info & SerializationTypeInfo.VALUE_MASK;

            if (!Helper.IsGenericType(typeInfo.info))
            {
                switch (serializationTypeInfo)
                {
                case SerializationTypeInfo.UserDefined:
                case SerializationTypeInfo.Enumeration:
                {
                    string type_name = reader.ReadKleiString();
                    typeInfo.type = Manager.GetType(type_name);
                    break;
                }

                case SerializationTypeInfo.Array:
                    typeInfo.subTypes    = new TypeInfo[1];
                    typeInfo.subTypes[0] = ReadType(reader);
                    if (typeInfo.subTypes[0].type != null)
                    {
                        typeInfo.type = typeInfo.subTypes[0].type.MakeArrayType();
                    }
                    else
                    {
                        typeInfo.type = null;
                    }
                    break;

                case SerializationTypeInfo.SByte:
                    typeInfo.type = typeof(sbyte);
                    break;

                case SerializationTypeInfo.Byte:
                    typeInfo.type = typeof(byte);
                    break;

                case SerializationTypeInfo.Boolean:
                    typeInfo.type = typeof(bool);
                    break;

                case SerializationTypeInfo.Int16:
                    typeInfo.type = typeof(short);
                    break;

                case SerializationTypeInfo.UInt16:
                    typeInfo.type = typeof(ushort);
                    break;

                case SerializationTypeInfo.Int32:
                    typeInfo.type = typeof(int);
                    break;

                case SerializationTypeInfo.UInt32:
                    typeInfo.type = typeof(uint);
                    break;

                case SerializationTypeInfo.Int64:
                    typeInfo.type = typeof(long);
                    break;

                case SerializationTypeInfo.UInt64:
                    typeInfo.type = typeof(ulong);
                    break;

                case SerializationTypeInfo.Single:
                    typeInfo.type = typeof(float);
                    break;

                case SerializationTypeInfo.Double:
                    typeInfo.type = typeof(double);
                    break;

                case SerializationTypeInfo.String:
                    typeInfo.type = typeof(string);
                    break;

                case SerializationTypeInfo.Vector2I:
                    typeInfo.type = typeof(Vector2I);
                    break;

                case SerializationTypeInfo.Vector2:
                    typeInfo.type = typeof(Vector2);
                    break;

                case SerializationTypeInfo.Vector3:
                    typeInfo.type = typeof(Vector3);
                    break;

                case SerializationTypeInfo.Colour:
                    typeInfo.type = typeof(Color);
                    break;

                default:
                    throw new ArgumentException("unknown type");
                }
            }
            else
            {
                Type type = null;
                switch (serializationTypeInfo)
                {
                case SerializationTypeInfo.Dictionary:
                    type = typeof(Dictionary <, >);
                    break;

                case SerializationTypeInfo.HashSet:
                    type = typeof(HashSet <>);
                    break;

                case SerializationTypeInfo.List:
                    type = typeof(List <>);
                    break;

                case SerializationTypeInfo.Pair:
                    type = typeof(KeyValuePair <, >);
                    break;

                case SerializationTypeInfo.Queue:
                    type = typeof(Queue <>);
                    break;

                case SerializationTypeInfo.UserDefined:
                {
                    string type_name2 = reader.ReadKleiString();
                    typeInfo.type = Manager.GetType(type_name2);
                    break;
                }

                default:
                    throw new ArgumentException("unknown type");
                }
                byte   b2    = reader.ReadByte();
                Type[] array = new Type[b2];
                typeInfo.subTypes = new TypeInfo[b2];
                for (int i = 0; i < b2; i++)
                {
                    typeInfo.subTypes[i] = ReadType(reader);
                    array[i]             = typeInfo.subTypes[i].type;
                }
                if (type != null)
                {
                    if (array == null || Array.IndexOf(array, null) != -1)
                    {
                        typeInfo.type = null;
                        return(typeInfo);
                    }
                    typeInfo.type = type.MakeGenericType(array);
                }
                else if (typeInfo.type != null)
                {
                    Type[] genericArguments = typeInfo.type.GetGenericArguments();
                    if (genericArguments.Length != b2)
                    {
                        throw new InvalidOperationException("User defined generic type mismatch");
                    }
                    for (int j = 0; j < b2; j++)
                    {
                        if (array[j] != genericArguments[j])
                        {
                            throw new InvalidOperationException("User defined generic type mismatch");
                        }
                    }
                }
            }
            return(typeInfo);
        }
        public static bool IsArray(SerializationTypeInfo type_info)
        {
            SerializationTypeInfo serializationTypeInfo = type_info & SerializationTypeInfo.VALUE_MASK;

            return(serializationTypeInfo == SerializationTypeInfo.Array);
        }
 private void OnObjectDeserializationStarted(Type refType, SerializationTypeInfo typeInfo, long streamPos, uint metaInfoLen, int version)
 {
     var lastVersion = typeInfo.IsValid ? _container.SerializerStorage.GetSerializer(typeInfo).Version : -1;
     var info = new InnerObjectInfo(refType, typeInfo, streamPos, metaInfoLen, version, lastVersion);
     _activeEntries.Push(info);
 }
 public static bool IsGenericType(SerializationTypeInfo type_info)
 {
     return((type_info & SerializationTypeInfo.IS_GENERIC_TYPE) != SerializationTypeInfo.UserDefined);
 }
 public static void SetTypeInfoMask(SerializationTypeInfo mask)
 {
     TYPE_INFO_MASK = mask;
 }
 public static bool IsValueType(SerializationTypeInfo type_info)
 {
     return((type_info & SerializationTypeInfo.IS_VALUE_TYPE) != SerializationTypeInfo.UserDefined);
 }
        public static SerializationTypeInfo EncodeSerializationType(Type type)
        {
            SerializationTypeInfo serializationTypeInfo = SerializationTypeInfo.UserDefined;

            if (type == typeof(sbyte))
            {
                serializationTypeInfo = SerializationTypeInfo.SByte;
            }
            else if (type == typeof(byte))
            {
                serializationTypeInfo = SerializationTypeInfo.Byte;
            }
            else if (type == typeof(bool))
            {
                serializationTypeInfo = SerializationTypeInfo.Boolean;
            }
            else if (type == typeof(short))
            {
                serializationTypeInfo = SerializationTypeInfo.Int16;
            }
            else if (type == typeof(ushort))
            {
                serializationTypeInfo = SerializationTypeInfo.UInt16;
            }
            else if (type == typeof(int))
            {
                serializationTypeInfo = SerializationTypeInfo.Int32;
            }
            else if (type == typeof(uint))
            {
                serializationTypeInfo = SerializationTypeInfo.UInt32;
            }
            else if (type == typeof(long))
            {
                serializationTypeInfo = SerializationTypeInfo.Int64;
            }
            else if (type == typeof(ulong))
            {
                serializationTypeInfo = SerializationTypeInfo.UInt64;
            }
            else if (type == typeof(float))
            {
                serializationTypeInfo = SerializationTypeInfo.Single;
            }
            else if (type == typeof(double))
            {
                serializationTypeInfo = SerializationTypeInfo.Double;
            }
            else if (type == typeof(string))
            {
                serializationTypeInfo = SerializationTypeInfo.String;
            }
            else if (type == typeof(Vector2I))
            {
                serializationTypeInfo = SerializationTypeInfo.Vector2I;
            }
            else if (type == typeof(Vector2))
            {
                serializationTypeInfo = SerializationTypeInfo.Vector2;
            }
            else if (type == typeof(Vector3))
            {
                serializationTypeInfo = SerializationTypeInfo.Vector3;
            }
            else if (type == typeof(Color))
            {
                serializationTypeInfo = SerializationTypeInfo.Colour;
            }
            else if (typeof(Array).IsAssignableFrom(type))
            {
                serializationTypeInfo = SerializationTypeInfo.Array;
            }
            else if (type.IsEnum)
            {
                serializationTypeInfo = SerializationTypeInfo.Enumeration;
            }
            else if (type.IsGenericType)
            {
                serializationTypeInfo = SerializationTypeInfo.IS_GENERIC_TYPE;
                Type genericTypeDefinition = type.GetGenericTypeDefinition();
                serializationTypeInfo = ((genericTypeDefinition == typeof(List <>)) ? (serializationTypeInfo | SerializationTypeInfo.List) : ((genericTypeDefinition == typeof(Dictionary <, >)) ? (serializationTypeInfo | SerializationTypeInfo.Dictionary) : ((genericTypeDefinition == typeof(HashSet <>)) ? (serializationTypeInfo | SerializationTypeInfo.HashSet) : ((genericTypeDefinition == typeof(KeyValuePair <, >)) ? (serializationTypeInfo | SerializationTypeInfo.Pair) : ((genericTypeDefinition != typeof(Queue <>)) ? (serializationTypeInfo | SerializationTypeInfo.UserDefined) : (serializationTypeInfo | SerializationTypeInfo.Queue))))));
            }
            else
            {
                serializationTypeInfo = SerializationTypeInfo.UserDefined;
                if (type.IsValueType)
                {
                    serializationTypeInfo |= SerializationTypeInfo.IS_VALUE_TYPE;
                }
            }
            return(serializationTypeInfo & TYPE_INFO_MASK);
        }