Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yushu2606 committed Mar 20, 2024
2 parents d9e0f8e + 214dd56 commit ef1125d
Show file tree
Hide file tree
Showing 9 changed files with 453 additions and 458 deletions.
811 changes: 405 additions & 406 deletions .gitignore

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Hosihikari.PluginManagement.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 25.0.1706.0
# Visual Studio Version 17
VisualStudioVersion = 17.10.34707.107
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hosihikari.PluginManagement", "src\Hosihikari.PluginManager.csproj", "{92BBCA93-08CB-4465-BA68-316A59FD8317}"
EndProject
Expand Down
31 changes: 15 additions & 16 deletions src/AssemblyPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.Loader;
using System.Reflection;

namespace Hosihikari.PluginManagement;

Expand All @@ -8,6 +7,8 @@ public sealed class AssemblyPlugin : Plugin
internal const string PluginDirectoryPath = "plugins";
internal static readonly List<AssemblyPlugin> Plugins = [];
private Assembly? _assembly;
private EntryPointAttributeBase? _attribute;
private PluginLoadContext? _context;

internal AssemblyPlugin(FileInfo file) : base(file)
{
Expand All @@ -22,10 +23,17 @@ protected internal override void Load()
return;
}

PluginLoadContext context = new(_fileInfo);
_assembly = context.LoadFromAssemblyPath(_fileInfo.FullName);
AssemblyName name = _assembly.GetName();
_context = new(_fileInfo);
_assembly = _context.LoadFromAssemblyPath(_fileInfo.FullName);
Plugins.Add(this);
_attribute = _assembly.GetCustomAttribute<EntryPointAttributeBase>();
if (_attribute is null)
{
Unload();
throw new EntryPointNotFoundException();
}

AssemblyName name = _assembly.GetName();
if (string.IsNullOrWhiteSpace(name.Name) || name.Version is null)
{
Unload();
Expand All @@ -43,14 +51,7 @@ protected internal override void Initialize()
throw new NullReferenceException();
}

EntryPointAttributeBase? attribute = _assembly!.GetCustomAttribute<EntryPointAttributeBase>();
if (attribute is null)
{
Unload();
throw new EntryPointNotFoundException();
}

IEntryPoint entry = attribute.CreateInstance();
IEntryPoint entry = _attribute!.CreateInstance();
entry.Initialize(this);
}

Expand All @@ -61,10 +62,8 @@ protected internal override void Unload()
throw new NullReferenceException();
}

AssemblyLoadContext context =
AssemblyLoadContext.GetLoadContext(_assembly!) ?? throw new NullReferenceException();
Unloading?.Invoke(this, EventArgs.Empty);
context.Unload();
_context!.Unload();
Plugins.Remove(this);
}
}
2 changes: 1 addition & 1 deletion src/EntryPointAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Hosihikari.PluginManagement;
namespace Hosihikari.PluginManagement;

public abstract class EntryPointAttributeBase : Attribute
{
Expand Down
2 changes: 1 addition & 1 deletion src/IEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Hosihikari.PluginManagement;
namespace Hosihikari.PluginManagement;

public interface IEntryPoint
{
Expand Down
2 changes: 1 addition & 1 deletion src/Main.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Immutable;
using System.Collections.Immutable;
using System.Runtime.InteropServices;

namespace Hosihikari.PluginManagement;
Expand Down
19 changes: 10 additions & 9 deletions src/Manager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Hosihikari.PluginManagement;
namespace Hosihikari.PluginManagement;

public static class Manager
{
Expand All @@ -14,6 +14,14 @@ public static void Load(Plugin plugin)
{
return;
}
catch (EntryPointNotFoundException)
{
return;
}
catch (FileLoadException)
{
return;
}

if (s_plugins.TryAdd(plugin.Name, plugin))
{
Expand All @@ -30,14 +38,7 @@ public static void Initialize(string name)
throw new NullReferenceException();
}

try
{
plugin.Initialize();
}
catch (EntryPointNotFoundException)
{
s_plugins.Remove(name);
}
plugin.Initialize();
}

public static void Unload(string name)
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Hosihikari.PluginManagement;
namespace Hosihikari.PluginManagement;

public abstract class Plugin(FileInfo fileInfo)
{
Expand Down
36 changes: 16 additions & 20 deletions src/PluginLoadContext.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
using System.Reflection;
using System.Reflection;
using System.Runtime.Loader;

namespace Hosihikari.PluginManagement;

internal class PluginLoadContext(FileSystemInfo fileInfo) : AssemblyLoadContext(fileInfo.Name, true)
internal class PluginLoadContext : AssemblyLoadContext
{
private static readonly Dictionary<string, Assembly> s_loadedAssembly = [];

private readonly AssemblyDependencyResolver _resolver = new(fileInfo.FullName);
private readonly AssemblyDependencyResolver _resolver;

protected override Assembly? Load(AssemblyName assemblyName)
public PluginLoadContext(FileSystemInfo fileInfo) : base(fileInfo.Name, true)
{
_resolver = new(fileInfo.FullName);
Unloading += _ =>
{
foreach (Assembly assembly in Assemblies)
{
s_loadedAssembly.Remove(assembly.GetName().FullName);
}
};
}

protected override Assembly? Load(AssemblyName assemblyName)
{
if (s_loadedAssembly.TryGetValue(assemblyName.FullName, out Assembly? assembly))
{
return assembly;
}

string? assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath is not null)
{
return LoadFromAssemblyPath(assemblyPath);
}

return null;
return assemblyPath is not null ? LoadFromAssemblyPath(assemblyPath) : null;
}

public new Assembly LoadFromAssemblyPath(string assemblyPath)
Expand All @@ -32,14 +38,4 @@ internal class PluginLoadContext(FileSystemInfo fileInfo) : AssemblyLoadContext(
s_loadedAssembly[assembly.GetName().FullName] = assembly;
return assembly;
}

public new void Unload()
{
foreach (Assembly assembly in Assemblies)
{
s_loadedAssembly.Remove(assembly.GetName().FullName);
}

base.Unload();
}
}

0 comments on commit ef1125d

Please sign in to comment.