Chainloader.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 IEnumerable<Type> Plugins { get; protected set; }
  17. public static GameObject ManagerObject { get; protected set; }
  18. public static void Initialize()
  19. {
  20. if (loaded)
  21. return;
  22. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  23. Console.WriteLine("Chainloader started");
  24. if (Directory.Exists(Utility.PluginsDirectory))
  25. {
  26. Plugins = LoadTypes<BaseUnityPlugin>(Utility.PluginsDirectory);
  27. //UnityInjector.ConsoleUtil.ConsoleEncoding.ConsoleCodePage = 932;
  28. Console.WriteLine($"{Plugins.Count()} plugins loaded");
  29. }
  30. else
  31. {
  32. Plugins = new List<Type>();
  33. Console.WriteLine("Plugins directory not found, skipping");
  34. }
  35. ManagerObject = BepInComponent.Create();
  36. loaded = true;
  37. }
  38. public static ICollection<Type> LoadTypes<T>(string directory)
  39. {
  40. List<Type> types = new List<Type>();
  41. Type pluginType = typeof(T);
  42. foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll"))
  43. {
  44. try
  45. {
  46. AssemblyName an = AssemblyName.GetAssemblyName(dll);
  47. Assembly assembly = Assembly.Load(an);
  48. foreach (Type type in assembly.GetTypes())
  49. {
  50. if (type.IsInterface || type.IsAbstract)
  51. {
  52. continue;
  53. }
  54. else
  55. {
  56. if (type.BaseType == pluginType)
  57. types.Add(type);
  58. }
  59. }
  60. }
  61. catch (BadImageFormatException)
  62. {
  63. }
  64. }
  65. return types;
  66. }
  67. }
  68. }