2018-10-05 06:39:39 +00:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2018-10-14 14:15:27 +00:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2018-10-05 06:39:39 +00:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2018-10-05 15:17:52 +00:00
|
|
|
|
using System.Drawing;
|
2018-10-05 06:39:39 +00:00
|
|
|
|
using System.Linq;
|
2018-10-05 12:52:02 +00:00
|
|
|
|
using System.Reflection;
|
2018-10-05 06:39:39 +00:00
|
|
|
|
using System.Text;
|
2018-10-05 15:17:52 +00:00
|
|
|
|
using Console = Colorful.Console;
|
2018-10-05 06:39:39 +00:00
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Modules
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Startup class for configurable modules
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class ModulesStartup
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IEnumerable<IModule> _modules;
|
|
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create an instance of <see cref="ModulesStartup"/>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="configuration">
|
|
|
|
|
|
/// Application configuration containing modules configuration
|
|
|
|
|
|
/// </param>
|
|
|
|
|
|
public ModulesStartup(IConfiguration configuration)
|
|
|
|
|
|
{
|
|
|
|
|
|
this._configuration = configuration ??
|
|
|
|
|
|
throw new ArgumentNullException(nameof(configuration));
|
|
|
|
|
|
ModulesOptions options = configuration.Get<ModulesOptions>();
|
|
|
|
|
|
|
2018-10-22 03:47:51 +00:00
|
|
|
|
if (options.Modules.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"Platform emulator not found.", Color.Red);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-05 06:39:39 +00:00
|
|
|
|
this._modules = options.Modules
|
|
|
|
|
|
.Select(s =>
|
2018-10-05 15:17:52 +00:00
|
|
|
|
{
|
|
|
|
|
|
Type type = Type.GetType($"{s.Type}.ModuleInjector, {s.Type}");
|
2018-10-05 06:39:39 +00:00
|
|
|
|
|
|
|
|
|
|
if (type == null)
|
|
|
|
|
|
{
|
2018-10-05 15:17:52 +00:00
|
|
|
|
/* hard to debug in docker */
|
|
|
|
|
|
/* throw new TypeLoadException(
|
|
|
|
|
|
$"Cannot load type \"{s.Type}\"");*/
|
|
|
|
|
|
Console.WriteLine($"Cannot load type \"{s.Type}\"", Color.Red);
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
IModule module = (IModule)Activator.CreateInstance(type);
|
2018-10-22 03:47:51 +00:00
|
|
|
|
Console.WriteLine($"Loaded module \"{s.Type}\"", Color.Green);
|
2018-10-05 15:17:52 +00:00
|
|
|
|
return module;
|
2018-10-05 06:39:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Configurates the services.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="services">
|
|
|
|
|
|
/// Instance of <see cref="IServiceCollection"/>.
|
|
|
|
|
|
/// </param>
|
|
|
|
|
|
/// <returns>
|
|
|
|
|
|
/// Instance of <see cref="IServiceProvider"/>.
|
|
|
|
|
|
/// </returns>
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (IModule module in this._modules)
|
|
|
|
|
|
{
|
2018-10-05 15:17:52 +00:00
|
|
|
|
if (module == null) continue;
|
2018-10-05 06:39:39 +00:00
|
|
|
|
module.ConfigureServices(services, this._configuration);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Configures module services.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="app">
|
|
|
|
|
|
/// Instance of <see cref="IApplicationBuilder"/>.
|
|
|
|
|
|
/// </param>
|
2018-10-14 14:15:27 +00:00
|
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
2018-10-05 06:39:39 +00:00
|
|
|
|
{
|
|
|
|
|
|
foreach (IModule module in this._modules)
|
|
|
|
|
|
{
|
2018-10-22 03:47:51 +00:00
|
|
|
|
if (module == null) continue;
|
2018-10-14 14:15:27 +00:00
|
|
|
|
module.Configure(app, env);
|
2018-10-05 06:39:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|