using System; using System.Collections.Generic; using System.IO; using System.Reflection; namespace BepInEx.NetLauncher { /// /// Generic helper properties and methods. /// public static class LocalUtility { /// /// Try to resolve and load the given assembly DLL. /// /// Name of the assembly, of the type . /// Directory to search the assembly from. /// The loaded assembly. /// True, if the assembly was found and loaded. Otherwise, false. private static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, Func loader, out T assembly) where T : class { assembly = null; var potentialDirectories = new List { directory }; potentialDirectories.AddRange(Directory.GetDirectories(directory, "*", SearchOption.AllDirectories)); foreach (string subDirectory in potentialDirectories) { string path = Path.Combine(subDirectory, $"{assemblyName.Name}.dll"); if (!File.Exists(path)) continue; try { assembly = loader(path); } catch (Exception) { continue; } return true; } return false; } /// /// Try to resolve and load the given assembly DLL. /// /// Name of the assembly, of the type . /// Directory to search the assembly from. /// The loaded assembly. /// True, if the assembly was found and loaded. Otherwise, false. public static bool TryResolveDllAssembly(AssemblyName assemblyName, string directory, out Assembly assembly) { return TryResolveDllAssembly(assemblyName, directory, Assembly.LoadFile, out assembly); } } }