-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PluginsProvider provides all plugins in specified directory
- Loading branch information
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Stéphane ANDRE. All Right Reserved. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
|
||
namespace MyNet.Utilities.Plugins | ||
{ | ||
internal class PluginLoadContext(string pluginPath) : AssemblyLoadContext | ||
{ | ||
private readonly AssemblyDependencyResolver _resolver = new(pluginPath); | ||
|
||
protected override Assembly? Load(AssemblyName assemblyName) | ||
{ | ||
var assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName); | ||
return assemblyPath != null ? LoadFromAssemblyPath(assemblyPath) : null; | ||
} | ||
|
||
protected override nint LoadUnmanagedDll(string unmanagedDllName) | ||
{ | ||
var libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName); | ||
return libraryPath != null ? LoadUnmanagedDllFromPath(libraryPath) : nint.Zero; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) Stéphane ANDRE. All Right Reserved. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace MyNet.Utilities.Plugins | ||
{ | ||
public static class PluginService | ||
{ | ||
public static IEnumerable<Type> GetTypes<T>(string pluginsDirectory) | ||
{ | ||
if (!Directory.Exists(pluginsDirectory)) return []; | ||
|
||
var result = new List<Type>(); | ||
|
||
foreach (var item in new DirectoryInfo(pluginsDirectory).GetDirectories()) | ||
{ | ||
var dllPath = Path.Combine(item.FullName, $"{item.Name}.dll"); | ||
|
||
if (File.Exists(dllPath)) | ||
{ | ||
var plugin = LoadAssemblyFromDll(dllPath); | ||
|
||
if (plugin is not null) | ||
result.AddRange(plugin.GetTypes().Where(x => x.IsAssignableTo(typeof(T)))); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static Type? GetType<T>(string pluginPath) | ||
{ | ||
if (string.IsNullOrEmpty(pluginPath)) return default; | ||
|
||
var plugin = LoadAssemblyFromDll(pluginPath); | ||
|
||
return plugin is null ? null : Array.Find(plugin.GetTypes(), x => x.IsAssignableTo(typeof(T))); | ||
} | ||
|
||
public static T? CreateInstance<T>(string pluginPath, params object[] constructorParameters) | ||
{ | ||
if (string.IsNullOrEmpty(pluginPath)) return default; | ||
|
||
var plugin = GetType<T>(pluginPath); | ||
|
||
return plugin is null ? default : (T?)Activator.CreateInstance(plugin, constructorParameters); | ||
} | ||
|
||
private static Assembly? LoadAssemblyFromDll(string dllPath, string? assemblyName = null) | ||
{ | ||
if (!File.Exists(dllPath)) return null; | ||
|
||
var finalAssemblyName = assemblyName ?? Path.GetFileNameWithoutExtension(dllPath); | ||
|
||
var loadContext = new PluginLoadContext(dllPath); | ||
return loadContext.LoadFromAssemblyName(new AssemblyName(finalAssemblyName)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Stéphane ANDRE. All Right Reserved. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using MyNet.Utilities.Caching; | ||
|
||
namespace MyNet.Utilities.Plugins | ||
{ | ||
public class PluginsProvider(string root) | ||
{ | ||
private readonly CacheStorage<Type, List<Type>> _cache = new(); | ||
|
||
public string Root { get; private set; } = root; | ||
|
||
public List<Type> GetTypes<T>() | ||
{ | ||
var types = _cache.Get(typeof(T)); | ||
|
||
if (types is null) | ||
{ | ||
types = PluginService.GetTypes<T>(Root).ToList(); | ||
_cache.Add(typeof(T), types); | ||
} | ||
|
||
return types; | ||
} | ||
|
||
public Type? Get<T>(string? assemblyName = null) | ||
{ | ||
var types = GetTypes<T>(); | ||
|
||
return !string.IsNullOrEmpty(assemblyName) ? types.Find(x => x.Assembly.GetName().Name == assemblyName) : types.FirstOrDefault(); | ||
} | ||
|
||
public T? Create<T>(string? assemblyName = null, params object[] constructorParameters) | ||
{ | ||
var type = Get<T>(assemblyName); | ||
|
||
return type is null ? default : (T?)Activator.CreateInstance(type, constructorParameters); | ||
} | ||
} | ||
} |