ffa5b5754b
Fixed the following compilation errors: -Moderators collection not initialized -Outdated method signatures for ban and kick in ModuleBase -Update author name in manifests -Fixed incorrect method signature in AutoScriptResponder Minor improvements: -Updated external dependencies -Remove unused variables in ConfDefinition of RegexModerator -Improve parallel execution of matches? -Send exception message on logging failure to reporting channel -Slightly change ModuleLoader logging output -Add Discord.Net unhandled exception output to logging -Updated link to Github -Changed GuildState exception handling message for conciseness Fixes: -SQL index creation in LoggingService -SQL view creation in UserCacheService -Add casts from ulong to long in SQL inserts -External modules no longer loaded twice -Non-Info messages from Discord.Net will now be reported -User data had not been recorded at proper times -Some modules had been returning null instead of Task with null -Guild state exception handling should not have handled config errors
72 lines
2.6 KiB
C#
72 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace Kerobot
|
|
{
|
|
static class ModuleLoader
|
|
{
|
|
private const string LogName = nameof(ModuleLoader);
|
|
|
|
/// <summary>
|
|
/// Given the instance configuration, loads all appropriate types from file specified in it.
|
|
/// </summary>
|
|
internal static ReadOnlyCollection<ModuleBase> Load(InstanceConfig conf, Kerobot k)
|
|
{
|
|
var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar;
|
|
var modules = new List<ModuleBase>();
|
|
|
|
foreach (var file in conf.EnabledAssemblies)
|
|
{
|
|
Assembly a = null;
|
|
try
|
|
{
|
|
a = Assembly.LoadFile(path + file);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("An error occurred when attempting to load a module assembly.");
|
|
Console.WriteLine($"File: {file}");
|
|
Console.WriteLine(ex.ToString());
|
|
Environment.Exit(2);
|
|
}
|
|
|
|
IEnumerable<ModuleBase> amods = null;
|
|
try
|
|
{
|
|
amods = LoadModulesFromAssembly(a, k);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("An error occurred when attempting to create a module instance.");
|
|
Console.WriteLine(ex.ToString());
|
|
Environment.Exit(2);
|
|
}
|
|
modules.AddRange(amods);
|
|
}
|
|
return modules.AsReadOnly();
|
|
}
|
|
|
|
static IEnumerable<ModuleBase> LoadModulesFromAssembly(Assembly asm, Kerobot k)
|
|
{
|
|
var eligibleTypes = from type in asm.GetTypes()
|
|
where !type.IsAssignableFrom(typeof(ModuleBase))
|
|
where type.GetCustomAttribute<KerobotModuleAttribute>() != null
|
|
select type;
|
|
k.InstanceLogAsync(false, LogName, $"Scanning {asm.GetName().Name}");
|
|
|
|
var newmods = new List<ModuleBase>();
|
|
foreach (var t in eligibleTypes)
|
|
{
|
|
var mod = Activator.CreateInstance(t, k);
|
|
k.InstanceLogAsync(false, LogName,
|
|
$"---> Loading module {t.FullName}");
|
|
newmods.Add((ModuleBase)mod);
|
|
}
|
|
return newmods;
|
|
}
|
|
}
|
|
}
|