1313using Mono . Tuner ;
1414#if ILLINK
1515using Microsoft . Android . Sdk . ILLink ;
16- #endif
16+ #endif // ILLINK
1717
1818namespace MonoDroid . Tuner
1919{
20- public class FixLegacyResourceDesignerStep :
21- #if ILLINK
22- BaseMarkHandler
23- #else // !ILLINK
24- BaseStep
25- #endif // !ILLINK
20+ public class FixLegacyResourceDesignerStep : BaseStep
2621 {
22+ #if ILLINK
23+ protected override void Process ( )
24+ {
25+ cache = Context ;
26+ }
27+ #else
28+ public FixLegacyResourceDesignerStep ( IMetadataResolver cache )
29+ {
30+ this . cache = cache ;
31+ }
32+
33+ readonly
34+ #endif
35+ IMetadataResolver cache ;
2736 AssemblyDefinition designerAssembly = null ;
37+ Dictionary < string , MethodDefinition > lookup ;
2838
29- #if ILLINK
30- public override void Initialize ( LinkContext context , MarkContext markContext )
39+ protected override void ProcessAssembly ( AssemblyDefinition assembly )
3140 {
32- base . Initialize ( context , markContext ) ;
33- markContext . RegisterMarkAssemblyAction ( asm => ProcessAssembly ( asm ) ) ;
34- var designerNameAssembly = AssemblyNameReference . Parse ( "Xamarin.Android.Resource.Designer, Version=1.0.0.0" ) ;
35- try {
36- designerAssembly = Context . Resolve ( designerNameAssembly ) ;
37- } catch ( Mono . Cecil . AssemblyResolutionException ) {
38- }
39- if ( designerAssembly == null ) {
40- context . LogMessage ( $ " Did not find Xamarin.Android.Resource.Designer") ;
41+ LoadDesigner ( ) ;
42+
43+ var action = Annotations . HasAction ( assembly ) ? Annotations . GetAction ( assembly ) : AssemblyAction . Skip ;
44+ if ( action == AssemblyAction . Delete )
4145 return ;
46+
47+ if ( ProcessAssemblyDesigner ( assembly ) ) {
48+ if ( action == AssemblyAction . Skip || action == AssemblyAction . Copy )
49+ Annotations . SetAction ( assembly , AssemblyAction . Save ) ;
4250 }
4351 }
4452
45- protected void ProcessAssembly ( AssemblyDefinition assembly )
53+ public virtual void LogMessage ( string message )
4654 {
47- ProcessAssemblyDesigner ( assembly ) ;
55+ Context . LogMessage ( message ) ;
4856 }
49- #else
50- protected override void Process ( )
57+
58+ public virtual AssemblyDefinition Resolve ( AssemblyNameReference name )
5159 {
60+ return Context . Resolve ( name ) ;
61+ }
62+
63+ protected bool LoadDesigner ( )
64+ {
65+ if ( designerAssembly != null )
66+ return true ;
5267 var designerNameAssembly = AssemblyNameReference . Parse ( "Xamarin.Android.Resource.Designer, Version=1.0.0.0" ) ;
5368 try {
54- designerAssembly = Context . Resolver . Resolve ( designerNameAssembly ) ;
69+ designerAssembly = Resolve ( designerNameAssembly ) ;
5570 } catch ( Mono . Cecil . AssemblyResolutionException ) {
5671 }
5772 if ( designerAssembly == null ) {
58- Context . LogMessage ( $ " Did not find Xamarin.Android.Resource.Designer") ;
59- return ;
73+ LogMessage ( $ " Did not find Xamarin.Android.Resource.Designer") ;
74+ return false ;
6075 }
76+ return true ;
6177 }
62-
63-
64- protected override void ProcessAssembly ( AssemblyDefinition assembly )
65- {
66- ProcessAssemblyDesigner ( assembly ) ;
67- }
68- #endif
69- Dictionary < string , MethodDefinition > lookup ;
70- void ProcessAssemblyDesigner ( AssemblyDefinition assembly )
78+ internal bool ProcessAssemblyDesigner ( AssemblyDefinition assembly )
7179 {
7280 if ( designerAssembly == null ) {
73- Context . LogMessage ( $ " Not using Xamarin.Android.Resource.Designer") ;
74- return ;
81+ LogMessage ( $ " Not using Xamarin.Android.Resource.Designer") ;
82+ return false ;
7583 }
84+
7685 if ( ! FindResourceDesigner ( assembly , mainApplication : false , out TypeDefinition designer , out CustomAttribute designerAttribute ) ) {
77- Context . LogMessage ( $ " { assembly . Name . Name } has not designer. ") ;
78- return ;
86+ LogMessage ( $ " { assembly . Name . Name } has not designer. ") ;
87+ return false ;
7988 }
80- Context . LogMessage ( $ " { assembly . Name . Name } has a designer. ") ;
89+
90+ LogMessage ( $ " { assembly . Name . Name } has a designer. ") ;
8191 if ( designer . BaseType . FullName == "Xamarin.Android.Resource.Designer.Resource" ) {
82- Context . LogMessage ( $ " { assembly . Name . Name } has already been processed. ") ;
83- return ;
92+ LogMessage ( $ " { assembly . Name . Name } has already been processed. ") ;
93+ return false ;
8494 }
95+
8596 assembly . MainModule . AssemblyReferences . Add ( designerAssembly . Name ) ;
8697 var designerAssemblyDef = assembly . MainModule . AssemblyResolver . Resolve ( designerAssembly . Name ) ;
8798 var t = designerAssemblyDef . MainModule . GetTypes ( ) . First ( x => x . FullName == "Xamarin.Android.Resource.Designer.Resource" ) ;
@@ -91,9 +102,11 @@ void ProcessAssemblyDesigner (AssemblyDefinition assembly)
91102
92103 // now replace all ldsfld with a call to the property get_ method.
93104 FixupAssemblyTypes ( assembly , designer ) ;
105+
94106 // then clean out the designer.
95107 ClearDesignerClass ( designer ) ;
96108 designer . BaseType = designerType ;
109+ return true ;
97110 }
98111
99112 Dictionary < string , MethodDefinition > BuildResourceDesignerPropertyLookup ( TypeDefinition type )
@@ -154,7 +167,7 @@ bool FindResourceDesigner (AssemblyDefinition assembly, bool mainApplication, ou
154167
155168 void ClearDesignerClass ( TypeDefinition designer )
156169 {
157- Context . LogMessage ( $ " TryRemoving { designer . FullName } ") ;
170+ LogMessage ( $ " TryRemoving { designer . FullName } ") ;
158171 designer . NestedTypes . Clear ( ) ;
159172 designer . Methods . Clear ( ) ;
160173 designer . Fields . Clear ( ) ;
@@ -166,11 +179,13 @@ void ClearDesignerClass (TypeDefinition designer)
166179
167180 void FixupAssemblyTypes ( AssemblyDefinition assembly , TypeDefinition designer )
168181 {
169- Context . LogMessage ( $ " Fixup Types for { assembly . FullName } using { designer . FullName } ") ;
182+ LogMessage ( $ " Fixup Types for { assembly . FullName } using { designer . FullName } ") ;
170183 foreach ( ModuleDefinition module in assembly . Modules )
171184 {
172185 foreach ( TypeDefinition type in module . Types )
173186 {
187+ if ( type . FullName == designer . FullName )
188+ continue ;
174189 FixType ( type , designer ) ;
175190 }
176191 }
@@ -179,9 +194,8 @@ void FixupAssemblyTypes (AssemblyDefinition assembly, TypeDefinition designer)
179194 void FixBody ( MethodBody body , TypeDefinition designer )
180195 {
181196 // look for ldsfld lines which call a designer field
182- if ( ! body . Method . FullName . StartsWith ( "System.Void Xamarin.Forms.Platform.Android.FormsAppCompatActivity::OnCreate" , StringComparison . Ordinal ) )
183- return ;
184- Context . LogMessage ( $ " Fixing up { body . Method . FullName } for { designer . FullName } ") ;
197+ //if (!body.Method.FullName.StartsWith ("System.Void Xamarin.Forms.Platform.Android.FormsAppCompatActivity::OnCreate", StringComparison.Ordinal))
198+ // return;
185199
186200 // IL_0068: ldsfld int32 Xamarin.Forms.Platform.Android.Resource/Layout::Toolbar
187201 // replace with
@@ -194,22 +208,23 @@ void FixBody (MethodBody body, TypeDefinition designer)
194208 string line = i . ToString ( ) ;
195209 int idx = line . IndexOf ( designerFullName , StringComparison . OrdinalIgnoreCase ) ;
196210 if ( idx >= 0 ) {
197- Context . LogMessage ( $ " { i . OpCode } => { line } ") ;
211+ LogMessage ( $ " Fixing up { body . Method . FullName } for { designer . FullName } ") ;
212+ LogMessage ( $ " { i . OpCode } => { line } ") ;
198213 string key = line . Substring ( idx + designerFullName . Length ) ;
199- Context . LogMessage ( $ " Looking for { key } ") ;
214+ LogMessage ( $ " Looking for { key } ") ;
200215 if ( lookup . TryGetValue ( key , out MethodDefinition method ) ) {
201- Context . LogMessage ( $ " Found { key } ") ;
216+ LogMessage ( $ " Found { key } ") ;
202217 var importedMethod = designer . Module . ImportReference ( method ) ;
203218 var newIn = Instruction . Create ( OpCodes . Call , importedMethod ) ;
204219 instructions . Add ( i , newIn ) ;
205220 }
206221 }
207222 }
208223 if ( instructions . Count > 0 )
209- Context . LogMessage ( $ " Fixing up { body . Method . FullName } ") ;
224+ LogMessage ( $ " Fixing up { body . Method . FullName } ") ;
210225 foreach ( var i in instructions )
211226 {
212- Context . LogMessage ( $ " Fixing up { i . Key } => { i . Value } ") ;
227+ LogMessage ( $ " Fixing up { i . Key } => { i . Value } ") ;
213228 processor . Replace ( i . Key , i . Value ) ;
214229 }
215230 }
0 commit comments