public TinyTablesContext(
            AssemblyDefinition assemblyDefinition,
            List <String> explicitTypesOrder,
            ICustomStringSorter stringSorter,
            Boolean applyAttributesCompression)
        {
            AssemblyDefinition = assemblyDefinition;

            var assemblyAttributes = new HashSet <String>(
                assemblyDefinition.CustomAttributes.Select(item => item.AttributeType.FullName),
                StringComparer.Ordinal)
            {
                "System.Reflection.AssemblyCultureAttribute",
                "System.Reflection.AssemblyVersionAttribute",
                "System.Runtime.CompilerServices.MethodImplAttribute",
                "System.Runtime.CompilerServices.MethodImplOptions",
                "System.Runtime.InteropServices.StructLayoutAttribute",
                "System.Runtime.InteropServices.OutAttribute",
                "System.Runtime.InteropServices.LayoutKind",
                "System.SerializableAttribute",
                "System.Runtime.CompilerServices.ExtensionAttribute",
                "System.Diagnostics.DebuggerBrowsableAttribute",
                "System.Diagnostics.DebuggerBrowsableState",
                "System.Diagnostics.DebuggerHiddenAttribute",
                "System.Diagnostics.ConditionalAttribute",
                "System.ParamArrayAttribute"
            };

            NativeMethodsCrc = new NativeMethodsCrc(assemblyDefinition);

            var mainModule = AssemblyDefinition.MainModule;

            // External references

            AssemblyReferenceTable = new TinyAssemblyReferenceTable(
                mainModule.AssemblyReferences, this);

            var typeReferences = mainModule.GetTypeReferences()
                                 .Where(item => !IsAttribute(item, assemblyAttributes))
                                 .ToList();

            TypeReferencesTable = new TinyTypeReferenceTable(
                typeReferences, this);

            var typeReferencesNames = new HashSet <String>(
                typeReferences.Select(item => item.FullName),
                StringComparer.Ordinal);
            var memberReferences = mainModule.GetMemberReferences()
                                   .Where(item => typeReferencesNames.Contains(item.DeclaringType.FullName))
                                   .ToList();

            FieldReferencesTable = new TinyFieldReferenceTable(
                memberReferences.OfType <FieldReference>(), this);
            MethodReferencesTable = new TinyMethodReferenceTable(
                memberReferences.OfType <MethodReference>(), this);

            // Internal types definitions

            var types = GetOrderedTypes(mainModule, explicitTypesOrder);

            TypeDefinitionTable = new TinyTypeDefinitionTable(types, this);

            var fields = types
                         .SelectMany(item => GetOrderedFields(item.Fields.Where(field => !field.HasConstant)))
                         .ToList();

            FieldsTable = new TinyFieldDefinitionTable(fields, this);

            var methods = types.SelectMany(item => GetOrderedMethods(item.Methods)).ToList();

            MethodDefinitionTable = new TinyMethodDefinitionTable(methods, this);

            var ignoringAttributes = new HashSet <String>(StringComparer.Ordinal)
            {
                "System.Runtime.CompilerServices.ExtensionAttribute",
                "System.Diagnostics.DebuggerBrowsableAttribute",
                "System.Diagnostics.DebuggerBrowsableState",
                "System.Diagnostics.DebuggerHiddenAttribute",
                "System.Diagnostics.ConditionalAttribute",
                "System.ParamArrayAttribute"
            };

            AttributesTable = new TinyAttributesTable(
                GetAttributes(types, applyAttributesCompression, ignoringAttributes),
                GetAttributes(fields, applyAttributesCompression, ignoringAttributes),
                GetAttributes(methods, applyAttributesCompression, ignoringAttributes),
                this);

            TypeSpecificationsTable = new TinyTypeSpecificationsTable(this);

            // Resources information

            ResourcesTable = new TinyResourcesTable(
                mainModule.Resources, this);
            ResourceDataTable = new TinyResourceDataTable();

            // Strings and signatures

            SignaturesTable = new TinySignaturesTable(this);
            StringTable     = new TinyStringTable(stringSorter);

            // Byte code table
            ByteCodeTable = new TinyByteCodeTable(this);

            // Additional information

            ResourceFileTable = new TinyResourceFileTable(this);

            // Pre-allocate strings from some tables
            AssemblyReferenceTable.AllocateStrings();
            TypeReferencesTable.AllocateStrings();
            foreach (var item in memberReferences)
            {
                StringTable.GetOrCreateStringId(item.Name);

                var fieldReference = item as FieldReference;
                if (fieldReference != null)
                {
                    SignaturesTable.GetOrCreateSignatureId(fieldReference);
                }

                var methodReference = item as MethodReference;
                if (methodReference != null)
                {
                    SignaturesTable.GetOrCreateSignatureId(methodReference);
                }
            }
        }
