TypeLoader.cs 2.8 KB

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