Esempio n. 1
0
 public void Setup()
 {
     assemblyList = new AssemblyList();
     testAssembly = assemblyList.OpenAssembly(typeof(MethodUsesAnalyzerTests).Assembly.Location);
     assemblyList.OpenAssembly(typeof(void).Assembly.Location);
     testAssemblyTypeSystem = testAssembly.GetTypeSystemOrNull();
     language       = new CSharpLanguage();
     typeDefinition = testAssemblyTypeSystem.FindType(typeof(TestCases.Main.MainAssembly)).GetDefinition();
 }
Esempio n. 2
0
 public void Setup()
 {
     Stub.SetupApplication();
     Options.DecompilerSettingsPanel.TestSetup(new Decompiler.DecompilerSettings());
     assemblyList = new AssemblyList("Test");
     testAssembly = assemblyList.OpenAssembly(typeof(MethodUsesAnalyzerTests).Assembly.Location);
     assemblyList.OpenAssembly(typeof(void).Assembly.Location);
     testAssemblyTypeSystem = testAssembly.GetTypeSystemOrNull();
     language       = new CSharpLanguage();
     typeDefinition = testAssemblyTypeSystem.FindType(typeof(TestCases.Main.MainAssembly)).GetDefinition();
 }
Esempio n. 3
0
        public static void Main(string[] args)
        {
            AssemblyList assemblyList = new AssemblyList("name");

            if (args.Length != 3)
            {
                Exit(0, "Usage: ILSpyMono.exe  list-of-dependencies.txt-or-dir  path/to/library.dll  path/to/output/dir");
            }
            string DepsFileOrDir = args [0], DllFile = args [1], OutPath = args [2];

            if (!File.Exists(DllFile))
            {
                Error("Assembly file not found: " + DllFile);
            }
            if (Directory.Exists(OutPath))
            {
                Console.Out.WriteLine("WARNING: Output path already exists and will be overwritten: " + OutPath);
            }
            else
            {
                Directory.CreateDirectory(OutPath);
            }
            if (DepsFileOrDir != "-")
            {
                foreach (string path in LoadDeps(DepsFileOrDir))
                {
                    assemblyList.OpenAssembly(path, true);
                }
            }
            LoadedAssembly asm = assemblyList.OpenAssembly(DllFile, true);

            if (asm.HasLoadError)
            {
                Error("Failed loading assembly");
            }

            using (var writer = new System.IO.StreamWriter(OutPath + "/" + asm.ShortName + ".csproj")) {
                try {
                    new CSharpLanguage().DecompileAssembly(asm,
                                                           new PlainTextOutput(writer),
                                                           new DecompilationOptions {
                        FullDecompilation = true, SaveAsProjectDirectory = OutPath
                    });
                } catch (Exception ex) {
                    Error("Exception: " + ex.ToString());
                }
            }

            Console.Out.WriteLine("Done.");
        }
Esempio n. 4
0
 public override void Drop(DragEventArgs e, int index)
 {
     string[] files = e.Data.GetData(AssemblyTreeNode.DataFormat) as string[];
     if (files == null)
     {
         files = e.Data.GetData(DataFormats.FileDrop) as string[];
     }
     if (files != null)
     {
         lock (assemblyList.assemblies) {
             var assemblies = (from file in files
                               where file != null
                               select assemblyList.OpenAssembly(file) into node
                               where node != null
                               select node).Distinct().ToList();
             foreach (LoadedAssembly asm in assemblies)
             {
                 int nodeIndex = assemblyList.assemblies.IndexOf(asm);
                 if (nodeIndex < index)
                 {
                     index--;
                 }
                 assemblyList.assemblies.RemoveAt(nodeIndex);
             }
             assemblies.Reverse();
             foreach (LoadedAssembly asm in assemblies)
             {
                 assemblyList.assemblies.Insert(index, asm);
             }
         }
     }
 }
