private IEnumerable <FieldDefinition> FilteredFields()
        {
            foreach (var f in TypeDef.Fields.Where(WillUnitySerialize))
            {
                if (UnitySerializationLogic.IsSupportedCollection(f.FieldType) || !f.FieldType.IsGenericInstance || UnitySerializationLogic.ShouldImplementIDeserializable(f.FieldType.Resolve()))
                {
                    yield return(f);
                }
            }

            yield break;
        }
 private bool WillSerialize(FieldDefinition field)
 {
     try
     {
         return(UnitySerializationLogic.WillUnitySerialize(field, typeResolver));
     }
     catch (Exception ex)
     {
         UnityEngine.Debug.LogFormat("Field '{0}' from '{1}', exception {2}", field.FullName, field.Module.FileName, ex.Message);
         // If assembly has unknown reference (for ex., see tests VariousPlugins, where Metro plugins are used), skip field
         return(false);
     }
 }
        private bool WillSerialize(FieldDefinition field)
        {
            bool result;

            try
            {
                result = UnitySerializationLogic.WillUnitySerialize(field, this.typeResolver);
            }
            catch (Exception)
            {
                result = false;
            }
            return(result);
        }
Пример #4
0
        private static bool RequiresAlignment(TypeReference typeRef)
        {
            switch (typeRef.MetadataType)
            {
            case MetadataType.Boolean:
            case MetadataType.Char:
            case MetadataType.SByte:
            case MetadataType.Byte:
            case MetadataType.Int16:
            case MetadataType.UInt16:
                return(true);

            default:
                return(UnitySerializationLogic.IsSupportedCollection(typeRef) && RequiresAlignment(CecilUtils.ElementTypeOfCollection(typeRef)));
            }
        }
Пример #5
0
        public List <TypeTreeNode> ConvertToTypeTreeNodes()
        {
            var nodes = new List <TypeTreeNode>();

            var baseTypes    = new Stack <TypeReference>();
            var lastBaseType = TypeDef.BaseType;

            while (!UnitySerializationLogic.IsNonSerialized(lastBaseType))
            {
                var genericInstanceType = lastBaseType as GenericInstanceType;
                if (genericInstanceType != null)
                {
                    TypeResolver.Add(genericInstanceType);
                }
                baseTypes.Push(lastBaseType);
                lastBaseType = lastBaseType.Resolve().BaseType;
            }
            while (baseTypes.Count > 0)
            {
                var typeReference  = baseTypes.Pop();
                var typeDefinition = typeReference.Resolve();
                foreach (var fieldDefinition in typeDefinition.Fields.Where(WillUnitySerialize))
                {
                    if (!IsHiddenByParentClass(baseTypes, fieldDefinition, TypeDef))
                    {
                        nodes.AddRange(ProcessingFieldRef(ResolveGenericFieldReference(fieldDefinition)));
                    }
                }

                var genericInstanceType = typeReference as GenericInstanceType;
                if (genericInstanceType != null)
                {
                    TypeResolver.Remove(genericInstanceType);
                }
            }
            foreach (var field in FilteredFields())
            {
                nodes.AddRange(ProcessingFieldRef(field));
            }

            return(nodes);
        }
Пример #6
0
        private bool WillSerialize(FieldDefinition field)
        {
            bool result;

            try
            {
                result = UnitySerializationLogic.WillUnitySerialize(field, this.typeResolver);
            }
            catch (Exception ex)
            {
                Debug.LogFormat("Field '{0}' from '{1}', exception {2}", new object[]
                {
                    field.get_FullName(),
                    field.get_Module().get_FullyQualifiedName(),
                    ex.Message
                });
                result = false;
            }
            return(result);
        }
