From 65458c92037beabc9a6b1ac63e2af7822113f663 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sat, 28 May 2016 00:14:56 +0900 Subject: [PATCH] fix #712 - Saving Project does not Convert *.resources to *.resx for use in Visual Studio 2015 on C# & VB Projects. --- ILSpy/Languages/CSharpLanguage.cs | 74 ------------------------- ILSpy/Languages/Language.cs | 89 +++++++++++++++++++++++++++++++ ILSpy/VB/VBLanguage.cs | 85 +---------------------------- 3 files changed, 90 insertions(+), 158 deletions(-) diff --git a/ILSpy/Languages/CSharpLanguage.cs b/ILSpy/Languages/CSharpLanguage.cs index 51e81a9b4..85235cb96 100644 --- a/ILSpy/Languages/CSharpLanguage.cs +++ b/ILSpy/Languages/CSharpLanguage.cs @@ -17,12 +17,10 @@ // DEALINGS IN THE SOFTWARE. using System; -using System.Collections; using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; using System.Linq; -using System.Resources; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Xml; @@ -508,78 +506,6 @@ namespace ICSharpCode.ILSpy } #endregion - #region WriteResourceFilesInProject - IEnumerable> WriteResourceFilesInProject(LoadedAssembly assembly, DecompilationOptions options, HashSet directories) - { - foreach (EmbeddedResource r in assembly.ModuleDefinition.Resources.OfType()) { - Stream stream = r.GetResourceStream(); - stream.Position = 0; - - IEnumerable entries; - if (r.Name.EndsWith(".resources", StringComparison.OrdinalIgnoreCase) && GetEntries(stream, out entries) && entries.All(e => e.Value is Stream)) { - foreach (var pair in entries) { - string fileName = Path.Combine(((string)pair.Key).Split('/').Select(p => TextView.DecompilerTextView.CleanUpName(p)).ToArray()); - string dirName = Path.GetDirectoryName(fileName); - if (!string.IsNullOrEmpty(dirName) && directories.Add(dirName)) { - Directory.CreateDirectory(Path.Combine(options.SaveAsProjectDirectory, dirName)); - } - Stream entryStream = (Stream)pair.Value; - bool handled = false; - foreach (var handler in App.CompositionContainer.GetExportedValues()) { - if (handler.CanHandle(fileName, options)) { - handled = true; - entryStream.Position = 0; - yield return Tuple.Create(handler.EntryType, handler.WriteResourceToFile(assembly, fileName, entryStream, options)); - break; - } - } - if (!handled) { - using (FileStream fs = new FileStream(Path.Combine(options.SaveAsProjectDirectory, fileName), FileMode.Create, FileAccess.Write)) - { - entryStream.Position = 0; - entryStream.CopyTo(fs); - } - yield return Tuple.Create("EmbeddedResource", fileName); - } - } - } else { - string fileName = GetFileNameForResource(r.Name, directories); - using (FileStream fs = new FileStream(Path.Combine(options.SaveAsProjectDirectory, fileName), FileMode.Create, FileAccess.Write)) - { - stream.CopyTo(fs); - } - yield return Tuple.Create("EmbeddedResource", fileName); - } - } - } - - string GetFileNameForResource(string fullName, HashSet directories) - { - string[] splitName = fullName.Split('.'); - string fileName = TextView.DecompilerTextView.CleanUpName(fullName); - for (int i = splitName.Length - 1; i > 0; i--) { - string ns = string.Join(".", splitName, 0, i); - if (directories.Contains(ns)) { - string name = string.Join(".", splitName, i, splitName.Length - i); - fileName = Path.Combine(ns, TextView.DecompilerTextView.CleanUpName(name)); - break; - } - } - return fileName; - } - - bool GetEntries(Stream stream, out IEnumerable entries) - { - try { - entries = new ResourceSet(stream).Cast(); - return true; - } catch (ArgumentException) { - entries = null; - return false; - } - } - #endregion - AstBuilder CreateAstBuilder(DecompilationOptions options, ModuleDefinition currentModule = null, TypeDefinition currentType = null, bool isSingleMember = false) { if (currentModule == null) diff --git a/ILSpy/Languages/Language.cs b/ILSpy/Languages/Language.cs index 750dd8186..dc169f81e 100644 --- a/ILSpy/Languages/Language.cs +++ b/ILSpy/Languages/Language.cs @@ -17,7 +17,11 @@ // DEALINGS IN THE SOFTWARE. using System; +using System.Collections; using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Resources; using ICSharpCode.Decompiler; using Mono.Cecil; @@ -162,5 +166,90 @@ namespace ICSharpCode.ILSpy { return member; } + + #region WriteResourceFilesInProject + protected virtual IEnumerable> WriteResourceFilesInProject(LoadedAssembly assembly, DecompilationOptions options, HashSet directories) + { + foreach (EmbeddedResource r in assembly.ModuleDefinition.Resources.OfType()) { + Stream stream = r.GetResourceStream(); + stream.Position = 0; + + IEnumerable entries; + if (r.Name.EndsWith(".resources", StringComparison.OrdinalIgnoreCase)) { + if (GetEntries(stream, out entries) && entries.All(e => e.Value is Stream)) { + foreach (var pair in entries) { + string fileName = Path.Combine(((string)pair.Key).Split('/').Select(p => TextView.DecompilerTextView.CleanUpName(p)).ToArray()); + string dirName = Path.GetDirectoryName(fileName); + if (!string.IsNullOrEmpty(dirName) && directories.Add(dirName)) { + Directory.CreateDirectory(Path.Combine(options.SaveAsProjectDirectory, dirName)); + } + Stream entryStream = (Stream)pair.Value; + bool handled = false; + foreach (var handler in App.CompositionContainer.GetExportedValues()) { + if (handler.CanHandle(fileName, options)) { + handled = true; + entryStream.Position = 0; + yield return Tuple.Create(handler.EntryType, handler.WriteResourceToFile(assembly, fileName, entryStream, options)); + break; + } + } + if (!handled) { + using (FileStream fs = new FileStream(Path.Combine(options.SaveAsProjectDirectory, fileName), FileMode.Create, FileAccess.Write)) { + entryStream.Position = 0; + entryStream.CopyTo(fs); + } + yield return Tuple.Create("EmbeddedResource", fileName); + } + } + } else { + stream.Position = 0; + string fileName = GetFileNameForResource(Path.ChangeExtension(r.Name, ".resx"), directories); + using (ResourceReader reader = new ResourceReader(stream)) + using (FileStream fs = new FileStream(Path.Combine(options.SaveAsProjectDirectory, fileName), FileMode.Create, FileAccess.Write)) + using (ResXResourceWriter writer = new ResXResourceWriter(fs)) { + foreach (DictionaryEntry entry in reader) { + writer.AddResource((string)entry.Key, entry.Value); + } + } + yield return Tuple.Create("EmbeddedResource", fileName); + } + } else { + string fileName = GetFileNameForResource(r.Name, directories); + using (FileStream fs = new FileStream(Path.Combine(options.SaveAsProjectDirectory, fileName), FileMode.Create, FileAccess.Write)) { + stream.Position = 0; + stream.CopyTo(fs); + } + yield return Tuple.Create("EmbeddedResource", fileName); + } + } + } + + string GetFileNameForResource(string fullName, HashSet directories) + { + string[] splitName = fullName.Split('.'); + string fileName = TextView.DecompilerTextView.CleanUpName(fullName); + for (int i = splitName.Length - 1; i > 0; i--) { + string ns = string.Join(".", splitName, 0, i); + if (directories.Contains(ns)) { + string name = string.Join(".", splitName, i, splitName.Length - i); + fileName = Path.Combine(ns, TextView.DecompilerTextView.CleanUpName(name)); + break; + } + } + return fileName; + } + + bool GetEntries(Stream stream, out IEnumerable entries) + { + try { + entries = new ResourceSet(stream).Cast(); + return true; + } catch (ArgumentException) { + entries = null; + return false; + } + } + #endregion + } } diff --git a/ILSpy/VB/VBLanguage.cs b/ILSpy/VB/VBLanguage.cs index d9e9801e2..c98d329f0 100644 --- a/ILSpy/VB/VBLanguage.cs +++ b/ILSpy/VB/VBLanguage.cs @@ -17,12 +17,10 @@ // DEALINGS IN THE SOFTWARE. using System; -using System.Collections; using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; using System.Linq; -using System.Resources; using System.Threading.Tasks; using System.Xml; @@ -30,12 +28,10 @@ using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.Ast; using ICSharpCode.Decompiler.Ast.Transforms; using ICSharpCode.ILSpy.XmlDoc; -using ICSharpCode.NRefactory.TypeSystem; -using ICSharpCode.NRefactory.TypeSystem.Implementation; -using CSharp = ICSharpCode.NRefactory.CSharp; using ICSharpCode.NRefactory.VB; using ICSharpCode.NRefactory.VB.Visitors; using Mono.Cecil; +using CSharp = ICSharpCode.NRefactory.CSharp; namespace ICSharpCode.ILSpy.VB { @@ -321,85 +317,6 @@ namespace ICSharpCode.ILSpy.VB } #endregion - #region WriteResourceFilesInProject - IEnumerable> WriteResourceFilesInProject(LoadedAssembly assembly, DecompilationOptions options, HashSet directories) - { - foreach (EmbeddedResource r in assembly.ModuleDefinition.Resources.OfType()) { - Stream stream = r.GetResourceStream(); - stream.Position = 0; - - IEnumerable entries; - if (r.Name.EndsWith(".resources", StringComparison.OrdinalIgnoreCase) && GetEntries(stream, out entries) && entries.All(e => e.Value is Stream)) { - foreach (var pair in entries) { - string fileName = Path.Combine(((string)pair.Key).Split('/').Select(p => TextView.DecompilerTextView.CleanUpName(p)).ToArray()); - string dirName = Path.GetDirectoryName(fileName); - if (!string.IsNullOrEmpty(dirName) && directories.Add(dirName)) - { - Directory.CreateDirectory(Path.Combine(options.SaveAsProjectDirectory, dirName)); - } - Stream entryStream = (Stream)pair.Value; - bool handled = false; - foreach (var handler in App.CompositionContainer.GetExportedValues()) - { - if (handler.CanHandle(fileName, options)) { - handled = true; - entryStream.Position = 0; - yield return Tuple.Create(handler.EntryType, handler.WriteResourceToFile(assembly, fileName, entryStream, options)); - break; - } - } - if (!handled) { - using (FileStream fs = new FileStream(Path.Combine(options.SaveAsProjectDirectory, fileName), FileMode.Create, FileAccess.Write)) - { - entryStream.Position = 0; - entryStream.CopyTo(fs); - } - yield return Tuple.Create("EmbeddedResource", fileName); - } - } - } else { - string fileName = GetFileNameForResource(r.Name, directories); - using (FileStream fs = new FileStream(Path.Combine(options.SaveAsProjectDirectory, fileName), FileMode.Create, FileAccess.Write)) - { - stream.CopyTo(fs); - } - yield return Tuple.Create("EmbeddedResource", fileName); - } - } - } - - string GetFileNameForResource(string fullName, HashSet directories) - { - string[] splitName = fullName.Split('.'); - string fileName = TextView.DecompilerTextView.CleanUpName(fullName); - for (int i = splitName.Length - 1; i > 0; i--) - { - string ns = string.Join(".", splitName, 0, i); - if (directories.Contains(ns)) - { - string name = string.Join(".", splitName, i, splitName.Length - i); - fileName = Path.Combine(ns, TextView.DecompilerTextView.CleanUpName(name)); - break; - } - } - return fileName; - } - - bool GetEntries(Stream stream, out IEnumerable entries) - { - try - { - entries = new ResourceSet(stream).Cast(); - return true; - } - catch (ArgumentException) - { - entries = null; - return false; - } - } - #endregion - public override void DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options) { WriteCommentLine(output, TypeToString(method.DeclaringType, includeNamespace: true));