forked from icsharpcode/ILSpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXamlContext.cs
205 lines (163 loc) · 6.58 KB
/
XamlContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Copyright (c) 2015 Ki
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Xml.Linq;
using ICSharpCode.Decompiler.TypeSystem;
using ILSpy.BamlDecompiler.Baml;
using ILSpy.BamlDecompiler.Xaml;
namespace ILSpy.BamlDecompiler {
internal class XamlContext {
XamlContext(IDecompilerTypeSystem typeSystem) {
TypeSystem = typeSystem;
NodeMap = new Dictionary<BamlRecord, BamlBlockNode>();
XmlNs = new XmlnsDictionary();
}
Dictionary<ushort, XamlType> typeMap = new Dictionary<ushort, XamlType>();
Dictionary<ushort, XamlProperty> propertyMap = new Dictionary<ushort, XamlProperty>();
Dictionary<string, XNamespace> xmlnsMap = new Dictionary<string, XNamespace>();
public IDecompilerTypeSystem TypeSystem { get; }
public CancellationToken CancellationToken { get; private set; }
public BamlDecompilerOptions BamlDecompilerOptions { get; private set; }
public BamlContext Baml { get; private set; }
public BamlNode RootNode { get; private set; }
public IDictionary<BamlRecord, BamlBlockNode> NodeMap { get; }
public XmlnsDictionary XmlNs { get; }
public static XamlContext Construct(IDecompilerTypeSystem typeSystem, BamlDocument document, CancellationToken token, BamlDecompilerOptions bamlDecompilerOptions) {
var ctx = new XamlContext(typeSystem);
ctx.CancellationToken = token;
ctx.BamlDecompilerOptions = bamlDecompilerOptions ?? new BamlDecompilerOptions();
ctx.Baml = BamlContext.ConstructContext(typeSystem, document, token);
ctx.RootNode = BamlNode.Parse(document, token);
ctx.BuildPIMappings(document);
ctx.BuildNodeMap(ctx.RootNode as BamlBlockNode);
return ctx;
}
void BuildNodeMap(BamlBlockNode node) {
if (node == null)
return;
NodeMap[node.Header] = node;
foreach (var child in node.Children) {
if (child is BamlBlockNode childBlock)
BuildNodeMap(childBlock);
}
}
void BuildPIMappings(BamlDocument document) {
foreach (var record in document) {
var piMap = record as PIMappingRecord;
if (piMap == null)
continue;
XmlNs.SetPIMapping(piMap.XmlNamespace, piMap.ClrNamespace, Baml.ResolveAssembly(piMap.AssemblyId).FullAssemblyName);
}
}
public XamlType ResolveType(ushort id) {
if (typeMap.TryGetValue(id, out var xamlType))
return xamlType;
IType type;
IModule assembly;
string fullAssemblyName;
if (id > 0x7fff) {
type = Baml.KnownThings.Types((KnownTypes)(short)-unchecked((short)id));
assembly = type.GetDefinition().ParentModule;
fullAssemblyName = assembly.FullAssemblyName;
}
else {
var typeRec = Baml.TypeIdMap[id];
(fullAssemblyName, assembly) = Baml.ResolveAssembly(typeRec.AssemblyId);
type = ReflectionHelper.ParseReflectionName(typeRec.TypeFullName).Resolve(new SimpleTypeResolveContext(TypeSystem));
}
var clrNs = type.Namespace;
var xmlNs = XmlNs.LookupXmlns(fullAssemblyName, clrNs);
typeMap[id] = xamlType = new XamlType(assembly, fullAssemblyName, clrNs, type.Name, GetXmlNamespace(xmlNs)) {
ResolvedType = type
};
return xamlType;
}
public XamlProperty ResolveProperty(ushort id) {
if (propertyMap.TryGetValue(id, out var xamlProp))
return xamlProp;
XamlType type;
string name;
IMember member;
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;
}
else {
var attrRec = Baml.AttributeIdMap[id];
type = ResolveType(attrRec.OwnerTypeId);
name = attrRec.Name;
member = null;
}
propertyMap[id] = xamlProp = new XamlProperty(type, name) {
ResolvedMember = member
};
xamlProp.TryResolve();
return xamlProp;
}
public string ResolveString(ushort id) {
if (id > 0x7fff)
return Baml.KnownThings.Strings(unchecked((short)-id));
else if (Baml.StringIdMap.ContainsKey(id))
return Baml.StringIdMap[id].Value;
return null;
}
public XNamespace GetXmlNamespace(string xmlns) {
if (xmlns == null)
return null;
if (!xmlnsMap.TryGetValue(xmlns, out var ns))
xmlnsMap[xmlns] = ns = XNamespace.Get(xmlns);
return ns;
}
public string TryGetXmlNamespace(IModule assembly, string typeNamespace) {
if (assembly == null)
return null;
HashSet<string> possibleXmlNs = new HashSet<string>();
foreach (var attr in assembly.GetAssemblyAttributes().Where(a => a.AttributeType.FullName == "System.Windows.Markup.XmlnsDefinitionAttribute")) {
Debug.Assert(attr.FixedArguments.Length == 2);
if (attr.FixedArguments.Length != 2)
continue;
var xmlNs = attr.FixedArguments[0].Value as string;
var typeNs = attr.FixedArguments[1].Value as string;
Debug.Assert((object)xmlNs != null && (object)typeNs != null);
if ((object)xmlNs == null || (object)typeNs == null)
continue;
if (typeNamespace == typeNs)
possibleXmlNs.Add(xmlNs);
}
if (possibleXmlNs.Contains("http://schemas.microsoft.com/winfx/2006/xaml/presentation"))
return "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
return possibleXmlNs.FirstOrDefault();
}
public XName GetXamlNsName(string name, XElement elem = null) {
var xNs = GetXmlNamespace("http://schemas.microsoft.com/winfx/2006/xaml");
XName xName;
if (elem != null && xNs == elem.GetDefaultNamespace())
xName = name;
else
xName = xNs + name;
return xName;
}
public XName GetPseudoName(string name) => XNamespace.Get("https://github.com/icsharpcode/ILSpy").GetName(name);
}
}