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

Worknet plugins/extensions #7

Merged
merged 11 commits into from
Jul 11, 2023
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ Console.WriteLine(JsonConvert.SerializeObject(payload));

### Extending NEO Worknet

Plugins/modules can be created to extend the functionality of Neo Worknet. [Neo Modules](https://github.com/neo-project/neo-modules/tree/master) contains a set of plugins that can be used with Neo. Many of these plugins can also be used with Neo Worknet. To use them, simply drop the dlls containing the plugins into the ~/.neo/plugins folder or /plugins folder in the worknet exe folder. Neo Worknet will automatically load and execute the plugins.
Neo Worknet's capabilities can be extended through the use of plugins or modules. The [Neo Modules](https://github.com/neo-project/neo-modules/tree/master) package offers a variety of plugins compatible with both Neo and Neo Worknet. To utilize these plugins, simply copy the DLL files containing the plugins into either the ~/.neo/plugins directory or the /plugins directory located within the worknet executable folder. Upon initiation, Neo Worknet will automatically load and activate these plugins.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also mention the need to override ConfigFile here in the readme?


A sample WorkNet plugin, WorkNetLogger, has been included in the /workenet-ext folder. The WorkNetLogger plugin serves as a simple example of a plugin. After building the plugin, a DLL file named "worknet-ext-filelogger.dll" will be generated. This DLL can be copied to the ~/.neo/plugins folder. Neo Worknet will automatically load and execute the plugin. Alternatively, you can create a /plugins directory in the same folder as the neo-worknet executable and copy the DLL to the /plugins folder.
As an illustrative example, we've included a sample Worknet plugin, WorkNetLogger, in the /workenet-ext directory. This plugin is designed to direct Worknet's logs to a specified file. It operates by reading the designated log file path from a custom config.json file, and then recording the logs into this file.

After building the plugin, copy both the "worknet-ext-filelogger.dll" and config.json files to the ~/.neo/plugins directory. Upon startup, Worknet will automatically identify, load and execute the plugin. If preferred, you may also create a /plugins directory within the same directory as the neo-worknet executable, and relocate the DLL to this /plugins directory.
4 changes: 3 additions & 1 deletion src/worknet-ext/logger/WorkNetLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public override void Dispose()
GC.SuppressFinalize(this);
}

// Overwrite Config file method to find the config.json from the same directory as the plugin dll directory
// If Worknet plugins require custom configuration. The plugins are required to override the ConfigFile property so GetConfiguration() method can locate the config.json file.
// Without this override, the default ConfigFile value is read from the PluginsDirectory property, which is relative to the assembly location.
// However, WorkNet plugins are loaded from either ~/.neo/plugins folder or /plugins folder in the worknet exe directory.
public override string ConfigFile => System.IO.Path.Combine(AppContext.BaseDirectory, "plugins", "config.json");

protected override void Configure()
Expand Down
12 changes: 3 additions & 9 deletions src/worknet/Node/PluginHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void LoadPlugins(string directory, TextWriter? writer = null)
try
{
assemblies.Add(Assembly.Load(File.ReadAllBytes(filename)));
Log(writer, $"Loaded plugin: {filename}");
writer?.WriteLine($"Loaded plugin: {filename}");
}
catch { }
}
Expand All @@ -38,15 +38,9 @@ public static void LoadPlugin(Assembly assembly, TextWriter? writer = null)
}
catch (Exception ex)
{
Log(writer, $"Failed to load plugin: {type.FullName}");
Log(writer, ex.Message);
writer?.WriteLine($"Failed to load plugin: {type.FullName}");
writer?.WriteLine(ex.Message);
}
}
}

private static void Log(TextWriter? writer, string message)
{
if (writer is null) return;
writer.WriteLine(message);
}
}