Chainloader.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 (bool.Parse(Config.GetEntry("console", "false")))
  32. {
  33. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  34. if (bool.Parse(Config.GetEntry("console-shiftjis", "false")))
  35. UnityInjector.ConsoleUtil.ConsoleEncoding.ConsoleCodePage = 932;
  36. }
  37. try
  38. {
  39. BepInLogger.Log($"Chainloader started");
  40. UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
  41. if (Directory.Exists(Utility.PluginsDirectory))
  42. {
  43. var pluginTypes = TypeLoader.LoadTypes<BaseUnityPlugin>(Utility.PluginsDirectory).ToList();
  44. pluginTypes = TopologicalSort(pluginTypes, x => TypeLoader.GetDependencies(x, pluginTypes)).ToList();
  45. BepInLogger.Log($"{pluginTypes.Count} plugins found");
  46. foreach (Type t in pluginTypes)
  47. {
  48. try
  49. {
  50. var metadata = TypeLoader.GetMetadata(t);
  51. var plugin = (BaseUnityPlugin)ManagerObject.AddComponent(t);
  52. Plugins.Add(plugin);
  53. BepInLogger.Log($"Loaded [{plugin.Name}]");
  54. }
  55. catch (Exception ex)
  56. {
  57. BepInLogger.Log($"Error loading [{t.Name}] : {ex.Message}");
  58. }
  59. }
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  65. Console.WriteLine("Error occurred starting the game");
  66. Console.WriteLine(ex.ToString());
  67. }
  68. loaded = true;
  69. }
  70. protected static IEnumerable<TNode> TopologicalSort<TNode>(
  71. IEnumerable<TNode> nodes,
  72. Func<TNode, IEnumerable<TNode>> dependencySelector)
  73. {
  74. List<TNode> sorted_list = new List<TNode>();
  75. HashSet<TNode> visited = new HashSet<TNode>();
  76. HashSet<TNode> sorted = new HashSet<TNode>();
  77. foreach (TNode input in nodes)
  78. Visit(input);
  79. return sorted_list;
  80. void Visit(TNode node)
  81. {
  82. if (visited.Contains(node))
  83. {
  84. if (!sorted.Contains(node))
  85. throw new Exception("Cyclic Dependency");
  86. }
  87. else
  88. {
  89. visited.Add(node);
  90. foreach (var dep in dependencySelector(node))
  91. Visit(dep);
  92. sorted.Add(node);
  93. sorted_list.Add(node);
  94. }
  95. }
  96. }
  97. }
  98. }