Skip to content

Commit

Permalink
Improve support for special attribute usages in BAML decompiler
Browse files Browse the repository at this point in the history
  • Loading branch information
ElektroKill committed Sep 18, 2023
1 parent 4eeea76 commit 8355a6a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Extensions/dnSpy.BamlDecompiler/BamlDisassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ void DisassembleRecord(BamlContext ctx, AttributeInfoRecord record) {
WriteFlagField("XmlSpace");
break;
case BamlAttributeUsage.RuntimeName:
WriteFlagField("XmlSpace");
WriteFlagField("RuntimeName");
break;
default:
WriteHexNumber((byte)record.AttributeUsage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent)
return null;

XAttribute ConstructXAttribute() {
switch (xamlProp.PropertyKind) {
case XamlPropertyKind.XmlSpace:
return new XAttribute(XNamespace.Xml + "space", value);
case XamlPropertyKind.XmlLang:
return new XAttribute(XNamespace.Xml + "lang", value);
case XamlPropertyKind.RuntimeName:
return new XAttribute(ctx.GetKnownNamespace("Name", XamlContext.KnownNamespace_Xaml), value);
}

if (xamlProp.IsAttachedTo(elemType))
return new XAttribute(xamlProp.ToXName(ctx, parent.Xaml), value);

Expand Down
2 changes: 2 additions & 0 deletions Extensions/dnSpy.BamlDecompiler/Xaml/XamlProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ sealed class XamlProperty {

public ITypeDefOrRef ResolvedMemberDeclaringType { get; set; }

public XamlPropertyKind PropertyKind { get; set; }

public XamlProperty(XamlType type, string name) {
DeclaringType = type;
PropertyName = name;
Expand Down
9 changes: 9 additions & 0 deletions Extensions/dnSpy.BamlDecompiler/Xaml/XamlPropertyKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace dnSpy.BamlDecompiler.Xaml {
enum XamlPropertyKind : byte {
// Note: The order of these should remain consistent with BamlAttributeUsage!
Default,
XmlLang,
XmlSpace,
RuntimeName,
}
}
6 changes: 5 additions & 1 deletion Extensions/dnSpy.BamlDecompiler/XamlContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,27 @@ public XamlProperty ResolveProperty(ushort id) {
XamlType type;
string name;
IMemberDef member;
XamlPropertyKind propertyKind;

if (id > 0x7fff) {
var knownProp = Baml.KnownThings.Members((KnownMembers)unchecked((short)-(short)id));
type = ResolveType(unchecked((ushort)(short)-(short)knownProp.Parent));
name = knownProp.Name;
member = knownProp.Property;
propertyKind = XamlPropertyKind.Default;
}
else {
var attrRec = Baml.AttributeIdMap[id];
type = ResolveType(attrRec.OwnerTypeId);
name = attrRec.Name;
propertyKind = (XamlPropertyKind)attrRec.AttributeUsage;

member = null;
}

propertyMap[id] = xamlProp = new XamlProperty(type, name) {
ResolvedMember = member
ResolvedMember = member,
PropertyKind = propertyKind
};
xamlProp.TryResolve();

Expand Down

0 comments on commit 8355a6a

Please sign in to comment.