TypeLoader.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. using BepInEx.Logging;
  8. using Mono.Cecil;
  9. namespace BepInEx.Bootstrap
  10. {
  11. /// <summary>
  12. /// Provides methods for loading specified types from an assembly.
  13. /// </summary>
  14. public static class TypeLoader
  15. {
  16. private static DefaultAssemblyResolver resolver;
  17. private static ReaderParameters readerParameters;
  18. public static event AssemblyResolveEventHandler AssemblyResolve;
  19. static TypeLoader()
  20. {
  21. resolver = new DefaultAssemblyResolver();
  22. readerParameters = new ReaderParameters { AssemblyResolver = resolver };
  23. resolver.ResolveFailure += (sender, reference) =>
  24. {
  25. var name = new AssemblyName(reference.FullName);
  26. if (Utility.TryResolveDllAssembly(name, Paths.BepInExAssemblyDirectory, readerParameters, out AssemblyDefinition assembly) ||
  27. Utility.TryResolveDllAssembly(name, Paths.PluginPath, readerParameters, out assembly) ||
  28. Utility.TryResolveDllAssembly(name, Paths.ManagedPath, readerParameters, out assembly))
  29. return assembly;
  30. return AssemblyResolve?.Invoke(sender, reference);
  31. };
  32. }
  33. /// <summary>
  34. /// Loads a list of types from a directory containing assemblies, that derive from a base type.
  35. /// </summary>
  36. /// <typeparam name="T">The specific base type to search for.</typeparam>
  37. /// <param name="directory">The directory to search for assemblies.</param>
  38. /// <returns>Returns a list of found derivative types.</returns>
  39. public static Dictionary<AssemblyDefinition, List<T>> FindPluginTypes<T>(string directory, Func<TypeDefinition, T> typeSelector) where T : class
  40. {
  41. var result = new Dictionary<AssemblyDefinition, List<T>>();
  42. foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll", SearchOption.AllDirectories))
  43. {
  44. try
  45. {
  46. var ass = AssemblyDefinition.ReadAssembly(dll, readerParameters);
  47. var matches = ass.MainModule.Types.Select(typeSelector).Where(t => t != null).ToList();
  48. if (matches.Count == 0)
  49. {
  50. ass.Dispose();
  51. continue;
  52. }
  53. result[ass] = matches;
  54. }
  55. catch (Exception e)
  56. {
  57. Logger.LogError(e.ToString());
  58. }
  59. }
  60. return result;
  61. }
  62. public static string TypeLoadExceptionToString(ReflectionTypeLoadException ex)
  63. {
  64. StringBuilder sb = new StringBuilder();
  65. foreach (Exception exSub in ex.LoaderExceptions)
  66. {
  67. sb.AppendLine(exSub.Message);
  68. if (exSub is FileNotFoundException exFileNotFound)
  69. {
  70. if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
  71. {
  72. sb.AppendLine("Fusion Log:");
  73. sb.AppendLine(exFileNotFound.FusionLog);
  74. }
  75. }
  76. else if (exSub is FileLoadException exLoad)
  77. {
  78. if (!string.IsNullOrEmpty(exLoad.FusionLog))
  79. {
  80. sb.AppendLine("Fusion Log:");
  81. sb.AppendLine(exLoad.FusionLog);
  82. }
  83. }
  84. sb.AppendLine();
  85. }
  86. return sb.ToString();
  87. }
  88. }
  89. }