Chainloader.cs 3.5 KB

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