예제 #1
0
        public void Inspect(string filePattern)
        {
            foreach (string file in Directory.GetFiles(Environment.CurrentDirectory, filePattern))
            {
                string extension = Path.GetExtension(file);
                if (extension != ".dll" && extension != ".exe")
                {
                    continue;
                }

                var       assembly = host.LoadUnitFrom(file) as IAssembly;
                PdbReader symbol   = LoadPdb(file, host);


                inspectors.ForEach((IInspector inspector) => inspector.Inspect(assembly, symbol));
                foreach (INamedTypeDefinition type in assembly.GetAllTypes())
                {
                    inspectors.ForEach((IInspector inspector) => inspector.Inspect(assembly, type, symbol));
                    foreach (IMethodDefinition method in type.Methods)
                    {
                        inspectors.ForEach((IInspector inspector) => inspector.Inspect(assembly, type, method, symbol));
                    }
                }
            }
        }
예제 #2
0
        private Stream ExtractFromAssembly(string assemblyFilePath, out string assemblyName, out bool containsResources)
        {
            assemblyName      = null;
            containsResources = true;

            IAssembly assembly = _host.LoadUnitFrom(assemblyFilePath) as IAssembly;

            if (assembly == null || assembly == Dummy.Assembly)
            {
                containsResources = false;
                return(null);
            }

            if (assembly.Resources == null)
            {
                containsResources = false;
                return(null);
            }

            assemblyName = assembly.Name.Value;
            string resourcesName = "FxResources." + assemblyName + ".SR.resources";
            int    resourceCount = 0;

            foreach (IResourceReference resourceReference in assembly.Resources)
            {
                resourceCount++;
                if (!resourceReference.Resource.IsInExternalFile && resourceReference.Name.Value.Equals(resourcesName, StringComparison.OrdinalIgnoreCase))
                {
                    const int BUFFERSIZE = 4096;
                    byte[]    buffer     = new byte[BUFFERSIZE];
                    int       index      = 0;

                    MemoryStream ms = new MemoryStream(BUFFERSIZE);

                    foreach (byte b in resourceReference.Resource.Data)
                    {
                        if (index == BUFFERSIZE)
                        {
                            ms.Write(buffer, 0, BUFFERSIZE);
                            index = 0;
                        }
                        buffer[index++] = b;
                    }
                    ms.Write(buffer, 0, index);
                    ms.Seek(0, SeekOrigin.Begin);
                    return(ms);
                }
            }

            if (resourceCount == 0) // no resources
            {
                containsResources = false;
            }

            return(null);
        }
예제 #3
0
        public bool Load(string location)
        {
            IModule module = MetadataReaderHost.LoadUnitFrom(location) as IModule;

            if (module == null || module is Dummy)
            {
                return(false);
            }
            else
            {
                PrimaryModule   = module;
                PrimaryAssembly = module as IAssembly;
                Environment.Message("Loaded primary assembly {0} with {1} assembly references.", PrimaryAssembly.Name.Value, PrimaryAssembly.AssemblyReferences.Count());
                return(true);
            }
        }
예제 #4
0
        private IAssembly LoadAssemblyFrom(string filePath)
        {
            IAssembly module = _host.LoadUnitFrom(filePath) as IAssembly;

            _host.RegisterAsLatest(module);
            if (module == null || module == Dummy.Module || module == Dummy.Assembly)
            {
                throw new AssemblyReadException(filePath + " is not a PE file containing a CLR module or assembly.");
            }

            PdbReader pdbReader;

            TryGetPdbReader(module, out pdbReader);

            module = new MetadataDeepCopier(_host).Copy(module);
            //  var decompiled = Decompiler.GetCodeModelFromMetadataModel(_host, module, pdbReader,
            //      DecompilerOptions.None);
            return(module);
            //  return new CodeDeepCopier(_host, pdbReader).Copy(decompiled);
        }
