Preloader.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Diagnostics;
  3. using BepInEx.Logging;
  4. using BepInEx.Preloader.Core;
  5. using BepInEx.Preloader.Core.Logging;
  6. namespace BepInEx.IL2CPP
  7. {
  8. public static class Preloader
  9. {
  10. public static string IL2CPPUnhollowedPath { get; internal set; }
  11. private static PreloaderConsoleListener PreloaderLog { get; set; }
  12. internal static ManualLogSource Log => PreloaderLogger.Log;
  13. internal static ManualLogSource UnhollowerLog { get; set; }
  14. public static IL2CPPChainloader Chainloader { get; private set; }
  15. public static void Run()
  16. {
  17. try
  18. {
  19. ConsoleManager.Initialize(false);
  20. PreloaderLog = new PreloaderConsoleListener();
  21. Logger.Listeners.Add(PreloaderLog);
  22. if (ConsoleManager.ConfigConsoleEnabled.Value)
  23. {
  24. ConsoleManager.CreateConsole();
  25. Logger.Listeners.Add(new ConsoleLogListener());
  26. }
  27. ChainloaderLogHelper.PrintLogInfo(Log);
  28. Log.LogInfo($"Running under Unity v{FileVersionInfo.GetVersionInfo(Paths.ExecutablePath).FileVersion}");
  29. Log.LogDebug($"Game executable path: {Paths.ExecutablePath}");
  30. Log.LogDebug($"Unhollowed assembly directory: {IL2CPPUnhollowedPath}");
  31. Log.LogDebug($"BepInEx root path: {Paths.BepInExRootPath}");
  32. UnhollowerLog = Logger.CreateLogSource("Unhollower");
  33. UnhollowerBaseLib.LogSupport.InfoHandler += UnhollowerLog.LogInfo;
  34. UnhollowerBaseLib.LogSupport.WarningHandler += UnhollowerLog.LogWarning;
  35. UnhollowerBaseLib.LogSupport.TraceHandler += UnhollowerLog.LogDebug;
  36. UnhollowerBaseLib.LogSupport.ErrorHandler += UnhollowerLog.LogError;
  37. if (ProxyAssemblyGenerator.CheckIfGenerationRequired())
  38. ProxyAssemblyGenerator.GenerateAssemblies();
  39. using (var assemblyPatcher = new AssemblyPatcher())
  40. {
  41. assemblyPatcher.AddPatchersFromDirectory(Paths.PatcherPluginPath);
  42. Log.LogInfo($"{assemblyPatcher.PatcherPlugins.Count} patcher plugin{(assemblyPatcher.PatcherPlugins.Count == 1 ? "" : "s")} loaded");
  43. assemblyPatcher.LoadAssemblyDirectory(IL2CPPUnhollowedPath);
  44. Log.LogInfo($"{assemblyPatcher.PatcherPlugins.Count} assemblies discovered");
  45. assemblyPatcher.PatchAndLoad();
  46. }
  47. Logger.Listeners.Remove(PreloaderLog);
  48. Chainloader = new IL2CPPChainloader();
  49. Chainloader.Initialize();
  50. }
  51. catch (Exception ex)
  52. {
  53. Log.LogFatal(ex);
  54. throw;
  55. }
  56. }
  57. }
  58. }