protected override void ProcessRecord() { string path = GetUnresolvedProviderPathFromPSPath(LiteralPath); try { var module = new PEFile(LiteralPath, new FileStream(LiteralPath, FileMode.Open, FileAccess.Read), PEStreamOptions.Default); var debugInfo = DebugInfoUtils.FromFile(module, PDBFilePath); var decompiler = new CSharpDecompiler(path, new DecompilerSettings(LanguageVersion) { ThrowOnAssemblyResolveErrors = false, RemoveDeadCode = RemoveDeadCode, RemoveDeadStores = RemoveDeadStores, UseDebugSymbols = debugInfo != null, ShowDebugInfo = debugInfo != null, }); decompiler.DebugInfoProvider = debugInfo; WriteObject(decompiler); } catch (Exception e) { WriteVerbose(e.ToString()); WriteError(new ErrorRecord(e, ErrorIds.AssemblyLoadFailed, ErrorCategory.OperationStopped, null)); } }
PEFile LoadAssembly(object state) { MetadataReaderOptions options; if (DecompilerSettingsPanel.CurrentDecompilerSettings.ApplyWindowsRuntimeProjections) { options = MetadataReaderOptions.ApplyWindowsRuntimeProjections; } else { options = MetadataReaderOptions.None; } PEFile module; // runs on background thread if (state is Stream stream) { // Read the module from a precrafted stream var streamOptions = stream is MemoryStream ? PEStreamOptions.PrefetchEntireImage : PEStreamOptions.Default; module = new PEFile(fileName, stream, streamOptions, metadataOptions: options); } else { // Read the module from disk (by default) stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); module = new PEFile(fileName, stream, PEStreamOptions.PrefetchEntireImage, metadataOptions: options); } if (DecompilerSettingsPanel.CurrentDecompilerSettings.UseDebugSymbols) { try { debugInfoProvider = DebugInfoUtils.FromFile(module, PdbFileOverride) ?? DebugInfoUtils.LoadSymbols(module); } catch (IOException) { } catch (UnauthorizedAccessException) { } catch (InvalidOperationException) { // ignore any errors during symbol loading } } lock (loadedAssemblies) { loadedAssemblies.Add(module, this); } return(module); }
IDebugInfoProvider?LoadDebugInfo(PEFile module) { if (DecompilerSettingsPanel.CurrentDecompilerSettings.UseDebugSymbols) { try { return(DebugInfoUtils.FromFile(module, PdbFileName) ?? DebugInfoUtils.LoadSymbols(module)); } catch (IOException) { } catch (UnauthorizedAccessException) { } catch (InvalidOperationException) { // ignore any errors during symbol loading } } return(null); }
IDebugInfoProvider?LoadDebugInfo(PEFile module) { if (useDebugSymbols) { try { return((PdbFileName != null ? DebugInfoUtils.FromFile(module, PdbFileName) : null) ?? DebugInfoUtils.LoadSymbols(module)); } catch (IOException) { } catch (UnauthorizedAccessException) { } catch (InvalidOperationException) { // ignore any errors during symbol loading } } return(null); }
LoadResult LoadAssembly(Stream stream, PEStreamOptions streamOptions) { MetadataReaderOptions options; if (DecompilerSettingsPanel.CurrentDecompilerSettings.ApplyWindowsRuntimeProjections) { options = MetadataReaderOptions.ApplyWindowsRuntimeProjections; } else { options = MetadataReaderOptions.None; } PEFile module = new PEFile(fileName, stream, streamOptions, metadataOptions: options); if (DecompilerSettingsPanel.CurrentDecompilerSettings.UseDebugSymbols) { try { debugInfoProvider = DebugInfoUtils.FromFile(module, PdbFileOverride) ?? DebugInfoUtils.LoadSymbols(module); } catch (IOException) { } catch (UnauthorizedAccessException) { } catch (InvalidOperationException) { // ignore any errors during symbol loading } } lock (loadedAssemblies) { loadedAssemblies.Add(module, this); } return(new LoadResult(module)); }
public static Task <string> DecompileCSharp(string assemblyFileName, DecompilerSettings settings = null) { if (settings == null) { settings = new DecompilerSettings(); } using (var file = new FileStream(assemblyFileName, FileMode.Open, FileAccess.Read)) { var module = new PEFile(assemblyFileName, file, PEStreamOptions.PrefetchEntireImage); string targetFramework = module.Metadata.DetectTargetFrameworkId(); var resolver = new UniversalAssemblyResolver(assemblyFileName, false, targetFramework, null, PEStreamOptions.PrefetchMetadata); resolver.AddSearchDirectory(targetFramework.Contains(".NETFramework") ? RefAsmPath : coreRefAsmPath); var typeSystem = new DecompilerTypeSystem(module, resolver, settings); CSharpDecompiler decompiler = new CSharpDecompiler(typeSystem, settings); decompiler.AstTransforms.Insert(0, new RemoveEmbeddedAttributes()); decompiler.AstTransforms.Insert(0, new RemoveCompilerAttribute()); decompiler.AstTransforms.Insert(0, new RemoveNamespaceMy()); decompiler.AstTransforms.Add(new EscapeInvalidIdentifiers()); var pdbFileName = Path.ChangeExtension(assemblyFileName, ".pdb"); if (File.Exists(pdbFileName)) { decompiler.DebugInfoProvider = DebugInfoUtils.FromFile(module, pdbFileName); } var syntaxTree = decompiler.DecompileWholeModuleAsSingleFile(sortTypes: true); StringWriter output = new StringWriter(); CSharpFormattingOptions formattingPolicy = CreateFormattingPolicyForTests(); var visitor = new CSharpOutputVisitor(output, formattingPolicy); syntaxTree.AcceptVisitor(visitor); string fileName = Path.GetTempFileName(); File.WriteAllText(fileName, output.ToString()); return(Task.FromResult(fileName)); } }