2022-03-29 05:03:01 +00:00
|
|
|
|
using System.Collections.ObjectModel;
|
2018-06-05 00:15:18 +00:00
|
|
|
|
using System.Reflection;
|
2022-05-26 02:27:53 +00:00
|
|
|
|
using System.Text;
|
2018-06-05 00:15:18 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
namespace RegexBot;
|
|
|
|
|
static class ModuleLoader {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Given the instance configuration, loads all appropriate types from file specified in it.
|
|
|
|
|
/// </summary>
|
2022-12-05 00:30:50 +00:00
|
|
|
|
internal static ReadOnlyCollection<RegexbotModule> Load(Configuration conf, RegexbotClient rb) {
|
2022-03-29 05:03:01 +00:00
|
|
|
|
var path = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location) + Path.DirectorySeparatorChar;
|
|
|
|
|
var modules = new List<RegexbotModule>();
|
2018-06-06 20:54:51 +00:00
|
|
|
|
|
2022-07-21 03:34:29 +00:00
|
|
|
|
// Load self, then others if defined
|
|
|
|
|
modules.AddRange(LoadModulesFromAssembly(Assembly.GetExecutingAssembly(), rb));
|
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
foreach (var file in conf.Assemblies) {
|
|
|
|
|
Assembly? a = null;
|
|
|
|
|
try {
|
2022-08-09 03:36:39 +00:00
|
|
|
|
a = Assembly.LoadFrom(path + file);
|
2022-03-29 05:03:01 +00:00
|
|
|
|
} 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);
|
2018-06-05 00:15:18 +00:00
|
|
|
|
}
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
|
|
|
|
IEnumerable<RegexbotModule>? amods = null;
|
|
|
|
|
try {
|
2022-07-21 03:34:29 +00:00
|
|
|
|
amods = LoadModulesFromAssembly(a, rb);
|
2022-03-29 05:03:01 +00:00
|
|
|
|
} 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);
|
2018-06-05 00:15:18 +00:00
|
|
|
|
}
|
2022-03-29 05:03:01 +00:00
|
|
|
|
return modules.AsReadOnly();
|
|
|
|
|
}
|
2018-06-05 00:15:18 +00:00
|
|
|
|
|
2024-06-09 04:07:35 +00:00
|
|
|
|
static List<RegexbotModule> LoadModulesFromAssembly(Assembly asm, RegexbotClient rb) {
|
2022-03-29 05:03:01 +00:00
|
|
|
|
var eligibleTypes = from type in asm.GetTypes()
|
|
|
|
|
where !type.IsAssignableFrom(typeof(RegexbotModule))
|
|
|
|
|
where type.GetCustomAttribute<RegexbotModuleAttribute>() != null
|
|
|
|
|
select type;
|
2018-06-05 00:15:18 +00:00
|
|
|
|
|
2022-07-21 03:34:29 +00:00
|
|
|
|
var newreport = new StringBuilder($"---> Modules in {asm.GetName().Name}:");
|
2022-03-29 05:03:01 +00:00
|
|
|
|
var newmods = new List<RegexbotModule>();
|
|
|
|
|
foreach (var t in eligibleTypes) {
|
2022-07-21 01:55:08 +00:00
|
|
|
|
var mod = Activator.CreateInstance(t, rb)!;
|
2022-05-26 02:27:53 +00:00
|
|
|
|
newreport.Append($" {t.Name}");
|
2022-03-29 05:03:01 +00:00
|
|
|
|
newmods.Add((RegexbotModule)mod);
|
2018-06-05 00:15:18 +00:00
|
|
|
|
}
|
2022-07-28 02:23:49 +00:00
|
|
|
|
rb._svcLogging.DoLog(nameof(ModuleLoader), newreport.ToString());
|
2022-03-29 05:03:01 +00:00
|
|
|
|
return newmods;
|
2018-06-05 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|