Skip to content

Commit

Permalink
#2718: Move XAML files that have an x:Class declaration next to their…
Browse files Browse the repository at this point in the history
… C# counterparts when using WholeProjectDecompiler.
  • Loading branch information
siegfriedpammer committed Jun 27, 2022
1 parent 485e77d commit 9ed4d08
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
7 changes: 6 additions & 1 deletion ILSpy.BamlDecompiler/BamlDecompilationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@
using System.Linq;
using System.Xml.Linq;

using ICSharpCode.Decompiler.TypeSystem;

namespace ILSpy.BamlDecompiler
{
public class BamlDecompilationResult
{
public XDocument Xaml { get; }
public List<string> AssemblyReferences { get; }

public BamlDecompilationResult(XDocument xaml, IEnumerable<string> assemblyReferences)
public FullTypeName? TypeName { get; }

public BamlDecompilationResult(XDocument xaml, FullTypeName? typeName, IEnumerable<string> assemblyReferences)
{
this.Xaml = xaml;
this.TypeName = typeName;
this.AssemblyReferences = assemblyReferences.ToList();
}
}
Expand Down
10 changes: 9 additions & 1 deletion ILSpy.BamlDecompiler/BamlResourceNodeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.ComponentModel.Composition;
using System.IO;

using ICSharpCode.Decompiler.CSharp.ProjectDecompiler;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.ILSpy;
using ICSharpCode.ILSpy.TreeNodes;
Expand Down Expand Up @@ -53,8 +54,15 @@ public string WriteResourceToFile(LoadedAssembly assembly, string fileName, Stre
ThrowOnAssemblyResolveErrors = options.DecompilerSettings.ThrowOnAssemblyResolveErrors
});
decompiler.CancellationToken = options.CancellationToken;
fileName = Path.ChangeExtension(fileName, ".xaml");
var result = decompiler.Decompile(stream);
if (result.TypeName.HasValue)
{
fileName = WholeProjectDecompiler.CleanUpPath(result.TypeName.Value.ReflectionName) + ".xaml";
}
else
{
fileName = Path.ChangeExtension(fileName, ".xaml");
}
result.Xaml.Save(Path.Combine(options.SaveAsProjectDirectory, fileName));
return fileName;
}
Expand Down
1 change: 1 addition & 0 deletions ILSpy.BamlDecompiler/Rewrite/XClassRewritePass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void RewriteClass(XamlContext ctx, XElement elem)
attrs.Insert(0, new XAttribute(classModifierName, "internal"));
}
attrs.Insert(0, new XAttribute(attrName, type.ResolvedType.FullName));
ctx.XClassNames.Add(type.ResolvedType.FullName);
elem.ReplaceAttributes(attrs);
}
}
Expand Down
3 changes: 3 additions & 0 deletions ILSpy.BamlDecompiler/XamlContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ internal class XamlContext
TypeSystem = typeSystem;
NodeMap = new Dictionary<BamlRecord, BamlBlockNode>();
XmlNs = new XmlnsDictionary();
XClassNames = new List<string>();
}

Dictionary<ushort, XamlType> typeMap = new Dictionary<ushort, XamlType>();
Expand All @@ -54,6 +55,8 @@ internal class XamlContext
public BamlNode RootNode { get; private set; }
public IDictionary<BamlRecord, BamlBlockNode> NodeMap { get; }

public List<string> XClassNames { get; }

public XmlnsDictionary XmlNs { get; }

public static XamlContext Construct(IDecompilerTypeSystem typeSystem, BamlDocument document, CancellationToken token, BamlDecompilerSettings bamlDecompilerOptions)
Expand Down
3 changes: 2 additions & 1 deletion ILSpy.BamlDecompiler/XamlDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public BamlDecompilationResult Decompile(Stream stream)
}

var assemblyReferences = ctx.Baml.AssemblyIdMap.Select(a => a.Value.AssemblyFullName);
return new BamlDecompilationResult(xaml, assemblyReferences);
var typeName = ctx.XClassNames.FirstOrDefault() is string s ? (FullTypeName?)new FullTypeName(s) : null;
return new BamlDecompilationResult(xaml, typeName, assemblyReferences);
}
}
}

0 comments on commit 9ed4d08

Please # to comment.