2023-09-12 12:50:18 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace Core
|
|
|
|
|
{
|
2023-09-14 13:40:15 +00:00
|
|
|
|
internal static class PluginFinder
|
2023-09-12 12:50:18 +00:00
|
|
|
|
{
|
|
|
|
|
private static Type[]? pluginTypes = null;
|
|
|
|
|
|
2023-09-14 13:40:15 +00:00
|
|
|
|
internal static Type[] GetPluginTypes()
|
2023-09-12 12:50:18 +00:00
|
|
|
|
{
|
|
|
|
|
if (pluginTypes != null) return pluginTypes;
|
|
|
|
|
|
|
|
|
|
// Reflection can be costly. Do this only once.
|
|
|
|
|
FindAndLoadPluginAssemblies();
|
|
|
|
|
|
|
|
|
|
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
|
|
|
|
pluginTypes = assemblies.SelectMany(a => a.GetTypes().Where(t =>
|
|
|
|
|
typeof(IProjectPlugin).IsAssignableFrom(t) &&
|
|
|
|
|
!t.IsAbstract)
|
|
|
|
|
).ToArray();
|
|
|
|
|
|
|
|
|
|
return pluginTypes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void FindAndLoadPluginAssemblies()
|
|
|
|
|
{
|
|
|
|
|
var files = Directory.GetFiles(".");
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
var f = file.ToLowerInvariant();
|
|
|
|
|
if (f.Contains("plugin") && f.EndsWith("dll"))
|
|
|
|
|
{
|
|
|
|
|
var name = Path.GetFileNameWithoutExtension(file);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Assembly.Load(name);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Failed to load plugin from file '{name}'.", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|