Пример #1
0
        void Decompile(ModuleDef module, BamlDocument document, ILanguage lang,
                       IDecompilerOutput output, CancellationToken token)
        {
            var decompiler = new XamlDecompiler();
            var xaml       = decompiler.Decompile(module, document, token, BamlDecompilerOptions.Create(lang), null);

            output.Write(xaml.ToString(), BoxedTextColor.Text);
        }
Пример #2
0
        void Decompile(ModuleDef module, BamlDocument document, IDecompiler lang,
                       IDecompilerOutput output, CancellationToken token)
        {
            var decompiler = new XamlDecompiler();
            var xaml       = decompiler.Decompile(module, document, token, BamlDecompilerOptions.Create(lang), null);
            var xamlText   = new XamlOutputCreator(xamlOutputOptionsProvider.Default).CreateText(xaml);

            documentWriterService.Write(output, xamlText, ContentTypes.Xaml);
        }
Пример #3
0
        void Decompile(ModuleDef module, BamlDocument document, ILanguage lang,
                       ITextOutput output, out string ext, CancellationToken token)
        {
            var decompiler = new XamlDecompiler();
            var xaml       = decompiler.Decompile(module, document, token, BamlDecompilerOptions.Create(lang), null);

            output.Write(xaml.ToString(), TextTokenKind.Text);
            ext = ".xml";
        }
Пример #4
0
        public static XamlContext Construct(ModuleDef module, BamlDocument document, CancellationToken token, BamlDecompilerOptions bamlDecompilerOptions)
        {
            var ctx = new XamlContext(module);

            ctx.CancellationToken     = token;
            ctx.BamlDecompilerOptions = bamlDecompilerOptions ?? BamlDecompilerOptions.CreateCSharp();

            ctx.Baml     = BamlContext.ConstructContext(module, document, token);
            ctx.RootNode = BamlNode.Parse(document, token);

            ctx.BuildPIMappings(document);
            ctx.BuildNodeMap(ctx.RootNode as BamlBlockNode, new RecursionCounter());

            return(ctx);
        }
Пример #5
0
        ProjectModuleOptions CreateProjectModuleOptions(ModuleDef mod)
        {
            mod.EnableTypeDefFindCache = true;
            moduleContext.AssemblyResolver.AddToCache(mod);
            AddSearchPath(Path.GetDirectoryName(mod.Location));
            var proj = new ProjectModuleOptions(mod, GetLanguage(), decompilationContext);

            proj.DontReferenceStdLib = !addCorlibRef;
            proj.UnpackResources     = unpackResources;
            proj.CreateResX          = createResX;
            proj.DecompileXaml       = decompileBaml && bamlDecompiler != null;
            var o = BamlDecompilerOptions.Create(GetLanguage());

            if (bamlDecompiler != null)
            {
                proj.DecompileBaml = (a, b, c, d) => bamlDecompiler.Decompile(a, b, c, o, d);
            }
            return(proj);
        }
