diff --git a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs index 4e8ef2664..4b4bbb903 100644 --- a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs +++ b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs @@ -193,7 +193,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); + builder.TypeReader = GetCustomTypeReader(service, paramType, (attribute as OverrideTypeReaderAttribute).TypeReader); else if (attribute is ParameterPreconditionAttribute) builder.AddPrecondition(attribute as ParameterPreconditionAttribute); else if (attribute is ParamArrayAttribute) @@ -213,22 +213,14 @@ namespace Discord.Commands builder.ParameterType = paramType; if (builder.TypeReader == null) - { - var readers = service.GetTypeReaders(paramType); - TypeReader reader = null; - - if (readers != null) - reader = readers.FirstOrDefault().Value; - else - reader = service.GetDefaultTypeReader(paramType); - - builder.TypeReader = reader; + { + builder.TypeReader = service.GetTypeReaders(paramType).FirstOrDefault().Value; } } - private static TypeReader GetTypeReader(CommandService service, Type paramType, Type typeReaderType) + private static TypeReader GetCustomTypeReader(CommandService service, Type paramType, Type typeReaderType) { - var readers = service.GetTypeReaders(paramType); + var readers = service.GetCustomTypeReaders(paramType); TypeReader reader = null; if (readers != null) { diff --git a/src/Discord.Net.Commands/Builders/ParameterBuilder.cs b/src/Discord.Net.Commands/Builders/ParameterBuilder.cs index 6761033b0..d4cd8f3fd 100644 --- a/src/Discord.Net.Commands/Builders/ParameterBuilder.cs +++ b/src/Discord.Net.Commands/Builders/ParameterBuilder.cs @@ -42,11 +42,7 @@ namespace Discord.Commands.Builders internal void SetType(Type type) { - var readers = Command.Module.Service.GetTypeReaders(type); - if (readers != null) - TypeReader = readers.FirstOrDefault().Value; - else - TypeReader = Command.Module.Service.GetDefaultTypeReader(type); + TypeReader = Command.Module.Service.GetTypeReaders(type).FirstOrDefault().Value; if (TypeReader == null) throw new InvalidOperationException($"{type} does not have a TypeReader registered for it"); diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index c0c20f80f..1914c8ce0 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -18,7 +18,7 @@ namespace Discord.Commands private readonly SemaphoreSlim _moduleLock; private readonly ConcurrentDictionary _typedModuleDefs; - private readonly ConcurrentDictionary> _typeReaders; + private readonly ConcurrentDictionary> _customTypeReaders; private readonly ConcurrentDictionary _defaultTypeReaders; private readonly ImmutableList> _entityTypeReaders; //TODO: Candidate for C#7 Tuple private readonly HashSet _moduleDefs; @@ -32,7 +32,7 @@ namespace Discord.Commands public IEnumerable Modules => _moduleDefs.Select(x => x); public IEnumerable Commands => _moduleDefs.SelectMany(x => x.Commands); - public ILookup TypeReaders => _typeReaders.SelectMany(x => x.Value.Select(y => new {y.Key, y.Value})).ToLookup(x => x.Key, x => x.Value); + public ILookup TypeReaders => _customTypeReaders.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) @@ -52,7 +52,7 @@ namespace Discord.Commands _typedModuleDefs = new ConcurrentDictionary(); _moduleDefs = new HashSet(); _map = new CommandMap(this); - _typeReaders = new ConcurrentDictionary>(); + _customTypeReaders = new ConcurrentDictionary>(); _defaultTypeReaders = new ConcurrentDictionary(); foreach (var type in PrimitiveParsers.SupportedTypes) @@ -188,21 +188,32 @@ namespace Discord.Commands //Type Readers public void AddTypeReader(TypeReader reader) { - var readers = _typeReaders.GetOrAdd(typeof(T), x => new ConcurrentDictionary()); + var readers = _customTypeReaders.GetOrAdd(typeof(T), x => new ConcurrentDictionary()); readers[reader.GetType()] = reader; } public void AddTypeReader(Type type, TypeReader reader) { - var readers = _typeReaders.GetOrAdd(type, x=> new ConcurrentDictionary()); + var readers = _customTypeReaders.GetOrAdd(type, x=> new ConcurrentDictionary()); readers[reader.GetType()] = reader; } - internal IDictionary GetTypeReaders(Type type) + internal IDictionary GetCustomTypeReaders(Type type) { ConcurrentDictionary definedTypeReaders; - if (_typeReaders.TryGetValue(type, out definedTypeReaders)) + if (_customTypeReaders.TryGetValue(type, out definedTypeReaders)) return definedTypeReaders; return null; } + public IDictionary GetTypeReaders(Type type) + { + var readers = GetCustomTypeReaders(type); + + if (readers == null) + readers = new ConcurrentDictionary(); + + readers.Add(type, GetDefaultTypeReader(type)); + + return readers; + } internal TypeReader GetDefaultTypeReader(Type type) { TypeReader reader;