Ejemplo n.º 1
0
        public void Compile(string inputFilePath, string outputFilePath)
        {
            ModuleContext modCtx = ModuleDef.CreateModuleContext();

            var corlibFilePath = _configuration.CorelibPath;

            if (string.IsNullOrEmpty(corlibFilePath))
            {
                var inputDirectoryName = Path.GetDirectoryName(inputFilePath);
                if (inputDirectoryName != null)
                {
                    corlibFilePath = Path.Combine(inputDirectoryName, "System.Private.CoreLib.dll");
                }
            }
            ModuleDefMD corlibModule      = ModuleDefMD.Load(corlibFilePath, modCtx);
            var         corlibAssemblyRef = corlibModule.Assembly.ToAssemblyRef();

            var options = new ModuleCreationOptions(modCtx)
            {
                CorLibAssemblyRef = corlibAssemblyRef
            };

            ModuleDefMD module = ModuleDefMD.Load(inputFilePath, options);

            var typesToCompile = new List <TypeDef>();

            typesToCompile.AddRange(corlibModule.Types);
            typesToCompile.AddRange(module.Types);

            var rootNode = _typeDependencyAnalyser.AnalyseDependencies(typesToCompile, module.EntryPoint);

            CompileNode(rootNode);

            _z80Writer.OutputCode(rootNode, inputFilePath, outputFilePath);
        }
