Better error handling on module load
This commit is contained in:
parent
2e178a2f2d
commit
4ca38fa881
1 changed files with 26 additions and 18 deletions
|
@ -19,27 +19,35 @@ namespace Kerobot
|
||||||
var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar;
|
var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar;
|
||||||
var modules = new List<ModuleBase>();
|
var modules = new List<ModuleBase>();
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
foreach (var file in conf.EnabledAssemblies)
|
foreach (var file in conf.EnabledAssemblies)
|
||||||
{
|
{
|
||||||
var a = Assembly.LoadFile(path + file);
|
Assembly a = null;
|
||||||
modules.AddRange(LoadModulesFromAssembly(a, k));
|
try
|
||||||
}
|
{
|
||||||
return modules.AsReadOnly();
|
a = Assembly.LoadFile(path + file);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
// TODO better (not lazy) exception handling
|
Console.WriteLine("An error occurred when attempting to load a module assembly.");
|
||||||
// Possible exceptions:
|
Console.WriteLine($"File: {file}");
|
||||||
// - Errors loading assemblies
|
|
||||||
// - Errors finding module paths
|
|
||||||
// - Errors creating module instances
|
|
||||||
// - Unknown errors
|
|
||||||
Console.WriteLine("Module load failed.");
|
|
||||||
Console.WriteLine(ex.ToString());
|
Console.WriteLine(ex.ToString());
|
||||||
return null;
|
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(LoadModulesFromAssembly(a, k));
|
||||||
|
}
|
||||||
|
return modules.AsReadOnly();
|
||||||
}
|
}
|
||||||
|
|
||||||
static IEnumerable<ModuleBase> LoadModulesFromAssembly(Assembly asm, Kerobot k)
|
static IEnumerable<ModuleBase> LoadModulesFromAssembly(Assembly asm, Kerobot k)
|
||||||
|
@ -49,7 +57,7 @@ namespace Kerobot
|
||||||
where type.GetCustomAttribute<KerobotModuleAttribute>() != null
|
where type.GetCustomAttribute<KerobotModuleAttribute>() != null
|
||||||
select type;
|
select type;
|
||||||
k.InstanceLogAsync(false, LogName,
|
k.InstanceLogAsync(false, LogName,
|
||||||
$"{asm.FullName} has {eligibleTypes.Count()} usable types:");
|
$"{asm.GetName().Name} has {eligibleTypes.Count()} usable types");
|
||||||
|
|
||||||
var newmods = new List<ModuleBase>();
|
var newmods = new List<ModuleBase>();
|
||||||
foreach (var t in eligibleTypes)
|
foreach (var t in eligibleTypes)
|
||||||
|
|
Loading…
Reference in a new issue