Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate plugins in different folder #2696

Merged
merged 10 commits into from
May 8, 2022
56 changes: 29 additions & 27 deletions src/neo/Plugins/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@ public abstract class Plugin : IDisposable
internal static readonly List<IMemoryPoolTxObserverPlugin> TxObserverPlugins = new();

/// <summary>
/// The directory containing the plugin dll files. Files can be contained in any subdirectory.
/// The directory containing the plugin folders. Files can be contained in any subdirectory.
/// </summary>
public static readonly string PluginsDirectory = Combine(GetDirectoryName(Assembly.GetEntryAssembly().Location), "Plugins");

private static readonly FileSystemWatcher configWatcher;

/// <summary>
/// Indicates the root path of the plugin.
/// </summary>
public string RootPath => Combine(PluginsDirectory, GetType().Assembly.GetName().Name);

/// <summary>
/// Indicates the location of the plugin configuration file.
/// </summary>
public virtual string ConfigFile => Combine(PluginsDirectory, GetType().Assembly.GetName().Name, "config.json");
public virtual string ConfigFile => Combine(RootPath, "config.json");

/// <summary>
/// Indicates the name of the plugin.
Expand All @@ -60,7 +65,7 @@ public abstract class Plugin : IDisposable
/// <summary>
/// Indicates the location of the plugin dll file.
/// </summary>
public virtual string Path => Combine(PluginsDirectory, GetType().Assembly.ManifestModule.ScopeName);
public virtual string Path => Combine(RootPath, GetType().Assembly.ManifestModule.ScopeName);

/// <summary>
/// Indicates the version of the plugin.
Expand All @@ -69,18 +74,16 @@ public abstract class Plugin : IDisposable

static Plugin()
{
if (Directory.Exists(PluginsDirectory))
if (!Directory.Exists(PluginsDirectory)) return;
configWatcher = new FileSystemWatcher(PluginsDirectory)
{
configWatcher = new FileSystemWatcher(PluginsDirectory)
{
EnableRaisingEvents = true,
IncludeSubdirectories = true,
NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.Size,
};
configWatcher.Changed += ConfigWatcher_Changed;
configWatcher.Created += ConfigWatcher_Changed;
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
EnableRaisingEvents = true,
IncludeSubdirectories = true,
NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.Size,
};
configWatcher.Changed += ConfigWatcher_Changed;
configWatcher.Created += ConfigWatcher_Changed;
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}

/// <summary>
Expand Down Expand Up @@ -120,7 +123,7 @@ private static void ConfigWatcher_Changed(object sender, FileSystemEventArgs e)
break;
case ".dll":
if (e.ChangeType != WatcherChangeTypes.Created) return;
if (GetDirectoryName(e.FullPath) != PluginsDirectory) return;
if (GetDirectoryName(GetDirectoryName(e.FullPath)) != PluginsDirectory) return;
try
{
LoadPlugin(Assembly.Load(File.ReadAllBytes(e.FullPath)));
Expand All @@ -137,9 +140,8 @@ private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEven

AssemblyName an = new(args.Name);

Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == args.Name);
if (assembly is null)
assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == an.Name);
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == args.Name) ??
AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == an.Name);
if (assembly != null) return assembly;

string filename = an.Name + ".dll";
Expand Down Expand Up @@ -196,13 +198,16 @@ internal static void LoadPlugins()
{
if (!Directory.Exists(PluginsDirectory)) return;
List<Assembly> assemblies = new();
foreach (string filename in Directory.EnumerateFiles(PluginsDirectory, "*.dll", SearchOption.TopDirectoryOnly))
foreach (string rootPath in Directory.GetDirectories(PluginsDirectory))
{
try
foreach (var filename in Directory.EnumerateFiles(rootPath, "*.dll", SearchOption.TopDirectoryOnly))
{
assemblies.Add(Assembly.Load(File.ReadAllBytes(filename)));
try
{
assemblies.Add(Assembly.Load(File.ReadAllBytes(filename)));
}
catch { }
}
catch { }
}
foreach (Assembly assembly in assemblies)
{
Expand Down Expand Up @@ -235,7 +240,7 @@ protected virtual bool OnMessage(object message)
/// Called when a <see cref="NeoSystem"/> is loaded.
/// </summary>
/// <param name="system">The loaded <see cref="NeoSystem"/>.</param>
internal protected virtual void OnSystemLoaded(NeoSystem system)
protected internal virtual void OnSystemLoaded(NeoSystem system)
{
}

Expand All @@ -246,10 +251,7 @@ internal protected virtual void OnSystemLoaded(NeoSystem system)
/// <returns><see langword="true"/> if the <paramref name="message"/> is handled by a plugin; otherwise, <see langword="false"/>.</returns>
public static bool SendMessage(object message)
{
foreach (Plugin plugin in Plugins)
if (plugin.OnMessage(message))
return true;
return false;
return Plugins.Any(plugin => plugin.OnMessage(message));
}
}
}