Pārlūkot izejas kodu

Resolve assemblies in common folders

Bepis 6 gadi atpakaļ
vecāks
revīzija
1600b189dc
2 mainītis faili ar 33 papildinājumiem un 0 dzēšanām
  1. 28 0
      BepInEx.Common/Utility.cs
  2. 5 0
      BepInEx/Bootstrap/Preloader.cs

+ 28 - 0
BepInEx.Common/Utility.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
+using System.Reflection;
 
 namespace BepInEx.Common
 {
@@ -78,5 +79,32 @@ 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="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)
+        {
+            assembly = null;
+            string path = Path.Combine(directory, $"{new AssemblyName(name).Name}.dll");
+
+            if (!File.Exists(path))
+                return false;
+
+            try
+            {
+                assembly = Assembly.LoadFile(path);
+            }
+            catch (Exception)
+            {
+                return false;
+            }
+
+            return true;
+        }
     }
 }

+ 5 - 0
BepInEx/Bootstrap/Preloader.cs

@@ -178,6 +178,11 @@ namespace BepInEx.Bootstrap
             if (args.Name == "0Harmony, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null")
                 return Assembly.LoadFile(Path.Combine(CurrentExecutingAssemblyDirectoryPath, "0Harmony.dll"));
 
+            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;
+
             return null;
         }
     }