public override bool View(DecompilerTextView textView)
 {
     try
     {
         AvalonEditTextOutput output = new AvalonEditTextOutput();
         Data.Position = 0;
         IconBitmapDecoder decoder = new IconBitmapDecoder(Data, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
         foreach (var frame in decoder.Frames)
         {
             output.Write(String.Format("{0}x{1}, {2} bit: ", frame.PixelHeight, frame.PixelWidth, frame.Thumbnail.Format.BitsPerPixel));
             AddIcon(output, frame);
             output.WriteLine();
         }
         output.AddButton(Images.Save, "Save", delegate
         {
             Save(null);
         });
         textView.ShowNode(output, this);
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Exemple #2
0
 public override bool View(TabPageModel tabPage)
 {
     try
     {
         AvalonEditTextOutput output = new AvalonEditTextOutput();
         using var data = OpenStream();
         if (data == null)
         {
             return(false);
         }
         IconBitmapDecoder decoder = new IconBitmapDecoder(data, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
         foreach (var frame in decoder.Frames)
         {
             output.Write(String.Format("{0}x{1}, {2} bit: ", frame.PixelHeight, frame.PixelWidth, frame.Thumbnail.Format.BitsPerPixel));
             AddIcon(output, frame);
             output.WriteLine();
         }
         output.AddButton(Images.Save, Resources.Save, delegate {
             Save(null);
         });
         tabPage.ShowTextView(textView => textView.ShowNode(output, this));
         tabPage.SupportsLanguageSwitching = false;
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
        public override bool View(DecompilerTextView textView)
        {
            try {
                AvalonEditTextOutput output = new AvalonEditTextOutput();
                Data.Position = 0;
                BitmapImage image = new BitmapImage();

                //HACK: windows imaging does not understand that .cur files have the same layout as .ico
                // so load to data, and modify the ResourceType in the header to make look like an icon...
                byte[] curData = ((MemoryStream)Data).ToArray();
                curData[2] = 1;
                using (Stream stream = new MemoryStream(curData)) {
                    image.BeginInit();
                    image.StreamSource = stream;
                    image.EndInit();
                }

                output.AddUIElement(() => new Image {
                    Source = image
                });
                output.WriteLine();
                output.AddButton(Images.Save, "Save", delegate {
                    Save(null);
                });
                textView.ShowNode(output, this, null);
                return(true);
            }
            catch (Exception) {
                return(false);
            }
        }
 public override bool View(TabPageModel tabPage)
 {
     try
     {
         AvalonEditTextOutput output = new AvalonEditTextOutput();
         BitmapImage          image  = new BitmapImage();
         image.BeginInit();
         image.StreamSource = OpenStream();
         image.EndInit();
         output.AddUIElement(() => new Image {
             Source = image
         });
         output.WriteLine();
         output.AddButton(Images.Save, Resources.Save, delegate {
             Save(null);
         });
         tabPage.ShowTextView(textView => textView.ShowNode(output, this));
         tabPage.SupportsLanguageSwitching = false;
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
        public void Execute(TextViewContext context)
        {
            // Get all assemblies in the selection that are stored inside a package.
            var selectedNodes = context.SelectedTreeNodes.OfType <AssemblyTreeNode>()
                                .Where(asm => asm.PackageEntry != null).ToArray();
            // Get root assembly to infer the initial directory for the save dialog.
            var bundleNode = selectedNodes.FirstOrDefault()?.Ancestors().OfType <AssemblyTreeNode>()
                             .FirstOrDefault(asm => asm.PackageEntry == null);

            if (bundleNode == null)
            {
                return;
            }
            var            assembly = selectedNodes[0].PackageEntry;
            SaveFileDialog dlg      = new SaveFileDialog();

            dlg.FileName         = Path.GetFileName(WholeProjectDecompiler.SanitizeFileName(assembly.Name));
            dlg.Filter           = ".NET assemblies|*.dll;*.exe;*.winmd" + Resources.AllFiles;
            dlg.InitialDirectory = Path.GetDirectoryName(bundleNode.LoadedAssembly.FileName);
            if (dlg.ShowDialog() != true)
            {
                return;
            }

            string fileName = dlg.FileName;
            string outputFolderOrFileName = fileName;

            if (selectedNodes.Length > 1)
            {
                outputFolderOrFileName = Path.GetDirectoryName(outputFolderOrFileName);
            }

            Docking.DockWorkspace.Instance.RunWithCancellation(ct => Task <AvalonEditTextOutput> .Factory.StartNew(() => {
                AvalonEditTextOutput output = new AvalonEditTextOutput();
                Stopwatch stopwatch         = Stopwatch.StartNew();
                stopwatch.Stop();

                if (selectedNodes.Length == 1)
                {
                    SaveEntry(output, selectedNodes[0].PackageEntry, outputFolderOrFileName);
                }
                else
                {
                    foreach (var node in selectedNodes)
                    {
                        var fileName = Path.GetFileName(WholeProjectDecompiler.SanitizeFileName(node.PackageEntry.Name));
                        SaveEntry(output, node.PackageEntry, Path.Combine(outputFolderOrFileName, fileName));
                    }
                }
                output.WriteLine(Resources.GenerationCompleteInSeconds, stopwatch.Elapsed.TotalSeconds.ToString("F1"));
                output.WriteLine();
                output.AddButton(null, Resources.OpenExplorer, delegate { Process.Start("explorer", "/select,\"" + fileName + "\""); });
                output.WriteLine();
                return(output);
            }, ct)).Then(output => Docking.DockWorkspace.Instance.ShowText(output)).HandleExceptions();
        }
Exemple #6
0
        public override bool View(TabPageModel tabPage)
        {
            try
            {
                AvalonEditTextOutput output = new AvalonEditTextOutput();
                BitmapImage          image  = new BitmapImage();
                byte[] curData;
                using (var data = OpenStream())
                {
                    if (data == null)
                    {
                        return(false);
                    }
                    //HACK: windows imaging does not understand that .cur files have the same layout as .ico
                    // so load to data, and modify the ResourceType in the header to make look like an icon...
                    MemoryStream s = data as MemoryStream;
                    if (s == null)
                    {
                        // data was stored in another stream type (e.g. PinnedBufferedMemoryStream)
                        s = new MemoryStream();
                        data.CopyTo(s);
                    }
                    curData = s.ToArray();
                }
                curData[2] = 1;
                using (Stream stream = new MemoryStream(curData))
                {
                    image.BeginInit();
                    image.StreamSource = stream;
                    image.EndInit();
                }

                output.AddUIElement(() => new Image {
                    Source = image
                });
                output.WriteLine();
                output.AddButton(Images.Save, Resources.Save, delegate {
                    Save(null);
                });
                tabPage.ShowTextView(textView => textView.ShowNode(output, this));
                tabPage.SupportsLanguageSwitching = false;
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        internal static void GeneratePdbForAssembly(LoadedAssembly assembly)
        {
            var file = assembly.GetPEFileOrNull();

            if (!PortablePdbWriter.HasCodeViewDebugDirectoryEntry(file))
            {
                MessageBox.Show(string.Format(Resources.CannotCreatePDBFile, Path.GetFileName(assembly.FileName)));
                return;
            }
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.FileName         = DecompilerTextView.CleanUpName(assembly.ShortName) + ".pdb";
            dlg.Filter           = Resources.PortablePDBPdbAllFiles;
            dlg.InitialDirectory = Path.GetDirectoryName(assembly.FileName);
            if (dlg.ShowDialog() != true)
            {
                return;
            }
            DecompilationOptions options = new DecompilationOptions();
            string fileName = dlg.FileName;

            Docking.DockWorkspace.Instance.RunWithCancellation(ct => Task <AvalonEditTextOutput> .Factory.StartNew(() => {
                AvalonEditTextOutput output = new AvalonEditTextOutput();
                Stopwatch stopwatch         = Stopwatch.StartNew();
                using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    try
                    {
                        var decompiler = new CSharpDecompiler(file, assembly.GetAssemblyResolver(), options.DecompilerSettings);
                        PortablePdbWriter.WritePdb(file, decompiler, options.DecompilerSettings, stream);
                    }
                    catch (OperationCanceledException)
                    {
                        output.WriteLine();
                        output.WriteLine(Resources.GenerationWasCancelled);
                        throw;
                    }
                }
                stopwatch.Stop();
                output.WriteLine(Resources.GenerationCompleteInSeconds, stopwatch.Elapsed.TotalSeconds.ToString("F1"));
                output.WriteLine();
                output.AddButton(null, Resources.OpenExplorer, delegate { Process.Start("explorer", "/select,\"" + fileName + "\""); });
                output.WriteLine();
                return(output);
            }, ct)).Then(output => Docking.DockWorkspace.Instance.ShowText(output)).HandleExceptions();
        }
Exemple #8
0
        internal static void GeneratePdbForAssembly(LoadedAssembly assembly)
        {
            var file = assembly.GetPEFileOrNull();

            if (!PortablePdbWriter.HasCodeViewDebugDirectoryEntry(file))
            {
                MessageBox.Show($"Cannot create PDB file for {Path.GetFileName(assembly.FileName)}, because it does not contain a PE Debug Directory Entry of type 'CodeView'.");
                return;
            }
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.FileName         = DecompilerTextView.CleanUpName(assembly.ShortName) + ".pdb";
            dlg.Filter           = "Portable PDB|*.pdb|All files|*.*";
            dlg.InitialDirectory = Path.GetDirectoryName(assembly.FileName);
            if (dlg.ShowDialog() != true)
            {
                return;
            }
            DecompilationOptions options = new DecompilationOptions();
            string fileName = dlg.FileName;

            MainWindow.Instance.TextView.RunWithCancellation(ct => Task <AvalonEditTextOutput> .Factory.StartNew(() => {
                AvalonEditTextOutput output = new AvalonEditTextOutput();
                Stopwatch stopwatch         = Stopwatch.StartNew();
                using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)) {
                    try {
                        var decompiler = new CSharpDecompiler(file, assembly.GetAssemblyResolver(), options.DecompilerSettings);
                        PortablePdbWriter.WritePdb(file, decompiler, options.DecompilerSettings, stream);
                    } catch (OperationCanceledException) {
                        output.WriteLine();
                        output.WriteLine("Generation was cancelled.");
                        throw;
                    }
                }
                stopwatch.Stop();
                output.WriteLine("Generation complete in " + stopwatch.Elapsed.TotalSeconds.ToString("F1") + " seconds.");
                output.WriteLine();
                output.AddButton(null, "Open Explorer", delegate { Process.Start("explorer", "/select,\"" + fileName + "\""); });
                output.WriteLine();
                return(output);
            }, ct)).Then(output => MainWindow.Instance.TextView.ShowText(output)).HandleExceptions();
        }
 internal override bool View(DecompilerTextView textView)
 {
     try {
         AvalonEditTextOutput output = new AvalonEditTextOutput();
         data.Position = 0;
         BitmapImage image = new BitmapImage();
         image.BeginInit();
         image.StreamSource = data;
         image.EndInit();
         output.AddUIElement(() => new Image {
             Source = image
         });
         output.WriteLine();
         output.AddButton(Images.Save, "Save", delegate { Save(null); });
         textView.Show(output, null);
         return(true);
     } catch (Exception) {
         return(false);
     }
 }
Exemple #10
0
        public override bool View(DecompilerTextView textView)
        {
            try {
                AvalonEditTextOutput output = new AvalonEditTextOutput();
                Data.Position = 0;
                BitmapImage image = new BitmapImage();

                //HACK: windows imaging does not understand that .cur files have the same layout as .ico
                // so load to data, and modify the ResourceType in the header to make look like an icon...
                MemoryStream s = Data as MemoryStream;
                if (null == s)
                {
                    // data was stored in another stream type (e.g. PinnedBufferedMemoryStream)
                    s = new MemoryStream();
                    Data.CopyTo(s);
                }
                byte[] curData = s.ToArray();
                curData[2] = 1;
                using (Stream stream = new MemoryStream(curData)) {
                    image.BeginInit();
                    image.StreamSource = stream;
                    image.EndInit();
                }

                output.AddUIElement(() => new Image {
                    Source = image
                });
                output.WriteLine();
                output.AddButton(ImageCache.Instance.GetImage("Save", BackgroundType.Button), "Save", delegate {
                    Save(null);
                });
                textView.ShowNode(output, this);
                return(true);
            }
            catch (Exception) {
                return(false);
            }
        }
 public override bool View(DecompilerTextView textView)
 {
     try {
         AvalonEditTextOutput output = new AvalonEditTextOutput();
         Data.Position = 0;
         BitmapImage image = new BitmapImage();
         image.BeginInit();
         image.StreamSource = Data;
         image.EndInit();
         output.AddUIElement(() => new Image {
             Source = image
         });
         output.WriteLine();
         output.AddButton(ImageCache.Instance.GetImage("Save", BackgroundType.Button), "Save", delegate {
             Save(null);
         });
         textView.ShowNode(output, this);
         return(true);
     }
     catch (Exception) {
         return(false);
     }
 }
Exemple #12
0
        async Task <AvalonEditTextOutput> CreateSolution(IEnumerable <LoadedAssembly> assemblies, Language language, CancellationToken ct)
        {
            var result = new AvalonEditTextOutput();

            var duplicates = new HashSet <string>();

            if (assemblies.Any(asm => !duplicates.Add(asm.ShortName)))
            {
                result.WriteLine("Duplicate assembly names selected, cannot generate a solution.");
                return(result);
            }

            Stopwatch stopwatch = Stopwatch.StartNew();

            try {
                await Task.Run(() => Parallel.ForEach(assemblies, n => WriteProject(n, language, solutionDirectory, ct)))
                .ConfigureAwait(false);

                await Task.Run(() => SolutionCreator.WriteSolutionFile(solutionFilePath, projects))
                .ConfigureAwait(false);
            } catch (AggregateException ae) {
                if (ae.Flatten().InnerExceptions.All(e => e is OperationCanceledException))
                {
                    result.WriteLine();
                    result.WriteLine("Generation was cancelled.");
                    return(result);
                }

                result.WriteLine();
                result.WriteLine("Failed to generate the Visual Studio Solution. Errors:");
                ae.Handle(e => {
                    result.WriteLine(e.Message);
                    return(true);
                });

                return(result);
            }

            foreach (var item in statusOutput)
            {
                result.WriteLine(item);
            }

            if (statusOutput.Count == 0)
            {
                result.WriteLine("Successfully decompiled the following assemblies into Visual Studio projects:");
                foreach (var item in assemblies.Select(n => n.Text.ToString()))
                {
                    result.WriteLine(item);
                }

                result.WriteLine();

                if (assemblies.Count() == projects.Count)
                {
                    result.WriteLine("Created the Visual Studio Solution file.");
                }

                result.WriteLine();
                result.WriteLine("Elapsed time: " + stopwatch.Elapsed.TotalSeconds.ToString("F1") + " seconds.");
                result.WriteLine();
                result.AddButton(null, "Open Explorer", delegate { Process.Start("explorer", "/select,\"" + solutionFilePath + "\""); });
            }

            return(result);
        }
Exemple #13
0
        async Task <AvalonEditTextOutput> CreateSolution(IEnumerable <LoadedAssembly> assemblies, Language language, CancellationToken ct)
        {
            var result = new AvalonEditTextOutput();

            var duplicates = new HashSet <string>();

            if (assemblies.Any(asm => !duplicates.Add(asm.ShortName)))
            {
                result.WriteLine("Duplicate assembly names selected, cannot generate a solution.");
                return(result);
            }

            Stopwatch stopwatch = Stopwatch.StartNew();

            try
            {
                // Explicitly create an enumerable partitioner here to avoid Parallel.ForEach's special cases for lists,
                // as those seem to use static partitioning which is inefficient if assemblies take differently
                // long to decompile.
                await Task.Run(() => Parallel.ForEach(Partitioner.Create(assemblies),
                                                      new ParallelOptions {
                    MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = ct
                },
                                                      n => WriteProject(n, language, solutionDirectory, ct)))
                .ConfigureAwait(false);

                if (projects.Count == 0)
                {
                    result.WriteLine();
                    result.WriteLine("Solution could not be created, because none of the selected assemblies could be decompiled into a project.");
                }
                else
                {
                    await Task.Run(() => SolutionCreator.WriteSolutionFile(solutionFilePath, projects))
                    .ConfigureAwait(false);
                }
            }
            catch (AggregateException ae)
            {
                if (ae.Flatten().InnerExceptions.All(e => e is OperationCanceledException))
                {
                    result.WriteLine();
                    result.WriteLine("Generation was cancelled.");
                    return(result);
                }

                result.WriteLine();
                result.WriteLine("Failed to generate the Visual Studio Solution. Errors:");
                ae.Handle(e => {
                    result.WriteLine(e.Message);
                    return(true);
                });

                return(result);
            }

            foreach (var item in statusOutput)
            {
                result.WriteLine(item);
            }

            if (statusOutput.Count == 0)
            {
                result.WriteLine("Successfully decompiled the following assemblies into Visual Studio projects:");
                foreach (var item in assemblies.Select(n => n.Text.ToString()))
                {
                    result.WriteLine(item);
                }

                result.WriteLine();

                if (assemblies.Count() == projects.Count)
                {
                    result.WriteLine("Created the Visual Studio Solution file.");
                }

                result.WriteLine();
                result.WriteLine("Elapsed time: " + stopwatch.Elapsed.TotalSeconds.ToString("F1") + " seconds.");
                result.WriteLine();
                result.AddButton(null, "Open Explorer", delegate { Process.Start("explorer", "/select,\"" + solutionFilePath + "\""); });
            }

            return(result);
        }