Esempio n. 5
0
 public void Setup()
 {
     assemblyList           = new AssemblyList();
     testAssembly           = assemblyList.OpenAssembly(typeof(MethodUsesAnalyzerTests).Assembly.Location);
     testAssemblyTypeSystem = new DecompilerTypeSystem(testAssembly.GetPEFileOrNull(), testAssembly.GetAssemblyResolver());
     language = new CSharpLanguage();
 }
Esempio n. 6
0
 public void Setup()
 {
     Stub.SetupApplication();
     Options.DecompilerSettingsPanel.TestSetup(new Decompiler.DecompilerSettings());
     assemblyList           = new AssemblyList("Test");
     testAssembly           = assemblyList.OpenAssembly(typeof(MethodUsesAnalyzerTests).Assembly.Location);
     testAssemblyTypeSystem = new SimpleCompilation(testAssembly.GetPEFileOrNull(), assemblyList.OpenAssembly(typeof(void).Assembly.Location).GetPEFileOrNull());
     language = new CSharpLanguage();
 }
 private IEnumerable <LoadedAssembly> OpenAssembly(AssemblyList assemblyList, string file)
 {
     if (file.EndsWith(".nupkg"))
     {
         LoadedNugetPackage package = new LoadedNugetPackage(file);
         // TODO: show dialog
         //var selectionDialog = new NugetPackageBrowserDialog(package);
         //selectionDialog.Owner = Application.Current.MainWindow;
         //if (await selectionDialog.ShowDialog<bool>() != true)
         //	yield break;
         //foreach (var entry in selectionDialog.SelectedItems) {
         foreach (var entry in package.SelectedEntries)
         {
             var nugetAsm = assemblyList.OpenAssembly("nupkg://" + file + ";" + entry.Name, entry.Stream, true);
             if (nugetAsm != null)
             {
                 yield return(nugetAsm);
             }
         }
         yield break;
     }
     yield return(assemblyList.OpenAssembly(file));
 }
Esempio n. 8
0
 public override void Drop(DragEventArgs e, int index)
 {
     string[] files = e.Data.GetData(AssemblyTreeNode.DataFormat) as string[];
     if (files == null)
     {
         files = e.Data.GetData(DataFormats.FileDrop) as string[];
     }
     if (files != null)
     {
         var assemblies = files
                          .Where(file => file != null)
                          .Select(file => assemblyList.OpenAssembly(file))
                          .Where(asm => asm != null)
                          .Distinct()
                          .ToArray();
         assemblyList.Move(assemblies, index);
         var nodes = assemblies.SelectArray(MainWindow.Instance.FindTreeNode);
         MainWindow.Instance.SelectNodes(nodes);
     }
 }
Esempio n. 9
0
    static int Main(string[] args)
    {
        if (args.Length != 2)
        {
            Console.WriteLine("usage: decompiler [dll] [output path]");
            return 2;
        }

        var assemblies = new AssemblyList("global");
        var assembly = assemblies.OpenAssembly(args[0]);
        assembly.WaitUntilLoaded();
        var root = new AssemblyTreeNode(assembly);
        root.Decompile(new CSharpLanguage(), new PlainTextOutput(), new DecompilationOptions
        {
            SaveAsProjectDirectory = args[1],
            FullDecompilation = true,
            DecompilerSettings = new DecompilerSettings
            {
                YieldReturn = false
            }
        });
        return 0;
    }
Esempio n. 10
0
 public override void Drop(DragEventArgs e, int index)
 {
     string[] files = e.Data.GetData(AssemblyTreeNode.DataFormat) as string[];
     if (files == null)
     {
         files = e.Data.GetData(DataFormats.FileDrop) as string[];
     }
     if (files != null)
     {
         lock (assemblyList.assemblies)
         {
             var assemblies = files
                              .Where(file => file != null)
                              .Select(file => assemblyList.OpenAssembly(file))
                              .Where(asm => asm != null)
                              .Distinct()
                              .ToArray();
             foreach (LoadedAssembly asm in assemblies)
             {
                 int nodeIndex = assemblyList.assemblies.IndexOf(asm);
                 if (nodeIndex < index)
                 {
                     index--;
                 }
                 assemblyList.assemblies.RemoveAt(nodeIndex);
             }
             Array.Reverse(assemblies);
             foreach (LoadedAssembly asm in assemblies)
             {
                 assemblyList.assemblies.Insert(index, asm);
             }
             var nodes = assemblies.SelectArray(MainWindow.Instance.FindTreeNode);
             MainWindow.Instance.SelectNodes(nodes);
         }
     }
 }
