-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add the feature to load action loader from external assembly
- Loading branch information
Showing
13 changed files
with
224 additions
and
33 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
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
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
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
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
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
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,31 @@ | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
|
||
namespace Libplanet.Node.Actions; | ||
|
||
internal sealed class PluginLoadContext(string pluginPath) : AssemblyLoadContext | ||
{ | ||
private readonly AssemblyDependencyResolver _resolver = new(pluginPath); | ||
|
||
protected override Assembly? Load(AssemblyName assemblyName) | ||
{ | ||
var assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName); | ||
if (assemblyPath is not null) | ||
{ | ||
return LoadFromAssemblyPath(assemblyPath); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) | ||
{ | ||
var libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName); | ||
if (libraryPath is not null) | ||
{ | ||
return LoadUnmanagedDllFromPath(libraryPath); | ||
} | ||
|
||
return IntPtr.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,50 @@ | ||
using System.Reflection; | ||
using Libplanet.Action; | ||
using Libplanet.Action.Loader; | ||
|
||
namespace Libplanet.Node.Actions; | ||
|
||
internal static class PluginLoader | ||
{ | ||
public static IActionLoader LoadActionLoader(string modulePath, string typeName) | ||
{ | ||
var assembly = LoadAssembly(modulePath); | ||
return Create<IActionLoader>(assembly, typeName); | ||
} | ||
|
||
public static IPolicyActionsRegistry LoadPolicyActionRegistry( | ||
string relativePath, string typeName) | ||
{ | ||
var assembly = LoadAssembly(relativePath); | ||
return Create<IPolicyActionsRegistry>(assembly, typeName); | ||
} | ||
|
||
private static T Create<T>(Assembly assembly, string typeName) | ||
where T : class | ||
{ | ||
if (assembly.GetType(typeName) is not { } type) | ||
{ | ||
throw new ApplicationException( | ||
$"Can't find {typeName} in {assembly} from {assembly.Location}"); | ||
} | ||
|
||
if (Activator.CreateInstance(type) is not T obj) | ||
{ | ||
throw new ApplicationException( | ||
$"Can't create an instance of {type} in {assembly} from {assembly.Location}"); | ||
} | ||
|
||
return obj; | ||
} | ||
|
||
private static Assembly LoadAssembly(string modulePath) | ||
{ | ||
var loadContext = new PluginLoadContext(modulePath); | ||
if (Path.GetFileNameWithoutExtension(modulePath) is { } filename) | ||
{ | ||
return loadContext.LoadFromAssemblyName(new AssemblyName(filename)); | ||
} | ||
|
||
throw new ApplicationException($"Can't load plugin from {modulePath}"); | ||
} | ||
} |
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
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,41 @@ | ||
using Libplanet.Action; | ||
using Libplanet.Action.Loader; | ||
using Libplanet.Node.Actions; | ||
|
||
namespace Libplanet.Node.Options; | ||
|
||
[Options(Position)] | ||
public sealed class ActionOptions : OptionsBase<ActionOptions> | ||
{ | ||
public const string Position = "Action"; | ||
|
||
public string ModulePath { get; set; } = string.Empty; | ||
|
||
public string ActionLoaderType { get; set; } = string.Empty; | ||
|
||
public string PolicyActionRegistryType { get; set; } = string.Empty; | ||
|
||
internal IActionLoader GetActionLoader() | ||
{ | ||
if (ActionLoaderType != string.Empty) | ||
{ | ||
var modulePath = ModulePath; | ||
var actionLoaderType = ActionLoaderType; | ||
return PluginLoader.LoadActionLoader(modulePath, actionLoaderType); | ||
} | ||
|
||
return new AggregateTypedActionLoader(); | ||
} | ||
|
||
internal IPolicyActionsRegistry GetPolicyActionsRegistry() | ||
{ | ||
if (PolicyActionRegistryType != string.Empty) | ||
{ | ||
var modulePath = ModulePath; | ||
var policyActionRegistryType = PolicyActionRegistryType; | ||
return PluginLoader.LoadPolicyActionRegistry(modulePath, policyActionRegistryType); | ||
} | ||
|
||
return new PolicyActionsRegistry(); | ||
} | ||
} |
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
Oops, something went wrong.