Browse Source

Add fuzzy version resolving for already loaded assemblies

Bepis 6 years ago
parent
commit
d3f78648e5
2 changed files with 13 additions and 9 deletions
  1. 3 3
      BepInEx.Common/Utility.cs
  2. 10 6
      BepInEx/Bootstrap/Preloader.cs

+ 3 - 3
BepInEx.Common/Utility.cs

@@ -84,14 +84,14 @@ namespace BepInEx.Common
         /// <summary>
         /// Try to resolve and load the given assembly DLL.
         /// </summary>
-        /// <param name="name">Name of the assembly. Follows the format of <see cref="AssemblyName" />.</param>
+        /// <param name="assemblyName">Name of the assembly, of the type <see cref="AssemblyName" />.</param>
         /// <param name="directory">Directory to search the assembly from.</param>
         /// <param name="assembly">The loaded assembly.</param>
         /// <returns>True, if the assembly was found and loaded. Otherwise, false.</returns>
-        public static bool TryResolveDllAssembly(string name, string directory, out Assembly assembly)
+        public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, out Assembly assembly)
         {
             assembly = null;
-            string path = Path.Combine(directory, $"{new AssemblyName(name).Name}.dll");
+            string path = Path.Combine(directory, $"{assemblyName.Name}.dll");
 
             if (!File.Exists(path))
                 return false;

+ 10 - 6
BepInEx/Bootstrap/Preloader.cs

@@ -244,13 +244,17 @@ namespace BepInEx.Bootstrap
 
         internal static Assembly LocalResolve(object sender, ResolveEventArgs args)
         {
-            if (args.Name == "0Harmony, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null")
-                return Assembly.LoadFile(Path.Combine(CurrentExecutingAssemblyDirectoryPath, "0Harmony.dll"));
+            AssemblyName assemblyName = new AssemblyName(args.Name);
 
-            if (Utility.TryResolveDllAssembly(args.Name, CurrentExecutingAssemblyDirectoryPath, out var assembly) ||
-                Utility.TryResolveDllAssembly(args.Name, PatcherPluginPath, out assembly) ||
-                Utility.TryResolveDllAssembly(args.Name, PluginPath, out assembly))
-                return assembly;
+            var foundAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name == assemblyName.Name);
+
+            if (foundAssembly != null)
+                return foundAssembly;
+
+            if (Utility.TryResolveDllAssembly(assemblyName, CurrentExecutingAssemblyDirectoryPath, out foundAssembly) ||
+                Utility.TryResolveDllAssembly(assemblyName, PatcherPluginPath, out foundAssembly) ||
+                Utility.TryResolveDllAssembly(assemblyName, PluginPath, out foundAssembly))
+                return foundAssembly;
 
             return null;
         }