Chainloader.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. Plugins = LoadTypes<BaseUnityPlugin>(Utility.PluginsDirectory);
  25. //UnityInjector.ConsoleUtil.ConsoleEncoding.ConsoleCodePage = 932;
  26. Console.WriteLine($"{Plugins.Count()} plugins loaded");
  27. ManagerObject = BepInComponent.Create();
  28. loaded = true;
  29. }
  30. public static ICollection<Type> LoadTypes<T>(string directory)
  31. {
  32. List<Type> types = new List<Type>();
  33. Type pluginType = typeof(T);
  34. foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll"))
  35. {
  36. try
  37. {
  38. AssemblyName an = AssemblyName.GetAssemblyName(dll);
  39. Assembly assembly = Assembly.Load(an);
  40. foreach (Type type in assembly.GetTypes())
  41. {
  42. if (type.IsInterface || type.IsAbstract)
  43. {
  44. continue;
  45. }
  46. else
  47. {
  48. if (type.BaseType == pluginType)
  49. types.Add(type);
  50. }
  51. }
  52. }
  53. catch (BadImageFormatException)
  54. {
  55. }
  56. }
  57. return types;
  58. }
  59. }
  60. }