void WriteProject(LoadedAssembly loadedAssembly, Language language, string targetDirectory, CancellationToken ct) { targetDirectory = Path.Combine(targetDirectory, loadedAssembly.ShortName); string projectFileName = Path.Combine(targetDirectory, loadedAssembly.ShortName + language.ProjectFileExtension); if (!Directory.Exists(targetDirectory)) { try { Directory.CreateDirectory(targetDirectory); } catch (Exception e) { statusOutput.Add($"Failed to create a directory '{targetDirectory}':{Environment.NewLine}{e}"); return; } } try { using (var projectFileWriter = new StreamWriter(projectFileName)) { var projectFileOutput = new PlainTextOutput(projectFileWriter); var options = new DecompilationOptions() { FullDecompilation = true, CancellationToken = ct, SaveAsProjectDirectory = targetDirectory }; var projectInfo = language.DecompileAssembly(loadedAssembly, projectFileOutput, options); if (projectInfo != null) { projects.Add(new ProjectItem(projectFileName, projectInfo.PlatformName, projectInfo.Guid, projectInfo.TypeGuid)); } } } catch (Exception e) when(!(e is OperationCanceledException)) { statusOutput.Add($"Failed to decompile the assembly '{loadedAssembly.FileName}':{Environment.NewLine}{e}"); } }
void WriteProject(LoadedAssembly loadedAssembly, Language language, string targetDirectory, CancellationToken ct) { targetDirectory = Path.Combine(targetDirectory, loadedAssembly.ShortName); if (language.ProjectFileExtension == null) { statusOutput.Add("-------------"); statusOutput.Add($"Language '{language.Name}' does not support exporting assemblies as projects!"); return; } string projectFileName = Path.Combine(targetDirectory, loadedAssembly.ShortName + language.ProjectFileExtension); if (File.Exists(targetDirectory)) { statusOutput.Add("-------------"); statusOutput.Add($"Failed to create a directory '{targetDirectory}':{Environment.NewLine}A file with the same name already exists!"); return; } if (!Directory.Exists(targetDirectory)) { try { Directory.CreateDirectory(targetDirectory); } catch (Exception e) { statusOutput.Add("-------------"); statusOutput.Add($"Failed to create a directory '{targetDirectory}':{Environment.NewLine}{e}"); return; } } try { using (var projectFileWriter = new StreamWriter(projectFileName)) { var projectFileOutput = new PlainTextOutput(projectFileWriter); var options = new DecompilationOptions() { FullDecompilation = true, CancellationToken = ct, SaveAsProjectDirectory = targetDirectory }; var projectInfo = language.DecompileAssembly(loadedAssembly, projectFileOutput, options); if (projectInfo != null) { projects.Add(new ProjectItem(projectFileName, projectInfo.PlatformName, projectInfo.Guid, projectInfo.TypeGuid)); } } } catch (NotSupportedException e) { statusOutput.Add("-------------"); statusOutput.Add($"Failed to decompile the assembly '{loadedAssembly.FileName}':{Environment.NewLine}{e.Message}"); } catch (PathTooLongException e) { statusOutput.Add("-------------"); statusOutput.Add(string.Format(Properties.Resources.ProjectExportPathTooLong, loadedAssembly.FileName) + Environment.NewLine + Environment.NewLine + e.ToString()); } catch (Exception e) when(!(e is OperationCanceledException)) { statusOutput.Add("-------------"); statusOutput.Add($"Failed to decompile the assembly '{loadedAssembly.FileName}':{Environment.NewLine}{e}"); } }
public string Decompile(string language, object o) { if (o == null) { return(String.Empty); } Language l = CreateLanguage(language); if (l == null) { return(String.Format("Can't create language: {0}", language)); } ITextOutput output = new RtfTextOutput(); DecompilationOptions options = new DecompilationOptions(); if (o is AssemblyDefinition) { l.DecompileAssembly((AssemblyDefinition)o, output, options); } else if (o is TypeDefinition) { l.DecompileType((TypeDefinition)o, output, options); } else if (o is MethodDefinition) { l.DecompileMethod((MethodDefinition)o, output, options); } else if (o is FieldDefinition) { l.DecompileField((FieldDefinition)o, output, options); } else if (o is PropertyDefinition) { l.DecompileProperty((PropertyDefinition)o, output, options); } else if (o is EventDefinition) { l.DecompileEvent((EventDefinition)o, output, options); } else if (o is AssemblyNameReference) { output.Write("// Assembly Reference "); output.WriteDefinition(o.ToString(), null); output.WriteLine(); } else if (o is ModuleReference) { output.Write("// Module Reference "); output.WriteDefinition(o.ToString(), null); output.WriteLine(); } else { output.Write(String.Format("// {0} ", o.GetType().Name)); output.WriteDefinition(o.ToString(), null); output.WriteLine(); } return(output.ToString()); }