diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-12-02 17:15:52 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-12-02 17:15:52 -0500 |
commit | ad773a947acaf02b472846bc3eefdac2e514a1a7 (patch) | |
tree | 013ee0ff21b928e696e7815b00fe477aff9a0db2 | |
parent | 67feb7e9a9ee057fdfb5b30840970b3952048b45 (diff) | |
download | SMAPI-ad773a947acaf02b472846bc3eefdac2e514a1a7.tar.gz SMAPI-ad773a947acaf02b472846bc3eefdac2e514a1a7.tar.bz2 SMAPI-ad773a947acaf02b472846bc3eefdac2e514a1a7.zip |
fix assembly resolution when mods try to load types from other mods (#166)
-rw-r--r-- | src/StardewModdingAPI/Framework/ModAssemblyLoader.cs | 15 | ||||
-rw-r--r-- | src/StardewModdingAPI/Program.cs | 21 |
2 files changed, 13 insertions, 23 deletions
diff --git a/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs b/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs index c5f9ec31..d92a04f3 100644 --- a/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs +++ b/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs @@ -103,10 +103,19 @@ namespace StardewModdingAPI.Framework } /// <summary>Resolve an assembly from its name.</summary> - /// <param name="shortName">The short assembly name.</param> - public Assembly ResolveAssembly(string shortName) + /// <param name="name">The assembly name.</param> + /// <remarks> + /// This implementation returns the first loaded assembly which matches the short form of + /// the assembly name, to resolve assembly resolution issues when rewriting + /// assemblies (especially with Mono). Since this is meant to be called on <see cref="AppDomain.AssemblyResolve"/>, + /// the implicit assumption is that loading the exact assembly failed. + /// </remarks> + public Assembly ResolveAssembly(string name) { - return this.AssemblyMap.Targets.FirstOrDefault(p => p.GetName().Name == shortName); + string shortName = name.Split(new[] { ',' }, 2).First(); // get simple name (without version and culture) + return AppDomain.CurrentDomain + .GetAssemblies() + .FirstOrDefault(p => p.GetName().Name == shortName); } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 4ca402e5..e648ed64 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -305,26 +305,7 @@ namespace StardewModdingAPI // get assembly loader ModAssemblyLoader modAssemblyLoader = new ModAssemblyLoader(Program.CachePath, Program.TargetPlatform, Program.Monitor); - - // 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; - }; + AppDomain.CurrentDomain.AssemblyResolve += (sender, e) => modAssemblyLoader.ResolveAssembly(e.Name); // load mods foreach (string directory in Directory.GetDirectories(Program.ModPath)) |