Chainloader.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using BepInEx.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using BepInEx.Logging;
  9. using UnityEngine;
  10. using UnityInjector.ConsoleUtil;
  11. using UnityLogWriter = BepInEx.Logging.UnityLogWriter;
  12. namespace BepInEx.Bootstrap
  13. {
  14. /// <summary>
  15. /// The manager and loader for all plugins, and the entry point for BepInEx.
  16. /// </summary>
  17. public class Chainloader
  18. {
  19. /// <summary>
  20. /// The loaded and initialized list of plugins.
  21. /// </summary>
  22. public static List<BaseUnityPlugin> Plugins { get; protected set; } = new List<BaseUnityPlugin>();
  23. /// <summary>
  24. /// The GameObject that all plugins are attached to as components.
  25. /// </summary>
  26. public static GameObject ManagerObject { get; protected set; } = new GameObject("BepInEx_Manager");
  27. private static bool _loaded = false;
  28. /// <summary>
  29. /// The entry point for BepInEx, called on the very first LoadScene() from UnityEngine.
  30. /// </summary>
  31. public static void Initialize()
  32. {
  33. if (_loaded)
  34. return;
  35. if (!Directory.Exists(Utility.PluginsDirectory))
  36. Directory.CreateDirectory(Utility.PluginsDirectory);
  37. Preloader.AllocateConsole();
  38. try
  39. {
  40. UnityLogWriter unityLogWriter = new UnityLogWriter();
  41. if (Preloader.PreloaderLog != null)
  42. unityLogWriter.WriteToLog($"{Preloader.PreloaderLog}\r\n");
  43. Logger.SetLogger(unityLogWriter);
  44. string consoleTile = $"BepInEx {Assembly.GetExecutingAssembly().GetName().Version} - {Application.productName}";
  45. ConsoleWindow.Title = consoleTile;
  46. Logger.Log(LogLevel.Message, "Chainloader started");
  47. UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
  48. string currentProcess = Process.GetCurrentProcess().ProcessName.ToLower();
  49. var pluginTypes = TypeLoader.LoadTypes<BaseUnityPlugin>(Utility.PluginsDirectory)
  50. .Where(plugin =>
  51. {
  52. //Perform a filter for currently running process
  53. var filters = MetadataHelper.GetAttributes<BepInProcess>(plugin);
  54. if (!filters.Any())
  55. return true;
  56. return filters.Any(x => x.ProcessName.ToLower().Replace(".exe", "") == currentProcess);
  57. })
  58. .ToList();
  59. Logger.Log(LogLevel.Info, $"{pluginTypes.Count} plugins selected");
  60. Dictionary<Type, IEnumerable<Type>> dependencyDict = new Dictionary<Type, IEnumerable<Type>>();
  61. foreach (Type t in pluginTypes)
  62. {
  63. try
  64. {
  65. IEnumerable<Type> dependencies = MetadataHelper.GetDependencies(t, pluginTypes);
  66. dependencyDict[t] = dependencies;
  67. }
  68. catch (MissingDependencyException)
  69. {
  70. var metadata = MetadataHelper.GetMetadata(t);
  71. Logger.Log(LogLevel.Info, $"Cannot load [{metadata.Name}] due to missing dependencies.");
  72. }
  73. }
  74. pluginTypes = Utility.TopologicalSort(dependencyDict.Keys, x => dependencyDict[x]).ToList();
  75. foreach (Type t in pluginTypes)
  76. {
  77. try
  78. {
  79. var metadata = MetadataHelper.GetMetadata(t);
  80. var plugin = (BaseUnityPlugin) ManagerObject.AddComponent(t);
  81. Plugins.Add(plugin);
  82. Logger.Log(LogLevel.Info, $"Loaded [{metadata.Name} {metadata.Version}]");
  83. }
  84. catch (Exception ex)
  85. {
  86. Logger.Log(LogLevel.Info, $"Error loading [{t.Name}] : {ex.Message}");
  87. }
  88. }
  89. }
  90. catch (Exception ex)
  91. {
  92. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  93. Console.WriteLine("Error occurred starting the game");
  94. Console.WriteLine(ex.ToString());
  95. }
  96. _loaded = true;
  97. }
  98. }
  99. }