public AssemblyTypeInfoGenerator(string assembly, IAssemblyResolver resolver)
        {
            this.classes_     = new List <ClassInfo>();
            this.typeResolver = new Unity.SerializationLogic.TypeResolver(null);
            ReaderParameters parameters = new ReaderParameters {
                AssemblyResolver = resolver
            };

            this.assembly_ = AssemblyDefinition.ReadAssembly(assembly, parameters);
        }
Beispiel #2
0
 public void Add(GenericInstanceMethod genericInstanceMethod)
 {
     this.Add(TypeResolver.ElementTypeFor(genericInstanceMethod).FullName, genericInstanceMethod);
 }
Beispiel #3
0
 public void Add(GenericInstanceType genericInstanceType)
 {
     this.Add(TypeResolver.ElementTypeFor(genericInstanceType).FullName, genericInstanceType);
 }
Beispiel #4
0
        private static bool CanFieldContainUnityEngineObjectReference(TypeReference typeReference, FieldDefinition t, TypeResolver typeResolver)
        {
            if (typeResolver.Resolve(t.FieldType) == typeReference)
            {
                return(false);
            }

            if (!WillUnitySerialize(t, typeResolver))
            {
                return(false);
            }

            if (UnityEngineTypePredicates.IsUnityEngineValueType(typeReference))
            {
                return(false);
            }

            return(true);
        }
Beispiel #5
0
        private static IEnumerable <KeyValuePair <FieldDefinition, TypeResolver> > AllFieldsFor(TypeDefinition definition, TypeResolver typeResolver)
        {
            var baseType = definition.BaseType;

            if (baseType != null)
            {
                var genericBaseInstanceType = baseType as GenericInstanceType;
                if (genericBaseInstanceType != null)
                {
                    typeResolver.Add(genericBaseInstanceType);
                }
                foreach (var kv in AllFieldsFor(baseType.Resolve(), typeResolver))
                {
                    yield return(kv);
                }
                if (genericBaseInstanceType != null)
                {
                    typeResolver.Remove(genericBaseInstanceType);
                }
            }

            foreach (var fieldDefinition in definition.Fields)
            {
                yield return(new KeyValuePair <FieldDefinition, TypeResolver>(fieldDefinition, typeResolver));
            }
        }
Beispiel #6
0
 private static bool HasFieldsThatCanContainUnityEngineObjectReferences(TypeDefinition definition, TypeResolver typeResolver)
 {
     return(AllFieldsFor(definition, typeResolver).Where(kv => kv.Value.Resolve(kv.Key.FieldType).Resolve() != definition).Any(kv => CanFieldContainUnityEngineObjectReference(definition, kv.Key, kv.Value)));
 }
Beispiel #7
0
        public static bool ShouldFieldBePPtrRemapped(FieldDefinition fieldDefinition, TypeResolver typeResolver)
        {
            if (!WillUnitySerialize(fieldDefinition, typeResolver))
            {
                return(false);
            }

            return(CanTypeContainUnityEngineObjectReference(typeResolver.Resolve(fieldDefinition.FieldType)));
        }
Beispiel #8
0
        public static bool WillUnitySerialize(FieldDefinition fieldDefinition, TypeResolver typeResolver)
        {
            if (fieldDefinition == null)
            {
                return(false);
            }

            //skip static, const and NotSerialized fields before even checking the type
            if (fieldDefinition.IsStatic || IsConst(fieldDefinition) || fieldDefinition.IsNotSerialized || fieldDefinition.IsInitOnly)
            {
                return(false);
            }

            // The field must have correct visibility/decoration to be serialized.
            if (!fieldDefinition.IsPublic &&
                !ShouldHaveHadAllFieldsPublic(fieldDefinition) &&
                !HasSerializeFieldAttribute(fieldDefinition) &&
                !HasSerializeReferenceAttribute(fieldDefinition))
            {
                return(false);
            }

            // Don't try to resolve types that come from Windows assembly,
            // as serialization weaver will fail to resolve that (due to it being in platform specific SDKs)
            if (ShouldNotTryToResolve(fieldDefinition.FieldType))
            {
                return(false);
            }

            if (IsFixedBuffer(fieldDefinition))
            {
                return(true);
            }

            // Resolving types is more complex and slower than checking their names or attributes,
            // thus keep those checks below
            var typeReference = typeResolver.Resolve(fieldDefinition.FieldType);

            //the type of the field must be serializable in the first place.

            if (typeReference.MetadataType == MetadataType.String)
            {
                return(true);
            }

            if (typeReference.IsValueType)
            {
                return(IsValueTypeSerializable(typeReference));
            }

            if (typeReference is ArrayType || CecilUtils.IsGenericList(typeReference))
            {
                if (!HasSerializeReferenceAttribute(fieldDefinition))
                {
                    return(IsSupportedCollection(typeReference));
                }
            }


            if (!IsReferenceTypeSerializable(typeReference) && !HasSerializeReferenceAttribute(fieldDefinition))
            {
                return(false);
            }

            if (IsDelegate(typeReference))
            {
                return(false);
            }

            return(true);
        }