123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using BepInEx.Bootstrap;
- using BepInEx.Configuration;
- using BepInEx.Logging;
- using BepInEx.NetLauncher.RuntimeFixes;
- using BepInEx.Preloader.Core;
- using BepInEx.Preloader.Core.Logging;
- namespace BepInEx.NetLauncher
- {
- public static class NetPreloader
- {
- private static readonly ManualLogSource Log = PreloaderLogger.Log;
- public static void Start(string[] args)
- {
- if (ConfigEntrypointExecutable.Value == null)
- {
- Log.LogFatal($"Entry executable was not set. Please set this in your config before launching the application");
- Program.ReadExit();
- return;
- }
- string executablePath = Path.GetFullPath(ConfigEntrypointExecutable.Value);
- if (!File.Exists(executablePath))
- {
- Log.LogFatal($"Unable to locate executable: {ConfigEntrypointExecutable.Value}");
- Program.ReadExit();
- return;
- }
- Paths.SetExecutablePath(executablePath);
- Program.ResolveDirectories.Add(Paths.GameRootPath);
- TypeLoader.SearchDirectories.Add(Paths.GameRootPath);
- Logger.Sources.Add(TraceLogSource.CreateSource());
- ChainloaderLogHelper.PrintLogInfo(Log);
- Log.LogInfo($"CLR runtime version: {Environment.Version}");
- Log.LogMessage("Preloader started");
- Assembly entrypointAssembly;
- using (var assemblyPatcher = new AssemblyPatcher())
- {
- assemblyPatcher.AddPatchersFromDirectory(Paths.PatcherPluginPath);
- Log.LogInfo($"{assemblyPatcher.PatcherPlugins.Count} patcher plugin(s) loaded");
- assemblyPatcher.LoadAssemblyDirectory(Paths.GameRootPath, "dll", "exe");
- Log.LogInfo($"{assemblyPatcher.AssembliesToPatch.Count} assemblies discovered");
- assemblyPatcher.PatchAndLoad();
- var assemblyName = AssemblyName.GetAssemblyName(executablePath);
- entrypointAssembly = assemblyPatcher.LoadedAssemblies.Values.FirstOrDefault(x => x.FullName == assemblyName.FullName);
- if (entrypointAssembly != null)
- {
- Log.LogDebug("Found patched entrypoint assembly! Using it");
- }
- else
- {
- Log.LogDebug("Using entrypoint assembly from disk");
- entrypointAssembly = Assembly.LoadFrom(executablePath);
- }
- }
- Log.LogMessage("Preloader finished");
- var chainloader = new NetChainloader();
- chainloader.Initialize();
- chainloader.Execute();
- AssemblyFix.Execute(entrypointAssembly);
- entrypointAssembly.EntryPoint.Invoke(null, new [] { args });
- }
- #region Config
- private static readonly ConfigEntry<string> ConfigEntrypointExecutable = ConfigFile.CoreConfig.Bind<string>(
- "Preloader.Entrypoint", "Assembly",
- null,
- "The local filename of the .NET executable to target.");
- #endregion
- }
- }
|