Esempio n. 11
0
        private static void Main(string[] args)
        {
            // Console.WriteLine("Args: {0}", string.Join(", ", args));
            // Console.Read();

            // AttachConsoleWriter();

            string             appPath         = null;
            string             slnName         = null;
            string             libPath         = null;
            string             expOpt          = null;
            string             outLanguageType = LAN_TYPE_CSHARP;
            DecompilerSettings ds = new DecompilerSettings();

            ds.AnonymousMethods = true;
            ds.AsyncAwait       = true;
            ds.YieldReturn      = true;
            string onlyDecomileClassName = null;
            bool   dllPathOnTop          = true;

            List <string> onlyDecompilingFileNameList = new List <string>();

            //parsing args
            foreach (string x in args)
            {
                if (x.StartsWith("-"))
                {
                    switch (x)
                    {
                    case "-n":
                    case "-l":
                    case "-t":
                    case "-C":
                    case "-D":
                        expOpt = x;
                        continue;

                    case "-q":
                        dllPathOnTop = false;
                        continue;

                    default:

                        if (x.Length < 2)
                        {
                            Console.WriteLine(" Unexpected options " + x);
                            showUsage();
                            return;
                        }

                        for (int i = 1; i < x.Length; i++)
                        {
                            if (!praseDecompileSetting(x[i], ds))
                            {
                                Console.WriteLine(" Unexpected options " + x);
                                showUsage();
                                return;
                            }
                        }
                        continue;
                    }
                }
                else if (expOpt != null)
                {
                    switch (expOpt)
                    {
                    case "-n":
                        slnName = x;
                        expOpt  = null;
                        break;

                    case "-l":
                        libPath = x;
                        expOpt  = null;
                        break;

                    case "-t":
                        if (x != LAN_TYPE_CSHARP && x != LAN_TYPE_IL)
                        {
                            Console.WriteLine(" Unexpected Output language type: " + x);
                            showUsage();
                            return;
                        }
                        outLanguageType = x;
                        expOpt          = null;
                        break;

                    case "-C":
                        onlyDecomileClassName = x;
                        expOpt = null;
                        break;

                    case "-D":
                        onlyDecompilingFileNameList.Add(x);
                        break;

                    default:
                        showUsage();
                        expOpt = null;
                        return;
                    }
                }
                else
                {
                    if (appPath == null)
                    {
                        appPath = x;
                        continue;
                    }
                    else
                    {
                        Console.WriteLine(" Unexpected options " + x);
                        showUsage();
                        return;
                    }
                }
            }

            if (appPath == null)
            {
                Console.WriteLine("directory/to/all/your/dll missing");
                showUsage();
                return;
            }

            if (slnName == null && outLanguageType == LAN_TYPE_CSHARP)
            {
                Console.WriteLine("Solution Name missing");
                showUsage();
                return;
            }

            Console.WriteLine("Decompiling all dll in  " + appPath);
            Console.WriteLine("Please wait...");

            // Extensions.WriteRead($"Path: {appPath}");

            DirectoryInfo di = new DirectoryInfo(appPath);

            appPath = di.FullName;
            FileInfo[] dllFileInfoList = di.GetFiles("*.dll");
            FileInfo[] exeFileInfoList = di.GetFiles("*.exe");

            AssemblyList asmlist = new AssemblyList("mylistname");

            foreach (var dllfile in dllFileInfoList)
            {
                bool bDecompile = isDecompilingFile(dllfile.FullName, onlyDecompilingFileNameList);
                asmlist.OpenAssembly(dllfile.FullName, !bDecompile);
            }

            foreach (var dllfile in exeFileInfoList)
            {
                bool bDecompile = isDecompilingFile(dllfile.FullName, onlyDecompilingFileNameList);

                asmlist.OpenAssembly(dllfile.FullName, !bDecompile);
            }

            if (libPath != null)
            {
                di              = new DirectoryInfo(libPath);
                libPath         = di.FullName;
                dllFileInfoList = di.GetFiles("*.dll");
                foreach (var dllfile in dllFileInfoList)
                {
                    asmlist.OpenAssembly(dllfile.FullName, true);
                }
            }

            StringBuilder projSln = new StringBuilder();

            projSln.Append("Microsoft Visual Studio Solution File, Format Version 11.00\n# Visual Studio 2010\n");

            StringBuilder globSec     = new StringBuilder();
            Guid          slnProjGuid = Guid.NewGuid();

            int num = 0;

            LoadedAssembly[] ls      = asmlist.GetAssemblies();
            var decompilationOptions = new DecompilationOptions();

            decompilationOptions.AssemblyFileNameOnTop = dllPathOnTop;
            decompilationOptions.FullDecompilation     = true;
            decompilationOptions.assenmlyList          = asmlist;
            decompilationOptions.DecompilerSettings    = ds;
            decompilationOptions.IncludedClassName     = onlyDecomileClassName;

            if (outLanguageType == LAN_TYPE_CSHARP)
            {
                foreach (LoadedAssembly asm in ls)
                {
                    if (asm.IsAutoLoaded)
                    {
                        continue;
                    }

                    string projectPath = appPath + "/" + asm.ShortName;
                    if (!Directory.Exists(projectPath))
                    {
                        Directory.CreateDirectory(projectPath);
                    }
                    string projectFileName = projectPath + "/" + asm.ShortName + ".csproj";
                    asm.ProjectGuid     = Guid.NewGuid();
                    asm.ProjectFileName = projectFileName;
                }
            }

            foreach (LoadedAssembly asm in ls)
            {
                num++;
                Console.WriteLine(asm.FileName + " " + num + "/" + ls.Length);
                if (asm.IsAutoLoaded)
                {
                    continue;
                }

                if (outLanguageType == LAN_TYPE_CSHARP)
                {
                    var csharpLanguage = new CSharpLanguage();
                    var textOutput     = new PlainTextOutput();
                    decompilationOptions.SaveAsProjectDirectory = appPath + "/" + asm.ShortName;

                    csharpLanguage.DecompileAssembly(asm, textOutput, decompilationOptions);
                    File.WriteAllText(asm.ProjectFileName, textOutput.ToString());

                    Guid createdProjGuid = asm.ProjectGuid;

                    projSln.Append("   Project(\"{");
                    projSln.Append(slnProjGuid.ToString());
                    projSln.Append("}\") = \"");
                    projSln.Append(asm.ShortName);
                    projSln.Append("\", \"");
                    projSln.Append(asm.ShortName + "/" + asm.ShortName + ".csproj");
                    projSln.Append("\", \"{");
                    projSln.Append(createdProjGuid.ToString());
                    projSln.Append("}\"\n");
                    projSln.Append("EndProject\n");

                    globSec.Append("   {" + createdProjGuid.ToString() + "}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n");
                    globSec.Append("   {" + createdProjGuid.ToString() + "}.Debug|Any CPU.Build.0 = Debug|Any CPU\n");
                    globSec.Append("   {" + createdProjGuid.ToString() + "}.Release|Any CPU.ActiveCfg = Release|Any CPU\n");
                    globSec.Append("   {" + createdProjGuid.ToString() + "}.Release|Any CPU.Build.0 = Release|Any CPU\n");
                }
                else
                {
                    var ilLanguage = new ILLanguage(true);
                    var textOutput = new PlainTextOutput();
                    ilLanguage.DecompileAssembly(asm, textOutput, decompilationOptions);
                    string ilFileName = appPath + "/" + asm.ShortName + ".il";
                    File.WriteAllText(ilFileName, textOutput.ToString());
                }
            }

            if (outLanguageType == LAN_TYPE_CSHARP)
            {
                projSln.Append("Global\n");
                projSln.Append("GlobalSection(SolutionConfigurationPlatforms) = preSolution\n");
                projSln.Append("\t\t\t\tDebug|Any CPU = Debug|Any CPU\n");
                projSln.Append("\t\t\t\tRelease|Any CPU = Release|Any CPU\n");
                projSln.Append("EndGlobalSection\n");

                projSln.Append("GlobalSection(ProjectConfigurationPlatforms) = postSolution\n");
                projSln.Append(globSec.ToString());
                projSln.Append("EndGlobalSection\n");

                projSln.Append("GlobalSection(MonoDevelopProperties) = preSolution\n");
                projSln.Append("\nEndGlobalSection\n");
                projSln.Append("EndGlobal\n\t\t");

                string slnFileName = appPath + "/" + slnName + ".sln";
                File.WriteAllText(slnFileName, projSln.ToString());
            }
        }
