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; if (!Directory.Exists(directory)) return false; var potentialDirectories = new List { directory }; potentialDirectories.AddRange(Directory.GetDirectories(directory, "*", SearchOption.AllDirectories)); foreach (var subDirectory in potentialDirectories) { var 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) => TryResolveDllAssembly(assemblyName, directory, Assembly.LoadFile, out assembly); }