-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38f4e44
commit 53f3f2e
Showing
7 changed files
with
154 additions
and
6 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Mono.Cecil; | ||
|
||
using Java.Interop.Tools.Cecil; | ||
|
||
using Mono.Linker; | ||
using Mono.Linker.Steps; | ||
|
||
using Mono.Tuner; | ||
#if NET5_LINKER | ||
using Microsoft.Android.Sdk.ILLink; | ||
#endif | ||
|
||
namespace MonoDroid.Tuner | ||
{ | ||
public class FixLegacyResourceDesignerStep : | ||
#if NET5_LINKER | ||
BaseMarkHandler | ||
#else // !NET5_LINKER | ||
BaseStep | ||
#endif // !NET5_LINKER | ||
{ | ||
AssemblyDefinition designerAssembly = null; | ||
|
||
#if NET5_LINKER | ||
protected void ProcessType (TypeDefinition type) | ||
{ | ||
} | ||
#else | ||
protected override void Process () | ||
{ | ||
var designerNameAssembly = AssemblyNameReference.Parse ("Xamarin.Android.Resource.Designer, Version=1.0.0.0"); | ||
designerAssembly = Context.Resolver.Resolve (designerNameAssembly); | ||
if (designerAssembly == null) { | ||
Context.LogMessage ($" Did not find Xamarin.Android.Resource.Designer"); | ||
return; | ||
} | ||
} | ||
protected override void ProcessAssembly (AssemblyDefinition assembly) | ||
{ | ||
if (designerAssembly == null) { | ||
Context.LogMessage ($" Not using Xamarin.Android.Resource.Designer"); | ||
return; | ||
} | ||
if (!FindResourceDesigner (assembly, mainApplication: false, out TypeDefinition designer, out CustomAttribute designerAttribute)) { | ||
Context.LogMessage ($" {assembly.Name.Name} has not designer. "); | ||
return; | ||
} | ||
Context.LogMessage ($" {assembly.Name.Name} has a designer. "); | ||
if (designer.BaseType.FullName == "Xamarin.Android.Resource.Designer.Resource") { | ||
Context.LogMessage ($" {assembly.Name.Name} has aleady been processed. "); | ||
return; | ||
} | ||
assembly.MainModule.AssemblyReferences.Add (designerAssembly.Name); | ||
var designerAssemblyDef = assembly.MainModule.AssemblyResolver.Resolve(designerAssembly.Name); | ||
var t = designerAssemblyDef.MainModule.GetTypes ().First (x => x.FullName == "Xamarin.Android.Resource.Designer.Resource"); | ||
var designerType = assembly.MainModule.ImportReference (t.Resolve ()); | ||
ClearDesignerClass (designer); | ||
designer.BaseType = designerType; | ||
// now replace all ldsfld with a call to the property get_ method. | ||
} | ||
#endif | ||
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)) | ||
return false; | ||
|
||
foreach (ModuleDefinition module in assembly.Modules) | ||
{ | ||
foreach (TypeDefinition type in module.Types) | ||
{ | ||
if (type.FullName == designerFullName) | ||
{ | ||
designer = type; | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
void ClearDesignerClass (TypeDefinition designer) | ||
{ | ||
Context.LogMessage ($" TryRemoving {designer.FullName}"); | ||
designer.NestedTypes.Clear (); | ||
designer.Methods.Clear (); | ||
designer.Fields.Clear (); | ||
designer.Properties.Clear (); | ||
designer.CustomAttributes.Clear (); | ||
designer.Interfaces.Clear (); | ||
designer.Events.Clear (); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters