using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using BepInEx.Logging;
namespace BepInEx.Bootstrap
{
///
/// Provides methods for loading specified types from an assembly.
///
public static class TypeLoader
{
///
/// Loads a list of types from a directory containing assemblies, that derive from a base type.
///
/// The specfiic base type to search for.
/// The directory to search for assemblies.
/// Returns a list of found derivative types.
public static IEnumerable LoadTypes(string directory)
{
List types = new List();
Type pluginType = typeof(T);
foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll"))
{
try
{
AssemblyName an = AssemblyName.GetAssemblyName(dll);
Assembly assembly = Assembly.Load(an);
foreach (Type type in assembly.GetTypes())
{
if (!type.IsInterface && !type.IsAbstract && type.BaseType == pluginType)
types.Add(type);
}
}
catch (BadImageFormatException) { } //unmanaged DLL
catch (ReflectionTypeLoadException)
{
Logger.Log(LogLevel.Error, $"Could not load \"{Path.GetFileName(dll)}\" as a plugin!");
}
}
return types;
}
}
}