Пример #7
0
 private bool WillUnitySerialize(FieldDefinition fieldDefinition)
 {
     try
     {
         var resolvedFieldType = TypeResolver.Resolve(fieldDefinition.FieldType);
         if (UnitySerializationLogic.ShouldNotTryToResolve(resolvedFieldType))
         {
             return(false);
         }
         if (!UnityEngineTypePredicates.IsUnityEngineObject(resolvedFieldType))
         {
             if (resolvedFieldType.FullName == fieldDefinition.DeclaringType.FullName)
             {
                 return(false);
             }
         }
         return(UnitySerializationLogic.WillUnitySerialize(fieldDefinition, TypeResolver));
     }
     catch (Exception ex)
     {
         throw new Exception(string.Format("Exception while processing {0} {1}, error {2}", fieldDefinition.FieldType.FullName, fieldDefinition.FullName, ex.Message));
     }
 }
        private void AddType(TypeReference typeRef, GenericInstanceTypeMap genericInstanceTypeMap)
        {
            // Prevent duplicates
            if (classes_.Any(x => x.name == GetMonoEmbeddedFullTypeNameFor(typeRef)))
            {
                return;
            }

            TypeDefinition type;

            try
            {
                type = typeRef.Resolve();
            } // This will happen for types which we don't have access to, like Windows.Foundation.IAsyncOperation<int>
            catch (AssemblyResolutionException)
            {
                return;
            }
            catch (NotSupportedException) // "NotSupportedException: Version not supported: 255.255.255.255" is thrown when assembly references WinRT assembly (e.g. mscorlib)
            {
                return;
            }

            if (type == null)
            {
                return;
            }

            if (typeRef.IsGenericInstance)
            {
                var arguments  = ((GenericInstanceType)typeRef).GenericArguments;
                var parameters = type.GenericParameters;

                for (int i = 0; i < arguments.Count; i++)
                {
                    if (parameters[i] != arguments[i])
                    {
                        genericInstanceTypeMap[parameters[i]] = arguments[i];
                    }
                }

                typeResolver.Add((GenericInstanceType)typeRef);
            }

            /* Process class itself before nested/base types in case user does something evil, for example:
             *
             *  class Outer
             *  {
             *      class Inner : Child
             *      {
             *      }
             *  }
             *
             *  class Child : Outer
             *  {
             *  }
             */

            bool shouldImplementDeserializable = false;

            try
            {
                shouldImplementDeserializable = UnitySerializationLogic.ShouldImplementIDeserializable(type);
            }
            catch
            {
                // If assembly has unknown reference (for ex., see tests VariousPlugins, where Metro plugins are used), skip field
            }

            if (!shouldImplementDeserializable)
            {
                // In this case we only take care of processing the nested types, if any.
                AddNestedTypes(type, genericInstanceTypeMap);
            }
            else
            {
                var ci = new ClassInfo();
                ci.name   = GetMonoEmbeddedFullTypeNameFor(typeRef);
                ci.fields = GetFields(type, typeRef.IsGenericInstance, genericInstanceTypeMap);

                classes_.Add(ci);

                // Fetch info for inner types
                AddNestedTypes(type, genericInstanceTypeMap);

                // Add base type
                AddBaseType(typeRef, genericInstanceTypeMap);
            }

            if (typeRef.IsGenericInstance)
            {
                typeResolver.Remove((GenericInstanceType)typeRef);
            }
        }
