From 1d029b36cb840de3835b56b589ffbb8f8f7cc565 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Wed, 27 Sep 2017 23:12:00 +0200 Subject: [PATCH] Fix build. --- ICSharpCode.Decompiler/IL/ILReader.cs | 1 + ILSpy.BamlDecompiler.Tests/BamlTestRunner.cs | 3 ++- ILSpy.BamlDecompiler/BamlResourceEntryNode.cs | 16 +++++++++------- ILSpy.BamlDecompiler/BamlResourceNodeFactory.cs | 2 +- ILSpy.BamlDecompiler/ConnectMethodDecompiler.cs | 10 ++++++---- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/ICSharpCode.Decompiler/IL/ILReader.cs b/ICSharpCode.Decompiler/IL/ILReader.cs index d98a50350..b401f5c5d 100644 --- a/ICSharpCode.Decompiler/IL/ILReader.cs +++ b/ICSharpCode.Decompiler/IL/ILReader.cs @@ -312,6 +312,7 @@ namespace ICSharpCode.Decompiler.IL /// public ILFunction ReadIL(Cil.MethodBody body, CancellationToken cancellationToken = default(CancellationToken)) { + cancellationToken.ThrowIfCancellationRequested(); Init(body); ReadInstructions(cancellationToken); var blockBuilder = new BlockBuilder(body, typeSystem, variableByExceptionHandler); diff --git a/ILSpy.BamlDecompiler.Tests/BamlTestRunner.cs b/ILSpy.BamlDecompiler.Tests/BamlTestRunner.cs index 1fc54cfa7..463260501 100644 --- a/ILSpy.BamlDecompiler.Tests/BamlTestRunner.cs +++ b/ILSpy.BamlDecompiler.Tests/BamlTestRunner.cs @@ -6,6 +6,7 @@ using System.Collections; using System.IO; using System.Linq; using System.Resources; +using System.Threading; using System.Xml.Linq; using ICSharpCode.Decompiler.Tests.Helpers; using Mono.Cecil; @@ -113,7 +114,7 @@ namespace ILSpy.BamlDecompiler.Tests Resource res = assembly.MainModule.Resources.First(); Stream bamlStream = LoadBaml(res, name + ".baml"); Assert.IsNotNull(bamlStream); - XDocument document = BamlResourceEntryNode.LoadIntoDocument(resolver, assembly, bamlStream); + XDocument document = BamlResourceEntryNode.LoadIntoDocument(resolver, assembly, bamlStream, CancellationToken.None); XamlIsEqual(File.ReadAllText(sourcePath), document.ToString()); } diff --git a/ILSpy.BamlDecompiler/BamlResourceEntryNode.cs b/ILSpy.BamlDecompiler/BamlResourceEntryNode.cs index 2117ff020..9b0a515dd 100644 --- a/ILSpy.BamlDecompiler/BamlResourceEntryNode.cs +++ b/ILSpy.BamlDecompiler/BamlResourceEntryNode.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading; using System.Threading.Tasks; using System.Xml.Linq; @@ -32,7 +33,7 @@ namespace ILSpy.BamlDecompiler () => { AvalonEditTextOutput output = new AvalonEditTextOutput(); try { - if (LoadBaml(output)) + if (LoadBaml(output, token)) highlighting = HighlightingManager.Instance.GetDefinitionByExtension(".xml"); } catch (Exception ex) { output.Write(ex.ToString()); @@ -44,33 +45,34 @@ namespace ILSpy.BamlDecompiler return true; } - bool LoadBaml(AvalonEditTextOutput output) + bool LoadBaml(AvalonEditTextOutput output, CancellationToken cancellationToken) { var asm = this.Ancestors().OfType().FirstOrDefault().LoadedAssembly; Data.Position = 0; - XDocument xamlDocument = LoadIntoDocument(asm.GetAssemblyResolver(), asm.AssemblyDefinition, Data); + XDocument xamlDocument = LoadIntoDocument(asm.GetAssemblyResolver(), asm.AssemblyDefinition, Data, cancellationToken); output.Write(xamlDocument.ToString()); return true; } - internal static XDocument LoadIntoDocument(IAssemblyResolver resolver, AssemblyDefinition asm, Stream stream) + internal static XDocument LoadIntoDocument(IAssemblyResolver resolver, AssemblyDefinition asm, Stream stream, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); XDocument xamlDocument; using (XmlBamlReader reader = new XmlBamlReader(stream, new CecilTypeResolver(resolver, asm))) { xamlDocument = XDocument.Load(reader); - ConvertConnectionIds(xamlDocument, asm); + ConvertConnectionIds(xamlDocument, asm, cancellationToken); ConvertToEmptyElements(xamlDocument.Root); MoveNamespacesToRoot(xamlDocument, reader.XmlnsDefinitions); return xamlDocument; } } - static void ConvertConnectionIds(XDocument xamlDocument, AssemblyDefinition asm) + static void ConvertConnectionIds(XDocument xamlDocument, AssemblyDefinition asm, CancellationToken cancellationToken) { var attr = xamlDocument.Root.Attribute(XName.Get("Class", XmlBamlReader.XWPFNamespace)); if (attr != null) { string fullTypeName = attr.Value; - var mappings = new ConnectMethodDecompiler(asm).DecompileEventMappings(fullTypeName); + var mappings = new ConnectMethodDecompiler(asm).DecompileEventMappings(fullTypeName, cancellationToken); RemoveConnectionIds(xamlDocument.Root, mappings); } } diff --git a/ILSpy.BamlDecompiler/BamlResourceNodeFactory.cs b/ILSpy.BamlDecompiler/BamlResourceNodeFactory.cs index 001b9003f..2a1e50477 100644 --- a/ILSpy.BamlDecompiler/BamlResourceNodeFactory.cs +++ b/ILSpy.BamlDecompiler/BamlResourceNodeFactory.cs @@ -35,7 +35,7 @@ namespace ILSpy.BamlDecompiler public string WriteResourceToFile(LoadedAssembly assembly, string fileName, Stream stream, DecompilationOptions options) { - var document = BamlResourceEntryNode.LoadIntoDocument(assembly.GetAssemblyResolver(), assembly.AssemblyDefinition, stream); + var document = BamlResourceEntryNode.LoadIntoDocument(assembly.GetAssemblyResolver(), assembly.AssemblyDefinition, stream, options.CancellationToken); fileName = Path.ChangeExtension(fileName, ".xaml"); document.Save(Path.Combine(options.SaveAsProjectDirectory, fileName)); return fileName; diff --git a/ILSpy.BamlDecompiler/ConnectMethodDecompiler.cs b/ILSpy.BamlDecompiler/ConnectMethodDecompiler.cs index 51accc294..7c970ea54 100644 --- a/ILSpy.BamlDecompiler/ConnectMethodDecompiler.cs +++ b/ILSpy.BamlDecompiler/ConnectMethodDecompiler.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; - +using System.Threading; using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.CSharp; using ICSharpCode.Decompiler.IL; @@ -34,7 +34,7 @@ namespace ILSpy.BamlDecompiler this.assembly = assembly; } - public Dictionary DecompileEventMappings(string fullTypeName) + public Dictionary DecompileEventMappings(string fullTypeName, CancellationToken cancellationToken) { var result = new Dictionary(); TypeDefinition type = this.assembly.MainModule.GetType(fullTypeName); @@ -57,9 +57,11 @@ namespace ILSpy.BamlDecompiler // decompile method and optimize the switch var typeSystem = new DecompilerTypeSystem(method.Module); var ilReader = new ILReader(typeSystem); - var function = ilReader.ReadIL(method.Body); + var function = ilReader.ReadIL(method.Body, cancellationToken); - var context = new ILTransformContext { Settings = new DecompilerSettings(), TypeSystem = typeSystem }; + var context = new ILTransformContext(function, typeSystem) { + CancellationToken = cancellationToken + }; function.RunTransforms(CSharpDecompiler.GetILTransforms(), context); var block = function.Body.Children.OfType().First();