summaryrefslogtreecommitdiff
path: root/src/SMAPI
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-05-11 01:40:46 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-05-11 01:40:46 -0400
commit2b9703f98fedcf97fd5e511f1e30bcc8fd94b5cc (patch)
tree8585ea7099bce72968fd5b0c2ca797c676e21b5a /src/SMAPI
parent10531e537fda7c4901304b295f4ef60ac1f83eea (diff)
downloadSMAPI-2b9703f98fedcf97fd5e511f1e30bcc8fd94b5cc.tar.gz
SMAPI-2b9703f98fedcf97fd5e511f1e30bcc8fd94b5cc.tar.bz2
SMAPI-2b9703f98fedcf97fd5e511f1e30bcc8fd94b5cc.zip
fix Harmony issue when assembly is loaded from memory (#711)
Diffstat (limited to 'src/SMAPI')
-rw-r--r--src/SMAPI/Constants.cs3
-rw-r--r--src/SMAPI/Framework/ModLoading/AssemblyLoader.cs27
-rw-r--r--src/SMAPI/Framework/SCore.cs16
3 files changed, 42 insertions, 4 deletions
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);