using System; using System.IO; using System.Linq; using System.Reflection; namespace BepInEx.Preloader { internal static class Entrypoint { /// /// The main entrypoint of BepInEx, called from Doorstop. /// /// /// The arguments passed in from Doorstop. First argument is the path of the currently executing /// process. /// public static void Main(string[] args) { EnvVars.LoadVars(); string bepinPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH))); Paths.SetExecutablePath(args[0], bepinPath, EnvVars.DOORSTOP_MANAGED_FOLDER_DIR); AppDomain.CurrentDomain.AssemblyResolve += LocalResolve; Preloader.Run(); } /// /// A handler for to perform some special handling. /// /// It attempts to check currently loaded assemblies (ignoring the version), and then checks the BepInEx/core path, /// BepInEx/patchers path and the BepInEx folder, all in that order. /// /// /// /// /// internal static Assembly LocalResolve(object sender, ResolveEventArgs args) { var assemblyName = new AssemblyName(args.Name); var foundAssembly = AppDomain.CurrentDomain.GetAssemblies() .FirstOrDefault(x => x.GetName().Name == assemblyName.Name); if (foundAssembly != null) return foundAssembly; if (Utility.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly) || Utility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly) || Utility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly)) return foundAssembly; return null; } } }