Chainloader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using BepInEx.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using UnityEngine;
  8. namespace BepInEx
  9. {
  10. /// <summary>
  11. /// The manager and loader for all plugins, and the entry point for BepInEx.
  12. /// </summary>
  13. public class Chainloader
  14. {
  15. /// <summary>
  16. /// The loaded and initialized list of plugins.
  17. /// </summary>
  18. public static List<BaseUnityPlugin> Plugins { get; protected set; } = new List<BaseUnityPlugin>();
  19. /// <summary>
  20. /// The GameObject that all plugins are attached to as components.
  21. /// </summary>
  22. public static GameObject ManagerObject { get; protected set; } = new GameObject("BepInEx_Manager");
  23. static bool loaded = false;
  24. /// <summary>
  25. /// The entry point for BepInEx, called on the very first LoadScene() from UnityEngine.
  26. /// </summary>
  27. public static void Initialize()
  28. {
  29. if (loaded)
  30. return;
  31. if (!Directory.Exists(Common.Utility.PluginsDirectory))
  32. Directory.CreateDirectory(Utility.PluginsDirectory);
  33. if (bool.Parse(Config.GetEntry("console", "false")) || bool.Parse(Config.GetEntry("console-shiftjis", "false")))
  34. {
  35. try
  36. {
  37. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  38. if (bool.Parse(Config.GetEntry("console-shiftjis", "false")))
  39. UnityInjector.ConsoleUtil.ConsoleEncoding.ConsoleCodePage = 932;
  40. }
  41. catch
  42. {
  43. BepInLogger.Log("Failed to allocate console!", true);
  44. }
  45. }
  46. try
  47. {
  48. BepInLogger.Log($"BepInEx {Assembly.GetExecutingAssembly().GetName().Version}");
  49. BepInLogger.Log($"Chainloader started");
  50. UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
  51. var pluginTypes = TypeLoader.LoadTypes<BaseUnityPlugin>(Common.Utility.PluginsDirectory).ToList();
  52. BepInLogger.Log($"{pluginTypes.Count} plugins found");
  53. Dictionary<Type, IEnumerable<Type>> dependencyDict = new Dictionary<Type, IEnumerable<Type>>();
  54. foreach (Type t in pluginTypes)
  55. {
  56. try
  57. {
  58. IEnumerable<Type> dependencies = TypeLoader.GetDependencies(t, pluginTypes);
  59. dependencyDict[t] = dependencies;
  60. }
  61. catch (MissingDependencyException)
  62. {
  63. var metadata = TypeLoader.GetMetadata(t);
  64. BepInLogger.Log($"Cannot load [{metadata.Name}] due to missing dependencies.");
  65. }
  66. }
  67. pluginTypes = TopologicalSort(dependencyDict.Keys, x => dependencyDict[x]).ToList();
  68. foreach (Type t in pluginTypes)
  69. {
  70. try
  71. {
  72. var metadata = TypeLoader.GetMetadata(t);
  73. var plugin = (BaseUnityPlugin) ManagerObject.AddComponent(t);
  74. Plugins.Add(plugin);
  75. BepInLogger.Log($"Loaded [{metadata.Name} {metadata.Version}]");
  76. }
  77. catch (Exception ex)
  78. {
  79. BepInLogger.Log($"Error loading [{t.Name}] : {ex.Message}");
  80. }
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  86. Console.WriteLine("Error occurred starting the game");
  87. Console.WriteLine(ex.ToString());
  88. }
  89. loaded = true;
  90. }
  91. protected static IEnumerable<TNode> TopologicalSort<TNode>(
  92. IEnumerable<TNode> nodes,
  93. Func<TNode, IEnumerable<TNode>> dependencySelector)
  94. {
  95. List<TNode> sorted_list = new List<TNode>();
  96. HashSet<TNode> visited = new HashSet<TNode>();
  97. HashSet<TNode> sorted = new HashSet<TNode>();
  98. foreach (TNode input in nodes)
  99. Visit(input);
  100. return sorted_list;
  101. void Visit(TNode node)
  102. {
  103. if (visited.Contains(node))
  104. {
  105. if (!sorted.Contains(node))
  106. throw new Exception("Cyclic Dependency");
  107. }
  108. else
  109. {
  110. visited.Add(node);
  111. foreach (var dep in dependencySelector(node))
  112. Visit(dep);
  113. sorted.Add(node);
  114. sorted_list.Add(node);
  115. }
  116. }
  117. }
  118. }
  119. }