-
Notifications
You must be signed in to change notification settings - Fork 537
/
Copy pathLinkDesignerBase.cs
260 lines (238 loc) · 7.54 KB
/
LinkDesignerBase.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using Mono.Cecil;
using Mono.Linker;
using Mono.Linker.Steps;
using System;
using System.Linq;
using Xamarin.Android.Tasks;
using System.Collections.Generic;
using System.Globalization;
using Mono.Cecil.Cil;
using System.Text.RegularExpressions;
using Mono.Collections.Generic;
#if ILLINK
using Microsoft.Android.Sdk.ILLink;
#endif
namespace MonoDroid.Tuner {
public abstract class LinkDesignerBase : BaseStep
{
#if ILLINK
protected IMetadataResolver Cache => Context;
#else // !ILLINK
public LinkDesignerBase (IMetadataResolver cache)
{
Cache = cache;
}
protected IMetadataResolver Cache { get; private set; }
#endif // !ILLINK
public virtual void LogMessage (string message)
{
Context.LogMessage (message);
}
public virtual void LogError (int code, string error)
{
#if ILLINK
Context.LogMessage (MessageContainer.CreateCustomErrorMessage (error, code, origin: new MessageOrigin ()));
#else // !ILLINK
Console.Error.WriteLine ($"error XA{code}: {error}");
#endif // !ILLINK
}
public virtual AssemblyDefinition Resolve (AssemblyNameReference name)
{
return Context.Resolve (name);
}
protected bool FindResourceDesigner (AssemblyDefinition assembly, bool mainApplication, out TypeDefinition designer, out CustomAttribute designerAttribute)
{
string designerFullName = null;
designer = null;
designerAttribute = null;
foreach (CustomAttribute attribute in assembly.CustomAttributes)
{
if (attribute.AttributeType.FullName == "Android.Runtime.ResourceDesignerAttribute")
{
designerAttribute = attribute;
if (attribute.HasProperties)
{
foreach (var p in attribute.Properties)
{
if (p.Name == "IsApplication" && (bool)p.Argument.Value == (mainApplication ? mainApplication : (bool)p.Argument.Value))
{
designerFullName = attribute.ConstructorArguments[0].Value.ToString ();
break;
}
}
}
break;
}
}
if (string.IsNullOrEmpty(designerFullName)) {
LogMessage ($"Inspecting member references for assembly: {assembly.FullName};");
var memberRefs = assembly.MainModule.GetMemberReferences ();
foreach (var memberRef in memberRefs) {
string declaringType = memberRef.DeclaringType?.ToString () ?? string.Empty;
if (!declaringType.Contains (".Resource/")) {
continue;
}
if (declaringType.Contains ("_Microsoft.Android.Resource.Designer")) {
continue;
}
var resolved = false;
try {
var def = memberRef.Resolve ();
if (resolved = def != null) {
LogMessage ($"Resolved member `{memberRef?.Name}`");
}
} catch (Exception ex) {
LogMessage ($"Exception resolving member `{memberRef?.Name}`: {ex}");
resolved = false;
}
if (!resolved) {
LogMessage ($"Adding _Linker.Generated.Resource to {assembly.Name.Name}. Could not resolve {memberRef?.Name} : {declaringType}");
designer = new TypeDefinition ("_Linker.Generated", "Resource", TypeAttributes.Public | TypeAttributes.AnsiClass);
designer.BaseType = new TypeDefinition ("System", "Object", TypeAttributes.Public | TypeAttributes.AnsiClass);
return true;
}
}
}
if (string.IsNullOrEmpty(designerFullName))
return false;
foreach (ModuleDefinition module in assembly.Modules)
{
foreach (TypeDefinition type in module.Types)
{
if (type.FullName == designerFullName)
{
designer = type;
return true;
}
}
}
return false;
}
protected void ClearDesignerClass (TypeDefinition designer, bool completely = false)
{
LogMessage ($" TryRemoving {designer.FullName}");
// for each of the nested types clear all but the
// int[] fields.
if (!completely) {
for (int i = designer.NestedTypes.Count -1; i >= 0; i--) {
var nestedType = designer.NestedTypes [i];
RemoveFieldsFromType (nestedType, designer.Module);
if (nestedType.Fields.Count == 0) {
// no fields we do not need this class at all.
designer.NestedTypes.RemoveAt (i);
}
}
RemoveUpdateIdValues (designer);
} else {
designer.NestedTypes.Clear ();
}
designer.Fields.Clear ();
designer.Properties.Clear ();
designer.CustomAttributes.Clear ();
designer.Interfaces.Clear ();
designer.Events.Clear ();
}
protected Dictionary<string, int> BuildResourceDesignerFieldLookup (TypeDefinition type)
{
var output = new Dictionary<string, int> ();
foreach (TypeDefinition definition in type.NestedTypes)
{
foreach (FieldDefinition field in definition.Fields)
{
string key = $"{definition.Name}::{field.Name}";
if (!output.ContainsKey (key))
output.Add(key, int.Parse (field.Constant?.ToString () ?? "0", CultureInfo.InvariantCulture));
}
}
return output;
}
protected void FixType (TypeDefinition type, TypeDefinition localDesigner)
{
foreach (MethodDefinition method in type.Methods)
{
if (!method.HasBody)
continue;
FixBody (method.Body, localDesigner);
}
foreach (PropertyDefinition property in type.Properties)
{
if (property.GetMethod != null && property.GetMethod.HasBody)
{
FixBody (property.GetMethod.Body, localDesigner);
}
if (property.SetMethod != null && property.SetMethod.HasBody)
{
FixBody (property.SetMethod.Body, localDesigner);
}
}
foreach (TypeDefinition nestedType in type.NestedTypes)
{
FixType (nestedType, localDesigner);
}
}
protected void RemoveFieldsFromType (TypeDefinition type, ModuleDefinition module)
{
for (int i = type.Fields.Count - 1; i >= 0; i--) {
var field = type.Fields [i];
if (field.FieldType.IsArray) {
continue;
}
LogMessage ($"Removing {type.Name}::{field.Name}");
type.Fields.RemoveAt (i);
}
}
protected void RemoveUpdateIdValues (TypeDefinition type)
{
foreach (var method in type.Methods) {
if (method.Name.Contains ("UpdateIdValues")) {
FixUpdateIdValuesBody (method);
} else {
FixBody (method.Body, type);
}
}
foreach (var nestedType in type.NestedTypes) {
RemoveUpdateIdValues (nestedType);
}
}
protected void FixUpdateIdValuesBody (MethodDefinition method)
{
List<Instruction> finalInstructions = new List<Instruction> ();
Collection<Instruction> instructions = method.Body.Instructions;
for (int i = 0; i < method.Body.Instructions.Count-1; i++) {
Instruction instruction = instructions[i];
string line = instruction.ToString ();
bool found = line.Contains ("Int32[]") || instruction.OpCode == OpCodes.Ret;
if (!found) {
method.Body.Instructions.Remove (instruction);
i--;
}
}
}
protected void FixupAssemblyTypes (AssemblyDefinition assembly, TypeDefinition designer)
{
foreach (ModuleDefinition module in assembly.Modules)
{
foreach (TypeDefinition type in module.Types)
{
if (type.FullName == designer.FullName)
continue;
FixType (type, designer);
}
}
}
protected override void ProcessAssembly (AssemblyDefinition assembly)
{
LoadDesigner ();
var action = Annotations.HasAction (assembly) ? Annotations.GetAction (assembly) : AssemblyAction.Skip;
if (action == AssemblyAction.Delete)
return;
if (ProcessAssemblyDesigner (assembly)) {
if (action == AssemblyAction.Skip || action == AssemblyAction.Copy)
Annotations.SetAction (assembly, AssemblyAction.Save);
}
}
internal abstract bool ProcessAssemblyDesigner (AssemblyDefinition assemblyDefinition);
protected abstract void LoadDesigner ();
protected abstract void FixBody (MethodBody body, TypeDefinition designer);
}
}