Add callback method for when a module class has been added (#934)

commit 5b047bf02b4299f34172cac05dc7e4a84ecc108c
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Fri Feb 2 22:22:00 2018 +0100

    [feature/OnModuleAdded] Quickstart fixes (#946)

    * Quickstart: fix minor derp

    * Other overdue fixes

commit bd3e9eee943b9092cc45217b19ff95bae359f888
Author: Christopher F <computerizedtaco@gmail.com>
Date:   Sat Jan 27 16:51:18 2018 -0500

    Resort usings in ModuleBase

commit 8042767579b337fdae7fe48e0a6ea2f007aef440
Author: Christopher F <computerizedtaco@gmail.com>
Date:   Sat Jan 27 16:41:39 2018 -0500

    Clean up removed owned IServiceProvider

commit 30066cb102ffbd65906ead72a377811aa501abba
Author: Christopher F <computerizedtaco@gmail.com>
Date:   Sat Jan 27 16:37:22 2018 -0500

    Remove redundant try-catch around OnModuleBuilding invocation

    If this exception is going to be rethrown, there's no reason to include
    a try-catch.

commit 60c7c31d4476c498a97ae0536ec5792f08efb89b
Author: Christopher F <computerizedtaco@gmail.com>
Date:   Sat Jan 27 16:36:27 2018 -0500

    Include the ModuleBuilder in OnModuleBuilding

    This allows modules hooking into OnModuleBuilding method to mutate
    theirselves at runtime.

commit b6a9ff57860ff3bddbad7ca850fd331529cb8e6e
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Mon Jan 22 13:17:14 2018 +0100

    #DERP

commit f623d19c68c5642a44898a561f77ed82d53fd103
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Mon Jan 22 13:15:31 2018 +0100

    Resolution for #937 because it's literally 4 lines of code

commit 8272c9675b0d63b4100aaf57f5067d635b68f5e6
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Mon Jan 22 11:39:28 2018 +0100

    Re-adjust quickstart

commit e30b9071351b69baa30a93a4851516dca9ea43cf
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Mon Jan 22 11:35:08 2018 +0100

    Undo experimental changes, request IServiceProvider instance everywhere instead

commit ad7e0a46c8709e845dfacdc298a893e22dc11567
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Fri Jan 19 03:40:27 2018 +0100

    Fix quickstart leftover from previous draft

commit e3349ef3d400bb3ad8cb28dd4234d5316a80bcc4
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Fri Jan 19 03:33:46 2018 +0100

    Doc comment on items

commit 81bd9111faaf98a52679daae863ab04dce96e63e
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Fri Jan 19 03:16:44 2018 +0100

    Add comment about the ServiceProviderFactory in the quickstart

commit 72b5e6c8a149d8e989b46351965daa14f8ca318c
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Fri Jan 19 03:10:40 2018 +0100

    Remove superfluous comments, provide simpler alternative for setting the ServiceProvider.

commit 74b17b0e04e2c413397a2e1b66ff814615326205
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Tue Jan 16 18:06:28 2018 +0100

    Experimental change for feedback

commit 7b100e99bb119be190006d1cd8e403776930e401
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Mon Jan 15 23:34:06 2018 +0100

    * Make the service provider parameters required

    * Adjust quickstart guide to reflect changes

commit 7f1b792946ac6b950922b06178aa5cc37d9f4144
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Mon Jan 15 20:04:37 2018 +0100

    I..... missed one.

commit 031b289d80604666dde62619e521af303203d48d
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Mon Jan 15 20:02:20 2018 +0100

    Rename method to more intuitive 'OnModuleBuilding'

commit 9a166ef1d0baecd21e4e5b965e2ac364feddbe2e
Author: Joe4evr <jii.geugten@gmail.com>
Date:   Mon Jan 15 19:09:10 2018 +0100

    Add callback method for when a module class has been added to the CommandService.
This commit is contained in:
Christopher F
2018-02-18 19:15:52 -05:00
parent 178ea8de4d
commit bb8ebc13d2
8 changed files with 196 additions and 136 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
@@ -18,6 +19,7 @@ namespace Discord.Commands.Builders
public string Name { get; set; }
public string Summary { get; set; }
public string Remarks { get; set; }
public string Group { get; set; }
public IReadOnlyList<CommandBuilder> Commands => _commands;
public IReadOnlyList<ModuleBuilder> Modules => _submodules;
@@ -25,6 +27,8 @@ namespace Discord.Commands.Builders
public IReadOnlyList<Attribute> Attributes => _attributes;
public IReadOnlyList<string> Aliases => _aliases;
internal TypeInfo TypeInfo { get; set; }
//Automatic
internal ModuleBuilder(CommandService service, ModuleBuilder parent)
{
@@ -111,17 +115,23 @@ namespace Discord.Commands.Builders
return this;
}
private ModuleInfo BuildImpl(CommandService service, ModuleInfo parent = null)
private ModuleInfo BuildImpl(CommandService service, IServiceProvider services, ModuleInfo parent = null)
{
//Default name to first alias
if (Name == null)
Name = _aliases[0];
return new ModuleInfo(this, service, parent);
if (TypeInfo != null)
{
var moduleInstance = ReflectionUtils.CreateObject<IModuleBase>(TypeInfo, service, services);
moduleInstance.OnModuleBuilding(service, this);
}
return new ModuleInfo(this, service, services, parent);
}
public ModuleInfo Build(CommandService service) => BuildImpl(service);
public ModuleInfo Build(CommandService service, IServiceProvider services) => BuildImpl(service, services);
internal ModuleInfo Build(CommandService service, ModuleInfo parent) => BuildImpl(service, parent);
internal ModuleInfo Build(CommandService service, IServiceProvider services, ModuleInfo parent) => BuildImpl(service, services, parent);
}
}

