RegexBot/ModuleLoader.cs
Noi 1149f2800d Reorganized project
Moved modules into the assembly itself to simplify development of
further features and reduce complexity in building this project.

Additionally, many small adjustments were made, including:
- Add documentation to most public methods that had it missing
- Minor style updates
- Updated readme to reflect near-completion of this rewrite
- Remove any last remaining references to old project name Kerobot
- Update dependencies
2022-07-20 18:55:08 -07:00

55 lines
2.3 KiB
C#

using System.Collections.ObjectModel;
using System.Reflection;
using System.Text;
namespace RegexBot;
static class ModuleLoader {
/// <summary>
/// Given the instance configuration, loads all appropriate types from file specified in it.
/// </summary>
internal static ReadOnlyCollection<RegexbotModule> Load(InstanceConfig conf, RegexbotClient k) {
var path = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location) + Path.DirectorySeparatorChar;
var modules = new List<RegexbotModule>();
foreach (var file in conf.Assemblies) {
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<RegexbotModule>? 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<RegexbotModule> LoadModulesFromAssembly(Assembly asm, RegexbotClient rb) {
var eligibleTypes = from type in asm.GetTypes()
where !type.IsAssignableFrom(typeof(RegexbotModule))
where type.GetCustomAttribute<RegexbotModuleAttribute>() != null
select type;
rb._svcLogging.DoLog(false, nameof(ModuleLoader), $"Scanning {asm.GetName().Name}");
var newreport = new StringBuilder("---> Found module(s):");
var newmods = new List<RegexbotModule>();
foreach (var t in eligibleTypes) {
var mod = Activator.CreateInstance(t, rb)!;
newreport.Append($" {t.Name}");
newmods.Add((RegexbotModule)mod);
}
rb._svcLogging.DoLog(false, nameof(ModuleLoader), newreport.ToString());
return newmods;
}
}