Ejemplo n.º 1
0
        /// <summary>
        /// "Decompiles" the data
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="stream">Stream</param>
        /// <param name="name">Name</param>
        /// <returns></returns>
        public static bool Decompile(IDecompileNodeContext context, Stream stream, string name)
        {
            if (stream == null || stream.Length > 500 * 1024)
            {
                return(false);
            }

            stream.Position = 0;
            FileType type = GuessFileType.DetectFileType(stream);

            if (type == FileType.Binary)
            {
                return(false);
            }

            stream.Position = 0;
            context.Output.Write(new StreamReader(stream, true).ReadToEnd(), BoxedTextColor.Text);
            string ext;

            if (type == FileType.Xml)
            {
                ext = ".xml";
            }
            else
            {
                try {
                    ext = Path.GetExtension(NameUtilities.CleanName(name));
                }
                catch (ArgumentException) {
                    ext = ".txt";
                }
            }
            context.ContentTypeString = ContentTypesHelper.TryGetContentTypeStringByExtension(ext) ?? ContentTypes.PlainText;
            return(true);
        }
Ejemplo n.º 2
0
        public override void OnShowAsync(IShowContext ctx, IAsyncShowResult result)
        {
            var decompileContext = (DecompileContext)ctx.Tag !;
            var documentViewer   = (IDocumentViewer)ctx.UIContext;

            Debug2.Assert(decompileContext.DecompileNodeContext is not null);
            Debug2.Assert(decompileContext.DocumentViewerContentFactory is not null);
            var contentType = decompileContext.DecompileNodeContext.ContentType;

            if (contentType is null)
            {
                var contentTypeString = decompileContext.DecompileNodeContext.ContentTypeString;
                if (contentTypeString is null)
                {
                    contentTypeString = ContentTypesHelper.TryGetContentTypeStringByExtension(decompileContext.DecompileNodeContext.Decompiler.FileExtension) ?? ContentTypes.PlainText;
                }
                contentType = decompileDocumentTabContentFactory.ContentTypeRegistryService.GetContentType(contentTypeString) ??
                              decompileDocumentTabContentFactory.ContentTypeRegistryService.GetContentType(ContentTypes.Text) ??
                              decompileDocumentTabContentFactory.ContentTypeRegistryService.UnknownContentType;
            }

            DocumentViewerContent?content;

            if (result.IsCanceled)
            {
                var docViewContentFactory = decompileDocumentTabContentFactory.DocumentViewerContentFactoryProvider.Create();
                docViewContentFactory.Output.Write(dnSpy_Resources.DecompilationCanceled, BoxedTextColor.Error);
                content = docViewContentFactory.CreateContent(documentViewer, contentType);
            }
            else if (result.Exception is not null)
            {
                var docViewContentFactory = decompileDocumentTabContentFactory.DocumentViewerContentFactoryProvider.Create();
                docViewContentFactory.Output.Write(dnSpy_Resources.DecompilationException, BoxedTextColor.Error);
                docViewContentFactory.Output.WriteLine();
                docViewContentFactory.Output.Write(result.Exception.ToString(), BoxedTextColor.Text);
                content = docViewContentFactory.CreateContent(documentViewer, contentType);
            }
            else
            {
                content = decompileContext.CachedContent;
                if (content is null)
                {
                    bool canBeCached = decompileContext.DocumentViewerContentFactory.Output.CanBeCached;
                    content = decompileContext.DocumentViewerContentFactory.CreateContent(documentViewer, contentType);
                    if (canBeCached)
                    {
                        decompileDocumentTabContentFactory.DecompilationCache.Cache(decompileContext.DecompileNodeContext.Decompiler, nodes, content, contentType);
                    }
                }
            }

            if (result.CanShowOutput)
            {
                WasNewContent = documentViewer.SetContent(content, contentType);
            }
            else
            {
                WasNewContent = false;
            }
        }
        public void EndAsyncShow(IShowContext ctx, IAsyncShowResult result)
        {
            var decompileContext = (DecompileContext)ctx.UserData;
            var documentViewer   = (IDocumentViewer)ctx.UIContext;

            var contentType = decompileContext.DecompileNodeContext.ContentType;

            if (contentType == null)
            {
                var contentTypeString = decompileContext.DecompileNodeContext.ContentTypeString;
                if (contentTypeString == null)
                {
                    contentTypeString = ContentTypesHelper.TryGetContentTypeStringByExtension(decompileContext.DecompileNodeContext.Decompiler.FileExtension) ?? ContentTypes.PlainText;
                }
                contentType = decompileDocumentTabContentFactory.ContentTypeRegistryService.GetContentType(contentTypeString);
                Debug.Assert(contentType != null);
            }

            DocumentViewerContent content;

            if (result.IsCanceled)
            {
                var docViewContentFactory = decompileDocumentTabContentFactory.DocumentViewerContentFactoryProvider.Create();
                docViewContentFactory.Output.Write(dnSpy_Resources.DecompilationCanceled, BoxedTextColor.Error);
                content = docViewContentFactory.CreateContent(documentViewer);
            }
            else if (result.Exception != null)
            {
                var docViewContentFactory = decompileDocumentTabContentFactory.DocumentViewerContentFactoryProvider.Create();
                docViewContentFactory.Output.Write(dnSpy_Resources.DecompilationException, BoxedTextColor.Error);
                docViewContentFactory.Output.WriteLine();
                docViewContentFactory.Output.Write(result.Exception.ToString(), BoxedTextColor.Text);
                content = docViewContentFactory.CreateContent(documentViewer);
            }
            else
            {
                content = decompileContext.CachedContent;
                if (content == null)
                {
                    bool canBeCached = decompileContext.DocumentViewerContentFactory.Output.CanBeCached;
                    content = decompileContext.DocumentViewerContentFactory.CreateContent(documentViewer);
                    if (canBeCached)
                    {
                        decompileDocumentTabContentFactory.DecompilationCache.Cache(decompileContext.DecompileNodeContext.Decompiler, nodes, content, contentType);
                    }
                }
            }

            if (result.CanShowOutput)
            {
                documentViewer.SetContent(content, contentType);
            }
        }