Fix changes after review
This commit is contained in:
@@ -183,9 +183,7 @@ namespace Discord.Commands
|
||||
if (attribute is SummaryAttribute)
|
||||
builder.Summary = (attribute as SummaryAttribute).Text;
|
||||
else if (attribute is OverrideTypeReaderAttribute)
|
||||
{
|
||||
builder.TypeReader = GetTypeReader(service, paramType, (attribute as OverrideTypeReaderAttribute).TypeReader);
|
||||
}
|
||||
else if (attribute is ParameterPreconditionAttribute)
|
||||
builder.AddPrecondition(attribute as ParameterPreconditionAttribute);
|
||||
else if (attribute is ParamArrayAttribute)
|
||||
@@ -204,8 +202,7 @@ namespace Discord.Commands
|
||||
|
||||
if (builder.TypeReader == null)
|
||||
{
|
||||
var readers = service.GetTypeReaders(paramType);
|
||||
var reader = readers?.FirstOrDefault();
|
||||
var reader = service.GetDefaultTypeReader(paramType);
|
||||
|
||||
if (reader == null)
|
||||
{
|
||||
@@ -229,20 +226,16 @@ namespace Discord.Commands
|
||||
private static TypeReader GetTypeReader(CommandService service, Type paramType, Type typeReaderType)
|
||||
{
|
||||
var readers = service.GetTypeReaders(paramType);
|
||||
TypeReader reader = null;
|
||||
if (readers != null)
|
||||
{
|
||||
var reader = readers.FirstOrDefault(x => x.GetType() == typeReaderType);
|
||||
if (reader != default(TypeReader))
|
||||
{
|
||||
if (readers.TryGetValue(typeReaderType, out reader))
|
||||
return reader;
|
||||
}
|
||||
}
|
||||
|
||||
//could not find any registered type reader: try to create one
|
||||
var typeReader = ReflectionUtils.CreateObject<TypeReader>(typeReaderType.GetTypeInfo(), service, DependencyMap.Empty);
|
||||
service.AddTypeReader(paramType, typeReader);
|
||||
reader = ReflectionUtils.CreateObject<TypeReader>(typeReaderType.GetTypeInfo(), service, DependencyMap.Empty);
|
||||
service.AddTypeReader(paramType, reader);
|
||||
|
||||
return typeReader;
|
||||
return reader;
|
||||
}
|
||||
|
||||
private static bool IsValidModuleDefinition(TypeInfo typeInfo)
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace Discord.Commands.Builders
|
||||
if (readers == null)
|
||||
throw new InvalidOperationException($"{type} does not have a TypeReader registered for it");
|
||||
|
||||
TypeReader = readers.FirstOrDefault();
|
||||
TypeReader = readers.FirstOrDefault().Value;
|
||||
|
||||
if (type.GetTypeInfo().IsValueType)
|
||||
DefaultValue = Activator.CreateInstance(type);
|
||||
|
||||
@@ -14,8 +14,9 @@ namespace Discord.Commands
|
||||
public class CommandService
|
||||
{
|
||||
private readonly SemaphoreSlim _moduleLock;
|
||||
private readonly ConcurrentDictionary<Type, ModuleInfo> _typedModuleDefs;
|
||||
private readonly ConcurrentDictionary<Type, ConcurrentBag<TypeReader>> _typeReaders;
|
||||
private readonly ConcurrentDictionary<Type, ModuleInfo> _typedModuleDefs;
|
||||
private readonly ConcurrentDictionary<Type, ConcurrentDictionary<Type, TypeReader>> _typeReaders;
|
||||
private readonly ConcurrentDictionary<Type, TypeReader> _defaultTypeReaders;
|
||||
private readonly ConcurrentBag<ModuleInfo> _moduleDefs;
|
||||
private readonly CommandMap _map;
|
||||
|
||||
@@ -24,7 +25,7 @@ namespace Discord.Commands
|
||||
|
||||
public IEnumerable<ModuleInfo> Modules => _moduleDefs.Select(x => x);
|
||||
public IEnumerable<CommandInfo> Commands => _moduleDefs.SelectMany(x => x.Commands);
|
||||
public ILookup<Type, TypeReader> TypeReaders => _typeReaders.SelectMany(x => x.Value, (a, value) => new {a.Key, value}).ToLookup(x => x.Key, x => x.value);
|
||||
public ILookup<Type, TypeReader> TypeReaders => _typeReaders.SelectMany(x => x.Value.Select(y => new {y.Key, y.Value})).ToLookup(x => x.Key, x => x.Value);
|
||||
|
||||
public CommandService() : this(new CommandServiceConfig()) { }
|
||||
public CommandService(CommandServiceConfig config)
|
||||
@@ -33,41 +34,43 @@ namespace Discord.Commands
|
||||
_typedModuleDefs = new ConcurrentDictionary<Type, ModuleInfo>();
|
||||
_moduleDefs = new ConcurrentBag<ModuleInfo>();
|
||||
_map = new CommandMap();
|
||||
_typeReaders = new ConcurrentDictionary<Type, ConcurrentBag<TypeReader>>
|
||||
_typeReaders = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, TypeReader>>();
|
||||
|
||||
_defaultTypeReaders = new ConcurrentDictionary<Type, TypeReader>
|
||||
{
|
||||
[typeof(bool)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<bool>()},
|
||||
[typeof(char)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<char>()},
|
||||
[typeof(string)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<string>()},
|
||||
[typeof(byte)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<byte>()},
|
||||
[typeof(sbyte)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<sbyte>()},
|
||||
[typeof(ushort)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<ushort>()},
|
||||
[typeof(short)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<short>()},
|
||||
[typeof(uint)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<uint>()},
|
||||
[typeof(int)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<int>()},
|
||||
[typeof(ulong)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<ulong>()},
|
||||
[typeof(long)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<long>()},
|
||||
[typeof(float)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<float>()},
|
||||
[typeof(double)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<double>()},
|
||||
[typeof(decimal)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<decimal>()},
|
||||
[typeof(DateTime)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<DateTime>()},
|
||||
[typeof(DateTimeOffset)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<DateTimeOffset>()},
|
||||
[typeof(bool)] = new SimpleTypeReader<bool>(),
|
||||
[typeof(char)] = new SimpleTypeReader<char>(),
|
||||
[typeof(string)] = new SimpleTypeReader<string>(),
|
||||
[typeof(byte)] = new SimpleTypeReader<byte>(),
|
||||
[typeof(sbyte)] = new SimpleTypeReader<sbyte>(),
|
||||
[typeof(ushort)] = new SimpleTypeReader<ushort>(),
|
||||
[typeof(short)] = new SimpleTypeReader<short>(),
|
||||
[typeof(uint)] = new SimpleTypeReader<uint>(),
|
||||
[typeof(int)] = new SimpleTypeReader<int>(),
|
||||
[typeof(ulong)] = new SimpleTypeReader<ulong>(),
|
||||
[typeof(long)] = new SimpleTypeReader<long>(),
|
||||
[typeof(float)] = new SimpleTypeReader<float>(),
|
||||
[typeof(double)] = new SimpleTypeReader<double>(),
|
||||
[typeof(decimal)] = new SimpleTypeReader<decimal>(),
|
||||
[typeof(DateTime)] = new SimpleTypeReader<DateTime>(),
|
||||
[typeof(DateTimeOffset)] = new SimpleTypeReader<DateTimeOffset>(),
|
||||
|
||||
[typeof(IMessage)] = new ConcurrentBag<TypeReader>{new MessageTypeReader<IMessage>()},
|
||||
[typeof(IUserMessage)] = new ConcurrentBag<TypeReader>{new MessageTypeReader<IUserMessage>()},
|
||||
[typeof(IChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IChannel>()},
|
||||
[typeof(IDMChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IDMChannel>()},
|
||||
[typeof(IGroupChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IGroupChannel>()},
|
||||
[typeof(IGuildChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IGuildChannel>()},
|
||||
[typeof(IMessageChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IMessageChannel>()},
|
||||
[typeof(IPrivateChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IPrivateChannel>()},
|
||||
[typeof(ITextChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<ITextChannel>()},
|
||||
[typeof(IVoiceChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IVoiceChannel>()},
|
||||
[typeof(IMessage)] = new MessageTypeReader<IMessage>(),
|
||||
[typeof(IUserMessage)] = new MessageTypeReader<IUserMessage>(),
|
||||
[typeof(IChannel)] = new ChannelTypeReader<IChannel>(),
|
||||
[typeof(IDMChannel)] = new ChannelTypeReader<IDMChannel>(),
|
||||
[typeof(IGroupChannel)] = new ChannelTypeReader<IGroupChannel>(),
|
||||
[typeof(IGuildChannel)] = new ChannelTypeReader<IGuildChannel>(),
|
||||
[typeof(IMessageChannel)] = new ChannelTypeReader<IMessageChannel>(),
|
||||
[typeof(IPrivateChannel)] = new ChannelTypeReader<IPrivateChannel>(),
|
||||
[typeof(ITextChannel)] = new ChannelTypeReader<ITextChannel>(),
|
||||
[typeof(IVoiceChannel)] = new ChannelTypeReader<IVoiceChannel>(),
|
||||
|
||||
[typeof(IRole)] = new ConcurrentBag<TypeReader>{new RoleTypeReader<IRole>()},
|
||||
[typeof(IRole)] = new RoleTypeReader<IRole>(),
|
||||
|
||||
[typeof(IUser)] = new ConcurrentBag<TypeReader>{new UserTypeReader<IUser>()},
|
||||
[typeof(IGroupUser)] = new ConcurrentBag<TypeReader>{new UserTypeReader<IGroupUser>()},
|
||||
[typeof(IGuildUser)] = new ConcurrentBag<TypeReader>{new UserTypeReader<IGuildUser>()},
|
||||
[typeof(IUser)] = new UserTypeReader<IUser>(),
|
||||
[typeof(IGroupUser)] = new UserTypeReader<IGroupUser>(),
|
||||
[typeof(IGuildUser)] = new UserTypeReader<IGuildUser>(),
|
||||
};
|
||||
_caseSensitive = config.CaseSensitiveCommands;
|
||||
_defaultRunMode = config.DefaultRunMode;
|
||||
@@ -197,21 +200,28 @@ namespace Discord.Commands
|
||||
//Type Readers
|
||||
public void AddTypeReader<T>(TypeReader reader)
|
||||
{
|
||||
var readers = _typeReaders.GetOrAdd(typeof(T), x => new ConcurrentBag<TypeReader>());
|
||||
readers.Add(reader);
|
||||
var readers = _typeReaders.GetOrAdd(typeof(T), x => new ConcurrentDictionary<Type, TypeReader>());
|
||||
readers[reader.GetType()] = reader;
|
||||
}
|
||||
public void AddTypeReader(Type type, TypeReader reader)
|
||||
{
|
||||
var readers = _typeReaders.GetOrAdd(type, x=> new ConcurrentBag<TypeReader>());
|
||||
readers.Add(reader);
|
||||
var readers = _typeReaders.GetOrAdd(type, x=> new ConcurrentDictionary<Type, TypeReader>());
|
||||
readers[reader.GetType()] = reader;
|
||||
}
|
||||
internal IEnumerable<TypeReader> GetTypeReaders(Type type)
|
||||
internal IDictionary<Type, TypeReader> GetTypeReaders(Type type)
|
||||
{
|
||||
ConcurrentBag<TypeReader> definedTypeReaders;
|
||||
ConcurrentDictionary<Type, TypeReader> definedTypeReaders;
|
||||
if (_typeReaders.TryGetValue(type, out definedTypeReaders))
|
||||
return definedTypeReaders;
|
||||
return null;
|
||||
}
|
||||
internal TypeReader GetDefaultTypeReader(Type type)
|
||||
{
|
||||
TypeReader reader;
|
||||
if (_defaultTypeReaders.TryGetValue(type, out reader))
|
||||
return reader;
|
||||
return null;
|
||||
}
|
||||
|
||||
//Execution
|
||||
public SearchResult Search(CommandContext context, int argPos) => Search(context, context.Message.Content.Substring(argPos));
|
||||
|
||||
@@ -135,26 +135,17 @@ namespace Discord.Commands
|
||||
if (map == null)
|
||||
map = DependencyMap.Empty;
|
||||
|
||||
object[] args = null;
|
||||
|
||||
try
|
||||
{
|
||||
args = GenerateArgs(argList, paramList);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ExecuteResult.FromError(ex);
|
||||
}
|
||||
object[] args = GenerateArgs(argList, paramList);
|
||||
|
||||
foreach (var parameter in Parameters)
|
||||
{
|
||||
var result = await parameter.CheckPreconditionsAsync(context, args, map).ConfigureAwait(false);
|
||||
if (!result.IsSuccess)
|
||||
return ExecuteResult.FromError(result);
|
||||
}
|
||||
foreach (var parameter in Parameters)
|
||||
{
|
||||
var result = await parameter.CheckPreconditionsAsync(context, args, map).ConfigureAwait(false);
|
||||
if (!result.IsSuccess)
|
||||
return ExecuteResult.FromError(result);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
switch (RunMode)
|
||||
{
|
||||
case RunMode.Sync: //Always sync
|
||||
|
||||
Reference in New Issue
Block a user