예제 #5
0
        private List <ResWInfo> ExtractAssemblyResWList(string assemblyFilePath, out bool containsFrameworkResources)
        {
            containsFrameworkResources = false;

            IAssembly assembly = _host.LoadUnitFrom(assemblyFilePath) as IAssembly;

            if (assembly == null || assembly == Dummy.Assembly)
            {
                return(null);
            }

            if (assembly.Resources == null)
            {
                return(null);
            }

            string neutralResourceLanguage;

            if (String.IsNullOrEmpty(assembly.Culture) && !TryGetNeutralResourcesLanguageAttribute(assembly, out neutralResourceLanguage))
            {
                // Must have NeutralResourcesLanguageAttribute
                // warning MSB3817: The assembly "<FullPath>\ClassLibrary1.dll" does not have a NeutralResourcesLanguageAttribute on it. To be used in an app package, portable libraries must define a NeutralResourcesLanguageAttribute on their main assembly (ie, the one containing code, not a satellite assembly).
                return(null);
            }

            List <ResWInfo> reswInfoList           = new List <ResWInfo>();
            string          frameworkResourcesName = "FxResources." + assembly.Name.Value + ".SR.resources";

            foreach (IResourceReference resourceReference in assembly.Resources)
            {
                if (!resourceReference.Resource.IsInExternalFile && resourceReference.Name.Value.EndsWith(".resources", StringComparison.OrdinalIgnoreCase))
                {
                    const int BUFFERSIZE = 4096;
                    byte[]    buffer     = new byte[BUFFERSIZE];
                    int       index      = 0;

                    using (MemoryStream ms = new MemoryStream(BUFFERSIZE))
                    {
                        foreach (byte b in resourceReference.Resource.Data)
                        {
                            if (index == BUFFERSIZE)
                            {
                                ms.Write(buffer, 0, BUFFERSIZE);
                                index = 0;
                            }
                            buffer[index++] = b;
                        }
                        ms.Write(buffer, 0, index);
                        ms.Seek(0, SeekOrigin.Begin);

                        string resourceFileName = resourceReference.Name.Value.Remove(resourceReference.Name.Value.Length - 10);
                        if (resourceReference.Name.Value.Equals(frameworkResourcesName, StringComparison.OrdinalIgnoreCase))
                        {
                            containsFrameworkResources = true;
                            reswInfoList.Add(ExtractResourcesFromStream(ms, assembly, resourceFileName, true));
                            return(reswInfoList);
                        }
                        else
                        {
                            reswInfoList.Add(ExtractResourcesFromStream(ms, assembly, resourceFileName, false));
                        }
                    }
                }
            }

            return(reswInfoList);
        }
예제 #6
0
        private List <ResWInfo> ExtractAssemblyResWList(string assemblyFilePath, out bool containsFrameworkResources)
        {
            containsFrameworkResources = false;

            IAssembly assembly = _host.LoadUnitFrom(assemblyFilePath) as IAssembly;

            if (assembly == null || assembly == Dummy.Assembly)
            {
                return(null);
            }

            if (assembly.Resources == null)
            {
                return(null);
            }

            List <ResWInfo> reswInfoList           = new List <ResWInfo>();
            string          frameworkResourcesName = "FxResources." + assembly.Name.Value + ".SR.resources";

            foreach (IResourceReference resourceReference in assembly.Resources)
            {
                if (!resourceReference.Resource.IsInExternalFile && resourceReference.Name.Value.EndsWith(".resources", StringComparison.OrdinalIgnoreCase))
                {
                    const int BUFFERSIZE = 4096;
                    byte[]    buffer     = new byte[BUFFERSIZE];
                    int       index      = 0;

                    using (MemoryStream ms = new MemoryStream(BUFFERSIZE))
                    {
                        foreach (byte b in resourceReference.Resource.Data)
                        {
                            if (index == BUFFERSIZE)
                            {
                                ms.Write(buffer, 0, BUFFERSIZE);
                                index = 0;
                            }
                            buffer[index++] = b;
                        }
                        ms.Write(buffer, 0, index);
                        ms.Seek(0, SeekOrigin.Begin);

                        string   resourceFileName    = resourceReference.Name.Value.Remove(resourceReference.Name.Value.Length - 10);
                        bool     isFrameworkResource = resourceReference.Name.Value.Equals(frameworkResourcesName, StringComparison.OrdinalIgnoreCase);
                        ResWInfo reswInfo            = ExtractResourcesFromStream(ms, assembly, resourceFileName, isFrameworkResource);

                        if (reswInfo != null)
                        {
                            reswInfoList.Add(reswInfo);
                        }

                        if (isFrameworkResource)
                        {
                            containsFrameworkResources = true;
                            return(reswInfoList);
                        }
                    }
                }
            }

            return(reswInfoList);
        }