Пример #9
0
 private static TypeReference ElementTypeFor(TypeReference typeRef) =>
 (!UnitySerializationLogic.IsSupportedCollection(typeRef) ? typeRef : CecilUtils.ElementTypeOfCollection(typeRef));
        private void AddType(TypeReference typeRef, Dictionary <TypeReference, TypeReference> genericInstanceTypeMap)
        {
            // ISSUE: object of a compiler-generated type is created
            // ISSUE: variable of a compiler-generated type
            AssemblyTypeInfoGenerator.\u003CAddType\u003Ec__AnonStorey67 typeCAnonStorey67 = new AssemblyTypeInfoGenerator.\u003CAddType\u003Ec__AnonStorey67();
            // ISSUE: reference to a compiler-generated field
            typeCAnonStorey67.typeRef = typeRef;
            // ISSUE: reference to a compiler-generated field
            typeCAnonStorey67.\u003C\u003Ef__this = this;
            // ISSUE: reference to a compiler-generated method
            if (this.classes_.Any <AssemblyTypeInfoGenerator.ClassInfo>(new Func <AssemblyTypeInfoGenerator.ClassInfo, bool>(typeCAnonStorey67.\u003C\u003Em__DE)))
            {
                return;
            }
            TypeDefinition type;

            try
            {
                // ISSUE: reference to a compiler-generated field
                type = typeCAnonStorey67.typeRef.Resolve();
            }
            catch (AssemblyResolutionException ex)
            {
                return;
            }
            catch (NotSupportedException ex)
            {
                return;
            }
            if (type == null)
            {
                return;
            }
            // ISSUE: reference to a compiler-generated field
            if (typeCAnonStorey67.typeRef.get_IsGenericInstance())
            {
                // ISSUE: reference to a compiler-generated field
                Collection <TypeReference>    genericArguments  = ((GenericInstanceType)typeCAnonStorey67.typeRef).get_GenericArguments();
                Collection <GenericParameter> genericParameters = type.get_GenericParameters();
                for (int index = 0; index < genericArguments.get_Count(); ++index)
                {
                    if (genericParameters.get_Item(index) != genericArguments.get_Item(index))
                    {
                        genericInstanceTypeMap[(TypeReference)genericParameters.get_Item(index)] = genericArguments.get_Item(index);
                    }
                }
                // ISSUE: reference to a compiler-generated field
                this.typeResolver.Add((GenericInstanceType)typeCAnonStorey67.typeRef);
            }
            bool flag = false;

            try
            {
                flag = UnitySerializationLogic.ShouldImplementIDeserializable((TypeReference)type);
            }
            catch
            {
            }
            if (!flag)
            {
                this.AddNestedTypes(type, genericInstanceTypeMap);
            }
            else
            {
                // ISSUE: reference to a compiler-generated field
                // ISSUE: reference to a compiler-generated field
                this.classes_.Add(new AssemblyTypeInfoGenerator.ClassInfo()
                {
                    name   = this.GetMonoEmbeddedFullTypeNameFor(typeCAnonStorey67.typeRef),
                    fields = this.GetFields(type, typeCAnonStorey67.typeRef.get_IsGenericInstance(), genericInstanceTypeMap)
                });
                this.AddNestedTypes(type, genericInstanceTypeMap);
                // ISSUE: reference to a compiler-generated field
                this.AddBaseType(typeCAnonStorey67.typeRef, genericInstanceTypeMap);
            }
            // ISSUE: reference to a compiler-generated field
            if (!typeCAnonStorey67.typeRef.get_IsGenericInstance())
            {
                return;
            }
            // ISSUE: reference to a compiler-generated field
            this.typeResolver.Remove((GenericInstanceType)typeCAnonStorey67.typeRef);
        }
        private void AddType(TypeReference typeRef, Dictionary <TypeReference, TypeReference> genericInstanceTypeMap)
        {
            if (this.classes_.Any((AssemblyTypeInfoGenerator.ClassInfo x) => x.name == this.GetMonoEmbeddedFullTypeNameFor(typeRef)))
            {
                return;
            }
            TypeDefinition typeDefinition;

            try
            {
                typeDefinition = typeRef.Resolve();
            }
            catch (AssemblyResolutionException)
            {
                return;
            }
            catch (NotSupportedException)
            {
                return;
            }
            if (typeDefinition == null)
            {
                return;
            }
            if (typeRef.IsGenericInstance)
            {
                Collection <TypeReference>    genericArguments  = ((GenericInstanceType)typeRef).GenericArguments;
                Collection <GenericParameter> genericParameters = typeDefinition.GenericParameters;
                for (int i = 0; i < genericArguments.Count; i++)
                {
                    if (genericParameters[i] != genericArguments[i])
                    {
                        genericInstanceTypeMap[genericParameters[i]] = genericArguments[i];
                    }
                }
                this.typeResolver.Add((GenericInstanceType)typeRef);
            }
            bool flag = false;

            try
            {
                flag = UnitySerializationLogic.ShouldImplementIDeserializable(typeDefinition);
            }
            catch
            {
            }
            if (!flag)
            {
                this.AddNestedTypes(typeDefinition, genericInstanceTypeMap);
            }
            else
            {
                AssemblyTypeInfoGenerator.ClassInfo item = default(AssemblyTypeInfoGenerator.ClassInfo);
                item.name   = this.GetMonoEmbeddedFullTypeNameFor(typeRef);
                item.fields = this.GetFields(typeDefinition, typeRef.IsGenericInstance, genericInstanceTypeMap);
                this.classes_.Add(item);
                this.AddNestedTypes(typeDefinition, genericInstanceTypeMap);
                this.AddBaseType(typeRef, genericInstanceTypeMap);
            }
            if (typeRef.IsGenericInstance)
            {
                this.typeResolver.Remove((GenericInstanceType)typeRef);
            }
        }
Пример #12
0
 protected static bool CanInlineLoopOn(TypeReference typeReference) =>
 (UnitySerializationLogic.IsSupportedCollection(typeReference) && !IsSystemByte(CecilUtils.ElementTypeOfCollection(typeReference)));