Fix OverrideTypeReader
This commit also adds a TypeReaders property to CommandService, so it is possible to see all of the registered TypeReaders. This makes it possible for users to implement their own parsing instead of using the built-in parsing.
This commit is contained in:
@@ -183,7 +183,9 @@ namespace Discord.Commands
|
||||
if (attribute is SummaryAttribute)
|
||||
builder.Summary = (attribute as SummaryAttribute).Text;
|
||||
else if (attribute is OverrideTypeReaderAttribute)
|
||||
builder.TypeReader = service.GetTypeReader((attribute as OverrideTypeReaderAttribute).TypeReader);
|
||||
{
|
||||
builder.TypeReader = GetTypeReader(service, paramType, (attribute as OverrideTypeReaderAttribute).TypeReader);
|
||||
}
|
||||
else if (attribute is ParameterPreconditionAttribute)
|
||||
builder.AddPrecondition(attribute as ParameterPreconditionAttribute);
|
||||
else if (attribute is ParamArrayAttribute)
|
||||
@@ -200,23 +202,47 @@ namespace Discord.Commands
|
||||
}
|
||||
}
|
||||
|
||||
var reader = service.GetTypeReader(paramType);
|
||||
if (reader == null)
|
||||
if (builder.TypeReader == null)
|
||||
{
|
||||
var paramTypeInfo = paramType.GetTypeInfo();
|
||||
if (paramTypeInfo.IsEnum)
|
||||
var readers = service.GetTypeReaders(paramType);
|
||||
var reader = readers?.FirstOrDefault();
|
||||
|
||||
if (reader == null)
|
||||
{
|
||||
reader = EnumTypeReader.GetReader(paramType);
|
||||
service.AddTypeReader(paramType, reader);
|
||||
var paramTypeInfo = paramType.GetTypeInfo();
|
||||
if (paramTypeInfo.IsEnum)
|
||||
{
|
||||
reader = EnumTypeReader.GetReader(paramType);
|
||||
service.AddTypeReader(paramType, reader);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException($"{paramType.FullName} is not supported as a command parameter, are you missing a TypeReader?");
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
builder.ParameterType = paramType;
|
||||
builder.TypeReader = reader;
|
||||
}
|
||||
}
|
||||
|
||||
private static TypeReader GetTypeReader(CommandService service, Type paramType, Type typeReaderType)
|
||||
{
|
||||
var readers = service.GetTypeReaders(paramType);
|
||||
if (readers != null)
|
||||
{
|
||||
var reader = readers.FirstOrDefault(x => x.GetType() == typeReaderType);
|
||||
if (reader != default(TypeReader))
|
||||
{
|
||||
throw new InvalidOperationException($"{paramType.FullName} is not supported as a command parameter, are you missing a TypeReader?");
|
||||
return reader;
|
||||
}
|
||||
}
|
||||
|
||||
builder.ParameterType = paramType;
|
||||
builder.TypeReader = 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);
|
||||
|
||||
return typeReader;
|
||||
}
|
||||
|
||||
private static bool IsValidModuleDefinition(TypeInfo typeInfo)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
using System.Collections.Generic;
|
||||
@@ -41,7 +42,11 @@ namespace Discord.Commands.Builders
|
||||
|
||||
internal void SetType(Type type)
|
||||
{
|
||||
TypeReader = Command.Module.Service.GetTypeReader(type);
|
||||
var readers = Command.Module.Service.GetTypeReaders(type);
|
||||
if (readers == null)
|
||||
throw new InvalidOperationException($"{type} does not have a TypeReader registered for it");
|
||||
|
||||
TypeReader = readers.FirstOrDefault();
|
||||
|
||||
if (type.GetTypeInfo().IsValueType)
|
||||
DefaultValue = Activator.CreateInstance(type);
|
||||
|
||||
@@ -14,8 +14,8 @@ namespace Discord.Commands
|
||||
public class CommandService
|
||||
{
|
||||
private readonly SemaphoreSlim _moduleLock;
|
||||
private readonly ConcurrentDictionary<Type, ModuleInfo> _typedModuleDefs;
|
||||
private readonly ConcurrentDictionary<Type, TypeReader> _typeReaders;
|
||||
private readonly ConcurrentDictionary<Type, ModuleInfo> _typedModuleDefs;
|
||||
private readonly ConcurrentDictionary<Type, ConcurrentBag<TypeReader>> _typeReaders;
|
||||
private readonly ConcurrentBag<ModuleInfo> _moduleDefs;
|
||||
private readonly CommandMap _map;
|
||||
|
||||
@@ -24,6 +24,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 CommandService() : this(new CommandServiceConfig()) { }
|
||||
public CommandService(CommandServiceConfig config)
|
||||
@@ -32,41 +33,41 @@ namespace Discord.Commands
|
||||
_typedModuleDefs = new ConcurrentDictionary<Type, ModuleInfo>();
|
||||
_moduleDefs = new ConcurrentBag<ModuleInfo>();
|
||||
_map = new CommandMap();
|
||||
_typeReaders = new ConcurrentDictionary<Type, TypeReader>
|
||||
_typeReaders = new ConcurrentDictionary<Type, ConcurrentBag<TypeReader>>
|
||||
{
|
||||
[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(TimeSpan)] = new SimpleTypeReader<TimeSpan>(),
|
||||
[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(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(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(IRole)] = new RoleTypeReader<IRole>(),
|
||||
[typeof(IRole)] = new ConcurrentBag<TypeReader>{new RoleTypeReader<IRole>()},
|
||||
|
||||
[typeof(IUser)] = new UserTypeReader<IUser>(),
|
||||
[typeof(IGroupUser)] = new UserTypeReader<IGroupUser>(),
|
||||
[typeof(IGuildUser)] = new UserTypeReader<IGuildUser>(),
|
||||
[typeof(IUser)] = new ConcurrentBag<TypeReader>{new UserTypeReader<IUser>()},
|
||||
[typeof(IGroupUser)] = new ConcurrentBag<TypeReader>{new UserTypeReader<IGroupUser>()},
|
||||
[typeof(IGuildUser)] = new ConcurrentBag<TypeReader>{new UserTypeReader<IGuildUser>()},
|
||||
};
|
||||
_caseSensitive = config.CaseSensitiveCommands;
|
||||
_defaultRunMode = config.DefaultRunMode;
|
||||
@@ -196,17 +197,19 @@ namespace Discord.Commands
|
||||
//Type Readers
|
||||
public void AddTypeReader<T>(TypeReader reader)
|
||||
{
|
||||
_typeReaders[typeof(T)] = reader;
|
||||
var readers = _typeReaders.GetOrAdd(typeof(T), x => new ConcurrentBag<TypeReader>());
|
||||
readers.Add(reader);
|
||||
}
|
||||
public void AddTypeReader(Type type, TypeReader reader)
|
||||
{
|
||||
_typeReaders[type] = reader;
|
||||
var readers = _typeReaders.GetOrAdd(type, x=> new ConcurrentBag<TypeReader>());
|
||||
readers.Add(reader);
|
||||
}
|
||||
internal TypeReader GetTypeReader(Type type)
|
||||
internal IEnumerable<TypeReader> GetTypeReaders(Type type)
|
||||
{
|
||||
TypeReader reader;
|
||||
if (_typeReaders.TryGetValue(type, out reader))
|
||||
return reader;
|
||||
ConcurrentBag<TypeReader> definedTypeReaders;
|
||||
if (_typeReaders.TryGetValue(type, out definedTypeReaders))
|
||||
return definedTypeReaders;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user