|
|
@ -41,20 +41,29 @@ namespace ILSpy.BamlDecompiler.Rewrite |
|
|
|
|
|
|
|
|
|
|
|
public void Run(XamlContext ctx, XDocument document) |
|
|
|
public void Run(XamlContext ctx, XDocument document) |
|
|
|
{ |
|
|
|
{ |
|
|
|
var mappings = DecompileEventMappings(ctx, document); |
|
|
|
var connections = DecompileConnections(ctx, document); |
|
|
|
ProcessConnectionIds(document.Root, mappings); |
|
|
|
ProcessConnectionIds(ctx, document.Root, connections); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void ProcessConnectionIds(XElement element, |
|
|
|
static void ProcessConnectionIds(XamlContext ctx, XElement element, |
|
|
|
List<(LongSet key, EventRegistration[] value)> eventMappings) |
|
|
|
(List<(LongSet key, FieldAssignment value)> fieldAssignments, |
|
|
|
|
|
|
|
List<(LongSet key, EventRegistration[] value)> eventMappings) connections) |
|
|
|
{ |
|
|
|
{ |
|
|
|
foreach (var child in element.Elements()) |
|
|
|
foreach (var child in element.Elements()) |
|
|
|
ProcessConnectionIds(child, eventMappings); |
|
|
|
ProcessConnectionIds(ctx, child, connections); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var fieldAssignments = connections.fieldAssignments; |
|
|
|
|
|
|
|
var eventMappings = connections.eventMappings; |
|
|
|
foreach (var annotation in element.Annotations<BamlConnectionId>()) |
|
|
|
foreach (var annotation in element.Annotations<BamlConnectionId>()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int index; |
|
|
|
int index; |
|
|
|
if ((index = eventMappings.FindIndex(item => item.key.Contains(annotation.Id))) > -1) |
|
|
|
if ((index = fieldAssignments.FindIndex(item => item.key.Contains(annotation.Id))) > -1) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var xName = ctx.GetKnownNamespace("Name", XamlContext.KnownNamespace_Xaml, element); |
|
|
|
|
|
|
|
if (element.Attribute("Name") is null && element.Attribute(xName) is null) |
|
|
|
|
|
|
|
element.Add(new XAttribute(xName, fieldAssignments[index].value.FieldName)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if ((index = eventMappings.FindIndex(item => item.key.Contains(annotation.Id))) > -1) |
|
|
|
{ |
|
|
|
{ |
|
|
|
foreach (var entry in eventMappings[index].value) |
|
|
|
foreach (var entry in eventMappings[index].value) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -79,28 +88,30 @@ namespace ILSpy.BamlDecompiler.Rewrite |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
List<(LongSet, EventRegistration[])> DecompileEventMappings(XamlContext ctx, XDocument document) |
|
|
|
(List<(LongSet, FieldAssignment)>, List<(LongSet, EventRegistration[])>) DecompileConnections |
|
|
|
|
|
|
|
(XamlContext ctx, XDocument document) |
|
|
|
{ |
|
|
|
{ |
|
|
|
var result = new List<(LongSet, EventRegistration[])>(); |
|
|
|
var fieldAssignments = new List<(LongSet key, FieldAssignment value)>(); |
|
|
|
|
|
|
|
var eventMappings = new List<(LongSet, EventRegistration[])>(); |
|
|
|
|
|
|
|
|
|
|
|
var xClass = document.Root |
|
|
|
var xClass = document.Root |
|
|
|
.Elements().First() |
|
|
|
.Elements().First() |
|
|
|
.Attribute(ctx.GetKnownNamespace("Class", XamlContext.KnownNamespace_Xaml)); |
|
|
|
.Attribute(ctx.GetKnownNamespace("Class", XamlContext.KnownNamespace_Xaml)); |
|
|
|
if (xClass == null) |
|
|
|
if (xClass == null) |
|
|
|
return result; |
|
|
|
return (fieldAssignments, eventMappings); |
|
|
|
|
|
|
|
|
|
|
|
var type = ctx.TypeSystem.FindType(new FullTypeName(xClass.Value)).GetDefinition(); |
|
|
|
var type = ctx.TypeSystem.FindType(new FullTypeName(xClass.Value)).GetDefinition(); |
|
|
|
if (type == null) |
|
|
|
if (type == null) |
|
|
|
return result; |
|
|
|
return (fieldAssignments, eventMappings); |
|
|
|
|
|
|
|
|
|
|
|
DecompileEventMappings(ctx, result, componentConnectorTypeName, type); |
|
|
|
DecompileConnections(ctx, fieldAssignments, eventMappings, componentConnectorTypeName, type); |
|
|
|
DecompileEventMappings(ctx, result, styleConnectorTypeName, type); |
|
|
|
DecompileConnections(ctx, fieldAssignments, eventMappings, styleConnectorTypeName, type); |
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
return (fieldAssignments, eventMappings); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void DecompileEventMappings(XamlContext ctx, List<(LongSet, EventRegistration[])> result, |
|
|
|
void DecompileConnections(XamlContext ctx, List<(LongSet, FieldAssignment)> fieldAssignments, |
|
|
|
FullTypeName connectorTypeName, ITypeDefinition type) |
|
|
|
List<(LongSet, EventRegistration[])> eventMappings, FullTypeName connectorTypeName, ITypeDefinition type) |
|
|
|
{ |
|
|
|
{ |
|
|
|
var connectorInterface = ctx.TypeSystem.FindType(connectorTypeName).GetDefinition(); |
|
|
|
var connectorInterface = ctx.TypeSystem.FindType(connectorTypeName).GetDefinition(); |
|
|
|
if (connectorInterface == null) |
|
|
|
if (connectorInterface == null) |
|
|
@ -148,11 +159,19 @@ namespace ILSpy.BamlDecompiler.Rewrite |
|
|
|
{ |
|
|
|
{ |
|
|
|
foreach (var section in ilSwitch.Sections) |
|
|
|
foreach (var section in ilSwitch.Sections) |
|
|
|
{ |
|
|
|
{ |
|
|
|
events.Clear(); |
|
|
|
var field = FindField(section.Body); |
|
|
|
FindEvents(section.Body, events); |
|
|
|
if (!(field is null)) |
|
|
|
if (events.Count > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
result.Add((section.Labels, events.ToArray())); |
|
|
|
fieldAssignments.Add((section.Labels, field)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
events.Clear(); |
|
|
|
|
|
|
|
FindEvents(section.Body, events); |
|
|
|
|
|
|
|
if (events.Count > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
eventMappings.Add((section.Labels, events.ToArray())); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -169,16 +188,54 @@ namespace ILSpy.BamlDecompiler.Rewrite |
|
|
|
var inst = comp.Kind == ComparisonKind.Inequality |
|
|
|
var inst = comp.Kind == ComparisonKind.Inequality |
|
|
|
? ifInst.FalseInst |
|
|
|
? ifInst.FalseInst |
|
|
|
: ifInst.TrueInst; |
|
|
|
: ifInst.TrueInst; |
|
|
|
events.Clear(); |
|
|
|
|
|
|
|
FindEvents(inst, events); |
|
|
|
var field = FindField(inst); |
|
|
|
if (events.Count > 0) |
|
|
|
if (!(field is null)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
result.Add((new LongSet(id), events.ToArray())); |
|
|
|
fieldAssignments.Add((new LongSet(id), field)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
events.Clear(); |
|
|
|
|
|
|
|
FindEvents(inst, events); |
|
|
|
|
|
|
|
if (events.Count > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
eventMappings.Add((new LongSet(id), events.ToArray())); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FieldAssignment FindField(ILInstruction inst) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
switch (inst) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
case Block b: |
|
|
|
|
|
|
|
var t = b.Instructions.FirstOrDefault(); |
|
|
|
|
|
|
|
if (!(t is null) && MatchFieldAssignment(t, out var field)) |
|
|
|
|
|
|
|
return field; |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
case Branch br: |
|
|
|
|
|
|
|
return FindField(br.TargetBlock); |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
if (MatchFieldAssignment(inst, out field)) |
|
|
|
|
|
|
|
return field; |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool MatchFieldAssignment(ILInstruction inst, out FieldAssignment field) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
field = null; |
|
|
|
|
|
|
|
if (!inst.MatchStFld(out _, out var fld, out var value) || !value.MatchCastClass(out var arg, out _) |
|
|
|
|
|
|
|
|| !(arg.MatchLdLoc(out var t) && t.Kind == VariableKind.Parameter && t.Index == 1)) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
field = new FieldAssignment { FieldName = fld.Name }; |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void FindEvents(ILInstruction inst, List<EventRegistration> events) |
|
|
|
void FindEvents(ILInstruction inst, List<EventRegistration> events) |
|
|
|
{ |
|
|
|
{ |
|
|
|
EventRegistration @event; |
|
|
|
EventRegistration @event; |
|
|
|