Ejemplo n.º 2
0
        private void LoadConfuserRuntimeModule()
        {
            const string runtimeDllName = "Confuser.Runtime.dll";

            var    module          = typeof(RuntimeService).Assembly.ManifestModule;
            string rtPath          = runtimeDllName;
            var    creationOptions = new ModuleCreationOptions()
            {
                TryToLoadPdbFromDisk = true
            };

            if (module.FullyQualifiedName[0] != '<')
            {
                rtPath = Path.Combine(Path.GetDirectoryName(module.FullyQualifiedName), rtPath);
                if (File.Exists(rtPath))
                {
                    try {
                        rtModule = ModuleDefMD.Load(rtPath, creationOptions);
                    }
                    catch (IOException) { }
                }
                if (rtModule == null)
                {
                    rtPath = runtimeDllName;
                }
            }
            if (rtModule == null)
            {
                rtModule = ModuleDefMD.Load(rtPath, creationOptions);
            }
            rtModule.EnableTypeDefFindCache = true;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds file to the list of available documents.
        /// </summary>
        /// <param name="uri">Document URI.</param>
        /// <param name="content">Document content</param>
        /// <param name="filePath">Optional file path, if different from the URI.</param>
        public IDocument OpenDocument(Uri uri, string content, string filePath = null)
        {
            bool          justOpened;
            DocumentEntry entry;

            lock (_lock) {
                entry = FindDocument(null, uri);
                if (entry == null)
                {
                    var resolver = _services.GetService <IPythonInterpreter>().ModuleResolution.CurrentPathResolver;

                    var moduleType = ModuleType.User;
                    var path       = uri.ToAbsolutePath();
                    if (Path.IsPathRooted(path))
                    {
                        moduleType = resolver.IsLibraryFile(uri.ToAbsolutePath()) ? ModuleType.Library : ModuleType.User;
                    }

                    var mco = new ModuleCreationOptions {
                        ModuleName = Path.GetFileNameWithoutExtension(uri.LocalPath),
                        Content    = content,
                        FilePath   = filePath,
                        Uri        = uri,
                        ModuleType = moduleType
                    };
                    entry = CreateDocument(mco);
                }
                justOpened = TryOpenDocument(entry, content);
            }
            if (justOpened)
            {
                Opened?.Invoke(this, new DocumentEventArgs(entry.Document));
            }
            return(entry.Document);
        }
        /// <summary>
        /// Adds library module to the list of available documents.
        /// </summary>
        public IDocument AddModule(ModuleCreationOptions mco)
        {
            IDocument document;

            lock (_lock) {
                if (mco.Uri == null)
                {
                    mco.FilePath = mco.FilePath ?? throw new ArgumentNullException(nameof(mco.FilePath));
                    if (!Uri.TryCreate(mco.FilePath, UriKind.Absolute, out var uri))
                    {
                        var message = $"Unable to determine URI from the file path {mco.FilePath}";
                        _log?.Log(TraceEventType.Warning, message);
                        throw new OperationCanceledException(message);
                    }

                    mco.Uri = uri;
                }

                var entry = FindDocument(mco.Uri) ?? CreateDocument(mco);
                entry.LockCount++;
                document = entry.Document;
            }

            _services.GetService <IPythonAnalyzer>().InvalidateAnalysis(document);
            return(document);
        }
        private DocumentEntry CreateDocument(ModuleCreationOptions mco)
        {
            IDocument document;

            switch (mco.ModuleType)
            {
            case ModuleType.Compiled when TryAddModulePath(mco):
                document = new CompiledPythonModule(mco.ModuleName, ModuleType.Compiled, mco.FilePath, mco.Stub, _services);

                break;

            case ModuleType.CompiledBuiltin:
                document = new CompiledBuiltinPythonModule(mco.ModuleName, mco.Stub, _services);
                break;

            case ModuleType.User:
                TryAddModulePath(mco);
                document = new PythonModule(mco, _services);
                break;

            case ModuleType.Library when TryAddModulePath(mco):
                document = new PythonModule(mco, _services);

                break;

            default:
                throw new InvalidOperationException($"CreateDocument does not support module type {mco.ModuleType}");
            }

            var entry = new DocumentEntry(document);

            _documentsByUri[document.Uri] = entry;
            return(entry);
        }
Ejemplo n.º 6
0
        private static void CreateAssembly(string assembly, Bitness bitness, Action <ModuleDefMD> modifyAction = null)
        {
            var creationOptions = new ModuleCreationOptions
            {
                Context = ModuleDef.CreateModuleContext(),
                TryToLoadPdbFromDisk = true
            };

            using var module = ModuleDefMD.Load(assembly, creationOptions);

            ChangeBitness(module, bitness);

            modifyAction?.Invoke(module);

            DisableEditAndContinueForModule(module);

            var path         = Path.GetDirectoryName(module.Location);
            var filename     = Path.GetFileNameWithoutExtension(module.Location);
            var extension    = Path.GetExtension(module.Location);
            var saveFilename = $"{filename}.{bitness}{extension}";

            module.Name = saveFilename;

            var moduleWriterOptions = new ModuleWriterOptions(module)
            {
                AddCheckSum = true,
                WritePdb    = true
            };
            var moduleWriter = new ModuleWriter(module, moduleWriterOptions);

            ReplaceMSCOREEReferenceWithIJWHostForNetCoreApp(module, moduleWriter);

            moduleWriter.Write(Path.Combine(path, saveFilename));
        }
        private IDocument CreateDocument(ModuleCreationOptions mco)
        {
            IDocument document;

            switch (mco.ModuleType)
            {
            case ModuleType.Stub:
                document = new StubPythonModule(mco.ModuleName, mco.FilePath, _services);
                break;

            case ModuleType.Compiled:
                document = new CompiledPythonModule(mco.ModuleName, ModuleType.Compiled, mco.FilePath, mco.Stub, _services);
                break;

            case ModuleType.CompiledBuiltin:
                document = new CompiledBuiltinPythonModule(mco.ModuleName, mco.Stub, _services);
                break;

            case ModuleType.User:
            case ModuleType.Library:
                document = new PythonModule(mco, _services);
                break;

            default:
                throw new InvalidOperationException($"CreateDocument does not support module type {mco.ModuleType}");
            }

            _documentsByUri[document.Uri]    = document;
            _documentsByName[mco.ModuleName] = document;

            _moduleResolution.AddModulePath(document.FilePath);
            return(OpenDocument(document, mco.LoadOptions));
        }
        protected override IPythonModule CreateModule(string name)
        {
            var moduleImport = CurrentPathResolver.GetModuleImportFromModuleName(name);

            if (moduleImport == null)
            {
                _log?.Log(TraceEventType.Verbose, "Import not found: ", name);
                return(null);
            }

            if (moduleImport.ModulePath != null)
            {
                var module = GetRdt().GetDocument(new Uri(moduleImport.ModulePath));
                if (module != null)
                {
                    GetRdt().LockDocument(module.Uri);
                    return(module);
                }
            }

            // If there is a stub, make sure it is loaded and attached
            // First check stub next to the module.
            if (!TryCreateModuleStub(name, moduleImport.ModulePath, out var stub))
            {
                // If nothing found, try Typeshed.
                stub = _interpreter.TypeshedResolution.GetOrLoadModule(moduleImport.IsBuiltin ? name : moduleImport.FullName);
            }

            // If stub is created and its path equals to module, return that stub as module
            if (stub != null && stub.FilePath.PathEquals(moduleImport.ModulePath))
            {
                return(stub);
            }

            if (moduleImport.IsBuiltin)
            {
                _log?.Log(TraceEventType.Verbose, "Create built-in compiled (scraped) module: ", name, Configuration.InterpreterPath);
                return(new CompiledBuiltinPythonModule(name, stub, _services));
            }

            if (moduleImport.IsCompiled)
            {
                _log?.Log(TraceEventType.Verbose, "Create compiled (scraped): ", moduleImport.FullName, moduleImport.ModulePath, moduleImport.RootPath);
                return(new CompiledPythonModule(moduleImport.FullName, ModuleType.Compiled, moduleImport.ModulePath, stub, _services));
            }

            _log?.Log(TraceEventType.Verbose, "Import: ", moduleImport.FullName, moduleImport.ModulePath);
            // Module inside workspace == user code.

            var mco = new ModuleCreationOptions {
                ModuleName = moduleImport.FullName,
                ModuleType = moduleImport.IsLibrary ? ModuleType.Library : ModuleType.User,
                FilePath   = moduleImport.ModulePath,
                Stub       = stub
            };

            return(GetRdt().AddModule(mco));
        }
Ejemplo n.º 9
0
        public ModuleDefMD Load(byte[] fileData)
        {
            var options = new ModuleCreationOptions(moduleContext)
            {
                TryToLoadPdbFromDisk = false
            };

            return(SetModule(ModuleDefMD.Load(fileData, options)));
        }
Ejemplo n.º 10
0
        public AssemblyReader(string assemblyFileName, AssemblyParserOptions options)
        {
            var creationOptions = new ModuleCreationOptions
            {
                TryToLoadPdbFromDisk = options.ProcessPdb
            };

            this.module  = ModuleDefMD.Load(assemblyFileName, creationOptions);
            this.Methods = this.ParseMethodsList();
        }
Ejemplo n.º 11
0
        protected async Task <IDocumentAnalysis> GetAnalysisAsync(
            string code,
            IServiceContainer services,
            string moduleName = null,
            string modulePath = null)
        {
            var moduleUri = modulePath != null ? new Uri(modulePath) : TestData.GetDefaultModuleUri();

            modulePath = modulePath ?? TestData.GetDefaultModulePath();
            moduleName = moduleName ?? Path.GetFileNameWithoutExtension(modulePath);

            IDocument doc;
            var       rdt = services.GetService <IRunningDocumentTable>();

            if (rdt != null)
            {
                doc = rdt.OpenDocument(moduleUri, code, modulePath);
            }
            else
            {
                var mco = new ModuleCreationOptions {
                    ModuleName = moduleName,
                    Content    = code,
                    FilePath   = modulePath,
                    Uri        = moduleUri,
                    ModuleType = ModuleType.User
                };
                doc = new PythonModule(mco, services);
            }

            TestLogger.Log(TraceEventType.Information, "Test: AST begin.");
            var ast = await doc.GetAstAsync(CancellationToken.None);

            ast.Should().NotBeNull();
            TestLogger.Log(TraceEventType.Information, "Test: AST end.");

            TestLogger.Log(TraceEventType.Information, "Test: Analysis begin.");

            IDocumentAnalysis analysis;

            using (var cts = new CancellationTokenSource(GetAnalysisTimeout())) {
                await services.GetService <IPythonAnalyzer>().WaitForCompleteAnalysisAsync(cts.Token);

                analysis = await doc.GetAnalysisAsync(-1, cts.Token);
            }

            analysis.Should().NotBeNull();
            TestLogger.Log(TraceEventType.Information, "Test: Analysis end.");

            return(analysis);
        }
Ejemplo n.º 12
0
        private static ModuleDef LoadTestModuleDef()
        {
            var asmResolver = new AssemblyResolver {
                EnableTypeDefCache = true
            };

            asmResolver.DefaultModuleContext = new ModuleContext(asmResolver);
            var options = new ModuleCreationOptions(asmResolver.DefaultModuleContext)
            {
                TryToLoadPdbFromDisk = false
            };

            return(ModuleDefMD.Load(typeof(VTableTest).Module, options));
        }
        /// <summary>
        /// Adds library module to the list of available documents.
        /// </summary>
        public IDocument AddModule(ModuleCreationOptions mco)
        {
            if (mco.Uri == null)
            {
                mco.FilePath = mco.FilePath ?? throw new ArgumentNullException(nameof(mco.FilePath));
                if (!Uri.TryCreate(mco.FilePath, UriKind.Absolute, out var uri))
                {
                    throw new ArgumentException("Unable to determine URI from the file path.");
                }
                mco.Uri = uri;
            }

            return(FindDocument(mco.FilePath, mco.Uri) ?? CreateDocument(mco));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Load <see cref="ModuleDefMD"/>
        /// </summary>
        /// <param name="data">.NET module/assembly</param>
        /// <param name="assemblyResolver">Assembly resolver</param>
        /// <returns></returns>
        public static ModuleDefMD LoadModule(byte[] data, out AssemblyResolver assemblyResolver)
        {
            assemblyResolver = new AssemblyResolver();
            var context = new ModuleContext(assemblyResolver);

            assemblyResolver.EnableTypeDefCache   = false;
            assemblyResolver.DefaultModuleContext = context;
            var options = new ModuleCreationOptions()
            {
                Context = context,
                TryToLoadPdbFromDisk = false
            };

            return(ModuleDefMD.Load(data, options));
        }
        public AssemblyDef Resolve(string name)
        {
            if (modules.TryGetValue(name, out var module))
            {
                return(module.Assembly);
            }
            if (!nameToPath.TryGetValue(name, out var path))
            {
                return(null);
            }
            var options = new ModuleCreationOptions(context);

            options.TryToLoadPdbFromDisk = false;
            module = ModuleDefMD.Load(path, context);
            modules.Add(name, module);
            return(module.Assembly ?? throw new InvalidOperationException("It's a netmodule"));
        }
Ejemplo n.º 16
0
        public static DatadogPdbReader CreatePdbReader(string assemblyFullPath)
        {
            var    module      = ModuleDefMD.Load(File.ReadAllBytes(assemblyFullPath));
            var    metadata    = MetadataFactory.Load(assemblyFullPath, CLRRuntimeReaderKind.CLR);
            string pdbFullPath = Path.ChangeExtension(assemblyFullPath, "pdb");
            var    pdbStream   = DataReaderFactoryFactory.Create(pdbFullPath, false);
            var    options     = new ModuleCreationOptions(CLRRuntimeReaderKind.CLR);
            var    dnlibReader = SymbolReaderFactory.Create(options.PdbOptions, metadata, pdbStream);

            if (dnlibReader == null)
            {
                return(null);
            }

            dnlibReader.Initialize(module);
            return(new DatadogPdbReader(dnlibReader, module));
        }
        internal async Task <IDocumentAnalysis> GetAnalysisAsync(
            string code,
            IServiceContainer services,
            string moduleName = null,
            string modulePath = null)
        {
            var moduleUri = TestData.GetDefaultModuleUri();

            modulePath = modulePath ?? TestData.GetDefaultModulePath();
            moduleName = moduleName ?? Path.GetFileNameWithoutExtension(modulePath);

            IDocument doc;
            var       rdt = services.GetService <IRunningDocumentTable>();

            if (rdt != null)
            {
                doc = rdt.AddDocument(moduleUri, code, modulePath);
            }
            else
            {
                var mco = new ModuleCreationOptions {
                    ModuleName  = moduleName,
                    Content     = code,
                    FilePath    = modulePath,
                    Uri         = moduleUri,
                    ModuleType  = ModuleType.User,
                    LoadOptions = ModuleLoadOptions.Analyze
                };
                doc = new PythonModule(mco, services);
            }

            TestLogger.Log(TraceEventType.Information, "Ast begin");
            var ast = await doc.GetAstAsync(CancellationToken.None);

            ast.Should().NotBeNull();
            TestLogger.Log(TraceEventType.Information, "Ast end");

            TestLogger.Log(TraceEventType.Information, "Analysis begin");
            var analysis = await doc.GetAnalysisAsync(CancellationToken.None);

            analysis.Should().NotBeNull();
            TestLogger.Log(TraceEventType.Information, "Analysis end");

            return(analysis);
        }
        private bool TryAddModulePath(ModuleCreationOptions mco)
        {
            var filePath = mco.FilePath ?? mco.Uri?.ToAbsolutePath();

            if (filePath == null)
            {
                throw new InvalidOperationException("Can't create document with no file path or URI specified");
            }

            if (!ModuleManagement.TryAddModulePath(filePath, true, out var fullName))
            {
                return(false);
            }

            mco.FilePath   = filePath;
            mco.ModuleName = fullName;
            return(true);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Adds library module to the list of available documents.
        /// </summary>
        public IDocument AddModule(ModuleCreationOptions mco)
        {
            lock (_lock) {
                if (mco.Uri == null)
                {
                    mco.FilePath = mco.FilePath ?? throw new ArgumentNullException(nameof(mco.FilePath));
                    if (!Uri.TryCreate(mco.FilePath, UriKind.Absolute, out var uri))
                    {
                        throw new ArgumentException("Unable to determine URI from the file path.");
                    }

                    mco.Uri = uri;
                }

                var entry = FindDocument(mco.ModuleName, mco.Uri) ?? CreateDocument(mco);
                entry.LockCount++;
                return(entry.Document);
            }
        }
Ejemplo n.º 20
0
        public static IDnSpyFile CreateDnSpyFileFromFile(DnSpyFileInfo fileInfo, string filename, bool useMemoryMappedIO, bool loadPDBFiles, IAssemblyResolver asmResolver, bool isModule)
        {
            try {
                // Quick check to prevent exceptions from being thrown
                if (!File.Exists(filename))
                {
                    return(new DnSpyUnknownFile(filename));
                }

                IPEImage peImage;

                if (useMemoryMappedIO)
                {
                    peImage = new PEImage(filename);
                }
                else
                {
                    peImage = new PEImage(File.ReadAllBytes(filename), filename);
                }

                var  dotNetDir = peImage.ImageNTHeaders.OptionalHeader.DataDirectories[14];
                bool isDotNet  = dotNetDir.VirtualAddress != 0 && dotNetDir.Size >= 0x48;
                if (isDotNet)
                {
                    try {
                        var options = new ModuleCreationOptions(DnSpyDotNetFileBase.CreateModuleContext(asmResolver));
                        if (isModule)
                        {
                            return(DnSpyDotNetFile.CreateModule(fileInfo, ModuleDefMD.Load(peImage, options), loadPDBFiles));
                        }
                        return(DnSpyDotNetFile.CreateAssembly(fileInfo, ModuleDefMD.Load(peImage, options), loadPDBFiles));
                    }
                    catch {
                    }
                }

                return(new DnSpyPEFile(peImage));
            }
            catch {
            }

            return(new DnSpyUnknownFile(filename));
        }
Ejemplo n.º 21
0
        public IDsDocument CreateDocument(DsDocumentInfo documentInfo, string filename, bool isModule)
        {
            try {
                // Quick check to prevent exceptions from being thrown
                if (!File.Exists(filename))
                {
                    return(new DsUnknownDocument(filename));
                }

                IPEImage peImage;

                if (Settings.UseMemoryMappedIO)
                {
                    peImage = new PEImage(filename);
                }
                else
                {
                    peImage = new PEImage(File.ReadAllBytes(filename), filename);
                }

                var  dotNetDir = peImage.ImageNTHeaders.OptionalHeader.DataDirectories[14];
                bool isDotNet  = dotNetDir.VirtualAddress != 0 && dotNetDir.Size >= 0x48;
                if (isDotNet)
                {
                    try {
                        var options = new ModuleCreationOptions(DsDotNetDocumentBase.CreateModuleContext(AssemblyResolver));
                        if (isModule)
                        {
                            return(DsDotNetDocument.CreateModule(documentInfo, ModuleDefMD.Load(peImage, options), true));
                        }
                        return(DsDotNetDocument.CreateAssembly(documentInfo, ModuleDefMD.Load(peImage, options), true));
                    }
                    catch {
                    }
                }

                return(new DsPEDocument(peImage));
            }
            catch {
            }

            return(new DsUnknownDocument(filename));
        }
        private static IDocument GetOrOpenModule(Uri uri, IRunningDocumentTable rdt)
        {
            var document = rdt.GetDocument(uri);

            if (document != null)
            {
                return(document); // Already opened by another analysis.
            }

            var filePath = uri.ToAbsolutePath();
            var mco      = new ModuleCreationOptions {
                ModuleName = Path.GetFileNameWithoutExtension(filePath),
                FilePath   = filePath,
                Uri        = uri,
                ModuleType = ModuleType.User
            };

            return(rdt.AddModule(mco));
        }
Ejemplo n.º 23
0
        public ModuleDefMD Reload(byte[] newModuleData, DumpedMethodsRestorer dumpedMethodsRestorer, IStringDecrypter stringDecrypter)
        {
            TheAssemblyResolver.Instance.Remove(module);
            var options = new ModuleCreationOptions(moduleContext)
            {
                TryToLoadPdbFromDisk = false
            };
            var mod = ModuleDefMD.Load(newModuleData, options);

            if (dumpedMethodsRestorer != null)
            {
                dumpedMethodsRestorer.Module = mod;
            }
            mod.StringDecrypter              = stringDecrypter;
            mod.MethodDecrypter              = dumpedMethodsRestorer;
            mod.TablesStream.ColumnReader    = dumpedMethodsRestorer;
            mod.TablesStream.MethodRowReader = dumpedMethodsRestorer;
            return(SetModule(mod));
        }
        /// <summary>
        /// Adds file to the list of available documents.
        /// </summary>
        /// <param name="uri">Document URI.</param>
        /// <param name="content">Document content</param>
        /// <param name="filePath">Optional file path, if different from the URI.</param>
        public IDocument AddDocument(Uri uri, string content, string filePath = null)
        {
            var document = FindDocument(null, uri);

            if (document != null)
            {
                return(OpenDocument(document, ModuleLoadOptions.Open));
            }

            var mco = new ModuleCreationOptions {
                ModuleName  = Path.GetFileNameWithoutExtension(uri.LocalPath),
                Content     = content,
                FilePath    = filePath,
                Uri         = uri,
                ModuleType  = ModuleType.User,
                LoadOptions = ModuleLoadOptions.Open
            };

            return(CreateDocument(mco));
        }
Ejemplo n.º 25
0
        internal static ModuleDefMD LoadTestModuleDef()
        {
            var asmResolver = new AssemblyResolver {
                EnableTypeDefCache = true
            };

            asmResolver.DefaultModuleContext = new ModuleContext(asmResolver);
            var options = new ModuleCreationOptions(asmResolver.DefaultModuleContext)
            {
                TryToLoadPdbFromDisk = false
            };

            asmResolver.AddToCache(ModuleDefMD.Load(typeof(Mock).Module, options));
            asmResolver.AddToCache(ModuleDefMD.Load(typeof(FactAttribute).Module, options));

            var thisModule = ModuleDefMD.Load(typeof(VTableTest).Module, options);

            asmResolver.AddToCache(thisModule);

            return(thisModule);
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Adds library module to the list of available documents.
        /// </summary>
        public IDocument AddModule(ModuleCreationOptions mco)
        {
            lock (_lock) {
                if (mco.Uri == null)
                {
                    mco.FilePath = mco.FilePath ?? throw new ArgumentNullException(nameof(mco.FilePath));
                    if (!Uri.TryCreate(mco.FilePath, UriKind.Absolute, out var uri))
                    {
                        var message = $"Unable to determine URI from the file path {mco.FilePath}";
                        _log?.Log(TraceEventType.Warning, message);
                        throw new OperationCanceledException(message);
                    }

                    mco.Uri = uri;
                }

                var entry = FindDocument(mco.ModuleName, mco.Uri) ?? CreateDocument(mco);
                entry.LockCount++;
                return(entry.Document);
            }
        }
Ejemplo n.º 27
0
        LoadedFile LoadAssembly(object state)
        {
            IPEImage peImage;

            if (OtherSettings.Instance.UseMemoryMappedIO)
            {
                peImage = new PEImage(fileName);
            }
            else
            {
                peImage = new PEImage(File.ReadAllBytes(fileName));
            }

            var  dotNetDir = peImage.ImageNTHeaders.OptionalHeader.DataDirectories[14];
            bool isDotNet  = dotNetDir.VirtualAddress != 0 && dotNetDir.Size >= 0x48;

            if (isDotNet)
            {
                try {
                    ModuleDef module;
                    var       opts = new ModuleCreationOptions(CreateModuleContext());
                    if (OtherSettings.Instance.UseMemoryMappedIO)
                    {
                        module = ModuleDefMD.Load(peImage, opts);
                    }
                    else
                    {
                        module = ModuleDefMD.Load(peImage, opts);
                    }
                    return(InitializeModule(module));
                }
                catch {
                }
            }

            return(new LoadedFile(peImage, null));
        }
        protected override IPythonModule CreateModule(string name)
        {
            var moduleImport = CurrentPathResolver.GetModuleImportFromModuleName(name);

            if (moduleImport == null)
            {
                Log?.Log(TraceEventType.Verbose, "Import not found: ", name);
                return(null);
            }

            IPythonModule module;

            if (moduleImport.ModulePath != null)
            {
                module = GetRdt().GetDocument(new Uri(moduleImport.ModulePath));
                if (module != null)
                {
                    GetRdt().LockDocument(module.Uri);
                    return(module);
                }
            }

            var moduleType = moduleImport.IsBuiltin ? ModuleType.CompiledBuiltin
                : moduleImport.IsCompiled ? ModuleType.Compiled
                : moduleImport.IsLibrary ? ModuleType.Library
                : ModuleType.User;

            var dbs = GetDbService();

            if (dbs != null)
            {
                var sw = Stopwatch.StartNew();
                module = dbs.RestoreModule(name, moduleImport.ModulePath, moduleType);
                sw.Stop();
                if (module != null)
                {
                    Log?.Log(TraceEventType.Verbose, $"Restored from database: {name} in {sw.ElapsedMilliseconds} ms.");
                    Interpreter.ModuleResolution.SpecializeModule(name, x => module, true);
                    return(module);
                }
            }

            // If there is a stub, make sure it is loaded and attached
            // First check stub next to the module.
            if (TryCreateModuleStub(name, moduleImport.ModulePath, out var stub))
            {
                Analyzer.InvalidateAnalysis(stub);
            }
            else
            {
                // If nothing found, try Typeshed.
                stub = Interpreter.TypeshedResolution.GetOrLoadModule(moduleImport.IsBuiltin ? name : moduleImport.FullName);
            }

            // If stub is created and its path equals to module, return that stub as module
            if (stub != null && stub.FilePath.PathEquals(moduleImport.ModulePath))
            {
                return(stub);
            }

            if (moduleImport.IsBuiltin)
            {
                Log?.Log(TraceEventType.Verbose, "Create built-in compiled (scraped) module: ", name, Configuration.InterpreterPath);
                return(new CompiledBuiltinPythonModule(name, stub, Services));
            }

            if (moduleImport.IsCompiled)
            {
                Log?.Log(TraceEventType.Verbose, "Create compiled (scraped): ", moduleImport.FullName, moduleImport.ModulePath, moduleImport.RootPath);
                return(new CompiledPythonModule(moduleImport.FullName, ModuleType.Compiled, moduleImport.ModulePath, stub, false, Services));
            }

            Log?.Log(TraceEventType.Verbose, "Import: ", moduleImport.FullName, moduleImport.ModulePath);
            // Module inside workspace == user code.

            var mco = new ModuleCreationOptions {
                ModuleName = moduleImport.FullName,
                ModuleType = moduleType,
                FilePath   = moduleImport.ModulePath,
                Stub       = stub
            };

            return(GetRdt().AddModule(mco));
        }