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); //} }
IEnumerable<Tuple<string, string>> WriteResourceFilesInProject(LoadedAssembly assembly, DecompilationOptions options, HashSet<string> directories) { //AppDomain bamlDecompilerAppDomain = null; //try { foreach (EmbeddedResource r in assembly.ModuleDefinition.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); //} }