From 83bc512ae2533cd08507f96357a9ec41b39c1031 Mon Sep 17 00:00:00 2001 From: learn_more Date: Wed, 11 Feb 2015 20:29:32 +0100 Subject: [PATCH 1/2] Add commandline arguments for a fixed guid, and for specifying a directory where decompiled projects will be stored. all assemblies passed on the commandline are exported to this dir. --- ILSpy/CommandLineArguments.cs | 14 ++++++++++++++ ILSpy/Languages/CSharpLanguage.cs | 3 ++- ILSpy/MainWindow.xaml.cs | 24 ++++++++++++++++++++++++ ILSpy/VB/VBLanguage.cs | 3 ++- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/ILSpy/CommandLineArguments.cs b/ILSpy/CommandLineArguments.cs index a82b9eac1..963075561 100644 --- a/ILSpy/CommandLineArguments.cs +++ b/ILSpy/CommandLineArguments.cs @@ -29,6 +29,8 @@ namespace ICSharpCode.ILSpy public string NavigateTo; public string Language; public bool NoActivate; + public Guid? FixedGuid; + public string SaveDirectory; public CommandLineArguments(IEnumerable arguments) { @@ -46,6 +48,18 @@ namespace ICSharpCode.ILSpy this.Language = arg.Substring("/language:".Length); else if (arg.Equals("/noActivate", StringComparison.OrdinalIgnoreCase)) this.NoActivate = true; + else if (arg.StartsWith("/fixedGuid:", StringComparison.OrdinalIgnoreCase)) { + string guid = arg.Substring("/fixedGuid:".Length); + try { + if (guid.Length < 32) + guid = guid + new string('0', 32 - guid.Length); + this.FixedGuid = new Guid(guid); + } catch { + this.FixedGuid = null; + } + } + else if (arg.StartsWith("/saveDir:", StringComparison.OrdinalIgnoreCase)) + this.SaveDirectory = arg.Substring("/saveDir:".Length); } else { this.AssembliesToLoad.Add(arg); } diff --git a/ILSpy/Languages/CSharpLanguage.cs b/ILSpy/Languages/CSharpLanguage.cs index 1fb9c5ca0..1ac469c15 100644 --- a/ILSpy/Languages/CSharpLanguage.cs +++ b/ILSpy/Languages/CSharpLanguage.cs @@ -309,6 +309,7 @@ namespace ICSharpCode.ILSpy { const string ns = "http://schemas.microsoft.com/developer/msbuild/2003"; string platformName = GetPlatformName(module); + Guid guid = App.CommandLineArguments.FixedGuid ?? Guid.NewGuid(); using (XmlTextWriter w = new XmlTextWriter(writer)) { w.Formatting = Formatting.Indented; w.WriteStartDocument(); @@ -317,7 +318,7 @@ namespace ICSharpCode.ILSpy w.WriteAttributeString("DefaultTargets", "Build"); w.WriteStartElement("PropertyGroup"); - w.WriteElementString("ProjectGuid", Guid.NewGuid().ToString("B").ToUpperInvariant()); + w.WriteElementString("ProjectGuid", guid.ToString("B").ToUpperInvariant()); w.WriteStartElement("Configuration"); w.WriteAttributeString("Condition", " '$(Configuration)' == '' "); diff --git a/ILSpy/MainWindow.xaml.cs b/ILSpy/MainWindow.xaml.cs index 78400b727..32ffeecb8 100644 --- a/ILSpy/MainWindow.xaml.cs +++ b/ILSpy/MainWindow.xaml.cs @@ -295,9 +295,33 @@ namespace ICSharpCode.ILSpy // Select the newly loaded assembly JumpToReference(commandLineLoadedAssemblies[0].ModuleDefinition); } + if (!string.IsNullOrEmpty(args.SaveDirectory)) { + foreach (var x in commandLineLoadedAssemblies) { + x.ContinueWhenLoaded( (Task moduleTask) => { + OnExportAssembly(moduleTask, args.SaveDirectory); + }, TaskScheduler.FromCurrentSynchronizationContext()); + } + } commandLineLoadedAssemblies.Clear(); // clear references once we don't need them anymore } + void OnExportAssembly(Task moduleTask, string path) + { + AssemblyTreeNode asmNode = assemblyListTreeNode.FindAssemblyNode(moduleTask.Result); + if (asmNode != null) { + string file = DecompilerTextView.CleanUpName(asmNode.LoadedAssembly.ShortName); + Language language = sessionSettings.FilterSettings.Language; + DecompilationOptions options = new DecompilationOptions(); + options.FullDecompilation = true; + options.SaveAsProjectDirectory = Path.Combine(App.CommandLineArguments.SaveDirectory, file); + if (!Directory.Exists(options.SaveAsProjectDirectory)) { + Directory.CreateDirectory(options.SaveAsProjectDirectory); + } + string fullFile = Path.Combine(options.SaveAsProjectDirectory, file + language.ProjectFileExtension); + TextView.SaveToDisk(language, new[] { asmNode }, options, fullFile); + } + } + void MainWindow_Loaded(object sender, RoutedEventArgs e) { ILSpySettings spySettings = this.spySettings; diff --git a/ILSpy/VB/VBLanguage.cs b/ILSpy/VB/VBLanguage.cs index c47a95817..40552d308 100644 --- a/ILSpy/VB/VBLanguage.cs +++ b/ILSpy/VB/VBLanguage.cs @@ -126,6 +126,7 @@ namespace ICSharpCode.ILSpy.VB { const string ns = "http://schemas.microsoft.com/developer/msbuild/2003"; string platformName = CSharpLanguage.GetPlatformName(module); + Guid guid = App.CommandLineArguments.FixedGuid ?? Guid.NewGuid(); using (XmlTextWriter w = new XmlTextWriter(writer)) { w.Formatting = Formatting.Indented; w.WriteStartDocument(); @@ -134,7 +135,7 @@ namespace ICSharpCode.ILSpy.VB w.WriteAttributeString("DefaultTargets", "Build"); w.WriteStartElement("PropertyGroup"); - w.WriteElementString("ProjectGuid", Guid.NewGuid().ToString("B").ToUpperInvariant()); + w.WriteElementString("ProjectGuid", guid.ToString("B").ToUpperInvariant()); w.WriteStartElement("Configuration"); w.WriteAttributeString("Condition", " '$(Configuration)' == '' "); From 8ea39b77e614f472d8ab3d0c5f884cfc806e0b9c Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sat, 28 Feb 2015 12:39:18 +0100 Subject: [PATCH 2/2] replace try-catch-all with Guid.TryParse --- ILSpy/CommandLineArguments.cs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/ILSpy/CommandLineArguments.cs b/ILSpy/CommandLineArguments.cs index 475ccff60..0c4aa3e4c 100644 --- a/ILSpy/CommandLineArguments.cs +++ b/ILSpy/CommandLineArguments.cs @@ -53,15 +53,12 @@ namespace ICSharpCode.ILSpy this.NoActivate = true; else if (arg.StartsWith("/fixedGuid:", StringComparison.OrdinalIgnoreCase)) { string guid = arg.Substring("/fixedGuid:".Length); - try { - if (guid.Length < 32) - guid = guid + new string('0', 32 - guid.Length); - this.FixedGuid = new Guid(guid); - } catch { - this.FixedGuid = null; - } - } - else if (arg.StartsWith("/saveDir:", StringComparison.OrdinalIgnoreCase)) + if (guid.Length < 32) + guid = guid + new string('0', 32 - guid.Length); + Guid fixedGuid; + if (Guid.TryParse(guid, out fixedGuid)) + this.FixedGuid = fixedGuid; + } else if (arg.StartsWith("/saveDir:", StringComparison.OrdinalIgnoreCase)) this.SaveDirectory = arg.Substring("/saveDir:".Length); } else { this.AssembliesToLoad.Add(arg);