fix Harmony issue when assembly is loaded from memory (#711)
This commit is contained in:
parent
10531e537f
commit
2b9703f98f
|
@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
using StardewModdingApi.Installer.Enums;
|
using StardewModdingApi.Installer.Enums;
|
||||||
using StardewModdingAPI.Installer.Framework;
|
using StardewModdingAPI.Installer.Framework;
|
||||||
|
@ -624,7 +623,7 @@ namespace StardewModdingApi.Installer
|
||||||
{
|
{
|
||||||
try
|
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;
|
break;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
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>
|
/// <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="print">A callback which prints a message to the console.</param>
|
||||||
/// <param name="message">The message to print.</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>
|
/// <param name="indent">The indentation to prefix to output.</param>
|
||||||
private string InteractivelyChoose(string message, string[] options, string indent = "", Action<string> print = null)
|
private string InteractivelyChoose(string message, string[] options, string indent = "", Action<string> print = null)
|
||||||
{
|
{
|
||||||
print = print ?? this.PrintInfo;
|
print ??= this.PrintInfo;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
|
|
@ -61,6 +61,9 @@ namespace StardewModdingAPI
|
||||||
/// <summary>The absolute path to the folder containing SMAPI's internal files.</summary>
|
/// <summary>The absolute path to the folder containing SMAPI's internal files.</summary>
|
||||||
internal static readonly string InternalFilesPath = Program.DllSearchPath;
|
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>
|
/// <summary>The file path for the SMAPI configuration file.</summary>
|
||||||
internal static string ApiConfigPath => Path.Combine(Constants.InternalFilesPath, "config.json");
|
internal static string ApiConfigPath => Path.Combine(Constants.InternalFilesPath, "config.json");
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,9 @@ namespace StardewModdingAPI.Framework.ModLoading
|
||||||
/// <summary>The objects to dispose as part of this instance.</summary>
|
/// <summary>The objects to dispose as part of this instance.</summary>
|
||||||
private readonly HashSet<IDisposable> Disposables = new HashSet<IDisposable>();
|
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
|
** Public methods
|
||||||
|
@ -44,11 +47,15 @@ namespace StardewModdingAPI.Framework.ModLoading
|
||||||
/// <param name="targetPlatform">The current game platform.</param>
|
/// <param name="targetPlatform">The current game platform.</param>
|
||||||
/// <param name="monitor">Encapsulates monitoring and logging.</param>
|
/// <param name="monitor">Encapsulates monitoring and logging.</param>
|
||||||
/// <param name="paranoidMode">Whether to detect paranoid mode issues.</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.Monitor = monitor;
|
||||||
this.ParanoidMode = paranoidMode;
|
this.ParanoidMode = paranoidMode;
|
||||||
this.AssemblyMap = this.TrackForDisposal(Constants.GetAssemblyMap(targetPlatform));
|
this.AssemblyMap = this.TrackForDisposal(Constants.GetAssemblyMap(targetPlatform));
|
||||||
|
this.TempFolderPath = tempFolderPath;
|
||||||
|
|
||||||
|
// init resolver
|
||||||
this.AssemblyDefinitionResolver = this.TrackForDisposal(new AssemblyDefinitionResolver());
|
this.AssemblyDefinitionResolver = this.TrackForDisposal(new AssemblyDefinitionResolver());
|
||||||
this.AssemblyDefinitionResolver.AddSearchDirectory(Constants.ExecutionPath);
|
this.AssemblyDefinitionResolver.AddSearchDirectory(Constants.ExecutionPath);
|
||||||
this.AssemblyDefinitionResolver.AddSearchDirectory(Constants.InternalFilesPath);
|
this.AssemblyDefinitionResolver.AddSearchDirectory(Constants.InternalFilesPath);
|
||||||
|
@ -124,9 +131,23 @@ namespace StardewModdingAPI.Framework.ModLoading
|
||||||
if (changed)
|
if (changed)
|
||||||
{
|
{
|
||||||
if (!oneAssembly)
|
if (!oneAssembly)
|
||||||
this.Monitor.Log($" Loading {assembly.File.Name} (rewritten in memory)...", LogLevel.Trace);
|
this.Monitor.Log($" Loading {assembly.File.Name} (rewritten)...", LogLevel.Trace);
|
||||||
using (MemoryStream outStream = new MemoryStream())
|
|
||||||
|
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);
|
assembly.Definition.Write(outStream);
|
||||||
byte[] bytes = outStream.ToArray();
|
byte[] bytes = outStream.ToArray();
|
||||||
lastAssembly = Assembly.Load(bytes);
|
lastAssembly = Assembly.Load(bytes);
|
||||||
|
|
|
@ -213,6 +213,20 @@ namespace StardewModdingAPI.Framework
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#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>
|
/// <summary>Launch SMAPI.</summary>
|
||||||
|
@ -748,7 +762,7 @@ namespace StardewModdingAPI.Framework
|
||||||
|
|
||||||
// load mods
|
// load mods
|
||||||
IDictionary<IModMetadata, Tuple<string, string>> skippedMods = new Dictionary<IModMetadata, Tuple<string, string>>();
|
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
|
// init
|
||||||
HashSet<string> suppressUpdateChecks = new HashSet<string>(this.Settings.SuppressUpdateChecks, StringComparer.InvariantCultureIgnoreCase);
|
HashSet<string> suppressUpdateChecks = new HashSet<string>(this.Settings.SuppressUpdateChecks, StringComparer.InvariantCultureIgnoreCase);
|
||||||
|
|
Loading…
Reference in New Issue