BotSharp/BotSharp.Core/Modules/PlatformModuleAssembyLoader.cs

50 lines
1.7 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>();
var platform = configuration.GetValue<string>("platformModuleName");
var module = options.Modules.Find(x => x.Name == platform);
2018-10-05 01:57:10 +00:00
var engine = configuration.GetValue<string>($"{platform}:BotEngine");
Formatter[] settings = new Formatter[]
{
new Formatter(platform, Color.Yellow),
2018-10-05 15:17:52 +00:00
new Formatter(module.Name, Color.Yellow),
2018-10-05 01:57:10 +00:00
new Formatter(engine, Color.Yellow),
};
// load platform emulator dynamically
Console.WriteLine();
2018-10-05 15:17:52 +00:00
var platformDllPath = Path.Combine(options.ModuleBasePath, module.Path, $"{module.Type}.dll");
2018-10-05 01:57:10 +00:00
if (File.Exists(platformDllPath))
{
2018-10-05 12:52:02 +00:00
Assembly library = AssemblyLoadContext.Default.LoadFromAssemblyPath(platformDllPath);
2018-10-05 01:57:10 +00:00
action(library);
Console.WriteLineFormatted("Loaded {0} platform emulator from {1} assembly which is using {2} engine.", Color.White, settings);
}
else
{
2018-10-05 15:17:52 +00:00
Console.WriteLine($"Can't load {module.Type} assembly.");
2018-10-05 01:57:10 +00:00
}
Console.WriteLine();
}
}
}