BotSharp/BotSharp.Core/Modules/PlatformModuleAssembyLoader.cs

49 lines
1.6 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
// 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);
2018-10-12 02:42:56 +00:00
Formatter[] settings = new Formatter[]
{
new Formatter(platform, Color.Yellow),
new Formatter(platformDllPath, Color.Yellow)
};
Console.WriteLineFormatted("Loaded {0} platform emulator from {1} assembly.", Color.White, settings);
2018-10-05 01:57:10 +00:00
}
else
{
2018-10-12 02:42:56 +00:00
Console.WriteLine($"Can't load {module.Type} assembly from {platformDllPath}.");
2018-10-05 01:57:10 +00:00
}
2018-10-12 02:42:56 +00:00
2018-10-05 01:57:10 +00:00
Console.WriteLine();
}
}
}