Implement non-working type sharing between plugins

This commit is contained in:
ChronosX88 2020-09-21 22:17:17 +04:00
parent ec5512ff6b
commit d14a058d41
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
3 changed files with 28 additions and 6 deletions

View File

@ -1,3 +1,5 @@
using System;
namespace Zirconium.Core.Plugins.Interfaces namespace Zirconium.Core.Plugins.Interfaces
{ {
public interface IPluginAPI public interface IPluginAPI
@ -6,5 +8,6 @@ namespace Zirconium.Core.Plugins.Interfaces
void PreInitialize(IPluginManager pluginManager); void PreInitialize(IPluginManager pluginManager);
string GetPluginUniqueName(); string GetPluginUniqueName();
dynamic GetExportedAPI(); dynamic GetExportedAPI();
Type[] GetExportedTypes();
} }
} }

View File

@ -2,8 +2,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.Loader;
using System.Threading; using System.Threading;
using McMaster.NETCore.Plugins; using McMaster.NETCore.Plugins;
using Zirconium.Core.Logging;
using Zirconium.Core.Models; using Zirconium.Core.Models;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
using Zirconium.Utils; using Zirconium.Utils;
@ -54,7 +56,7 @@ namespace Zirconium.Core.Plugins
public IPluginAPI LoadPlugin(string pluginName) public IPluginAPI LoadPlugin(string pluginName)
{ {
PluginLoader loader; PluginLoader loader;
var pluginDll = Path.Combine(_currentPluginFolderPath, pluginName + ".dll"); var pluginDll = Path.Combine(_currentPluginFolderPath, pluginName, pluginName + ".dll");
if (File.Exists(pluginDll)) if (File.Exists(pluginDll))
{ {
Logging.Log.Debug("Found plugin " + pluginName); Logging.Log.Debug("Found plugin " + pluginName);
@ -62,6 +64,7 @@ namespace Zirconium.Core.Plugins
loader = PluginLoader.CreateFromAssemblyFile( loader = PluginLoader.CreateFromAssemblyFile(
pluginDll, pluginDll,
sharedTypes: new[] { sharedTypes: new[] {
typeof(Log),
typeof(IPluginAPI), typeof(IPluginAPI),
typeof(IPluginHostAPI), typeof(IPluginHostAPI),
typeof(IPluginManager), typeof(IPluginManager),
@ -71,7 +74,8 @@ namespace Zirconium.Core.Plugins
typeof(ICoreEventHandler), typeof(ICoreEventHandler),
typeof(BaseMessage), typeof(BaseMessage),
typeof(CoreEvent) typeof(CoreEvent)
} },
config => config.PreferSharedTypes = true
); );
} }
else else
@ -79,15 +83,24 @@ namespace Zirconium.Core.Plugins
throw new Exception("specified plugin is not found"); throw new Exception("specified plugin is not found");
} }
var pluginTypes = loader.LoadDefaultAssembly().GetTypes();
IPluginAPI plugin = null; IPluginAPI plugin = null;
foreach (var pluginType in loader foreach (var pluginType in pluginTypes
.LoadDefaultAssembly()
.GetTypes()
.Where(t => typeof(IPluginAPI).IsAssignableFrom(t) && !t.IsAbstract)) .Where(t => typeof(IPluginAPI).IsAssignableFrom(t) && !t.IsAbstract))
{ {
// This assumes the implementation of IPlugin has a parameterless constructor // This assumes the implementation of IPlugin has a parameterless constructor
plugin = (IPluginAPI)Activator.CreateInstance(pluginType); plugin = (IPluginAPI)Activator.CreateInstance(pluginType);
Logging.Log.Debug($"Created plugin instance '{plugin.GetPluginUniqueName()}'."); Logging.Log.Debug($"Created plugin instance '{plugin.GetPluginUniqueName()}'.");
var exportedTypes = plugin.GetExportedTypes();
if (exportedTypes != null)
{
foreach (var type in exportedTypes)
{
var path = new Uri(type.Module.Assembly.CodeBase).LocalPath;
AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
}
}
plugin.PreInitialize(this); plugin.PreInitialize(this);
plugin.Initialize(_pluginHostAPI); plugin.Initialize(_pluginHostAPI);
} }

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json; using System;
using Newtonsoft.Json;
using Zirconium.Core.Logging; using Zirconium.Core.Logging;
using Zirconium.Core.Models; using Zirconium.Core.Models;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
@ -19,6 +20,11 @@ namespace HelloWorldPlugin
public void PreInitialize(IPluginManager pluginManager) { } public void PreInitialize(IPluginManager pluginManager) { }
public dynamic GetExportedAPI() { return null; } public dynamic GetExportedAPI() { return null; }
public Type[] GetExportedTypes()
{
return null;
}
} }
internal class C2SHandler : IC2SMessageHandler internal class C2SHandler : IC2SMessageHandler