-
Notifications
You must be signed in to change notification settings - Fork 565
[marshal methods] Make AndroidEnvironmentInternal.UnhandledException … #10607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using Microsoft.Android.Build.Tasks; | ||
| using Microsoft.Build.Framework; | ||
|
|
||
| namespace Xamarin.Android.Tasks; | ||
|
|
||
| public class FixUpMonoAndroidRuntime : AndroidTask | ||
| { | ||
| public override string TaskPrefix => "FUMAR"; | ||
|
|
||
| [Required] | ||
| public ITaskItem[] ResolvedAssemblies { get; set; } = []; | ||
|
|
||
| public override bool RunTask () | ||
| { | ||
| List<ITaskItem> monoAndroidRuntimeItems = new (); | ||
| foreach (ITaskItem item in ResolvedAssemblies) { | ||
| if (!MonoAndroidHelper.StringEquals (Path.GetFileName (item.ItemSpec), "Mono.Android.Runtime.dll", StringComparison.OrdinalIgnoreCase)) { | ||
| continue; | ||
| } | ||
| monoAndroidRuntimeItems.Add (item); | ||
| } | ||
|
|
||
| if (monoAndroidRuntimeItems.Count == 0) { | ||
| Log.LogDebugMessage ("No 'Mono.Android.Runtime.dll' items found"); | ||
| return !Log.HasLoggedErrors; | ||
| } | ||
|
|
||
| return MonoAndroidRuntimeMarshalMethodsFixUp.Run (Log, monoAndroidRuntimeItems) && !Log.HasLoggedErrors; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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; | ||
| } | ||
| } | ||
grendello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you find you needed this before this PR was green? Is it because the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it would hit the sharing violation exception from time to time. It might be due to the code you pointed to, but fixing this should be done in a separate PR. |
||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
grendello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return null; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.