Skip to content

Commit

Permalink
rework to handle libs with IsApplication=true
Browse files Browse the repository at this point in the history
  • Loading branch information
dellis1972 committed Dec 11, 2020
1 parent 22576bf commit 8af10e9
Showing 1 changed file with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class RemoveResourceDesignerStep : BaseStep
{
TypeDefinition mainDesigner = null;
AssemblyDefinition mainAssembly = null;
CustomAttribute mainDesignerAttribute;
Dictionary<string, int> designerConstants;
Regex opCodeRegex = new Regex (@"([\w]+): ([\w]+) ([\w.]+) ([\w:./]+)");

Expand All @@ -24,40 +25,47 @@ protected override void Process ()
if (config == null)
return;
foreach(var asm in config.Assemblies) {
if (FindResourceDesigner (asm, isApplication: true, designer: out mainDesigner)) {
if (FindResourceDesigner (asm, isApplication: true, designer: out mainDesigner, designerAttribute: out mainDesignerAttribute)) {
mainAssembly = asm;
break;
}
}
if (mainDesigner == null) {
Context.LogMessage ($" Main Designer not found.");
Context.LogMessage ($" Main Designer not found.");
return;
}
Context.LogMessage ($" Main Designer found {mainDesigner.FullName}.");
Context.LogMessage ($" Main Designer found {mainDesigner.FullName}.");
designerConstants = BuildResourceDesignerFieldLookup (mainDesigner);
}

protected override void EndProcess ()
{
Context.LogMessage ($" End Process Called.");
if (mainDesigner != null) {
Context.LogMessage ($" Removing Main Designer {mainDesigner.FullName}");
RemoveDesigner (mainDesigner);
Context.LogMessage ($" Removing Designer Custom Attribute {mainDesignerAttribute.AttributeType.FullName}");
mainAssembly.CustomAttributes.Remove (mainDesignerAttribute);
Context.LogMessage ($" Setting Action on {mainAssembly.Name} to Save.");
Annotations.SetAction (mainAssembly, AssemblyAction.Save);
}
}

bool FindResourceDesigner (AssemblyDefinition assembly, bool isApplication, out TypeDefinition designer)
bool FindResourceDesigner (AssemblyDefinition assembly, bool isApplication, 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 == isApplication)
if (p.Name == "IsApplication" && (bool)p.Argument.Value == (isApplication ? isApplication : (bool)p.Argument.Value))
{
designerFullName = attribute.ConstructorArguments[0].Value.ToString();
break;
Expand Down Expand Up @@ -172,10 +180,19 @@ protected override void ProcessAssembly (AssemblyDefinition assembly)
if (MonoAndroidHelper.IsFrameworkAssembly (fileName))
return;

Context.LogMessage ($" Fixing up {assembly.Name.Name}");
if (!FindResourceDesigner (assembly, false, out TypeDefinition localDesigner)) {
Context.LogMessage ($" {assembly.Name.Name} does not have a designer file.");
return;
Context.LogMessage ($" Fixing up {assembly.Name.Name}");
TypeDefinition localDesigner = null;
CustomAttribute designerAttribute;
if (assembly != mainAssembly) {
Context.LogMessage ($" {assembly.Name.Name} is not the main assembly. ");
if (!FindResourceDesigner (assembly, isApplication: false, designer: out localDesigner, designerAttribute: out designerAttribute)) {
Context.LogMessage ($" {assembly.Name.Name} does not have a designer file.");
return;
}
} else {
Context.LogMessage ($" {assembly.Name.Name} is the main assembly. ");
localDesigner = mainDesigner;
designerAttribute = mainDesignerAttribute;
}

Context.LogMessage ($" {assembly.Name.Name} has designer {localDesigner.FullName}.");
Expand All @@ -187,8 +204,12 @@ protected override void ProcessAssembly (AssemblyDefinition assembly)
ProcessTypeR (type, localDesigner);
}
}

RemoveDesigner (localDesigner);
if (assembly != mainAssembly) {
RemoveDesigner (localDesigner);
if (designerAttribute != null) {
assembly.CustomAttributes.Remove (designerAttribute);
}
}
}
}
}

0 comments on commit 8af10e9

Please sign in to comment.