Beispiel #1
0
        private static Type UnpackTypes(TypeRegistry typeRegistry, TypeIdsAndCount input)
        {
            var types = input.TypeIds.Take(input.Count).Select(typeRegistry.GetTypeOrThrow).ToArray();

            // special case: if types has 1 element and it's a generic type definition, return it
            if (types.Length == 1 && types[0].IsGenericTypeDefinition)
            {
                return(types[0]);
            }

            var s = new Stack <Type>();

            foreach (var type in types)
            {
                if (type == typeof(Array))
                {
                    s.Push(s.Pop().MakeArrayType());
                }
                else if (type.IsGenericTypeDefinition)
                {
                    var genericArgs = Util.Generate(type.GetGenericArguments().Length, s.Pop);
                    s.Push(type.MakeGenericType(genericArgs));
                }
                else
                {
                    s.Push(type);
                }
            }
            if (s.Count != 1)
            {
                throw new InvalidStateException($"Expected s.Count == 1 but found {s.Count}.");
            }
            return(s.Pop());
        }
Beispiel #2
0
        public Type ReadType(VoxBinaryReader reader)
        {
            var typeCount = reader.ReadVariableInt();
            var typeIds   = GetTypeIdBuffer(typeCount);

            for (var i = 0; i < typeCount; i++)
            {
                typeIds[i] = reader.ReadVariableInt();
            }
            var key = new TypeIdsAndCount {
                Count = typeCount, TypeIds = typeIds
            };
            var result = typesByTypeIdParts.GetOrAdd(key, cloneKeyFunc, unpackTypesFunc);

            return(result);
        }
Beispiel #3
0
 private static TypeIdsAndCount CloneKey(TypeIdsAndCount arg)
 {
     return(new TypeIdsAndCount {
         Count = arg.Count, TypeIds = arg.TypeIds.Take(arg.Count).ToArray()
     });
 }