public override void DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options) { if (options.FullDecompilation && options.SaveAsProjectDirectory != null) { //Checks if must create a solution if (options.CreateSolution) { //Solution directory var solutionDir = options.SaveAsProjectDirectory; //List of the project names and their guid List <Tuple <string, string> > projects = new List <Tuple <string, string> >(); //For each module foreach (var module in assembly.AssemblyDefinition.Modules) { //Creates the project and the various files var projectDir = Path.Combine(solutionDir, TextView.DecompilerTextView.CleanUpName(Path.GetFileNameWithoutExtension(module.Name))); Directory.CreateDirectory(projectDir); options.SaveAsProjectDirectory = projectDir; HashSet <string> directories = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var files = WriteCodeFilesInProject(module, options, directories).ToList(); files.AddRange(WriteResourceFilesInProject(module, options, directories)); using (var writer = new StreamWriter(Path.Combine(projectDir, Path.GetFileName(projectDir) + this.ProjectFileExtension), false, System.Text.Encoding.UTF8)) projects.Add(Tuple.Create( Path.GetFileName(projectDir), "{" + WriteProjectFile(writer, files, module, options).ToString().ToUpperInvariant() + "}" )); } //Writes the solution WriteSolutionFile(new TextOutputWriter(output), Enumerable.Reverse(projects)); } else { HashSet <string> directories = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var files = assembly.AssemblyDefinition.Modules.SelectMany(m => WriteCodeFilesInProject(m, options, directories)).ToList(); files.AddRange(assembly.AssemblyDefinition.Modules.SelectMany(m => WriteResourceFilesInProject(m, options, directories))); WriteProjectFile(new TextOutputWriter(output), files, assembly.AssemblyDefinition.MainModule, options); } } else { base.DecompileAssembly(assembly, output, options); output.WriteLine(); output.WriteLine("// Main module:"); ModuleDefinition mainModule = assembly.AssemblyDefinition.MainModule; WriteModuleAttributes(mainModule, output, options); output.WriteLine(); // don't automatically load additional assemblies when an assembly node is selected in the tree view using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) { AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.AssemblyDefinition.MainModule); codeDomBuilder.AddAssembly(assembly.AssemblyDefinition, onlyAssemblyLevel: !options.FullDecompilation); codeDomBuilder.RunTransformations(transformAbortCondition); codeDomBuilder.GenerateCode(output); } } OnDecompilationFinished(null); }
public override void DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options) { output.WriteLine("// " + assembly.FileName); output.WriteLine(); var module = assembly.GetPEFileOrNull(); var metadata = module.Metadata; var dis = CreateDisassembler(output, options); // don't automatically load additional assemblies when an assembly node is selected in the tree view using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) { dis.AssemblyResolver = module.GetAssemblyResolver(); dis.DebugInfo = module.GetDebugInfoOrNull(); if (options.FullDecompilation) { dis.WriteAssemblyReferences(metadata); } if (metadata.IsAssembly) { dis.WriteAssemblyHeader(module); } output.WriteLine(); dis.WriteModuleHeader(module); if (options.FullDecompilation) { output.WriteLine(); output.WriteLine(); dis.WriteModuleContents(module); } } }
public override void DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options) { var module = assembly.GetModuleDefinitionAsync().Result; if (options.FullDecompilation && options.SaveAsProjectDirectory != null) { var decompiler = new ILSpyWholeProjectDecompiler(assembly, options); decompiler.ProjectGuid = App.CommandLineArguments.FixedGuid; decompiler.DecompileProject(module, options.SaveAsProjectDirectory, new TextOutputWriter(output), options.CancellationToken); } else { AddReferenceWarningMessage(module.Assembly, output); output.WriteLine(); base.DecompileAssembly(assembly, output, options); ModuleDefinition mainModule = module; if (mainModule.Types.Count > 0) { output.Write("// Global type: "); output.WriteReference(mainModule.Types[0].FullName, mainModule.Types[0]); output.WriteLine(); } if (mainModule.EntryPoint != null) { output.Write("// Entry point: "); output.WriteReference(mainModule.EntryPoint.DeclaringType.FullName + "." + mainModule.EntryPoint.Name, mainModule.EntryPoint); output.WriteLine(); } output.WriteLine("// Architecture: " + GetPlatformDisplayName(mainModule)); if ((mainModule.Attributes & ModuleAttributes.ILOnly) == 0) { output.WriteLine("// This assembly contains unmanaged code."); } string runtimeName = GetRuntimeDisplayName(mainModule); if (runtimeName != null) { output.WriteLine("// Runtime: " + runtimeName); } output.WriteLine(); // don't automatically load additional assemblies when an assembly node is selected in the tree view using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) { CSharpDecompiler decompiler = new CSharpDecompiler(module, options.DecompilerSettings); decompiler.CancellationToken = options.CancellationToken; SyntaxTree st; if (options.FullDecompilation) { st = decompiler.DecompileWholeModuleAsSingleFile(); } else { st = decompiler.DecompileModuleAndAssemblyAttributes(); } WriteCode(output, options.DecompilerSettings, st, decompiler.TypeSystem); } } }
public override void DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options) { if (options.FullDecompilation && options.SaveAsProjectDirectory != null) { HashSet <string> directories = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var files = WriteCodeFilesInProject(assembly.ModuleDefinition, options, directories).ToList(); files.AddRange(WriteResourceFilesInProject(assembly, options, directories)); WriteProjectFile(new TextOutputWriter(output), files, assembly.ModuleDefinition); } else { base.DecompileAssembly(assembly, output, options); output.WriteLine(); ModuleDefinition mainModule = assembly.ModuleDefinition; if (mainModule.EntryPoint != null) { output.Write("// Entry point: "); output.WriteReference(mainModule.EntryPoint.DeclaringType.FullName + "." + mainModule.EntryPoint.Name, mainModule.EntryPoint); output.WriteLine(); } output.WriteLine("// Architecture: " + GetPlatformDisplayName(mainModule)); if ((mainModule.Attributes & ModuleAttributes.ILOnly) == 0) { output.WriteLine("// This assembly contains unmanaged code."); } switch (mainModule.Runtime) { case TargetRuntime.Net_1_0: output.WriteLine("// Runtime: .NET 1.0"); break; case TargetRuntime.Net_1_1: output.WriteLine("// Runtime: .NET 1.1"); break; case TargetRuntime.Net_2_0: output.WriteLine("// Runtime: .NET 2.0"); break; case TargetRuntime.Net_4_0: output.WriteLine("// Runtime: .NET 4.0"); break; } output.WriteLine(); // don't automatically load additional assemblies when an assembly node is selected in the tree view using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) { AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.ModuleDefinition); codeDomBuilder.AddAssembly(assembly.ModuleDefinition, onlyAssemblyLevel: !options.FullDecompilation); codeDomBuilder.RunTransformations(transformAbortCondition); GenerateCode(codeDomBuilder, output); } } }
public override void DecompileModule(ModuleDefinition module, ITextOutput output, DecompilationOptions options) { WriteModuleAttributes(module, output, options); // don't automatically load additional assemblies when an assembly node is selected in the tree view output.WriteLine(); using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) { AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module); codeDomBuilder.AddModule(module, onlyModuleLevel: !options.FullDecompilation); codeDomBuilder.RunTransformations(transformAbortCondition); codeDomBuilder.GenerateCode(output); } }
IEnumerable <Tuple <string, string> > WriteAssemblyInfo(ModuleDefinition module, DecompilationOptions options, HashSet <string> directories) { // don't automatically load additional assemblies when an assembly node is selected in the tree view using (LoadedAssembly.DisableAssemblyLoad()) { AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module); codeDomBuilder.AddAssembly(module, onlyAssemblyLevel: true); codeDomBuilder.RunTransformations(transformAbortCondition); string prop = "Properties"; if (directories.Add("Properties")) { Directory.CreateDirectory(Path.Combine(options.SaveAsProjectDirectory, prop)); } string assemblyInfo = Path.Combine(prop, "AssemblyInfo" + this.FileExtension); using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, assemblyInfo))) codeDomBuilder.GenerateCode(new PlainTextOutput(w)); return(new Tuple <string, string>[] { Tuple.Create("Compile", assemblyInfo) }); } }
public override void DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options) { if (options.FullDecompilation && options.SaveAsProjectDirectory != null) { HashSet <string> directories = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var files = WriteCodeFilesInProject(assembly.AssemblyDefinition, options, directories).ToList(); files.AddRange(WriteResourceFilesInProject(assembly, options, directories)); WriteProjectFile(new TextOutputWriter(output), files, assembly.AssemblyDefinition.MainModule); } else { base.DecompileAssembly(assembly, output, options); // don't automatically load additional assemblies when an assembly node is selected in the tree view using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) { AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.AssemblyDefinition.MainModule); codeDomBuilder.AddAssembly(assembly.AssemblyDefinition, onlyAssemblyLevel: !options.FullDecompilation); codeDomBuilder.RunTransformations(transformAbortCondition); codeDomBuilder.GenerateCode(output); } } }
public override ProjectId DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options) { output.WriteLine("// " + assembly.FileName); output.WriteLine(); var module = assembly.GetPEFileOrNull(); var metadata = module.Metadata; var dis = CreateDisassembler(output, options); if (options.FullDecompilation && options.SaveAsProjectDirectory != null) { throw new NotSupportedException($"Language '{Name}' does not support exporting assemblies as projects!"); } // don't automatically load additional assemblies when an assembly node is selected in the tree view using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad(assembly.AssemblyList)) { dis.AssemblyResolver = module.GetAssemblyResolver(); dis.DebugInfo = module.GetDebugInfoOrNull(); if (options.FullDecompilation) { dis.WriteAssemblyReferences(metadata); } if (metadata.IsAssembly) { dis.WriteAssemblyHeader(module); } output.WriteLine(); dis.WriteModuleHeader(module); if (options.FullDecompilation) { output.WriteLine(); output.WriteLine(); dis.WriteModuleContents(module); } } return(null); }
public override ProjectId DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options) { var module = assembly.GetPEFileOrNull(); if (options.FullDecompilation && options.SaveAsProjectDirectory != null) { var decompiler = new ILSpyWholeProjectDecompiler(assembly, options); return(decompiler.DecompileProject(module, options.SaveAsProjectDirectory, new TextOutputWriter(output), options.CancellationToken)); } else { AddReferenceAssemblyWarningMessage(module, output); AddReferenceWarningMessage(module, output); output.WriteLine(); base.DecompileAssembly(assembly, output, options); // don't automatically load additional assemblies when an assembly node is selected in the tree view using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) { IAssemblyResolver assemblyResolver = assembly.GetAssemblyResolver(); var typeSystem = new DecompilerTypeSystem(module, assemblyResolver, options.DecompilerSettings); var globalType = typeSystem.MainModule.TypeDefinitions.FirstOrDefault(); if (globalType != null) { output.Write("// Global type: "); output.WriteReference(globalType, globalType.FullName); output.WriteLine(); } var metadata = module.Metadata; var corHeader = module.Reader.PEHeaders.CorHeader; var entrypointHandle = MetadataTokenHelpers.EntityHandleOrNil(corHeader.EntryPointTokenOrRelativeVirtualAddress); if (!entrypointHandle.IsNil && entrypointHandle.Kind == HandleKind.MethodDefinition) { var entrypoint = typeSystem.MainModule.ResolveMethod(entrypointHandle, new Decompiler.TypeSystem.GenericContext()); if (entrypoint != null) { output.Write("// Entry point: "); output.WriteReference(entrypoint, entrypoint.DeclaringType.FullName + "." + entrypoint.Name); output.WriteLine(); } } output.WriteLine("// Architecture: " + GetPlatformDisplayName(module)); if ((corHeader.Flags & System.Reflection.PortableExecutable.CorFlags.ILOnly) == 0) { output.WriteLine("// This assembly contains unmanaged code."); } string runtimeName = GetRuntimeDisplayName(module); if (runtimeName != null) { output.WriteLine("// Runtime: " + runtimeName); } if ((corHeader.Flags & System.Reflection.PortableExecutable.CorFlags.StrongNameSigned) != 0) { output.WriteLine("// This assembly is signed with a strong name key."); } if (metadata.IsAssembly) { var asm = metadata.GetAssemblyDefinition(); if (asm.HashAlgorithm != AssemblyHashAlgorithm.None) { output.WriteLine("// Hash algorithm: " + asm.HashAlgorithm.ToString().ToUpper()); } if (!asm.PublicKey.IsNil) { output.Write("// Public key: "); var reader = metadata.GetBlobReader(asm.PublicKey); while (reader.RemainingBytes > 0) { output.Write(reader.ReadByte().ToString("x2")); } output.WriteLine(); } } var debugInfo = assembly.GetDebugInfoOrNull(); if (debugInfo != null) { output.WriteLine("// Debug info: " + debugInfo.Description); } output.WriteLine(); CSharpDecompiler decompiler = new CSharpDecompiler(typeSystem, options.DecompilerSettings); decompiler.CancellationToken = options.CancellationToken; SyntaxTree st; if (options.FullDecompilation) { st = decompiler.DecompileWholeModuleAsSingleFile(); } else { st = decompiler.DecompileModuleAndAssemblyAttributes(); } WriteCode(output, options.DecompilerSettings, st, decompiler.TypeSystem); } return(null); } }