diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-05-11 01:40:46 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-05-11 01:40:46 -0400 |
commit | 2b9703f98fedcf97fd5e511f1e30bcc8fd94b5cc (patch) | |
tree | 8585ea7099bce72968fd5b0c2ca797c676e21b5a | |
parent | 10531e537fda7c4901304b295f4ef60ac1f83eea (diff) | |
download | SMAPI-2b9703f98fedcf97fd5e511f1e30bcc8fd94b5cc.tar.gz SMAPI-2b9703f98fedcf97fd5e511f1e30bcc8fd94b5cc.tar.bz2 SMAPI-2b9703f98fedcf97fd5e511f1e30bcc8fd94b5cc.zip |
fix Harmony issue when assembly is loaded from memory (#711)
-rw-r--r-- | src/SMAPI.Installer/InteractiveInstaller.cs | 40 | ||||
-rw-r--r-- | src/SMAPI/Constants.cs | 3 | ||||
-rw-r--r-- | src/SMAPI/Framework/ModLoading/AssemblyLoader.cs | 27 | ||||
-rw-r--r-- | src/SMAPI/Framework/SCore.cs | 16 |
4 files changed, 44 insertions, 42 deletions
diff --git a/src/SMAPI.Installer/InteractiveInstaller.cs b/src/SMAPI.Installer/InteractiveInstaller.cs index 5b0c6e1f..1457848b 100644 --- a/src/SMAPI.Installer/InteractiveInstaller.cs +++ b/src/SMAPI.Installer/InteractiveInstaller.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; -using System.Threading; using Microsoft.Win32; using StardewModdingApi.Installer.Enums; using StardewModdingAPI.Installer.Framework; @@ -624,7 +623,7 @@ namespace StardewModdingApi.Installer { try { - this.ForceDelete(Directory.Exists(path) ? new DirectoryInfo(path) : (FileSystemInfo)new FileInfo(path)); + FileUtilities.ForceDelete(Directory.Exists(path) ? new DirectoryInfo(path) : (FileSystemInfo)new FileInfo(path)); break; } catch (Exception ex) @@ -665,41 +664,6 @@ namespace StardewModdingApi.Installer } } - /// <summary>Delete a file or folder regardless of file permissions, and block until deletion completes.</summary> - /// <param name="entry">The file or folder to reset.</param> - /// <remarks>This method is mirrored from <c>FileUtilities.ForceDelete</c> in the toolkit.</remarks> - private void ForceDelete(FileSystemInfo entry) - { - // ignore if already deleted - entry.Refresh(); - if (!entry.Exists) - return; - - // delete children - if (entry is DirectoryInfo folder) - { - foreach (FileSystemInfo child in folder.GetFileSystemInfos()) - this.ForceDelete(child); - } - - // reset permissions & delete - entry.Attributes = FileAttributes.Normal; - entry.Delete(); - - // wait for deletion to finish - for (int i = 0; i < 10; i++) - { - entry.Refresh(); - if (entry.Exists) - Thread.Sleep(500); - } - - // throw exception if deletion didn't happen before timeout - entry.Refresh(); - if (entry.Exists) - throw new IOException($"Timed out trying to delete {entry.FullName}"); - } - /// <summary>Interactively ask the user to choose a value.</summary> /// <param name="print">A callback which prints a message to the console.</param> /// <param name="message">The message to print.</param> @@ -707,7 +671,7 @@ namespace StardewModdingApi.Installer /// <param name="indent">The indentation to prefix to output.</param> private string InteractivelyChoose(string message, string[] options, string indent = "", Action<string> print = null) { - print = print ?? this.PrintInfo; + print ??= this.PrintInfo; while (true) { diff --git a/src/SMAPI/Constants.cs b/src/SMAPI/Constants.cs index a898fccd..907a93b2 100644 --- a/src/SMAPI/Constants.cs +++ b/src/SMAPI/Constants.cs @@ -61,6 +61,9 @@ namespace StardewModdingAPI /// <summary>The absolute path to the folder containing SMAPI's internal files.</summary> internal static readonly string InternalFilesPath = Program.DllSearchPath; + /// <summary>The folder containing temporary files that are only valid for the current session.</summary> + internal static string InternalTempFilesPath => Path.Combine(Program.DllSearchPath, ".temp"); + /// <summary>The file path for the SMAPI configuration file.</summary> internal static string ApiConfigPath => Path.Combine(Constants.InternalFilesPath, "config.json"); diff --git a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs index 570686fe..78e717e9 100644 --- a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs +++ b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs @@ -36,6 +36,9 @@ namespace StardewModdingAPI.Framework.ModLoading /// <summary>The objects to dispose as part of this instance.</summary> private readonly HashSet<IDisposable> Disposables = new HashSet<IDisposable>(); + /// <summary>The full path to the folder in which to save rewritten assemblies.</summary> + private readonly string TempFolderPath; + /********* ** Public methods @@ -44,11 +47,15 @@ namespace StardewModdingAPI.Framework.ModLoading /// <param name="targetPlatform">The current game platform.</param> /// <param name="monitor">Encapsulates monitoring and logging.</param> /// <param name="paranoidMode">Whether to detect paranoid mode issues.</param> - public AssemblyLoader(Platform targetPlatform, IMonitor monitor, bool paranoidMode) + /// <param name="tempFolderPath">The full path to the folder in which to save rewritten assemblies.</param> + public AssemblyLoader(Platform targetPlatform, IMonitor monitor, bool paranoidMode, string tempFolderPath) { this.Monitor = monitor; this.ParanoidMode = paranoidMode; this.AssemblyMap = this.TrackForDisposal(Constants.GetAssemblyMap(targetPlatform)); + this.TempFolderPath = tempFolderPath; + + // init resolver this.AssemblyDefinitionResolver = this.TrackForDisposal(new AssemblyDefinitionResolver()); this.AssemblyDefinitionResolver.AddSearchDirectory(Constants.ExecutionPath); this.AssemblyDefinitionResolver.AddSearchDirectory(Constants.InternalFilesPath); @@ -124,9 +131,23 @@ namespace StardewModdingAPI.Framework.ModLoading if (changed) { if (!oneAssembly) - this.Monitor.Log($" Loading {assembly.File.Name} (rewritten in memory)...", LogLevel.Trace); - using (MemoryStream outStream = new MemoryStream()) + this.Monitor.Log($" Loading {assembly.File.Name} (rewritten)...", LogLevel.Trace); + + if (assembly.Definition.MainModule.AssemblyReferences.Any(p => p.Name == "0Harmony")) + { + // Note: the assembly must be loaded from disk for Harmony compatibility. + // Loading it from memory sets the assembly module's FullyQualifiedName to + // "<Unknown>", so Harmony incorrectly identifies the module in its + // Patch.PatchMethod when handling multiple patches for the same method, + // leading to "Token 0x... is not valid in the scope of module HarmonySharedState" + // errors (e.g. https://smapi.io/log/A0gAsc3M). + string tempPath = Path.Combine(this.TempFolderPath, $"{Path.GetFileNameWithoutExtension(assemblyPath)}.{Guid.NewGuid()}.dll"); + assembly.Definition.Write(tempPath); + lastAssembly = Assembly.LoadFile(tempPath); + } + else { + using MemoryStream outStream = new MemoryStream(); assembly.Definition.Write(outStream); byte[] bytes = outStream.ToArray(); lastAssembly = Assembly.Load(bytes); diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index de9c955d..12dc9c3d 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -213,6 +213,20 @@ namespace StardewModdingAPI.Framework return; } #endif + + // reset temp folder + if (Directory.Exists(Constants.InternalTempFilesPath)) + { + try + { + FileUtilities.ForceDelete(new DirectoryInfo(Constants.InternalTempFilesPath)); + } + catch (Exception ex) + { + this.Monitor.Log($"Couldn't delete temporary files at {Constants.InternalTempFilesPath}: {ex}", LogLevel.Trace); + } + } + Directory.CreateDirectory(Constants.InternalTempFilesPath); } /// <summary>Launch SMAPI.</summary> @@ -748,7 +762,7 @@ namespace StardewModdingAPI.Framework // load mods IDictionary<IModMetadata, Tuple<string, string>> skippedMods = new Dictionary<IModMetadata, Tuple<string, string>>(); - using (AssemblyLoader modAssemblyLoader = new AssemblyLoader(Constants.Platform, this.Monitor, this.Settings.ParanoidWarnings)) + using (AssemblyLoader modAssemblyLoader = new AssemblyLoader(Constants.Platform, this.Monitor, this.Settings.ParanoidWarnings, Constants.InternalTempFilesPath)) { // init HashSet<string> suppressUpdateChecks = new HashSet<string>(this.Settings.SuppressUpdateChecks, StringComparer.InvariantCultureIgnoreCase); |