Example #1
0
        async Task StartDecompileAsync()
        {
            bool canCompile = false, canceled = false;
            var  assemblyReferences = Array.Empty <CompilerMetadataReference>();

            SimpleDocument[] simpleDocuments = Array.Empty <SimpleDocument>();
            try {
                var result = await DecompileAndGetRefsAsync();

                assemblyReferences = result.Value;
                simpleDocuments    = result.Key.Documents.ToArray();
                canCompile         = true;
            }
            catch (OperationCanceledException) {
                canceled = true;
            }
            catch (Exception ex) {
                simpleDocuments = new SimpleDocument[] {
                    new SimpleDocument(MAIN_CODE_NAME, ex.ToString(), null)
                };
            }

            var codeDocs = Array.Empty <ICodeDocument>();

            if (!canceled)
            {
                // This helps a little to speed up the code
                ProfileOptimizationHelper.StartProfile("add-decompiled-code-" + decompiler.UniqueGuid.ToString());
                var docs = new List <IDecompiledDocument>();
                foreach (var simpleDoc in simpleDocuments)
                {
                    docs.Add(new DecompiledDocument(simpleDoc.Text, simpleDoc.NameNoExtension));
                }
                codeDocs = languageCompiler.AddDecompiledCode(new DecompiledCodeResult(docs.ToArray(), assemblyReferences, assemblyReferenceResolver, PlatformHelper.GetPlatform(sourceModule)));
            }

            decompileCodeState?.Dispose();
            decompileCodeState = null;

            foreach (var doc in codeDocs)
            {
                doc.TextView.Properties.AddProperty(editCodeTextViewKey, this);
            }
            Documents.AddRange(codeDocs.Select(a => new CodeDocument(a)));
            SelectedDocument = Documents.FirstOrDefault(a => a.NameNoExtension == MAIN_CODE_NAME) ?? Documents.FirstOrDefault();
            Debug.Assert(Documents.Count == simpleDocuments.Length);
            for (int i = 0; i < Documents.Count; i++)
            {
                var doc       = Documents[i];
                var caretSpan = simpleDocuments[i].CaretSpan;
                if (caretSpan != null && caretSpan.Value.End <= doc.TextView.TextSnapshot.Length)
                {
                    doc.Initialize(new SnapshotPoint(doc.TextView.TextSnapshot, caretSpan.Value.Start));
                }
                else
                {
                    doc.Initialize(new SnapshotPoint(doc.TextView.TextSnapshot, 0));
                }
            }

            CanCompile    = canCompile;
            HasDecompiled = true;
            OnPropertyChanged(nameof(HasDecompiled));
        }
Example #2
0
        async Task StartDecompileAsync(MethodDef method, MethodSourceStatement?methodSourceStatement)
        {
            bool   canCompile = false, canceled = false;
            var    assemblyReferences = Array.Empty <CompilerMetadataReference>();
            string mainCode, hiddenCode;
            var    refSpan = new Span(0, 0);

            try {
                assemblyReferences = await DecompileAndGetRefsAsync(method, methodSourceStatement);

                mainCode   = decompileCodeState.MainOutput.ToString();
                hiddenCode = decompileCodeState.HiddenOutput.ToString();
                canCompile = true;
                var span = decompileCodeState.MainOutput.Span;
                if (span != null)
                {
                    refSpan = span.Value;
                }
            }
            catch (OperationCanceledException) {
                canceled   = true;
                mainCode   = string.Empty;
                hiddenCode = string.Empty;
            }
            catch (Exception ex) {
                mainCode   = ex.ToString();
                hiddenCode = string.Empty;
            }

            const string MAIN_CODE_NAME = "main";
            var          codeDocs       = Array.Empty <ICodeDocument>();

            if (!canceled)
            {
                // This helps a little to speed up the code
                ProfileOptimizationHelper.StartProfile("add-decompiled-code-" + decompiler.UniqueGuid.ToString());
                var docs = new List <IDecompiledDocument>();
                docs.Add(new DecompiledDocument(mainCode, MAIN_CODE_NAME));
                if (hiddenCode != string.Empty)
                {
                    docs.Add(new DecompiledDocument(hiddenCode, MAIN_CODE_NAME + ".g"));
                }
                codeDocs = languageCompiler.AddDecompiledCode(new DecompiledCodeResult(docs.ToArray(), assemblyReferences, assemblyReferenceResolver, PlatformHelper.GetPlatform(method.Module)));
            }

            decompileCodeState?.Dispose();
            decompileCodeState = null;

            foreach (var doc in codeDocs)
            {
                doc.TextView.Properties.AddProperty(editCodeTextViewKey, this);
            }
            Documents.AddRange(codeDocs.Select(a => new CodeDocument(a)));
            SelectedDocument = Documents.FirstOrDefault(a => a.NameNoExtension == MAIN_CODE_NAME) ?? Documents.FirstOrDefault();
            foreach (var doc in Documents)
            {
                if (doc.NameNoExtension == MAIN_CODE_NAME && refSpan.End <= doc.TextView.TextSnapshot.Length)
                {
                    doc.Initialize(new SnapshotPoint(doc.TextView.TextSnapshot, refSpan.Start));
                }
                else
                {
                    doc.Initialize(new SnapshotPoint(doc.TextView.TextSnapshot, 0));
                }
            }

            CanCompile    = canCompile;
            HasDecompiled = true;
            OnPropertyChanged(nameof(HasDecompiled));
        }
Example #3
0
        async Task StartDecompileAsync(MethodDef method)
        {
            bool   canCompile = false, canceled = false;
            var    assemblyReferences = Array.Empty <CompilerMetadataReference>();
            string mainCode, hiddenCode;

            try {
                assemblyReferences = await DecompileAndGetRefsAsync(method);

                mainCode   = decompileCodeState.MainOutput.ToString();
                hiddenCode = decompileCodeState.HiddenOutput.ToString();
                canCompile = true;
            }
            catch (OperationCanceledException) {
                canceled   = true;
                mainCode   = string.Empty;
                hiddenCode = string.Empty;
            }
            catch (Exception ex) {
                mainCode   = ex.ToString();
                hiddenCode = string.Empty;
            }

            const string MAIN_CODE_NAME = "main";
            var          codeDocs       = Array.Empty <ICodeDocument>();

            if (!canceled)
            {
                var docs = new List <IDecompiledDocument>();
                docs.Add(new DecompiledDocument(mainCode, MAIN_CODE_NAME));
                if (hiddenCode != string.Empty)
                {
                    docs.Add(new DecompiledDocument(hiddenCode, MAIN_CODE_NAME + ".g"));
                }
                codeDocs = languageCompiler.AddDecompiledCode(new DecompiledCodeResult(docs.ToArray(), assemblyReferences, assemblyReferenceResolver, PlatformHelper.GetPlatform(method.Module)));
            }

            decompileCodeState?.Dispose();
            decompileCodeState = null;

            Documents.AddRange(codeDocs);
            SelectedDocument = Documents.FirstOrDefault(a => a.NameNoExtension == MAIN_CODE_NAME) ?? Documents.FirstOrDefault();
            foreach (var doc in Documents)
            {
                doc.TextView.MoveCaretTo(0, 0);
                doc.TextView.Selection.Clear();
            }

            CanCompile    = canCompile;
            HasDecompiled = true;
            OnPropertyChanged(nameof(HasDecompiled));
        }