mirror of
https://github.com/cadmium-im/zirconium-sharp.git
synced 2024-11-22 18:32:22 +00:00
Merge branch 'master' into task/2
This commit is contained in:
commit
fe462614a3
@ -77,7 +77,7 @@ namespace Zirconium.Core.Logging
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LogType.Debug: {
|
case LogType.Debug: {
|
||||||
tagFormatter = new Formatter("DEBUG", Color.Lime);
|
tagFormatter = new Formatter("DEBUG", Color.LightGray);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
16
src/ZirconiumPlugins/MongoDB/MongoDB.csproj
Normal file
16
src/ZirconiumPlugins/MongoDB/MongoDB.csproj
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="../../Zirconium/Zirconium.csproj">
|
||||||
|
<ExcludeAssets>runtime</ExcludeAssets>
|
||||||
|
</ProjectReference>
|
||||||
|
<PackageReference Include="MongoDB.Bson" Version="2.10.4" />
|
||||||
|
<PackageReference Include="MongoDB.Driver" Version="2.10.4" />
|
||||||
|
<PackageReference Include="MongoDB.Driver.Core" Version="2.10.4" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
58
src/ZirconiumPlugins/MongoDB/Plugin.cs
Normal file
58
src/ZirconiumPlugins/MongoDB/Plugin.cs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Zirconium.Core.Plugins.Interfaces;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using Zirconium.Core.Logging;
|
||||||
|
|
||||||
|
namespace MongoDBPlugin
|
||||||
|
{
|
||||||
|
class Plugin : IPluginAPI
|
||||||
|
{
|
||||||
|
private IMongoDatabase _database;
|
||||||
|
|
||||||
|
public dynamic GetExportedAPI()
|
||||||
|
{
|
||||||
|
return _database;
|
||||||
|
}
|
||||||
|
|
||||||
|
public System.Type[] GetExportedTypes()
|
||||||
|
{
|
||||||
|
// var mongoCore = Assembly.Load("MongoDB.Driver.Core");
|
||||||
|
// var mongoBson = Assembly.Load("MongoDB.Bson");
|
||||||
|
return new System.Type[] {typeof(IMongoDatabase)};
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetPluginUniqueName()
|
||||||
|
{
|
||||||
|
return "MongoDB";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Initialize(IPluginHostAPI pluginHostAPI)
|
||||||
|
{
|
||||||
|
var settingsDynamic = pluginHostAPI.GetSettings(this);
|
||||||
|
var settings = (IImmutableDictionary<string, dynamic>) ((IDictionary<string, object>)settingsDynamic).ToImmutableDictionary();
|
||||||
|
|
||||||
|
var host = (string) settings.GetValueOrDefault("Host");
|
||||||
|
var port = (int) settings.GetValueOrDefault("Port");
|
||||||
|
var user = (string) settings.GetValueOrDefault("User");
|
||||||
|
var password = (string) settings.GetValueOrDefault("Password");
|
||||||
|
var database = (string) settings.GetValueOrDefault("Database");
|
||||||
|
|
||||||
|
MongoClient client;
|
||||||
|
if (user == null && password == null)
|
||||||
|
{
|
||||||
|
client = new MongoClient($"mongodb://{host}:{port}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
client = new MongoClient($"mongodb://{user}:{password}@{host}:{port}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var db = client.GetDatabase(database);
|
||||||
|
Log.Info("MongoDB is connected");
|
||||||
|
_database = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void PreInitialize(IPluginManager pluginManager) { }
|
||||||
|
}
|
||||||
|
}
|
57
src/ZirconiumPlugins/TestMongoDB/Plugin.cs
Normal file
57
src/ZirconiumPlugins/TestMongoDB/Plugin.cs
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
using Zirconium.Core.Plugins.Interfaces;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using Zirconium.Core.Logging;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace TestMongoDB
|
||||||
|
{
|
||||||
|
class Plugin : IPluginAPI
|
||||||
|
{
|
||||||
|
private IMongoDatabase _database;
|
||||||
|
|
||||||
|
public dynamic GetExportedAPI()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Type[] GetExportedTypes()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetPluginUniqueName()
|
||||||
|
{
|
||||||
|
return "TestMongoDB";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Initialize(IPluginHostAPI pluginHostAPI)
|
||||||
|
{
|
||||||
|
var tm = new TestModel();
|
||||||
|
tm.ABC = "qweqowie";
|
||||||
|
_database.GetCollection<TestModel>("test_model").InsertOne(tm);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void PreInitialize(IPluginManager pluginManager)
|
||||||
|
{
|
||||||
|
var db = pluginManager.Depends(this, "MongoDB");
|
||||||
|
var receivedType = db.GetType();
|
||||||
|
Log.Debug(db.GetType().FullName);
|
||||||
|
Log.Debug(db.GetType().AssemblyQualifiedName);
|
||||||
|
foreach (Type i in db.GetType().GetInterfaces()) {
|
||||||
|
Log.Debug("DB object interface: " + i.AssemblyQualifiedName);
|
||||||
|
}
|
||||||
|
Log.Debug("Type info in current assembly context:");
|
||||||
|
var mongoDBType = typeof(IMongoDatabase);
|
||||||
|
Log.Debug(mongoDBType.FullName);
|
||||||
|
Log.Debug(mongoDBType.AssemblyQualifiedName);
|
||||||
|
Log.Debug(new Uri(mongoDBType.Module.Assembly.CodeBase).LocalPath);
|
||||||
|
Log.Debug($"Casting compatibility: {mongoDBType.IsAssignableFrom(db.GetType())}");
|
||||||
|
_database = (IMongoDatabase) db;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestModel
|
||||||
|
{
|
||||||
|
public string ABC { get; set; }
|
||||||
|
}
|
||||||
|
}
|
15
src/ZirconiumPlugins/TestMongoDB/TestMongoDB.csproj
Normal file
15
src/ZirconiumPlugins/TestMongoDB/TestMongoDB.csproj
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="../../Zirconium/Zirconium.csproj">
|
||||||
|
<ExcludeAssets>runtime</ExcludeAssets>
|
||||||
|
</ProjectReference>
|
||||||
|
<ProjectReference Include="../MongoDB/MongoDB.csproj"/>
|
||||||
|
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
Loading…
Reference in New Issue
Block a user