From 44dfb6fac5650aa2c10cc76832d836a440ad8a2d Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 30 Nov 2016 22:44:40 -0500 Subject: fix assembly resolution failing for rewritten mods that reference a different version of Json.NET (#166) --- .../Framework/ModAssemblyLoader.cs | 5 ++--- src/StardewModdingAPI/Program.cs | 23 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) (limited to 'src/StardewModdingAPI') diff --git a/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs b/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs index e095cde8..6c5e6032 100644 --- a/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs +++ b/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs @@ -103,10 +103,9 @@ namespace StardewModdingAPI.Framework } /// Resolve an assembly from its name. - /// The assembly name. - public Assembly ResolveAssembly(string name) + /// The short assembly name. + public Assembly ResolveAssembly(string shortName) { - string shortName = name.Split(new[] { ',' }, 2).First(); return this.AssemblyMap.Targets.FirstOrDefault(p => p.GetName().Name == shortName); } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index f0686039..4ca402e5 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -303,9 +303,30 @@ namespace StardewModdingAPI { Program.Monitor.Log("Loading mods..."); + // get assembly loader ModAssemblyLoader modAssemblyLoader = new ModAssemblyLoader(Program.CachePath, Program.TargetPlatform, Program.Monitor); - AppDomain.CurrentDomain.AssemblyResolve += (sender, e) => modAssemblyLoader.ResolveAssembly(e.Name); // supplement Mono's assembly resolution which doesn't handle assembly rewrites very well + // handle assembly resolution failure + AppDomain.CurrentDomain.AssemblyResolve += (sender, e) => + { + // get assembly name (without version, culture, etc) + string shortName = e.Name.Split(new[] { ',' }, 2).First(); + + // one of the assembly references injected during assembly rewriting? + // technically this shouldn't happen, but apparently Mono's assembly resolution doesn't handle assembly rewrites very well + Assembly assembly = modAssemblyLoader.ResolveAssembly(shortName); + if (assembly != null) + return assembly; + + // Json.NET? + // mods often reference one version of Json.NET, but SMAPI might use a different version + if (shortName == typeof(JsonConvert).Assembly.GetName().Name) + return typeof(JsonConvert).Assembly; + + return null; + }; + + // load mods foreach (string directory in Directory.GetDirectories(Program.ModPath)) { // ignore internal directory -- cgit