Ejemplo n.º 2
0
        public TinyTablesContext(
            AssemblyDefinition assemblyDefinition,
            List <String> explicitTypesOrder,
            ICustomStringSorter stringSorter,
            Boolean applyAttributesCompression)
        {
            AssemblyDefinition = assemblyDefinition;

            foreach (var item in assemblyDefinition.CustomAttributes)
            {
                _ignoringAttributes.Add(item.AttributeType.FullName);
            }

            NativeMethodsCrc = new NativeMethodsCrc(assemblyDefinition);

            var mainModule = AssemblyDefinition.MainModule;

            // External references

            AssemblyReferenceTable = new TinyAssemblyReferenceTable(
                mainModule.AssemblyReferences, this);

            var typeReferences = mainModule.GetTypeReferences()
                                 .Where(item => !IsAttribute(item))
                                 .ToList();

            TypeReferencesTable = new TinyTypeReferenceTable(
                typeReferences, this);

            var typeReferencesNames = new HashSet <String>(
                typeReferences.Select(item => item.FullName),
                StringComparer.Ordinal);
            var memberReferences = mainModule.GetMemberReferences()
                                   .Where(item => typeReferencesNames.Contains(item.DeclaringType.FullName))
                                   .ToList();

            FieldReferencesTable = new TinyFieldReferenceTable(
                memberReferences.OfType <FieldReference>(), this);
            MethodReferencesTable = new TinyMethodReferenceTable(
                memberReferences.OfType <MethodReference>(), this);

            // Internal types definitions

            var types = GetOrderedTypes(mainModule, explicitTypesOrder);

            TypeDefinitionTable = new TinyTypeDefinitionTable(types, this);

            var fields = types
                         .SelectMany(item => GetOrderedFields(item.Fields.Where(field => !field.HasConstant)))
                         .ToList();

            FieldsTable = new TinyFieldDefinitionTable(fields, this);

            var methods = types.SelectMany(item => GetOrderedMethods(item.Methods)).ToList();

            MethodDefinitionTable = new TinyMethodDefinitionTable(methods, this);

            AttributesTable = new TinyAttributesTable(
                GetAttributes(types, applyAttributesCompression),
                GetAttributes(fields, applyAttributesCompression),
                GetAttributes(methods, applyAttributesCompression),
                this);

            TypeSpecificationsTable = new TinyTypeSpecificationsTable(this);

            // Resources information

            ResourcesTable = new TinyResourcesTable(
                mainModule.Resources, this);
            ResourceDataTable = new TinyResourceDataTable();

            // Strings and signatures

            SignaturesTable = new TinySignaturesTable(this);
            StringTable     = new TinyStringTable(stringSorter);

            // Byte code table
            ByteCodeTable = new TinyByteCodeTable(this);

            // Additional information

            ResourceFileTable = new TinyResourceFileTable(this);

            // Pre-allocate strings from some tables
            AssemblyReferenceTable.AllocateStrings();
            TypeReferencesTable.AllocateStrings();
            foreach (var item in memberReferences)
            {
                StringTable.GetOrCreateStringId(item.Name);

                var fieldReference = item as FieldReference;
                if (fieldReference != null)
                {
                    SignaturesTable.GetOrCreateSignatureId(fieldReference);
                }

                var methodReference = item as MethodReference;
                if (methodReference != null)
                {
                    SignaturesTable.GetOrCreateSignatureId(methodReference);
                }
            }
        }