-
Notifications
You must be signed in to change notification settings - Fork 1k
[Add] Plugin Loading
#4225
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
Merged
NGDAdmin
merged 17 commits into
neo-project:dev
from
cschuchardt88:dev/add/plugin-security
Oct 23, 2025
Merged
[Add] Plugin Loading
#4225
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
82152e1
[`Add`] Fix Plugin Security & Dependency Loading
cschuchardt88 3d0b5d3
Update src/Neo/Plugins/Plugin.cs
cschuchardt88 3af43e2
Merge branch 'dev' into dev/add/plugin-security
cschuchardt88 157b0e0
Update src/Directory.Build.props
shargon f867ab1
Update src/Neo/Plugins/Plugin.cs
shargon 25d2f6f
Merge branch 'dev' into dev/add/plugin-security
shargon b9a738e
Merge branch 'dev' into dev/add/plugin-security
cschuchardt88 b55efc0
Remove isolation from Plugins
cschuchardt88 88fb6d5
Merge branch 'dev/add/plugin-security' of github.com:cschuchardt88/ne…
cschuchardt88 82e716c
Merge branch 'dev' into dev/add/plugin-security
cschuchardt88 bbd64b6
Update src/Neo/Plugins/PluginAssemblyLoadContext.cs
cschuchardt88 a67a842
Merge branch 'dev' into dev/add/plugin-security
ajara87 9524746
Merge branch 'dev' into dev/add/plugin-security
NGDAdmin 043fd0c
Apply suggestions from code review
shargon 6246506
Removed unloading plugins on error
cschuchardt88 4419da0
dotnet format
cschuchardt88 e31a197
Merge branch 'dev' into dev/add/plugin-security
NGDAdmin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,113 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // PluginAssemblyLoadContext.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.IO; | ||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
| using System.Runtime.Loader; | ||
|
|
||
| namespace Neo.Plugins | ||
| { | ||
| internal class PluginAssemblyLoadContext : AssemblyLoadContext | ||
cschuchardt88 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
cschuchardt88 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| private readonly string _pluginName; | ||
|
|
||
| public PluginAssemblyLoadContext(AssemblyName pluginAssemblyName) : base(isCollectible: true) | ||
| { | ||
| _pluginName = pluginAssemblyName.Name!; | ||
| } | ||
|
|
||
| [return: MaybeNull] | ||
| protected override Assembly Load(AssemblyName assemblyName) | ||
| { | ||
| // Attempt to load the assembly from the specified plugin path | ||
| var assemblyFile = Path.Combine(Plugin.PluginsDirectory, _pluginName, $"{assemblyName.Name}.dll"); | ||
|
|
||
| if (File.Exists(assemblyFile)) | ||
| { | ||
| return LoadFromAssemblyPath(assemblyFile); | ||
| } | ||
|
|
||
| // Load plugin dependencies | ||
| assemblyFile = Path.Combine(Plugin.PluginsDirectory, $"{assemblyName.Name}", $"{assemblyName.Name}.dll"); | ||
| if (File.Exists(assemblyFile)) | ||
| { | ||
| return LoadFromAssemblyPath(assemblyFile); | ||
| } | ||
|
|
||
| // If not found in the plugin path, defer to the default load context | ||
| // This allows shared dependencies (like .NET runtime assemblies) to be resolved | ||
| return null; | ||
| } | ||
|
|
||
| protected override nint LoadUnmanagedDll(string unmanagedDllName) | ||
| { | ||
| var unmanagedDllFilename = GetUnmanagedDllFilename(Path.GetFileNameWithoutExtension(unmanagedDllName)); | ||
|
|
||
| // Checks "Plugins\<Plugin Name>" directory | ||
| var unmanagedDllFile = Path.Combine(Plugin.PluginsDirectory, _pluginName, unmanagedDllFilename); | ||
| if (File.Exists(unmanagedDllFile)) | ||
| { | ||
| return LoadUnmanagedDllFromPath(unmanagedDllFile); | ||
| } | ||
|
|
||
| // Checks "Plugins\<Plugin Name>\runtimes" directory | ||
| unmanagedDllFile = Path.Combine( | ||
| Plugin.PluginsDirectory, | ||
| _pluginName, | ||
| "runtimes", | ||
| RuntimeInformation.RuntimeIdentifier, | ||
| "native", | ||
| unmanagedDllFilename); | ||
| if (File.Exists(unmanagedDllFile)) | ||
| { | ||
| return LoadUnmanagedDllFromPath(unmanagedDllFile); | ||
| } | ||
|
|
||
| // Checks "runtimes" directory | ||
| unmanagedDllFile = Path.Combine( | ||
| AppContext.BaseDirectory, | ||
| "runtimes", | ||
| RuntimeInformation.RuntimeIdentifier, | ||
| "native", | ||
| unmanagedDllFilename); | ||
| if (File.Exists(unmanagedDllFile)) | ||
| { | ||
| return LoadUnmanagedDllFromPath(unmanagedDllFile); | ||
| } | ||
|
|
||
| // Checks "base" directory | ||
| unmanagedDllFile = Path.Combine( | ||
| AppContext.BaseDirectory, | ||
| unmanagedDllFilename); | ||
| if (File.Exists(unmanagedDllFile)) | ||
| { | ||
| return LoadUnmanagedDllFromPath(unmanagedDllFile); | ||
| } | ||
|
|
||
| return nint.Zero; | ||
| } | ||
|
|
||
| private static string GetUnmanagedDllFilename(string unmanagedDllName) | ||
| { | ||
| var filename = $"{unmanagedDllName}.dll"; | ||
|
|
||
| if (OperatingSystem.IsLinux()) | ||
| filename = $"{unmanagedDllName}.so"; | ||
| else if (OperatingSystem.IsMacOS()) | ||
| filename = $"{unmanagedDllName}.dylib"; | ||
|
|
||
| return filename; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.