Browse Source

Add some debug logging into ModuleClassBuilder

Fixes #675 by logging a message when a module is not public and contains
commands
Also logs how many modules have been successfully built
pull/678/head
FiniteReality 8 years ago
parent
commit
a6e7c3f178
2 changed files with 30 additions and 11 deletions
  1. +26
    -8
      src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs
  2. +4
    -3
      src/Discord.Net.Commands/CommandService.cs

+ 26
- 8
src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs View File

@@ -12,21 +12,37 @@ namespace Discord.Commands
{ {
private static readonly TypeInfo _moduleTypeInfo = typeof(IModuleBase).GetTypeInfo(); private static readonly TypeInfo _moduleTypeInfo = typeof(IModuleBase).GetTypeInfo();


public static IEnumerable<TypeInfo> Search(Assembly assembly)
public static async Task<IReadOnlyList<TypeInfo>> Search(Assembly assembly, CommandService service)
{ {
foreach (var type in assembly.ExportedTypes)
bool IsLoadableModule(TypeInfo info)
{ {
var typeInfo = type.GetTypeInfo();
if (IsValidModuleDefinition(typeInfo) &&
!typeInfo.IsDefined(typeof(DontAutoLoadAttribute)))
return info.DeclaredMethods.Any(x => x.GetCustomAttribute<CommandAttribute>() != null) &&
info.GetCustomAttribute<DontAutoLoadAttribute>() == null;
}

List<TypeInfo> result = new List<TypeInfo>();

foreach (var typeInfo in assembly.DefinedTypes)
{
if (typeInfo.IsPublic)
{ {
yield return typeInfo;
if (IsValidModuleDefinition(typeInfo) &&
!typeInfo.IsDefined(typeof(DontAutoLoadAttribute)))
{
result.Add(typeInfo);
}
}
else if (IsLoadableModule(typeInfo))
{
await service._cmdLogger.WarningAsync($"Class {typeInfo.FullName} is not public and cannot be loaded. To suppress this message, mark the class with {nameof(DontAutoLoadAttribute)}.");
} }
} }

return result;
} }


public static Dictionary<Type, ModuleInfo> Build(CommandService service, params TypeInfo[] validTypes) => Build(validTypes, service);
public static Dictionary<Type, ModuleInfo> Build(IEnumerable<TypeInfo> validTypes, CommandService service)
public static Task<Dictionary<Type, ModuleInfo>> Build(CommandService service, params TypeInfo[] validTypes) => Build(validTypes, service);
public static async Task<Dictionary<Type, ModuleInfo>> Build(IEnumerable<TypeInfo> validTypes, CommandService service)
{ {
/*if (!validTypes.Any()) /*if (!validTypes.Any())
throw new InvalidOperationException("Could not find any valid modules from the given selection");*/ throw new InvalidOperationException("Could not find any valid modules from the given selection");*/
@@ -52,6 +68,8 @@ namespace Discord.Commands
result[typeInfo.AsType()] = module.Build(service); result[typeInfo.AsType()] = module.Build(service);
} }


await service._cmdLogger.DebugAsync($"Successfully loaded {builtTypes.Count} modules.").ConfigureAwait(false);

return result; return result;
} }




+ 4
- 3
src/Discord.Net.Commands/CommandService.cs View File

@@ -102,7 +102,8 @@ namespace Discord.Commands
if (_typedModuleDefs.ContainsKey(type)) if (_typedModuleDefs.ContainsKey(type))
throw new ArgumentException($"This module has already been added."); throw new ArgumentException($"This module has already been added.");


var module = ModuleClassBuilder.Build(this, typeInfo).FirstOrDefault();
var module = (await ModuleClassBuilder.Build(this, typeInfo).ConfigureAwait(false))
.FirstOrDefault();


if (module.Value == default(ModuleInfo)) if (module.Value == default(ModuleInfo))
throw new InvalidOperationException($"Could not build the module {type.FullName}, did you pass an invalid type?"); throw new InvalidOperationException($"Could not build the module {type.FullName}, did you pass an invalid type?");
@@ -121,8 +122,8 @@ namespace Discord.Commands
await _moduleLock.WaitAsync().ConfigureAwait(false); await _moduleLock.WaitAsync().ConfigureAwait(false);
try try
{ {
var types = ModuleClassBuilder.Search(assembly).ToArray();
var moduleDefs = ModuleClassBuilder.Build(types, this);
var types = await ModuleClassBuilder.Search(assembly, this).ConfigureAwait(false);
var moduleDefs = await ModuleClassBuilder.Build(types, this).ConfigureAwait(false);


foreach (var info in moduleDefs) foreach (var info in moduleDefs)
{ {


Loading…
Cancel
Save