Chainloader.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using BepInEx.Common;
  2. using ChaCustom;
  3. using Harmony;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using UnityEngine;
  11. namespace BepInEx
  12. {
  13. public class Chainloader
  14. {
  15. static bool loaded = false;
  16. public static List<BaseUnityPlugin> Plugins { get; protected set; } = new List<BaseUnityPlugin>();
  17. public static GameObject ManagerObject { get; protected set; } = new GameObject("BepInEx_Manager");
  18. public static void Initialize()
  19. {
  20. if (loaded)
  21. return;
  22. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  23. Console.WriteLine("Chainloader started");
  24. UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
  25. if (Directory.Exists(Utility.PluginsDirectory))
  26. {
  27. var pluginTypes = LoadTypes<BaseUnityPlugin>(Utility.PluginsDirectory);
  28. //UnityInjector.ConsoleUtil.ConsoleEncoding.ConsoleCodePage = 932;
  29. Console.WriteLine($"{pluginTypes.Count()} plugins found");
  30. foreach (Type t in pluginTypes)
  31. {
  32. var plugin = (BaseUnityPlugin)ManagerObject.AddComponent(t);
  33. Plugins.Add(plugin);
  34. Console.WriteLine($"Loaded [{plugin.Name}]");
  35. }
  36. }
  37. else
  38. {
  39. Console.WriteLine("Plugins directory not found, skipping");
  40. }
  41. loaded = true;
  42. }
  43. public static List<Type> LoadTypes<T>(string directory)
  44. {
  45. List<Type> types = new List<Type>();
  46. Type pluginType = typeof(T);
  47. foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll"))
  48. {
  49. try
  50. {
  51. AssemblyName an = AssemblyName.GetAssemblyName(dll);
  52. Assembly assembly = Assembly.Load(an);
  53. foreach (Type type in assembly.GetTypes())
  54. {
  55. if (type.IsInterface || type.IsAbstract)
  56. {
  57. continue;
  58. }
  59. else
  60. {
  61. if (type.BaseType == pluginType)
  62. types.Add(type);
  63. }
  64. }
  65. }
  66. catch (BadImageFormatException)
  67. {
  68. }
  69. }
  70. return types;
  71. }
  72. }
  73. }