-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathMonoAndroidRuntimeMarshalMethodsFixUp.cs
More file actions
102 lines (84 loc) · 2.88 KB
/
MonoAndroidRuntimeMarshalMethodsFixUp.cs
File metadata and controls
102 lines (84 loc) · 2.88 KB
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
using System.Collections.Generic;
using System.IO;
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Mono.Cecil;
namespace Xamarin.Android.Tasks;
class MonoAndroidRuntimeMarshalMethodsFixUp
{
const string RuntimeTypeName = "Android.Runtime.AndroidEnvironmentInternal";
public static bool Run (TaskLoggingHelper log, List<ITaskItem> items)
{
bool everythingWorked = true;
foreach (ITaskItem item in items) {
if (!ApplyFixUp (log, item)) {
everythingWorked = false;
}
}
return everythingWorked;
}
static bool ApplyFixUp (TaskLoggingHelper log, ITaskItem monoAndroidRuntime)
{
string newDirPath = Path.Combine (Path.GetDirectoryName (monoAndroidRuntime.ItemSpec), "new")!;
string newFilePath = Path.Combine (newDirPath, Path.GetFileName (monoAndroidRuntime.ItemSpec));
Directory.CreateDirectory (newDirPath);
string origPdbPath = Path.ChangeExtension (monoAndroidRuntime.ItemSpec, ".pdb");
bool havePdb = File.Exists (origPdbPath);
log.LogDebugMessage ($"Fixing up {monoAndroidRuntime.ItemSpec}");
var readerParams = new ReaderParameters () {
InMemory = true,
ReadSymbols = havePdb,
};
using AssemblyDefinition asmdef = AssemblyDefinition.ReadAssembly (monoAndroidRuntime.ItemSpec, readerParams);
TypeDefinition? androidRuntimeInternal = null;
foreach (ModuleDefinition module in asmdef.Modules) {
androidRuntimeInternal = FindAndroidRuntimeInternal (module);
if (androidRuntimeInternal != null) {
break;
}
}
if (androidRuntimeInternal == null) {
log.LogDebugMessage ($"'{RuntimeTypeName}' not found in {monoAndroidRuntime.ItemSpec}");
return true; // Not an error, per se...
}
log.LogDebugMessage ($"Found '{RuntimeTypeName}', making it public");
androidRuntimeInternal.IsPublic = true;
var writerParams = new WriterParameters {
WriteSymbols = havePdb,
};
asmdef.Write (newFilePath, writerParams);
CopyFile (log, newFilePath, monoAndroidRuntime.ItemSpec);
RemoveFile (log, newFilePath);
if (!havePdb) {
return true;
}
string pdbPath = Path.ChangeExtension (newFilePath, ".pdb");
havePdb = File.Exists (pdbPath);
if (!havePdb) {
return true;
}
CopyFile (log, pdbPath, origPdbPath);
RemoveFile (log, pdbPath);
return true;
}
static void CopyFile (TaskLoggingHelper log, string source, string target)
{
log.LogDebugMessage ($"Copying rewritten assembly: {source} -> {target}");
MonoAndroidHelper.CopyFileAvoidSharingViolations (log, source, target);
}
static void RemoveFile (TaskLoggingHelper log, string? path)
{
log.LogDebugMessage ($"Deleting: {path}");
MonoAndroidHelper.TryRemoveFile (log, path);
}
static TypeDefinition? FindAndroidRuntimeInternal (ModuleDefinition module)
{
foreach (TypeDefinition t in module.Types) {
if (MonoAndroidHelper.StringEquals (RuntimeTypeName, t.FullName)) {
return t;
}
}
return null;
}
}