Fix changes after review

This commit is contained in:
FiniteReality
2016-11-25 20:22:06 +00:00
parent 254e874c99
commit 704b2b75f4
4 changed files with 64 additions and 70 deletions

View File

@@ -183,9 +183,7 @@ namespace Discord.Commands
if (attribute is SummaryAttribute) if (attribute is SummaryAttribute)
builder.Summary = (attribute as SummaryAttribute).Text; builder.Summary = (attribute as SummaryAttribute).Text;
else if (attribute is OverrideTypeReaderAttribute) else if (attribute is OverrideTypeReaderAttribute)
{
builder.TypeReader = GetTypeReader(service, paramType, (attribute as OverrideTypeReaderAttribute).TypeReader); builder.TypeReader = GetTypeReader(service, paramType, (attribute as OverrideTypeReaderAttribute).TypeReader);
}
else if (attribute is ParameterPreconditionAttribute) else if (attribute is ParameterPreconditionAttribute)
builder.AddPrecondition(attribute as ParameterPreconditionAttribute); builder.AddPrecondition(attribute as ParameterPreconditionAttribute);
else if (attribute is ParamArrayAttribute) else if (attribute is ParamArrayAttribute)
@@ -204,8 +202,7 @@ namespace Discord.Commands
if (builder.TypeReader == null) if (builder.TypeReader == null)
{ {
var readers = service.GetTypeReaders(paramType); var reader = service.GetDefaultTypeReader(paramType);
var reader = readers?.FirstOrDefault();
if (reader == null) if (reader == null)
{ {
@@ -229,20 +226,16 @@ namespace Discord.Commands
private static TypeReader GetTypeReader(CommandService service, Type paramType, Type typeReaderType) private static TypeReader GetTypeReader(CommandService service, Type paramType, Type typeReaderType)
{ {
var readers = service.GetTypeReaders(paramType); var readers = service.GetTypeReaders(paramType);
TypeReader reader = null;
if (readers != null) if (readers != null)
{ if (readers.TryGetValue(typeReaderType, out reader))
var reader = readers.FirstOrDefault(x => x.GetType() == typeReaderType);
if (reader != default(TypeReader))
{
return reader; return reader;
}
}
//could not find any registered type reader: try to create one //could not find any registered type reader: try to create one
var typeReader = ReflectionUtils.CreateObject<TypeReader>(typeReaderType.GetTypeInfo(), service, DependencyMap.Empty); reader = ReflectionUtils.CreateObject<TypeReader>(typeReaderType.GetTypeInfo(), service, DependencyMap.Empty);
service.AddTypeReader(paramType, typeReader); service.AddTypeReader(paramType, reader);
return typeReader; return reader;
} }
private static bool IsValidModuleDefinition(TypeInfo typeInfo) private static bool IsValidModuleDefinition(TypeInfo typeInfo)

View File

@@ -46,7 +46,7 @@ namespace Discord.Commands.Builders
if (readers == null) if (readers == null)
throw new InvalidOperationException($"{type} does not have a TypeReader registered for it"); throw new InvalidOperationException($"{type} does not have a TypeReader registered for it");
TypeReader = readers.FirstOrDefault(); TypeReader = readers.FirstOrDefault().Value;
if (type.GetTypeInfo().IsValueType) if (type.GetTypeInfo().IsValueType)
DefaultValue = Activator.CreateInstance(type); DefaultValue = Activator.CreateInstance(type);

View File

@@ -15,7 +15,8 @@ namespace Discord.Commands
{ {
private readonly SemaphoreSlim _moduleLock; private readonly SemaphoreSlim _moduleLock;
private readonly ConcurrentDictionary<Type, ModuleInfo> _typedModuleDefs; private readonly ConcurrentDictionary<Type, ModuleInfo> _typedModuleDefs;
private readonly ConcurrentDictionary<Type, ConcurrentBag<TypeReader>> _typeReaders; private readonly ConcurrentDictionary<Type, ConcurrentDictionary<Type, TypeReader>> _typeReaders;
private readonly ConcurrentDictionary<Type, TypeReader> _defaultTypeReaders;
private readonly ConcurrentBag<ModuleInfo> _moduleDefs; private readonly ConcurrentBag<ModuleInfo> _moduleDefs;
private readonly CommandMap _map; private readonly CommandMap _map;
@@ -24,7 +25,7 @@ namespace Discord.Commands
public IEnumerable<ModuleInfo> Modules => _moduleDefs.Select(x => x); public IEnumerable<ModuleInfo> Modules => _moduleDefs.Select(x => x);
public IEnumerable<CommandInfo> Commands => _moduleDefs.SelectMany(x => x.Commands); 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() : this(new CommandServiceConfig()) { }
public CommandService(CommandServiceConfig config) public CommandService(CommandServiceConfig config)
@@ -33,41 +34,43 @@ namespace Discord.Commands
_typedModuleDefs = new ConcurrentDictionary<Type, ModuleInfo>(); _typedModuleDefs = new ConcurrentDictionary<Type, ModuleInfo>();
_moduleDefs = new ConcurrentBag<ModuleInfo>(); _moduleDefs = new ConcurrentBag<ModuleInfo>();
_map = new CommandMap(); _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(bool)] = new SimpleTypeReader<bool>(),
[typeof(char)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<char>()}, [typeof(char)] = new SimpleTypeReader<char>(),
[typeof(string)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<string>()}, [typeof(string)] = new SimpleTypeReader<string>(),
[typeof(byte)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<byte>()}, [typeof(byte)] = new SimpleTypeReader<byte>(),
[typeof(sbyte)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<sbyte>()}, [typeof(sbyte)] = new SimpleTypeReader<sbyte>(),
[typeof(ushort)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<ushort>()}, [typeof(ushort)] = new SimpleTypeReader<ushort>(),
[typeof(short)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<short>()}, [typeof(short)] = new SimpleTypeReader<short>(),
[typeof(uint)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<uint>()}, [typeof(uint)] = new SimpleTypeReader<uint>(),
[typeof(int)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<int>()}, [typeof(int)] = new SimpleTypeReader<int>(),
[typeof(ulong)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<ulong>()}, [typeof(ulong)] = new SimpleTypeReader<ulong>(),
[typeof(long)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<long>()}, [typeof(long)] = new SimpleTypeReader<long>(),
[typeof(float)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<float>()}, [typeof(float)] = new SimpleTypeReader<float>(),
[typeof(double)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<double>()}, [typeof(double)] = new SimpleTypeReader<double>(),
[typeof(decimal)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<decimal>()}, [typeof(decimal)] = new SimpleTypeReader<decimal>(),
[typeof(DateTime)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<DateTime>()}, [typeof(DateTime)] = new SimpleTypeReader<DateTime>(),
[typeof(DateTimeOffset)] = new ConcurrentBag<TypeReader>{new SimpleTypeReader<DateTimeOffset>()}, [typeof(DateTimeOffset)] = new SimpleTypeReader<DateTimeOffset>(),
[typeof(IMessage)] = new ConcurrentBag<TypeReader>{new MessageTypeReader<IMessage>()}, [typeof(IMessage)] = new MessageTypeReader<IMessage>(),
[typeof(IUserMessage)] = new ConcurrentBag<TypeReader>{new MessageTypeReader<IUserMessage>()}, [typeof(IUserMessage)] = new MessageTypeReader<IUserMessage>(),
[typeof(IChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IChannel>()}, [typeof(IChannel)] = new ChannelTypeReader<IChannel>(),
[typeof(IDMChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IDMChannel>()}, [typeof(IDMChannel)] = new ChannelTypeReader<IDMChannel>(),
[typeof(IGroupChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IGroupChannel>()}, [typeof(IGroupChannel)] = new ChannelTypeReader<IGroupChannel>(),
[typeof(IGuildChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IGuildChannel>()}, [typeof(IGuildChannel)] = new ChannelTypeReader<IGuildChannel>(),
[typeof(IMessageChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IMessageChannel>()}, [typeof(IMessageChannel)] = new ChannelTypeReader<IMessageChannel>(),
[typeof(IPrivateChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IPrivateChannel>()}, [typeof(IPrivateChannel)] = new ChannelTypeReader<IPrivateChannel>(),
[typeof(ITextChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<ITextChannel>()}, [typeof(ITextChannel)] = new ChannelTypeReader<ITextChannel>(),
[typeof(IVoiceChannel)] = new ConcurrentBag<TypeReader>{new ChannelTypeReader<IVoiceChannel>()}, [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(IUser)] = new UserTypeReader<IUser>(),
[typeof(IGroupUser)] = new ConcurrentBag<TypeReader>{new UserTypeReader<IGroupUser>()}, [typeof(IGroupUser)] = new UserTypeReader<IGroupUser>(),
[typeof(IGuildUser)] = new ConcurrentBag<TypeReader>{new UserTypeReader<IGuildUser>()}, [typeof(IGuildUser)] = new UserTypeReader<IGuildUser>(),
}; };
_caseSensitive = config.CaseSensitiveCommands; _caseSensitive = config.CaseSensitiveCommands;
_defaultRunMode = config.DefaultRunMode; _defaultRunMode = config.DefaultRunMode;
@@ -197,21 +200,28 @@ namespace Discord.Commands
//Type Readers //Type Readers
public void AddTypeReader<T>(TypeReader reader) public void AddTypeReader<T>(TypeReader reader)
{ {
var readers = _typeReaders.GetOrAdd(typeof(T), x => new ConcurrentBag<TypeReader>()); var readers = _typeReaders.GetOrAdd(typeof(T), x => new ConcurrentDictionary<Type, TypeReader>());
readers.Add(reader); readers[reader.GetType()] = reader;
} }
public void AddTypeReader(Type type, TypeReader reader) public void AddTypeReader(Type type, TypeReader reader)
{ {
var readers = _typeReaders.GetOrAdd(type, x=> new ConcurrentBag<TypeReader>()); var readers = _typeReaders.GetOrAdd(type, x=> new ConcurrentDictionary<Type, TypeReader>());
readers.Add(reader); 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)) if (_typeReaders.TryGetValue(type, out definedTypeReaders))
return definedTypeReaders; return definedTypeReaders;
return null; return null;
} }
internal TypeReader GetDefaultTypeReader(Type type)
{
TypeReader reader;
if (_defaultTypeReaders.TryGetValue(type, out reader))
return reader;
return null;
}
//Execution //Execution
public SearchResult Search(CommandContext context, int argPos) => Search(context, context.Message.Content.Substring(argPos)); public SearchResult Search(CommandContext context, int argPos) => Search(context, context.Message.Content.Substring(argPos));

View File

@@ -135,26 +135,17 @@ namespace Discord.Commands
if (map == null) if (map == null)
map = DependencyMap.Empty; map = DependencyMap.Empty;
object[] args = null;
try try
{ {
args = GenerateArgs(argList, paramList); object[] args = GenerateArgs(argList, paramList);
}
catch (Exception ex)
{
return ExecuteResult.FromError(ex);
}
foreach (var parameter in Parameters) foreach (var parameter in Parameters)
{ {
var result = await parameter.CheckPreconditionsAsync(context, args, map).ConfigureAwait(false); var result = await parameter.CheckPreconditionsAsync(context, args, map).ConfigureAwait(false);
if (!result.IsSuccess) if (!result.IsSuccess)
return ExecuteResult.FromError(result); return ExecuteResult.FromError(result);
} }
try
{
switch (RunMode) switch (RunMode)
{ {
case RunMode.Sync: //Always sync case RunMode.Sync: //Always sync