Esempio n. 12
0
 public override void Drop(DragEventArgs e, int index)
 {
     Debug.Assert(!DisableDrop);
     if (DisableDrop)
     {
         return;
     }
     string[] files = e.Data.GetData(AssemblyTreeNode.DataFormat) as string[];
     if (files == null)
     {
         files = e.Data.GetData(DataFormats.FileDrop) as string[];
     }
     if (files != null && files.Length > 0)
     {
         LoadedAssembly newSelectedAsm        = null;
         bool           newSelectedAsmExisted = false;
         var            oldIgnoreSelChg       = MainWindow.Instance.TreeView_SelectionChanged_ignore;
         try {
             lock (assemblyList.GetLockObj()) {
                 int numFiles = assemblyList.Count_NoLock;
                 var old      = assemblyList.IsReArranging;
                 try {
                     MainWindow.Instance.TreeView_SelectionChanged_ignore = true;
                     var assemblies = (from file in files
                                       where file != null
                                       select assemblyList.OpenAssembly(file) into node
                                       where node != null
                                       select node).Distinct().ToList();
                     var oldAsm = new Dictionary <LoadedAssembly, bool>(assemblies.Count);
                     foreach (LoadedAssembly asm in assemblies)
                     {
                         int nodeIndex = assemblyList.IndexOf_NoLock(asm);
                         oldAsm[asm] = nodeIndex < numFiles;
                         if (newSelectedAsm == null)
                         {
                             newSelectedAsm        = asm;
                             newSelectedAsmExisted = oldAsm[asm];
                         }
                         if (nodeIndex < index)
                         {
                             index--;
                         }
                         numFiles--;
                         assemblyList.IsReArranging = oldAsm[asm];
                         assemblyList.RemoveAt_NoLock(nodeIndex);
                         assemblyList.IsReArranging = old;
                     }
                     assemblies.Reverse();
                     foreach (LoadedAssembly asm in assemblies)
                     {
                         assemblyList.IsReArranging = oldAsm[asm];
                         assemblyList.Insert_NoLock(index, asm);
                         assemblyList.IsReArranging = old;
                     }
                 }
                 finally {
                     assemblyList.IsReArranging = old;
                 }
             }
             if (newSelectedAsm != null)
             {
                 if (!newSelectedAsmExisted)
                 {
                     MainWindow.Instance.TreeView_SelectionChanged_ignore = oldIgnoreSelChg;
                 }
                 var node = MainWindow.Instance.FindTreeNode(newSelectedAsm.AssemblyDefinition) ??
                            MainWindow.Instance.FindTreeNode(newSelectedAsm.ModuleDefinition);
                 if (node != null)
                 {
                     MainWindow.Instance.treeView.FocusNode(node);
                     MainWindow.Instance.treeView.SelectedItem = node;
                 }
             }
         }
         finally {
             MainWindow.Instance.TreeView_SelectionChanged_ignore = oldIgnoreSelChg;
         }
     }
 }