PluginLoader.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace BepInEx
  8. {
  9. public static class PluginLoader
  10. {
  11. public static ICollection<T> LoadPlugins<T>(string directory)
  12. {
  13. List<T> plugins = new List<T>();
  14. Type pluginType = typeof(T);
  15. foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll"))
  16. {
  17. try
  18. {
  19. AssemblyName an = AssemblyName.GetAssemblyName(dll);
  20. Assembly assembly = Assembly.Load(an);
  21. foreach (Type type in assembly.GetTypes())
  22. {
  23. if (type.IsInterface || type.IsAbstract)
  24. {
  25. continue;
  26. }
  27. else
  28. {
  29. if (type.GetInterface(pluginType.FullName) != null)
  30. {
  31. plugins.Add((T)Activator.CreateInstance(type));
  32. }
  33. }
  34. }
  35. }
  36. catch (BadImageFormatException ex)
  37. {
  38. }
  39. }
  40. return plugins;
  41. }
  42. }
  43. }