From a4a7fda684f966a0ea2d92d7c0bef7b8628043ee Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Wed, 18 Apr 2018 21:18:12 +0200 Subject: [PATCH] [jnimarshalmethod-gen] Update the originating assembly (#300) Update the source assembly instead of creating a *new* assembly named `-new.dll` I expected I would need to load the originating assembly in the separate AppDomain, but instead it was enough to load it with Cecil with reading parameters having `ReadWrite = true`. Because the referenced assemblies might not be writable, we need to keep loading them as read only, thus with default `ReadWrite = false`. For that I added new method to our Cecil assembly resolver, to add existing assembly definition to its cache. We first read the originating assemblies as `ReadWrite` and add them to the resolver cache. --- Makefile | 4 +--- .../DirectoryAssemblyResolver.cs | 12 ++++++++++++ tools/jnimarshalmethod-gen/App.cs | 8 +++++++- tools/jnimarshalmethod-gen/TypeMover.cs | 5 ++--- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 8b47b4507..1caa2f69a 100644 --- a/Makefile +++ b/Makefile @@ -159,9 +159,7 @@ run-android: $(ATESTS) run-test-jnimarshal: bin/Test$(CONFIGURATION)/Java.Interop.Export-Tests.dll bin/Test$(CONFIGURATION)/$(JAVA_INTEROP_LIB) MONO_TRACE_LISTENER=Console.Out \ $(RUNTIME) bin/$(CONFIGURATION)/jnimarshalmethod-gen.exe -v -L $(JI_MONO_LIB_PATH)mono/4.5 -L $(JI_MONO_LIB_PATH)mono/4.5/Facades "$<" - $(RM) "$<" "$(basename $<)-JniMarshalMethods.dll" - mv "$(basename $<)-new.dll" "$<" - mv "$(basename $<)-new.pdb" "$(basename $<).pdb" + $(RM) "$(basename $<)-JniMarshalMethods.dll" $(call RUN_TEST,$<) diff --git a/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs b/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs index c596fed86..921e9bef9 100644 --- a/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs +++ b/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs @@ -110,6 +110,18 @@ public Dictionary ToResolverCache () return new Dictionary(cache); } + public bool AddToCache (AssemblyDefinition assembly) + { + var name = Path.GetFileNameWithoutExtension (assembly.MainModule.FileName); + + if (cache.ContainsKey (name)) + return false; + + cache [name] = assembly; + + return true; + } + public virtual AssemblyDefinition Load (string fileName, bool forceLoad = false) { if (!File.Exists (fileName)) diff --git a/tools/jnimarshalmethod-gen/App.cs b/tools/jnimarshalmethod-gen/App.cs index 0c1902ac9..b098dac5e 100644 --- a/tools/jnimarshalmethod-gen/App.cs +++ b/tools/jnimarshalmethod-gen/App.cs @@ -62,6 +62,11 @@ public static int Main (string [] args) return 0; } + var readWriteParameters = new ReaderParameters { + AssemblyResolver = resolver, + ReadSymbols = true, + ReadWrite = true, + }; foreach (var assembly in assemblies) { if (!File.Exists (assembly)) { Error ($"Path '{assembly}' does not exist."); @@ -69,7 +74,8 @@ public static int Main (string [] args) } resolver.SearchDirectories.Add (Path.GetDirectoryName (assembly)); - resolver.Load (assembly); + var ad = AssemblyDefinition.ReadAssembly (assembly, readWriteParameters); + resolver.AddToCache (ad); try { CreateMarshalMethodAssembly (assembly); diff --git a/tools/jnimarshalmethod-gen/TypeMover.cs b/tools/jnimarshalmethod-gen/TypeMover.cs index 0ba1baacf..e2e425666 100644 --- a/tools/jnimarshalmethod-gen/TypeMover.cs +++ b/tools/jnimarshalmethod-gen/TypeMover.cs @@ -46,11 +46,10 @@ public void Move () return; } - var newName = $"{Path.Combine (Path.GetDirectoryName (Destination.MainModule.FileName), Path.GetFileNameWithoutExtension (Destination.MainModule.FileName))}-new{Path.GetExtension (Destination.MainModule.FileName)}"; - Destination.Write (newName, new WriterParameters () { WriteSymbols = true }); + Destination.Write (new WriterParameters () { WriteSymbols = true }); if (App.Verbose) - App.ColorWriteLine ($"Wrote {newName} assembly", ConsoleColor.Cyan); + App.ColorWriteLine ($"Wrote updated {Destination.MainModule.FileName} assembly", ConsoleColor.Cyan); } static readonly string nestedName = "__<$>_jni_marshal_methods";