BotSharp/BotSharp.Core/Modules/PlatformModuleAssembyLoader.cs

53 lines
1.8 KiB
C#
Raw Normal View History

2018-10-05 15:17:52 +00:00
using BotSharp.Core.Modules;
using Colorful;
2018-10-05 01:57:10 +00:00
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
2018-10-05 12:52:02 +00:00
using System.Runtime.Loader;
2018-10-05 01:57:10 +00:00
using System.Text;
using Console = Colorful.Console;
namespace Microsoft.Extensions.DependencyInjection
{
2018-10-05 15:17:52 +00:00
public static class PlatformModuleAssembyLoader
2018-10-05 01:57:10 +00:00
{
2018-10-05 15:17:52 +00:00
public static void LoadAssemblies(IConfiguration configuration, Action<Assembly> action)
2018-10-05 01:57:10 +00:00
{
2018-10-05 15:17:52 +00:00
ModulesOptions options = configuration.Get<ModulesOptions>();
2018-10-05 01:57:10 +00:00
// load platform emulator dynamically
Console.WriteLine();
2018-10-14 14:15:27 +00:00
options.Modules.ForEach(module => {
2018-10-21 14:03:12 +00:00
if (String.IsNullOrEmpty(module.Path))
{
module.Path = AppContext.BaseDirectory;
}
var dllPath = Path.Combine(module.Path, $"{module.Type}.dll");
2018-10-14 14:15:27 +00:00
if (File.Exists(dllPath))
{
Assembly library = AssemblyLoadContext.Default.LoadFromAssemblyPath(dllPath);
action(library);
2018-10-12 02:42:56 +00:00
2018-10-14 14:15:27 +00:00
Formatter[] settings = new Formatter[]
{
new Formatter(module.Name, Color.Yellow),
new Formatter(module.Type, Color.Yellow),
new Formatter(dllPath, Color.Yellow)
};
Console.WriteLineFormatted("Loaded {0} module, type: {1}, path: {2}", Color.White, settings);
2018-10-21 14:03:12 +00:00
Console.WriteLine();
2018-10-14 14:15:27 +00:00
}
else
2018-10-12 02:42:56 +00:00
{
2018-10-21 14:03:12 +00:00
Console.WriteFormatted($"Can't load {module.Type} assembly from {dllPath}.", Color.Red);
Console.WriteLine();
2018-10-14 14:15:27 +00:00
}
});
2018-10-05 01:57:10 +00:00
}
}
}