View File

@@ -42,8 +42,8 @@ namespace Discord.Commands
}
public static Task<Dictionary<Type, ModuleInfo>> BuildAsync(CommandService service, params TypeInfo[] validTypes) => BuildAsync(validTypes, service);
public static async Task<Dictionary<Type, ModuleInfo>> BuildAsync(IEnumerable<TypeInfo> validTypes, CommandService service)
public static Task<Dictionary<Type, ModuleInfo>> BuildAsync(CommandService service, IServiceProvider services, params TypeInfo[] validTypes) => BuildAsync(validTypes, service, services);
public static async Task<Dictionary<Type, ModuleInfo>> BuildAsync(IEnumerable<TypeInfo> validTypes, CommandService service, IServiceProvider services)
{
/*if (!validTypes.Any())
throw new InvalidOperationException("Could not find any valid modules from the given selection");*/
@@ -63,11 +63,11 @@ namespace Discord.Commands
var module = new ModuleBuilder(service, null);
BuildModule(module, typeInfo, service);
BuildSubTypes(module, typeInfo.DeclaredNestedTypes, builtTypes, service);
BuildModule(module, typeInfo, service, services);
BuildSubTypes(module, typeInfo.DeclaredNestedTypes, builtTypes, service, services);
builtTypes.Add(typeInfo);
result[typeInfo.AsType()] = module.Build(service);
result[typeInfo.AsType()] = module.Build(service, services);
}
await service._cmdLogger.DebugAsync($"Successfully built {builtTypes.Count} modules.").ConfigureAwait(false);
@@ -75,7 +75,7 @@ namespace Discord.Commands
return result;
}
private static void BuildSubTypes(ModuleBuilder builder, IEnumerable<TypeInfo> subTypes, List<TypeInfo> builtTypes, CommandService service)
private static void BuildSubTypes(ModuleBuilder builder, IEnumerable<TypeInfo> subTypes, List<TypeInfo> builtTypes, CommandService service, IServiceProvider services)
{
foreach (var typeInfo in subTypes)
{
@@ -87,17 +87,18 @@ namespace Discord.Commands
builder.AddModule((module) =>
{
BuildModule(module, typeInfo, service);
BuildSubTypes(module, typeInfo.DeclaredNestedTypes, builtTypes, service);
BuildModule(module, typeInfo, service, services);
BuildSubTypes(module, typeInfo.DeclaredNestedTypes, builtTypes, service, services);
});
builtTypes.Add(typeInfo);
}
}
private static void BuildModule(ModuleBuilder builder, TypeInfo typeInfo, CommandService service)
private static void BuildModule(ModuleBuilder builder, TypeInfo typeInfo, CommandService service, IServiceProvider services)
{
var attributes = typeInfo.GetCustomAttributes();
builder.TypeInfo = typeInfo;
foreach (var attribute in attributes)
{
@@ -117,6 +118,7 @@ namespace Discord.Commands
break;
case GroupAttribute group:
builder.Name = builder.Name ?? group.Prefix;
builder.Group = group.Prefix;
builder.AddAliases(group.Prefix);
break;
case PreconditionAttribute precondition:
@@ -140,12 +142,12 @@ namespace Discord.Commands
{
builder.AddCommand((command) =>
{
BuildCommand(command, typeInfo, method, service);
BuildCommand(command, typeInfo, method, service, services);
});
}
}
private static void BuildCommand(CommandBuilder builder, TypeInfo typeInfo, MethodInfo method, CommandService service)
private static void BuildCommand(CommandBuilder builder, TypeInfo typeInfo, MethodInfo method, CommandService service, IServiceProvider serviceprovider)
{
var attributes = method.GetCustomAttributes();
@@ -191,7 +193,7 @@ namespace Discord.Commands
{
builder.AddParameter((parameter) =>
{
BuildParameter(parameter, paramInfo, pos++, count, service);
BuildParameter(parameter, paramInfo, pos++, count, service, serviceprovider);
});
}
@@ -227,7 +229,7 @@ namespace Discord.Commands
builder.Callback = ExecuteCallback;
}
private static void BuildParameter(ParameterBuilder builder, System.Reflection.ParameterInfo paramInfo, int position, int count, CommandService service)
private static void BuildParameter(ParameterBuilder builder, System.Reflection.ParameterInfo paramInfo, int position, int count, CommandService service, IServiceProvider services)
{
var attributes = paramInfo.GetCustomAttributes();
var paramType = paramInfo.ParameterType;
@@ -245,7 +247,7 @@ namespace Discord.Commands
builder.Summary = summary.Text;
break;
case OverrideTypeReaderAttribute typeReader:
builder.TypeReader = GetTypeReader(service, paramType, typeReader.TypeReader);
builder.TypeReader = GetTypeReader(service, paramType, typeReader.TypeReader, services);
break;
case ParamArrayAttribute _:
builder.IsMultiple = true;
@@ -285,7 +287,7 @@ namespace Discord.Commands
}
}
private static TypeReader GetTypeReader(CommandService service, Type paramType, Type typeReaderType)
private static TypeReader GetTypeReader(CommandService service, Type paramType, Type typeReaderType, IServiceProvider services)
{
var readers = service.GetTypeReaders(paramType);
TypeReader reader = null;
@@ -296,7 +298,7 @@ namespace Discord.Commands
}
//We dont have a cached type reader, create one
reader = ReflectionUtils.CreateObject<TypeReader>(typeReaderType.GetTypeInfo(), service, EmptyServiceProvider.Instance);
reader = ReflectionUtils.CreateObject<TypeReader>(typeReaderType.GetTypeInfo(), service, services);
service.AddTypeReader(paramType, reader);
return reader;