protected override void ProcessRecord() { string path = GetUnresolvedProviderPathFromPSPath(LiteralPath); if (!Directory.Exists(path)) { WriteObject("Destination directory must exist prior to decompilation"); return; } WholeProjectDecompiler decompiler = new WholeProjectDecompiler(); try { decompiler.Settings = this.Decompiler.Settings; PEFile module = Decompiler.TypeSystem.MainModule.PEFile; decompiler.AssemblyResolver = new UniversalAssemblyResolver(module.FileName, false, module.Reader.DetectTargetFrameworkId()); decompiler.DebugInfoProvider = this.Decompiler.DebugInfoProvider; decompiler.DecompileProject(module, path); } catch (Exception e) { WriteVerbose(e.ToString()); WriteError(new ErrorRecord(e, ErrorIds.DecompilationFailed, ErrorCategory.OperationStopped, null)); } WriteObject(decompiler); }
static void DecompileAsProject(string assemblyFileName, string outputDirectory) { ModuleDefinition module = LoadModule(assemblyFileName); WholeProjectDecompiler decompiler = new WholeProjectDecompiler(); decompiler.DecompileProject(module, outputDirectory); }
public void DecompileAsProject(string assemblyFileName, string outputDirectory) { ModuleDefinition module = UniversalAssemblyResolver.LoadMainModule(assemblyFileName); WholeProjectDecompiler decompiler = new WholeProjectDecompiler(); decompiler.DecompileProject(module, outputDirectory); }
static void DecompileAsProject(string assemblyFileName, string outputDirectory) { WholeProjectDecompiler decompiler = new WholeProjectDecompiler(); var module = new PEFile(assemblyFileName); decompiler.AssemblyResolver = new UniversalAssemblyResolver(assemblyFileName, false, module.Reader.DetectTargetFrameworkId()); decompiler.DecompileProject(module, outputDirectory); }
internal static void Decompile(this ScopedFileInfo file, DirectoryInfo outputDirectory) { var decompiler = new WholeProjectDecompiler() { AssemblyResolver = new UniversalAssemblyResolver(file.Name, false, string.Empty) }; using var peFile = new PEFile(file.FullName); decompiler.DecompileProject(peFile, outputDirectory.FullName); }
private void DoDecompile(string path) { PEFile module = Decompiler.TypeSystem.MainModule.PEFile; var assemblyResolver = new UniversalAssemblyResolver(module.FileName, false, module.Reader.DetectTargetFrameworkId()); WholeProjectDecompiler decompiler = new WholeProjectDecompiler(assemblyResolver); decompiler.ProgressIndicator = this; fileName = module.FileName; completed = 0; decompiler.DecompileProject(module, path); }
static void DecompileAsProject(string assemblyFileName, string outputDirectory, string[] referencePaths) { var decompiler = new WholeProjectDecompiler(); var module = new PEFile(assemblyFileName); var resolver = new UniversalAssemblyResolver(assemblyFileName, false, module.Reader.DetectTargetFrameworkId()); foreach (var path in referencePaths) { resolver.AddSearchDirectory(path); } decompiler.AssemblyResolver = resolver; decompiler.DecompileProject(module, outputDirectory); }
public static void DecompileProject(string assemblyName, string outputDirectory) { Directory.CreateDirectory(outputDirectory); var assemblyFileName = Path.Combine(Program.ManagedDir, assemblyName); WholeProjectDecompiler decompiler = new WholeProjectDecompiler(); var settings = decompiler.Settings; settings.OutVariables = false; Console.WriteLine($"Decompiling {assemblyName} - {decompiler.LanguageVersion}"); var module = new PEFile(assemblyFileName); decompiler.AssemblyResolver = new UniversalAssemblyResolver(assemblyFileName, false, module.Reader.DetectTargetFrameworkId()); decompiler.DecompileProject(module, outputDirectory); FixReferences(Path.Combine(outputDirectory, $"{module.Name}.csproj")); }
/// <summary> /// 使用 ILSpy 反编译 Assembly。 /// </summary> /// <param name="assemblyPath">Assembly 路径</param> /// <param name="outputPath">输出路径</param> private static void DecompileAssembly(string assemblyPath, string outputPath) { if (Directory.Exists(outputPath)) { Directory.Delete(outputPath, true); } Directory.CreateDirectory(outputPath); WholeProjectDecompiler decompiler = new WholeProjectDecompiler(); var module = new PEFile(assemblyPath); decompiler.AssemblyResolver = new UniversalAssemblyResolver(assemblyPath, false, module.Reader.DetectTargetFrameworkId(assemblyPath)); decompiler.DecompileProject(module, outputPath); }
private static string DecompileTarget(string assemblyFileName) { string decompileDirectory = string.Empty; if (File.Exists(assemblyFileName)) { decompileDirectory = FileUtilities.GetDecompileDirectory(assemblyFileName, false); ModuleDefinition module = null; WholeProjectDecompiler decompiler = null; if (Directory.Exists(decompileDirectory) && Directory.GetFiles(decompileDirectory).Count() > 0) { module = UniversalAssemblyResolver.LoadMainModule(assemblyFileName, false); decompiler = new WholeProjectDecompiler(); decompiler.Settings.ThrowOnAssemblyResolveErrors = false; decompileDirectory = FileUtilities.GetDecompileDirectory(assemblyFileName, false); if (Directory.Exists(decompileDirectory) && Directory.GetFiles(decompileDirectory).Count() > 0) { ConsoleOutput.SystemMessage($"Already decompiled located here {decompileDirectory}"); return(decompileDirectory); } else { Directory.CreateDirectory(decompileDirectory); } try { ConsoleOutput.SystemMessage($"Decompiling {assemblyFileName} to {decompileDirectory}"); decompiler.DecompileProject(module, decompileDirectory); } catch (Exception ex) { var message = ex.InnerException != null ? ex.InnerException.Message : ex.Message; ConsoleOutput.ErrorMessage($"Decompiling {assemblyFileName} threw an exception with the message {message}"); } } else { ConsoleOutput.ErrorMessage($"The assembly '{assemblyFileName}' does not exist"); } } return(decompileDirectory); }
int DecompileAsProject(string assemblyFileName, string outputDirectory) { var decompiler = new WholeProjectDecompiler() { Settings = GetSettings() }; var module = new PEFile(assemblyFileName); var resolver = new UniversalAssemblyResolver(assemblyFileName, false, module.Reader.DetectTargetFrameworkId()); foreach (var path in ReferencePaths) { resolver.AddSearchDirectory(path); } decompiler.AssemblyResolver = resolver; decompiler.DecompileProject(module, outputDirectory); module.Reader.Dispose(); return(0); }
protected override void ProcessRecord() { string path = GetUnresolvedProviderPathFromPSPath(LiteralPath); if (!Directory.Exists(path)) { WriteObject("Destination directory must exist prior to decompilation"); return; } try { WholeProjectDecompiler decompiler = new WholeProjectDecompiler(); decompiler.DecompileProject(Decompiler.TypeSystem.MainModule.PEFile, path); WriteObject("Decompilation finished"); } catch (Exception e) { WriteVerbose(e.ToString()); WriteError(new ErrorRecord(e, ErrorIds.DecompilationFailed, ErrorCategory.OperationStopped, null)); } }
protected override void ProcessRecord() { string path = GetUnresolvedProviderPathFromPSPath(LiteralPath); if (!Directory.Exists(path)) { WriteObject("Destination directory must exist prior to decompilation"); return; } try { string assemblyFileName = Decompiler.TypeSystem.Compilation.MainAssembly.UnresolvedAssembly.Location; // just to keep the API "the same" across all cmdlets ModuleDefinition module = UniversalAssemblyResolver.LoadMainModule(assemblyFileName); WholeProjectDecompiler decompiler = new WholeProjectDecompiler(); decompiler.DecompileProject(module, path); WriteObject("Decompilation finished"); } catch (Exception e) { WriteVerbose(e.ToString()); WriteError(new ErrorRecord(e, ErrorIds.DecompilationFailed, ErrorCategory.OperationStopped, null)); } }
static void DecompileAsProject(string assemblyFileName, string outputDirectory) { WholeProjectDecompiler decompiler = new WholeProjectDecompiler(); decompiler.DecompileProject(new PEFile(assemblyFileName), outputDirectory); }
public bool Decomple(string targetProj, string source, bool bConsole) { targetProj = Environment.ExpandEnvironmentVariables(targetProj); source = Environment.ExpandEnvironmentVariables(source); string targetDirectory = Path.GetDirectoryName(targetProj); string targetName = Path.GetFileNameWithoutExtension(targetProj); bool bSuccess = false; try { using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read)) { WholeProjectDecompiler decompiler = new WholeProjectDecompiler(); PEFile moduleDefinition = new PEFile(source, fs, PEStreamOptions.PrefetchEntireImage); CancellationToken cancellationToken = default(CancellationToken); UniversalAssemblyResolver universalAssemblyResolver = new UniversalAssemblyResolver(source, false, moduleDefinition.Reader.DetectTargetFrameworkId(), PEStreamOptions.PrefetchEntireImage); decompiler.AssemblyResolver = universalAssemblyResolver; decompiler.DecompileProject(moduleDefinition, targetDirectory, cancellationToken); // Set runtime to SB runtime // Update case target name // Copy and rename paths for dlls string runtime; try { string sblPath = MainWindow.InstallDir + "\\SmallBasicLibrary.dll"; Assembly sblAssembly = Assembly.LoadFile(sblPath); TargetFrameworkAttribute attrib = (TargetFrameworkAttribute)sblAssembly.GetCustomAttribute(typeof(TargetFrameworkAttribute)); runtime = attrib.FrameworkName.Substring(attrib.FrameworkName.Length - 4); } catch { runtime = "v4.5"; } string result = targetDirectory + "\\" + moduleDefinition.Name + ".csproj"; XmlDocument doc = new XmlDocument(); doc.Load(result); XmlNodeList elemList = doc.GetElementsByTagName("AssemblyName"); if (elemList.Count == 1 && elemList[0].InnerText == moduleDefinition.Name) { elemList[0].InnerText = targetName; } elemList = doc.GetElementsByTagName("TargetFrameworkVersion"); if (elemList.Count == 1) { elemList[0].InnerText = runtime; } elemList = doc.GetElementsByTagName("OutputType"); if (bConsole && elemList.Count == 1 && elemList[0].InnerText == "WinExe") { elemList[0].InnerText = "Exe"; } elemList = doc.GetElementsByTagName("HintPath"); foreach (XmlNode xmlNode in elemList) { if (xmlNode.InnerText.EndsWith(".dll")) { string dll = targetDirectory + "\\" + Path.GetFileName(xmlNode.InnerText); File.Copy(xmlNode.InnerText, dll); xmlNode.InnerText = dll; } } File.Delete(result); doc.Save(targetProj); //Set Main string projectFile = targetDirectory + "\\_SmallBasicProgram.cs"; if (File.Exists(projectFile)) { string prog = File.ReadAllText(projectFile); prog = prog.Replace("static void _Main()", "static void Main()"); if (bConsole) { prog = prog.Replace("SmallBasicApplication.BeginProgram();", "SmallBasicApplication.BeginProgram();\r\n\t\t//Initialise and hide TextWindow for Console App\r\n\t\tTextWindow.Show();\r\n\t\tTextWindow.Hide();"); } File.WriteAllText(projectFile, prog); } } MainWindow.Errors.Add(new Error("Decompile : " + "Successfully decompiled assembly to " + targetProj)); bSuccess = true; } catch (Exception ex) { MainWindow.Errors.Add(new Error("Decomple : " + ex.Message)); bSuccess = false; } //Start VS or open containing folder //if (File.Exists(targetProj)) Process.Start(targetProj); //else if (Directory.Exists(targetDirectory)) Process.Start("explorer.exe", "\"" + targetDirectory + "\""); if (Directory.Exists(targetDirectory)) { Process.Start("explorer.exe", "\"" + targetDirectory + "\""); } return(bSuccess); }