From 83bdcd28386cebd36890565065912be4e3443603 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 14 Jan 2017 12:43:50 -0500 Subject: fix error loading mods if they have a .cache folder created on a different platform (#211) --- .../Framework/AssemblyRewriting/CacheEntry.cs | 19 +++++++++++++++++-- src/StardewModdingAPI/Framework/ModAssemblyLoader.cs | 7 +++++-- 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'src/StardewModdingAPI') diff --git a/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs b/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs index a747eaa8..4c3b86fe 100644 --- a/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs +++ b/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs @@ -1,4 +1,5 @@ using System.IO; +using StardewModdingAPI.AssemblyRewriters; namespace StardewModdingAPI.Framework.AssemblyRewriting { @@ -14,6 +15,12 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting /// The SMAPI version used to rewrite the assembly. public readonly string ApiVersion; + /// The target platform. + public readonly Platform Platform; + + /// The value for the machine used to rewrite the assembly. + public readonly string MachineName; + /// Whether to use the cached assembly instead of the original assembly. public readonly bool UseCachedAssembly; @@ -24,11 +31,15 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting /// Construct an instance. /// The MD5 hash for the original assembly. /// The SMAPI version used to rewrite the assembly. + /// The target platform. + /// The value for the machine used to rewrite the assembly. /// Whether to use the cached assembly instead of the original assembly. - public CacheEntry(string hash, string apiVersion, bool useCachedAssembly) + public CacheEntry(string hash, string apiVersion, Platform platform, string machineName, bool useCachedAssembly) { this.Hash = hash; this.ApiVersion = apiVersion; + this.Platform = platform; + this.MachineName = machineName; this.UseCachedAssembly = useCachedAssembly; } @@ -36,10 +47,14 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting /// The paths for the cached assembly. /// The MD5 hash of the original assembly. /// The current SMAPI version. - public bool IsUpToDate(CachePaths paths, string hash, ISemanticVersion currentVersion) + /// The target platform. + /// The value for the machine reading the assembly. + public bool IsUpToDate(CachePaths paths, string hash, ISemanticVersion currentVersion, Platform platform, string machineName) { return hash == this.Hash && this.ApiVersion == currentVersion.ToString() + && this.Platform == platform + && this.MachineName == machineName && (!this.UseCachedAssembly || File.Exists(paths.Assembly)); } } diff --git a/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs b/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs index a2c4ac23..e4760398 100644 --- a/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs +++ b/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs @@ -29,6 +29,8 @@ namespace StardewModdingAPI.Framework /// Encapsulates monitoring and logging. private readonly IMonitor Monitor; + /// The current game platform. + private readonly Platform TargetPlatform; /********* ** Public methods @@ -40,6 +42,7 @@ namespace StardewModdingAPI.Framework public ModAssemblyLoader(string cacheDirName, Platform targetPlatform, IMonitor monitor) { this.CacheDirName = cacheDirName; + this.TargetPlatform = targetPlatform; this.Monitor = monitor; this.AssemblyMap = Constants.GetAssemblyMap(targetPlatform); this.AssemblyTypeRewriter = new AssemblyTypeRewriter(this.AssemblyMap, monitor); @@ -58,7 +61,7 @@ namespace StardewModdingAPI.Framework CachePaths cachePaths = this.GetCachePaths(assemblyPath); { CacheEntry cacheEntry = File.Exists(cachePaths.Metadata) ? JsonConvert.DeserializeObject(File.ReadAllText(cachePaths.Metadata)) : null; - if (cacheEntry != null && cacheEntry.IsUpToDate(cachePaths, hash, Constants.ApiVersion)) + if (cacheEntry != null && cacheEntry.IsUpToDate(cachePaths, hash, Constants.ApiVersion, this.TargetPlatform, Environment.MachineName)) return new RewriteResult(assemblyPath, cachePaths, assemblyBytes, cacheEntry.Hash, cacheEntry.UseCachedAssembly, isNewerThanCache: false); // no rewrite needed } this.Monitor.Log($"Preprocessing {Path.GetFileName(assemblyPath)} for compatibility...", LogLevel.Trace); @@ -99,7 +102,7 @@ namespace StardewModdingAPI.Framework // cache all results foreach (RewriteResult result in results) { - CacheEntry cacheEntry = new CacheEntry(result.Hash, Constants.ApiVersion.ToString(), forceCacheAssemblies || result.UseCachedAssembly); + CacheEntry cacheEntry = new CacheEntry(result.Hash, Constants.ApiVersion.ToString(), this.TargetPlatform, Environment.MachineName, forceCacheAssemblies || result.UseCachedAssembly); File.WriteAllText(result.CachePaths.Metadata, JsonConvert.SerializeObject(cacheEntry)); if (forceCacheAssemblies || result.UseCachedAssembly) File.WriteAllBytes(result.CachePaths.Assembly, result.AssemblyBytes); -- cgit