Пример #6
0
        public override void Create(DecompileContext ctx)
        {
            var settings = new XmlWriterSettings {
                Encoding           = Encoding.UTF8,
                Indent             = true,
                OmitXmlDeclaration = true,
            };

            using (var writer = XmlWriter.Create(Filename, settings)) {
                writer.WriteStartDocument();
                writer.WriteStartElement("Application", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");

                writer.WriteAttributeString("x", "Class", "http://schemas.microsoft.com/winfx/2006/xaml", type.ReflectionFullName);

                if (type.IsNotPublic)
                {
                    var opts = BamlDecompilerOptions.Create(language);
                    writer.WriteAttributeString("x", "ClassModifier", "http://schemas.microsoft.com/winfx/2006/xaml", opts.InternalClassModifier);
                }

                var body = GetInitializeComponentBody();
                Debug.Assert(body != null);
                if (body != null)
                {
                    var startupUri = GetStartupUri(body);
                    if (startupUri != null)
                    {
                        writer.WriteAttributeString("StartupUri", startupUri);
                    }

                    foreach (var info in GetEvents(body))
                    {
                        writer.WriteAttributeString(info.Item1, info.Item2);
                    }
                }

                writer.WriteElementString("Application.Resources", "\r\n");

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }
Пример #7
0
        public XDocument Decompile(IDecompilerTypeSystem typeSystem, BamlDocument document, CancellationToken token, BamlDecompilerOptions bamlDecompilerOptions, List <string> assemblyReferences)
        {
            var ctx = XamlContext.Construct(typeSystem, document, token, bamlDecompilerOptions);

            var handler = HandlerMap.LookupHandler(ctx.RootNode.Type);
            var elem    = handler.Translate(ctx, ctx.RootNode, null);

            var xaml = new XDocument();

            xaml.Add(elem.Xaml.Element);

            foreach (var pass in rewritePasses)
            {
                token.ThrowIfCancellationRequested();
                pass.Run(ctx, xaml);
            }

            if (assemblyReferences != null)
            {
                assemblyReferences.AddRange(ctx.Baml.AssemblyIdMap.Select(a => a.Value.AssemblyFullName));
            }

            return(xaml);
        }
Пример #8
0
            public void Execute(ExportToProjectVM vm)
            {
                vm.ProgressMinimum = 0;
                vm.ProgressMaximum = 1;
                vm.TotalProgress   = 0;
                vm.IsIndeterminate = false;
                Task.Factory.StartNew(() => {
                    var decompilationContext = new DecompilationContext {
                        CancellationToken      = cancellationToken,
                        GetDisableAssemblyLoad = () => owner.documentTreeView.DocumentService.DisableAssemblyLoad(),
                    };
                    var options            = new ProjectCreatorOptions(vm.Directory, cancellationToken);
                    options.ProjectVersion = vm.ProjectVersion;
                    if (vm.CreateSolution)
                    {
                        options.SolutionFilename = vm.SolutionFilename;
                    }
                    options.Logger           = this;
                    options.ProgressListener = this;

                    bool hasProjectGuid = vm.ProjectGuid.Value != null;
                    string guidFormat   = null;
                    int guidNum         = 0;
                    if (hasProjectGuid)
                    {
                        string guidStr = vm.ProjectGuid.Value.ToString();
                        guidNum        = int.Parse(guidStr.Substring(36 - 8, 8), NumberStyles.HexNumber);
                        guidFormat     = guidStr.Substring(0, 36 - 8) + "{0:X8}";
                    }
                    foreach (var module in modules.OrderBy(a => a.Location, StringComparer.InvariantCultureIgnoreCase))
                    {
                        var projOpts = new ProjectModuleOptions(module, vm.Decompiler, decompilationContext)
                        {
                            DontReferenceStdLib = vm.DontReferenceStdLib,
                            UnpackResources     = vm.UnpackResources,
                            CreateResX          = vm.CreateResX,
                            DecompileXaml       = vm.DecompileXaml,
                            ProjectGuid         = hasProjectGuid ? new Guid(string.Format(guidFormat, guidNum++)) : Guid.NewGuid(),
                        };
                        if (bamlDecompiler != null)
                        {
                            var o = BamlDecompilerOptions.Create(vm.Decompiler);
                            projOpts.DecompileBaml = (a, b, c, d) => bamlDecompiler.Decompile(a, b, c, o, d);
                        }
                        options.ProjectModules.Add(projOpts);
                    }
                    var creator = new MSBuildProjectCreator(options);

                    creator.Create();
                    if (vm.CreateSolution)
                    {
                        fileToOpen = creator.SolutionFilename;
                    }
                    else
                    {
                        fileToOpen = creator.ProjectFilenames.FirstOrDefault();
                    }
                }, cancellationToken)
                .ContinueWith(t => {
                    var ex = t.Exception;
                    if (ex != null)
                    {
                        Error(string.Format(dnSpy_Resources.ErrorExceptionOccurred, ex));
                    }
                    EmtpyErrorList();
                    vm.OnExportComplete();
                    if (!vm.ExportErrors)
                    {
                        dlg.Close();
                        if (vm.OpenProject)
                        {
                            OpenProject();
                        }
                    }
                }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
            }
Пример #9
0
        public IList <string> Decompile(ModuleDef module, byte[] data, CancellationToken token, BamlDecompilerOptions bamlDecompilerOptions, Stream output, XamlOutputOptions outputOptions)
        {
            var doc     = BamlReader.ReadDocument(new MemoryStream(data), token);
            var asmRefs = new List <string>();
            var xaml    = new XamlDecompiler().Decompile(module, doc, token, bamlDecompilerOptions, asmRefs);
            var resData = Encoding.UTF8.GetBytes(new XamlOutputCreator(outputOptions).CreateText(xaml));

            output.Write(resData, 0, resData.Length);
            return(asmRefs);
        }
Пример #10
0
        public static XamlContext Construct(IDecompilerTypeSystem typeSystem, BamlDocument document, CancellationToken token, BamlDecompilerOptions bamlDecompilerOptions)
        {
            var ctx = new XamlContext(typeSystem);

            ctx.CancellationToken     = token;
            ctx.BamlDecompilerOptions = bamlDecompilerOptions ?? new BamlDecompilerOptions();

            ctx.Baml     = BamlContext.ConstructContext(typeSystem, document, token);
            ctx.RootNode = BamlNode.Parse(document, token);

            ctx.BuildPIMappings(document);
            ctx.BuildNodeMap(ctx.RootNode as BamlBlockNode);

            return(ctx);
        }
Пример #11
0
            public void Execute(ExportToProjectVM vm)
            {
                vm.ProgressMinimum = 0;
                vm.ProgressMaximum = 1;
                vm.TotalProgress   = 0;
                vm.IsIndeterminate = false;
                Task.Factory.StartNew(() => {
                    AppCulture.InitializeCulture();
                    var decompilationContext = new DecompilationContext {
                        CancellationToken      = cancellationTokenSource.Token,
                        GetDisableAssemblyLoad = () => owner.fileTreeView.FileManager.DisableAssemblyLoad(),
                    };
                    var options            = new ProjectCreatorOptions(vm.Directory, cancellationTokenSource.Token);
                    options.ProjectVersion = vm.ProjectVersion;
                    if (vm.CreateSolution)
                    {
                        options.SolutionFilename = vm.SolutionFilename;
                    }
                    options.Logger           = this;
                    options.ProgressListener = this;
                    foreach (var module in modules)
                    {
                        var projOpts = new ProjectModuleOptions(module, vm.Language, decompilationContext)
                        {
                            DontReferenceStdLib = vm.DontReferenceStdLib,
                            UnpackResources     = vm.UnpackResources,
                            CreateResX          = vm.CreateResX,
                            DecompileXaml       = vm.DecompileXaml,
                        };
                        if (owner.bamlDecompiler != null)
                        {
                            var o = BamlDecompilerOptions.Create(vm.Language);
                            projOpts.DecompileBaml = (a, b, c, d) => owner.bamlDecompiler.Value.Decompile(a, b, c, o, d);
                        }
                        options.ProjectModules.Add(projOpts);
                    }
                    var creator = new MSBuildProjectCreator(options);

                    creator.Create();
                    if (vm.CreateSolution)
                    {
                        fileToOpen = creator.SolutionFilename;
                    }
                    else
                    {
                        fileToOpen = creator.ProjectFilenames.FirstOrDefault();
                    }
                }, cancellationTokenSource.Token)
                .ContinueWith(t => {
                    var ex = t.Exception;
                    if (ex != null)
                    {
                        Error(string.Format(dnSpy_Resources.ErrorExceptionOccurred, ex));
                    }
                    EmtpyErrorList();
                    vm.OnExportComplete();
                    if (!vm.ExportErrors)
                    {
                        dlg.Close();
                        if (vm.OpenProject)
                        {
                            OpenProject();
                        }
                    }
                }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
            }