void Decompile() { Stream s = resource.GetResourceStream(); s.Position = 0; if (resource.Name.EndsWith(".g.resources", StringComparison.OrdinalIgnoreCase)) { IEnumerable<DictionaryEntry> rs = null; try { rs = new ResourceSet(s).Cast<DictionaryEntry>(); } catch (ArgumentException) { } if (rs != null && rs.All(e => e.Value is Stream)) { foreach (var pair in rs) { Stream entryStream = (Stream)pair.Value; byte[] d = new byte[entryStream.Length]; entryStream.Position = 0; if (pair.Key.ToString().EndsWith(".baml", StringComparison.OrdinalIgnoreCase)) { MemoryStream ms = new MemoryStream(); entryStream.CopyTo(ms); // TODO implement extension point // var decompiler = Baml.BamlResourceEntryNode.CreateBamlDecompilerInAppDomain(ref bamlDecompilerAppDomain, assembly.FileName); // string xaml = null; // try { // xaml = decompiler.DecompileBaml(ms, assembly.FileName, new ConnectMethodDecompiler(assembly), new AssemblyResolver(assembly)); // } // catch (XamlXmlWriterException) { } // ignore XAML writer exceptions // if (xaml != null) { // File.WriteAllText(Path.Combine(options.SaveAsProjectDirectory, Path.ChangeExtension(fileName, ".xaml")), xaml); // yield return Tuple.Create("Page", Path.ChangeExtension(fileName, ".xaml")); // continue; // } } else { entryStream.Read(d, 0, (int)entryStream.Length); } string tmp = Path.GetTempFileName(); File.WriteAllBytes(tmp, d); Entries.Add(pair.Key.ToString(), tmp); } } } }
IEnumerable<Tuple<string, string>> WriteResourceFilesInProject(LoadedAssembly assembly, DecompilationOptions options, HashSet<string> directories) { //AppDomain bamlDecompilerAppDomain = null; //try { foreach (EmbeddedResource r in assembly.AssemblyDefinition.MainModule.Resources.OfType<EmbeddedResource>()) { string fileName; Stream s = r.GetResourceStream(); s.Position = 0; if (r.Name.EndsWith(".g.resources", StringComparison.OrdinalIgnoreCase)) { IEnumerable<DictionaryEntry> rs = null; try { rs = new ResourceSet(s).Cast<DictionaryEntry>(); } catch (ArgumentException) { } if (rs != null && rs.All(e => e.Value is Stream)) { foreach (var pair in rs) { 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; entryStream.Position = 0; if (fileName.EndsWith(".baml", StringComparison.OrdinalIgnoreCase)) { MemoryStream ms = new MemoryStream(); entryStream.CopyTo(ms); // TODO implement extension point // var decompiler = Baml.BamlResourceEntryNode.CreateBamlDecompilerInAppDomain(ref bamlDecompilerAppDomain, assembly.FileName); // string xaml = null; // try { // xaml = decompiler.DecompileBaml(ms, assembly.FileName, new ConnectMethodDecompiler(assembly), new AssemblyResolver(assembly)); // } // catch (XamlXmlWriterException) { } // ignore XAML writer exceptions // if (xaml != null) { // File.WriteAllText(Path.Combine(options.SaveAsProjectDirectory, Path.ChangeExtension(fileName, ".xaml")), xaml); // yield return Tuple.Create("Page", Path.ChangeExtension(fileName, ".xaml")); // continue; // } } using (FileStream fs = new FileStream(Path.Combine(options.SaveAsProjectDirectory, fileName), FileMode.Create, FileAccess.Write)) { entryStream.CopyTo(fs); } yield return Tuple.Create("Resource", fileName); } continue; } } fileName = GetFileNameForResource(r.Name, directories); using (FileStream fs = new FileStream(Path.Combine(options.SaveAsProjectDirectory, fileName), FileMode.Create, FileAccess.Write)) { s.CopyTo(fs); } yield return Tuple.Create("EmbeddedResource", fileName); } //} //finally { // if (bamlDecompilerAppDomain != null) // AppDomain.Unload(bamlDecompilerAppDomain); //} }
private IEnumerable<Tuple<string, string>> WriteResourceFilesInProject() { foreach (var r in iAsm.ModuleDefinition.Resources.OfType<EmbeddedResource>()) { string absolutePath; string relativePath; Stream s = r.GetResourceStream(); s.Position = 0; char separator; if (r.Name.EndsWith(".g.resources", StringComparison.OrdinalIgnoreCase)) { separator = '/'; IEnumerable<DictionaryEntry> rs = null; try { rs = new ResourceSet(s).Cast<DictionaryEntry>(); } catch (ArgumentException) { } if (rs != null && rs.All(e => e.Value is Stream)) { foreach (var pair in rs) { GetFilePath((string)pair.Key, separator, out absolutePath, out relativePath); Stream entryStream = (Stream)pair.Value; entryStream.Position = 0; if (relativePath.EndsWith(".baml", StringComparison.OrdinalIgnoreCase)) { var xdoc = GetXamlDocument(relativePath, entryStream); if (xdoc != null) { xdoc.Save(Path.ChangeExtension(absolutePath, ".xaml")); yield return Tuple.Create("Page", Path.ChangeExtension(relativePath, ".xaml")); continue; } } using (FileStream fs = new FileStream(absolutePath, FileMode.Create, FileAccess.Write)) { entryStream.CopyTo(fs); } yield return Tuple.Create("Resource", relativePath); } continue; } } else { separator = '.'; } GetFilePath(r.Name, separator, out absolutePath, out relativePath); var content = relativePath.EndsWith(".resx", StringComparison.InvariantCultureIgnoreCase) ? GetResxContent(r) : null; if (content != null) { File.WriteAllText(absolutePath, content, Encoding.UTF8); } else { using (FileStream fs = new FileStream(absolutePath, FileMode.Create, FileAccess.Write)) { s.CopyTo(fs); } } yield return Tuple.Create("EmbeddedResource", relativePath); } }