Skip to content

Commit

Permalink
emergency commit of all the stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dellis1972 committed Nov 4, 2021
1 parent 38f4e44 commit 53f3f2e
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 6 deletions.
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 ();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ static Pipeline CreatePipeline (LinkerOptions options)
pipeline.AppendStep (new RemoveResources (options.I18nAssemblies)); // remove collation tables
// end monodroid specific

pipeline.AppendStep (new FixLegacyResourceDesignerStep ());
pipeline.AppendStep (new FixAbstractMethodsStep (cache));
pipeline.AppendStep (new MonoDroidMarkStep (cache));
pipeline.AppendStep (new SweepStep ());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected override bool IsSdk (string assemblyName)
{
return assemblyName.Equals ("Java.Interop", StringComparison.Ordinal)
|| assemblyName.Equals ("Java.Interop.GenericMarshaler", StringComparison.Ordinal)
|| assemblyName.Equals ("Xamarin.Android.Resource.Designer", StringComparison.Ordinal)
|| base.IsSdk (assemblyName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Copyright (C) 2016 Xamarin. All rights reserved.
ProjectDir="$(ProjectDir)"
Resources="@(AndroidResource);@(AndroidBoundLayout)"
ResourceDirectory="$(MonoAndroidResourcePrefix)"
FrameworkDirectories="$(_XATargetFrameworkDirectories);$(_XATargetFrameworkDirectories)Facades"
AdditionalResourceDirectories="@(LibraryResourceDirectories)"
>
</GenerateResourceDesignerAssembly>
Expand All @@ -68,6 +69,7 @@ Copyright (C) 2016 Xamarin. All rights reserved.
<ReferencePath Include="$(_GenerateResourceDesignerAssemblyOutput)">
<CopyLocal>true</CopyLocal>
</ReferencePath>
<ResolvedFileToPublish Include="$(_GenerateResourceDesignerAssemblyOutput)" />
<Compile Remove="$(_AndroidResourceDesignerFile)" />
</ItemGroup>
</Target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<AndroidHttpClientHandlerType Condition=" '$(AndroidHttpClientHandlerType)' == '' and '$(UsingAndroidNETSdk)' == 'true' ">Xamarin.Android.Net.AndroidMessageHandler</AndroidHttpClientHandlerType>
<AndroidHttpClientHandlerType Condition=" '$(AndroidHttpClientHandlerType)' == '' and '$(UsingAndroidNETSdk)' != 'true' ">Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
<AndroidGenerateResourceDesigner Condition=" '$(AndroidGenerateResourceDesigner)' == '' ">true</AndroidGenerateResourceDesigner>
<AndroidUseIntermediateDesignerFile Condition=" '$(AndroidUseIntermediateDesignerFile)' == '' ">$(AndroidGenerateResourceDesigner)</AndroidUseIntermediateDesignerFile>
<AndroidUseIntermediateDesignerFile Condition=" '$(AndroidUseIntermediateDesignerFile)' == '' And '$(AndroidUseDesignerAssembly)' == 'False' ">$(AndroidGenerateResourceDesigner)</AndroidUseIntermediateDesignerFile>
<GenerateDependencyFile Condition=" '$(GenerateDependencyFile)' == '' ">false</GenerateDependencyFile>
<CopyLocalLockFileAssemblies Condition=" '$(CopyLocalLockFileAssemblies)' == '' ">false</CopyLocalLockFileAssemblies>
<ComputeNETCoreBuildOutputFiles Condition=" '$(ComputeNETCoreBuildOutputFiles)' == '' ">false</ComputeNETCoreBuildOutputFiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This file contains the implementation for 'dotnet publish'.
<_AllPackageFormats Include="$(AndroidPackageFormat);$(AndroidPackageFormats)" />
<_AndroidPackageFormats Include="@(_AllPackageFormats->Distinct())" />
<_AndroidFilesToPublish Include="$(OutputPath)*.%(_AndroidPackageFormats.Identity)" />
<_AndroidFilesToPublish Include="$(_GenerateResourceDesignerAssemblyOutput)" Condition="Exists('$(_GenerateResourceDesignerAssemblyOutput)')" />
<ResolvedFileToPublish Remove="@(ResolvedFileToPublish)" />
<ResolvedFileToPublish Include="@(_AndroidFilesToPublish)" RelativePath="%(FileName)%(Extension)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Android.Build.Tasks;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Java.Interop.Tools.Cecil;

namespace Xamarin.Android.Tasks
{
Expand Down Expand Up @@ -49,24 +50,41 @@ public class GenerateResourceDesignerAssembly : AndroidTask

public ITaskItem[] AdditionalResourceDirectories { get; set; }

public ITaskItem[] FrameworkDirectories { get; set; }

TypeReference intArray;
TypeReference intRef;
TypeReference objectRef;
Dictionary<string, string> resource_fixup = new Dictionary<string, string> (StringComparer.OrdinalIgnoreCase);

public override bool RunTask ()
{
using (var res = new DirectoryAssemblyResolver (this.CreateTaskLogger (), loadDebugSymbols: false)) {
Run(res);
}
return !Log.HasLoggedErrors;
}

bool Run(DirectoryAssemblyResolver res) {

foreach (var dir in FrameworkDirectories) {
if (Directory.Exists (dir.ItemSpec))
res.SearchDirectories.Add (dir.ItemSpec);
}
// ResourceDirectory may be a relative path, and
// we need to compare it to absolute paths
ResourceDirectory = Path.GetFullPath (ResourceDirectory);

BuildRenameMap (ResourceDirectory);
// Generate an assembly which contains all the values in the provided
// R.txt file.
var mp = new ModuleParameters ();
mp.AssemblyResolver = res;
mp.Kind = ModuleKind.Dll;
var assembly = AssemblyDefinition.CreateAssembly (
new AssemblyNameDefinition (DesignerAssemblyName, new Version (1, 0)),
DesignerAssemblyName,
ModuleKind.Dll);
mp);

var module = assembly.MainModule;

Expand All @@ -85,10 +103,11 @@ public override bool RunTask ()
var attr = new CustomAttribute (targetFrameworkConstructor);
// ".NETStandard,Version=v2.0"
// {TargetFrameworkIdentifier},Version={TargetFrameworkVersion}
attr.ConstructorArguments.Add (new CustomAttributeArgument (module.TypeSystem.String, $".NETStandard,Version=2.0"));
attr.ConstructorArguments.Add (new CustomAttributeArgument (module.TypeSystem.String, $"{TargetFrameworkIdentifier},Version={TargetFrameworkVersion}"));
attr.Properties.Add (new CustomAttributeNamedArgument("FrameworkDisplayName", new CustomAttributeArgument(module.TypeSystem.String, "")));
module.Assembly.CustomAttributes.Add (attr);


var att = TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.BeforeFieldInit;

intArray = new ArrayType (module.TypeSystem.Int32);
Expand All @@ -105,13 +124,17 @@ public override bool RunTask ()

ProcessRtxtFile (RTxtFile.ItemSpec, resourceDesigner, module);

// var mscorlib = module.AssemblyReferences.FirstOrDefault(x => x.Name == "mscorlib");
// if (mscorlib != null)
// module.AssemblyReferences.Remove(mscorlib);

assembly.Write (OutputFile.ItemSpec);
return !Log.HasLoggedErrors;
}

MethodReference ImportCustomAttributeConstructor (string type, ModuleDefinition module, ModuleDefinition sourceModule)
MethodReference ImportCustomAttributeConstructor (string type, ModuleDefinition module, ModuleDefinition sourceModule = null)
{
var tr = module.ImportReference (sourceModule.ExportedTypes.First(x => x.FullName == type).Resolve ());
var tr = module.ImportReference ((sourceModule ?? module).ExportedTypes.First(x => x.FullName == type).Resolve ());
var tv = tr.Resolve();
return module.ImportReference (tv.Methods.First(x => x.IsConstructor));
}
Expand Down Expand Up @@ -185,7 +208,7 @@ void CreateIntArrayProperty (string resourceClass, string propertyName, int[] va
nestedType.Methods.Insert (Math.Max(0, nestedType.Methods.Count () -1), p.GetMethod);
}

Dictionary<string, TypeDefinition> resourceClasses = new Dictionary<string, TypeDefinition> ();
Dictionary<string, TypeDefinition> resourceClasses = new Dictionary<string, TypeDefinition> (StringComparer.OrdinalIgnoreCase);

void CreateCtor (TypeDefinition type, ModuleDefinition module)
{
Expand Down

0 comments on commit 53f3f2e

Please sign in to comment.