Beispiel #1
0
        public string WriteResourceToFile(LoadedAssembly assembly, string fileName, Stream stream, ResourceFileHandlerContext context)
        {
            BamlDecompilerTypeSystem typeSystem = new BamlDecompilerTypeSystem(assembly.GetPEFileOrNull(), assembly.GetAssemblyResolver());
            var decompiler = new XamlDecompiler(typeSystem, new BamlDecompilerSettings()
            {
                ThrowOnAssemblyResolveErrors = context.DecompilationOptions.DecompilerSettings.ThrowOnAssemblyResolveErrors
            });

            decompiler.CancellationToken = context.DecompilationOptions.CancellationToken;
            var result         = decompiler.Decompile(stream);
            var typeDefinition = result.TypeName.HasValue ? typeSystem.MainModule.GetTypeDefinition(result.TypeName.Value.TopLevelTypeName) : null;

            if (typeDefinition != null)
            {
                fileName = WholeProjectDecompiler.CleanUpPath(typeDefinition.ReflectionName) + ".xaml";
                var partialTypeInfo = new PartialTypeInfo(typeDefinition);
                foreach (var member in result.GeneratedMembers)
                {
                    partialTypeInfo.AddDeclaredMember(member);
                }
                context.AddPartialTypeInfo(partialTypeInfo);
            }
            else
            {
                fileName = Path.ChangeExtension(fileName, ".xaml");
            }
            string saveFileName = Path.Combine(context.DecompilationOptions.SaveAsProjectDirectory, fileName);

            Directory.CreateDirectory(Path.GetDirectoryName(saveFileName));
            result.Xaml.Save(saveFileName);
            return(fileName);
        }
Beispiel #2
0
 internal XamlDecompiler(BamlDecompilerTypeSystem typeSystem, BamlDecompilerSettings settings)
 {
     this.typeSystem = typeSystem ?? throw new ArgumentNullException(nameof(typeSystem));
     this.settings   = settings;
     this.module     = typeSystem.MainModule;
     if (module.TypeSystemOptions.HasFlag(TypeSystemOptions.Uncached))
     {
         throw new ArgumentException("Cannot use an uncached type system in the decompiler.");
     }
 }
        void LoadBaml(AvalonEditTextOutput output, CancellationToken cancellationToken)
        {
            var asm = this.Ancestors().OfType <AssemblyTreeNode>().First().LoadedAssembly;

            using var data = OpenStream();
            BamlDecompilerTypeSystem typeSystem = new BamlDecompilerTypeSystem(asm.GetPEFileOrNull(), asm.GetAssemblyResolver());
            var decompiler = new XamlDecompiler(typeSystem, new BamlDecompilerSettings());

            decompiler.CancellationToken = cancellationToken;
            var result = decompiler.Decompile(data);

            output.Write(result.Xaml.ToString());
        }
Beispiel #4
0
        public string WriteResourceToFile(LoadedAssembly assembly, string fileName, Stream stream, DecompilationOptions options)
        {
            BamlDecompilerTypeSystem typeSystem = new BamlDecompilerTypeSystem(assembly.GetPEFileOrNull(), assembly.GetAssemblyResolver());
            var decompiler = new XamlDecompiler(typeSystem, new BamlDecompilerSettings()
            {
                ThrowOnAssemblyResolveErrors = options.DecompilerSettings.ThrowOnAssemblyResolveErrors
            });

            decompiler.CancellationToken = options.CancellationToken;
            fileName = Path.ChangeExtension(fileName, ".xaml");
            var result = decompiler.Decompile(stream);

            result.Xaml.Save(Path.Combine(options.SaveAsProjectDirectory, fileName));
            return(fileName);
        }