123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using BepInEx.Preloader.RuntimeFixes;
- using HarmonyXInterop;
- namespace BepInEx.Preloader
- {
- internal static class PreloaderRunner
- {
- public static void PreloaderPreMain()
- {
- string bepinPath = Utility.ParentDirectory(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH), 2);
- Paths.SetExecutablePath(EnvVars.DOORSTOP_PROCESS_PATH, bepinPath, EnvVars.DOORSTOP_MANAGED_FOLDER_DIR);
- HarmonyInterop.Initialize();
- AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
- PreloaderMain();
- }
- private static void PreloaderMain()
- {
- if (Preloader.ConfigApplyRuntimePatches.Value)
- {
- XTermFix.Apply();
- ConsoleSetOutFix.Apply();
- }
- Preloader.Run();
- }
- private static Assembly LocalResolve(object sender, ResolveEventArgs args)
- {
- if (!Utility.TryParseAssemblyName(args.Name, out var assemblyName))
- return null;
-
-
- var foundAssembly = AppDomain.CurrentDomain.GetAssemblies()
- .FirstOrDefault(x => Utility.TryParseAssemblyName(x.FullName, out var name) && name.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;
- }
- }
- internal static class Entrypoint
- {
- private static string preloaderPath;
-
-
-
- public static void Main()
- {
-
- string silentExceptionLog = $"preloader_{DateTime.Now:yyyyMMdd_HHmmss_fff}.log";
- try
- {
- EnvVars.LoadVars();
- string gamePath = Path.GetDirectoryName(EnvVars.DOORSTOP_PROCESS_PATH) ?? ".";
- silentExceptionLog = Path.Combine(gamePath, silentExceptionLog);
-
- preloaderPath = Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH));
- AppDomain.CurrentDomain.AssemblyResolve += ResolveCurrentDirectory;
-
-
- typeof(Entrypoint).Assembly.GetType($"BepInEx.Preloader.{nameof(PreloaderRunner)}")
- ?.GetMethod(nameof(PreloaderRunner.PreloaderPreMain))
- ?.Invoke(null, null);
- AppDomain.CurrentDomain.AssemblyResolve -= ResolveCurrentDirectory;
- }
- catch (Exception ex)
- {
- File.WriteAllText(silentExceptionLog, ex.ToString());
- }
- }
- private static Assembly ResolveCurrentDirectory(object sender, ResolveEventArgs args)
- {
-
- var name = new AssemblyName(args.Name);
- try
- {
- return Assembly.LoadFile(Path.Combine(preloaderPath, $"{name.Name}.dll"));
- }
- catch (Exception)
- {
- return null;
- }
- }
- }
- }
|