Cleaned up command builders and async func names
This commit is contained in:
@@ -7,107 +7,109 @@ namespace Discord.Commands.Builders
|
|||||||
{
|
{
|
||||||
public class CommandBuilder
|
public class CommandBuilder
|
||||||
{
|
{
|
||||||
private List<PreconditionAttribute> preconditions;
|
private readonly List<PreconditionAttribute> _preconditions;
|
||||||
private List<ParameterBuilder> parameters;
|
private readonly List<ParameterBuilder> _parameters;
|
||||||
private List<string> aliases;
|
private readonly List<string> _aliases;
|
||||||
|
|
||||||
internal CommandBuilder(ModuleBuilder module)
|
public ModuleBuilder Module { get; }
|
||||||
{
|
internal Func<CommandContext, object[], IDependencyMap, Task> Callback { get; set; }
|
||||||
preconditions = new List<PreconditionAttribute>();
|
|
||||||
parameters = new List<ParameterBuilder>();
|
|
||||||
aliases = new List<string>();
|
|
||||||
|
|
||||||
Module = module;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Summary { get; set; }
|
public string Summary { get; set; }
|
||||||
public string Remarks { get; set; }
|
public string Remarks { get; set; }
|
||||||
public RunMode RunMode { get; set; }
|
public RunMode RunMode { get; set; }
|
||||||
public int Priority { get; set; }
|
public int Priority { get; set; }
|
||||||
public Func<CommandContext, object[], IDependencyMap, Task> Callback { get; set; }
|
|
||||||
public ModuleBuilder Module { get; }
|
|
||||||
|
|
||||||
public List<PreconditionAttribute> Preconditions => preconditions;
|
public IReadOnlyList<PreconditionAttribute> Preconditions => _preconditions;
|
||||||
public List<ParameterBuilder> Parameters => parameters;
|
public IReadOnlyList<ParameterBuilder> Parameters => _parameters;
|
||||||
public List<string> Aliases => aliases;
|
public IReadOnlyList<string> Aliases => _aliases;
|
||||||
|
|
||||||
public CommandBuilder SetName(string name)
|
//Automatic
|
||||||
|
internal CommandBuilder(ModuleBuilder module)
|
||||||
|
{
|
||||||
|
Module = module;
|
||||||
|
|
||||||
|
_preconditions = new List<PreconditionAttribute>();
|
||||||
|
_parameters = new List<ParameterBuilder>();
|
||||||
|
_aliases = new List<string>();
|
||||||
|
}
|
||||||
|
//User-defined
|
||||||
|
internal CommandBuilder(ModuleBuilder module, string primaryAlias, Func<CommandContext, object[], IDependencyMap, Task> callback)
|
||||||
|
: this(module)
|
||||||
|
{
|
||||||
|
Discord.Preconditions.NotNull(primaryAlias, nameof(primaryAlias));
|
||||||
|
Discord.Preconditions.NotNull(callback, nameof(callback));
|
||||||
|
|
||||||
|
Callback = callback;
|
||||||
|
_aliases.Add(primaryAlias);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandBuilder WithName(string name)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public CommandBuilder WithSummary(string summary)
|
||||||
public CommandBuilder SetSummary(string summary)
|
|
||||||
{
|
{
|
||||||
Summary = summary;
|
Summary = summary;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public CommandBuilder WithRemarks(string remarks)
|
||||||
public CommandBuilder SetRemarks(string remarks)
|
|
||||||
{
|
{
|
||||||
Remarks = remarks;
|
Remarks = remarks;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public CommandBuilder WithRunMode(RunMode runMode)
|
||||||
public CommandBuilder SetRunMode(RunMode runMode)
|
|
||||||
{
|
{
|
||||||
RunMode = runMode;
|
RunMode = runMode;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public CommandBuilder WithPriority(int priority)
|
||||||
public CommandBuilder SetPriority(int priority)
|
|
||||||
{
|
{
|
||||||
Priority = priority;
|
Priority = priority;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandBuilder SetCallback(Func<CommandContext, object[], IDependencyMap, Task> callback)
|
public CommandBuilder AddAliases(params string[] aliases)
|
||||||
{
|
{
|
||||||
Callback = callback;
|
_aliases.AddRange(aliases);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandBuilder AddPrecondition(PreconditionAttribute precondition)
|
public CommandBuilder AddPrecondition(PreconditionAttribute precondition)
|
||||||
{
|
{
|
||||||
preconditions.Add(precondition);
|
_preconditions.Add(precondition);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public CommandBuilder AddParameter(string name, Type type, Action<ParameterBuilder> createFunc)
|
||||||
public CommandBuilder AddParameter(Action<ParameterBuilder> createFunc)
|
|
||||||
{
|
{
|
||||||
var param = new ParameterBuilder();
|
var param = new ParameterBuilder(this, name, type);
|
||||||
createFunc(param);
|
createFunc(param);
|
||||||
parameters.Add(param);
|
_parameters.Add(param);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
internal CommandBuilder AddParameter(Action<ParameterBuilder> createFunc)
|
||||||
public CommandBuilder AddAliases(params string[] newAliases)
|
|
||||||
{
|
{
|
||||||
aliases.AddRange(newAliases);
|
var param = new ParameterBuilder(this);
|
||||||
|
createFunc(param);
|
||||||
|
_parameters.Add(param);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal CommandInfo Build(ModuleInfo info, CommandService service)
|
internal CommandInfo Build(ModuleInfo info, CommandService service)
|
||||||
{
|
{
|
||||||
if (aliases.Count == 0)
|
//Default name to first alias
|
||||||
throw new InvalidOperationException("Commands require at least one alias to be registered");
|
|
||||||
|
|
||||||
if (Callback == null)
|
|
||||||
throw new InvalidOperationException("Commands require a callback to be built");
|
|
||||||
|
|
||||||
if (Name == null)
|
if (Name == null)
|
||||||
Name = aliases[0];
|
Name = _aliases[0];
|
||||||
|
|
||||||
if (parameters.Count > 0)
|
if (_parameters.Count > 0)
|
||||||
{
|
{
|
||||||
var lastParam = parameters[parameters.Count - 1];
|
var lastParam = _parameters[_parameters.Count - 1];
|
||||||
|
|
||||||
var firstMultipleParam = parameters.FirstOrDefault(x => x.Multiple);
|
var firstMultipleParam = _parameters.FirstOrDefault(x => x.IsMultiple);
|
||||||
if ((firstMultipleParam != null) && (firstMultipleParam != lastParam))
|
if ((firstMultipleParam != null) && (firstMultipleParam != lastParam))
|
||||||
throw new InvalidOperationException("Only the last parameter in a command may have the Multiple flag.");
|
throw new InvalidOperationException("Only the last parameter in a command may have the Multiple flag.");
|
||||||
|
|
||||||
var firstRemainderParam = parameters.FirstOrDefault(x => x.Remainder);
|
var firstRemainderParam = _parameters.FirstOrDefault(x => x.IsRemainder);
|
||||||
if ((firstRemainderParam != null) && (firstRemainderParam != lastParam))
|
if ((firstRemainderParam != null) && (firstRemainderParam != lastParam))
|
||||||
throw new InvalidOperationException("Only the last parameter in a command may have the Remainder flag.");
|
throw new InvalidOperationException("Only the last parameter in a command may have the Remainder flag.");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,96 +1,107 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Discord.Commands.Builders
|
namespace Discord.Commands.Builders
|
||||||
{
|
{
|
||||||
public class ModuleBuilder
|
public class ModuleBuilder
|
||||||
{
|
{
|
||||||
private List<CommandBuilder> commands;
|
private readonly List<CommandBuilder> _commands;
|
||||||
private List<ModuleBuilder> submodules;
|
private readonly List<ModuleBuilder> _submodules;
|
||||||
private List<PreconditionAttribute> preconditions;
|
private readonly List<PreconditionAttribute> _preconditions;
|
||||||
private List<string> aliases;
|
private readonly List<string> _aliases;
|
||||||
|
|
||||||
public ModuleBuilder()
|
|
||||||
{
|
|
||||||
commands = new List<CommandBuilder>();
|
|
||||||
submodules = new List<ModuleBuilder>();
|
|
||||||
preconditions = new List<PreconditionAttribute>();
|
|
||||||
aliases = new List<string>();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal ModuleBuilder(ModuleBuilder parent)
|
|
||||||
: this()
|
|
||||||
{
|
|
||||||
ParentModule = parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public CommandService Service { get; }
|
||||||
|
public ModuleBuilder Parent { get; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Summary { get; set; }
|
public string Summary { get; set; }
|
||||||
public string Remarks { get; set; }
|
public string Remarks { get; set; }
|
||||||
public ModuleBuilder ParentModule { get; }
|
|
||||||
|
|
||||||
public List<CommandBuilder> Commands => commands;
|
public IReadOnlyList<CommandBuilder> Commands => _commands;
|
||||||
public List<ModuleBuilder> Modules => submodules;
|
public IReadOnlyList<ModuleBuilder> Modules => _submodules;
|
||||||
public List<PreconditionAttribute> Preconditions => preconditions;
|
public IReadOnlyList<PreconditionAttribute> Preconditions => _preconditions;
|
||||||
public List<string> Aliases => aliases;
|
public IReadOnlyList<string> Aliases => _aliases;
|
||||||
|
|
||||||
public ModuleBuilder SetName(string name)
|
//Automatic
|
||||||
|
internal ModuleBuilder(CommandService service, ModuleBuilder parent)
|
||||||
|
{
|
||||||
|
Service = service;
|
||||||
|
Parent = parent;
|
||||||
|
|
||||||
|
_commands = new List<CommandBuilder>();
|
||||||
|
_submodules = new List<ModuleBuilder>();
|
||||||
|
_preconditions = new List<PreconditionAttribute>();
|
||||||
|
_aliases = new List<string>();
|
||||||
|
}
|
||||||
|
//User-defined
|
||||||
|
internal ModuleBuilder(CommandService service, ModuleBuilder parent, string primaryAlias)
|
||||||
|
: this(service, parent)
|
||||||
|
{
|
||||||
|
Discord.Preconditions.NotNull(primaryAlias, nameof(primaryAlias));
|
||||||
|
|
||||||
|
_aliases = new List<string> { primaryAlias };
|
||||||
|
}
|
||||||
|
|
||||||
|
public ModuleBuilder WithName(string name)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public ModuleBuilder WithSummary(string summary)
|
||||||
public ModuleBuilder SetSummary(string summary)
|
|
||||||
{
|
{
|
||||||
Summary = summary;
|
Summary = summary;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public ModuleBuilder WithRemarks(string remarks)
|
||||||
public ModuleBuilder SetRemarks(string remarks)
|
|
||||||
{
|
{
|
||||||
Remarks = remarks;
|
Remarks = remarks;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ModuleBuilder AddAliases(params string[] newAliases)
|
public ModuleBuilder AddAlias(params string[] newAliases)
|
||||||
{
|
{
|
||||||
aliases.AddRange(newAliases);
|
_aliases.AddRange(newAliases);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ModuleBuilder AddPrecondition(PreconditionAttribute precondition)
|
public ModuleBuilder AddPrecondition(PreconditionAttribute precondition)
|
||||||
{
|
{
|
||||||
preconditions.Add(precondition);
|
_preconditions.Add(precondition);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public ModuleBuilder AddCommand(string primaryAlias, Func<CommandContext, object[], IDependencyMap, Task> callback, Action<CommandBuilder> createFunc)
|
||||||
public ModuleBuilder AddCommand(Action<CommandBuilder> createFunc)
|
{
|
||||||
|
var builder = new CommandBuilder(this, primaryAlias, callback);
|
||||||
|
createFunc(builder);
|
||||||
|
_commands.Add(builder);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
internal ModuleBuilder AddCommand(Action<CommandBuilder> createFunc)
|
||||||
{
|
{
|
||||||
var builder = new CommandBuilder(this);
|
var builder = new CommandBuilder(this);
|
||||||
createFunc(builder);
|
createFunc(builder);
|
||||||
commands.Add(builder);
|
_commands.Add(builder);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public ModuleBuilder AddModule(string primaryAlias, Action<ModuleBuilder> createFunc)
|
||||||
public ModuleBuilder AddSubmodule(Action<ModuleBuilder> createFunc)
|
|
||||||
{
|
{
|
||||||
var builder = new ModuleBuilder(this);
|
var builder = new ModuleBuilder(Service, this, primaryAlias);
|
||||||
createFunc(builder);
|
createFunc(builder);
|
||||||
submodules.Add(builder);
|
_submodules.Add(builder);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
internal ModuleBuilder AddModule(Action<ModuleBuilder> createFunc)
|
||||||
|
{
|
||||||
|
var builder = new ModuleBuilder(Service, this);
|
||||||
|
createFunc(builder);
|
||||||
|
_submodules.Add(builder);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ModuleInfo Build(CommandService service)
|
public ModuleInfo Build(CommandService service)
|
||||||
{
|
{
|
||||||
if (aliases.Count == 0)
|
//Default name to first alias
|
||||||
throw new InvalidOperationException("Modules require at least one alias to be registered");
|
|
||||||
|
|
||||||
if (commands.Count == 0 && submodules.Count == 0)
|
|
||||||
throw new InvalidOperationException("Tried to build empty module");
|
|
||||||
|
|
||||||
if (Name == null)
|
if (Name == null)
|
||||||
Name = aliases[0];
|
Name = _aliases[0];
|
||||||
|
|
||||||
return new ModuleInfo(this, service);
|
return new ModuleInfo(this, service);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,11 +40,11 @@ namespace Discord.Commands
|
|||||||
|
|
||||||
foreach (var typeInfo in topLevelGroups)
|
foreach (var typeInfo in topLevelGroups)
|
||||||
{
|
{
|
||||||
// this shouldn't be the case; may be safe to remove?
|
// TODO: This shouldn't be the case; may be safe to remove?
|
||||||
if (result.ContainsKey(typeInfo.AsType()))
|
if (result.ContainsKey(typeInfo.AsType()))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var module = new ModuleBuilder();
|
var module = new ModuleBuilder(service, null);
|
||||||
|
|
||||||
BuildModule(module, typeInfo, service);
|
BuildModule(module, typeInfo, service);
|
||||||
BuildSubTypes(module, typeInfo.DeclaredNestedTypes, builtTypes, service);
|
BuildSubTypes(module, typeInfo.DeclaredNestedTypes, builtTypes, service);
|
||||||
@@ -65,7 +65,7 @@ namespace Discord.Commands
|
|||||||
if (builtTypes.Contains(typeInfo))
|
if (builtTypes.Contains(typeInfo))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
builder.AddSubmodule((module) => {
|
builder.AddModule((module) => {
|
||||||
BuildModule(module, typeInfo, service);
|
BuildModule(module, typeInfo, service);
|
||||||
BuildSubTypes(module, typeInfo.DeclaredNestedTypes, builtTypes, service);
|
BuildSubTypes(module, typeInfo.DeclaredNestedTypes, builtTypes, service);
|
||||||
});
|
});
|
||||||
@@ -88,17 +88,20 @@ namespace Discord.Commands
|
|||||||
else if (attribute is RemarksAttribute)
|
else if (attribute is RemarksAttribute)
|
||||||
builder.Remarks = (attribute as RemarksAttribute).Text;
|
builder.Remarks = (attribute as RemarksAttribute).Text;
|
||||||
else if (attribute is AliasAttribute)
|
else if (attribute is AliasAttribute)
|
||||||
builder.AddAliases((attribute as AliasAttribute).Aliases);
|
builder.AddAlias((attribute as AliasAttribute).Aliases);
|
||||||
else if (attribute is GroupAttribute)
|
else if (attribute is GroupAttribute)
|
||||||
{
|
{
|
||||||
var groupAttr = attribute as GroupAttribute;
|
var groupAttr = attribute as GroupAttribute;
|
||||||
builder.Name = builder.Name ?? groupAttr.Prefix;
|
builder.Name = builder.Name ?? groupAttr.Prefix;
|
||||||
builder.AddAliases(groupAttr.Prefix);
|
builder.AddAlias(groupAttr.Prefix);
|
||||||
}
|
}
|
||||||
else if (attribute is PreconditionAttribute)
|
else if (attribute is PreconditionAttribute)
|
||||||
builder.AddPrecondition(attribute as PreconditionAttribute);
|
builder.AddPrecondition(attribute as PreconditionAttribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (builder.Name == null)
|
||||||
|
builder.Name = typeInfo.Name;
|
||||||
|
|
||||||
var validCommands = typeInfo.DeclaredMethods.Where(x => IsValidCommandDefinition(x));
|
var validCommands = typeInfo.DeclaredMethods.Where(x => IsValidCommandDefinition(x));
|
||||||
|
|
||||||
foreach (var method in validCommands)
|
foreach (var method in validCommands)
|
||||||
@@ -168,7 +171,7 @@ namespace Discord.Commands
|
|||||||
|
|
||||||
builder.Name = paramInfo.Name;
|
builder.Name = paramInfo.Name;
|
||||||
|
|
||||||
builder.Optional = paramInfo.IsOptional;
|
builder.IsOptional = paramInfo.IsOptional;
|
||||||
builder.DefaultValue = paramInfo.HasDefaultValue ? paramInfo.DefaultValue : null;
|
builder.DefaultValue = paramInfo.HasDefaultValue ? paramInfo.DefaultValue : null;
|
||||||
|
|
||||||
foreach (var attribute in attributes)
|
foreach (var attribute in attributes)
|
||||||
@@ -178,7 +181,7 @@ namespace Discord.Commands
|
|||||||
builder.Summary = (attribute as SummaryAttribute).Text;
|
builder.Summary = (attribute as SummaryAttribute).Text;
|
||||||
else if (attribute is ParamArrayAttribute)
|
else if (attribute is ParamArrayAttribute)
|
||||||
{
|
{
|
||||||
builder.Multiple = true;
|
builder.IsMultiple = true;
|
||||||
paramType = paramType.GetElementType();
|
paramType = paramType.GetElementType();
|
||||||
}
|
}
|
||||||
else if (attribute is RemainderAttribute)
|
else if (attribute is RemainderAttribute)
|
||||||
@@ -186,7 +189,7 @@ namespace Discord.Commands
|
|||||||
if (position != count-1)
|
if (position != count-1)
|
||||||
throw new InvalidOperationException("Remainder parameters must be the last parameter in a command.");
|
throw new InvalidOperationException("Remainder parameters must be the last parameter in a command.");
|
||||||
|
|
||||||
builder.Remainder = true;
|
builder.IsRemainder = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,100 +1,79 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Reflection;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Discord.Commands.Builders
|
namespace Discord.Commands.Builders
|
||||||
{
|
{
|
||||||
public class ParameterBuilder
|
public class ParameterBuilder
|
||||||
{
|
{
|
||||||
public ParameterBuilder()
|
public CommandBuilder Command { get; }
|
||||||
{ }
|
public string Name { get; internal set; }
|
||||||
|
public Type ParameterType { get; internal set; }
|
||||||
public ParameterBuilder(string name)
|
|
||||||
{
|
|
||||||
Name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Name { get; set; }
|
|
||||||
public string Summary { get; set; }
|
|
||||||
public object DefaultValue { get; set; }
|
|
||||||
public Type ParameterType { get; set; }
|
|
||||||
|
|
||||||
public TypeReader TypeReader { get; set; }
|
public TypeReader TypeReader { get; set; }
|
||||||
|
public bool IsOptional { get; set; }
|
||||||
|
public bool IsRemainder { get; set; }
|
||||||
|
public bool IsMultiple { get; set; }
|
||||||
|
public object DefaultValue { get; set; }
|
||||||
|
public string Summary { get; set; }
|
||||||
|
|
||||||
public bool Optional { get; set; }
|
//Automatic
|
||||||
public bool Remainder { get; set; }
|
internal ParameterBuilder(CommandBuilder command)
|
||||||
public bool Multiple { get; set; }
|
|
||||||
|
|
||||||
public ParameterBuilder SetName(string name)
|
|
||||||
{
|
{
|
||||||
|
Command = command;
|
||||||
|
}
|
||||||
|
//User-defined
|
||||||
|
internal ParameterBuilder(CommandBuilder command, string name, Type type)
|
||||||
|
: this(command)
|
||||||
|
{
|
||||||
|
Preconditions.NotNull(name, nameof(name));
|
||||||
|
|
||||||
Name = name;
|
Name = name;
|
||||||
return this;
|
SetType(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParameterBuilder SetSummary(string summary)
|
internal void SetType(Type type)
|
||||||
|
{
|
||||||
|
TypeReader = Command.Module.Service.GetTypeReader(type);
|
||||||
|
|
||||||
|
if (type.GetTypeInfo().IsValueType)
|
||||||
|
DefaultValue = Activator.CreateInstance(type);
|
||||||
|
else if (type.IsArray)
|
||||||
|
type = ParameterType.GetElementType();
|
||||||
|
ParameterType = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ParameterBuilder WithSummary(string summary)
|
||||||
{
|
{
|
||||||
Summary = summary;
|
Summary = summary;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public ParameterBuilder WithDefault(object defaultValue)
|
||||||
public ParameterBuilder SetDefault<T>(T defaultValue)
|
|
||||||
{
|
{
|
||||||
Optional = true;
|
|
||||||
DefaultValue = defaultValue;
|
DefaultValue = defaultValue;
|
||||||
ParameterType = typeof(T);
|
return this;
|
||||||
|
}
|
||||||
if (ParameterType.IsArray)
|
public ParameterBuilder WithIsOptional(bool isOptional)
|
||||||
ParameterType = ParameterType.GetElementType();
|
{
|
||||||
|
IsOptional = isOptional;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public ParameterBuilder WithIsRemainder(bool isRemainder)
|
||||||
|
{
|
||||||
|
IsRemainder = isRemainder;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public ParameterBuilder WithIsMultiple(bool isMultiple)
|
||||||
|
{
|
||||||
|
IsMultiple = isMultiple;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParameterBuilder SetType(Type parameterType)
|
internal ParameterInfo Build(CommandInfo info)
|
||||||
{
|
{
|
||||||
ParameterType = parameterType;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ParameterBuilder SetTypeReader(TypeReader reader)
|
|
||||||
{
|
|
||||||
TypeReader = reader;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ParameterBuilder SetOptional(bool isOptional)
|
|
||||||
{
|
|
||||||
Optional = isOptional;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ParameterBuilder SetRemainder(bool isRemainder)
|
|
||||||
{
|
|
||||||
Remainder = isRemainder;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ParameterBuilder SetMultiple(bool isMultiple)
|
|
||||||
{
|
|
||||||
Multiple = isMultiple;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal ParameterInfo Build(CommandInfo info, CommandService service)
|
|
||||||
{
|
|
||||||
// TODO: should we throw when we don't have a name?
|
|
||||||
if (Name == null)
|
|
||||||
Name = "[unknown parameter]";
|
|
||||||
|
|
||||||
if (ParameterType == null)
|
|
||||||
throw new InvalidOperationException($"Could not build parameter {Name} from command {info.Name} - An invalid parameter type was given");
|
|
||||||
|
|
||||||
if (TypeReader == null)
|
if (TypeReader == null)
|
||||||
TypeReader = service.GetTypeReader(ParameterType);
|
throw new InvalidOperationException($"No default TypeReader found, one must be specified");
|
||||||
|
|
||||||
if (TypeReader == null)
|
return new ParameterInfo(this, info, Command.Module.Service);
|
||||||
throw new InvalidOperationException($"Could not build parameter {Name} from command {info.Name} - A valid TypeReader could not be found");
|
|
||||||
|
|
||||||
return new ParameterInfo(this, info, service);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,12 +67,12 @@ namespace Discord.Commands
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Modules
|
//Modules
|
||||||
public async Task<ModuleInfo> BuildModule(Action<ModuleBuilder> buildFunc)
|
public async Task<ModuleInfo> CreateModuleAsync(string primaryAlias, Action<ModuleBuilder> buildFunc)
|
||||||
{
|
{
|
||||||
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var builder = new ModuleBuilder();
|
var builder = new ModuleBuilder(this, null, primaryAlias);
|
||||||
buildFunc(builder);
|
buildFunc(builder);
|
||||||
|
|
||||||
var module = builder.Build(this);
|
var module = builder.Build(this);
|
||||||
@@ -83,7 +83,7 @@ namespace Discord.Commands
|
|||||||
_moduleLock.Release();
|
_moduleLock.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public async Task<ModuleInfo> AddModule<T>()
|
public async Task<ModuleInfo> AddModuleAsync<T>()
|
||||||
{
|
{
|
||||||
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
||||||
try
|
try
|
||||||
@@ -107,7 +107,7 @@ namespace Discord.Commands
|
|||||||
_moduleLock.Release();
|
_moduleLock.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public async Task<IEnumerable<ModuleInfo>> AddModules(Assembly assembly)
|
public async Task<IEnumerable<ModuleInfo>> AddModulesAsync(Assembly assembly)
|
||||||
{
|
{
|
||||||
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
||||||
try
|
try
|
||||||
@@ -141,7 +141,7 @@ namespace Discord.Commands
|
|||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> RemoveModule(ModuleInfo module)
|
public async Task<bool> RemoveModuleAsync(ModuleInfo module)
|
||||||
{
|
{
|
||||||
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
||||||
try
|
try
|
||||||
@@ -153,7 +153,7 @@ namespace Discord.Commands
|
|||||||
_moduleLock.Release();
|
_moduleLock.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public async Task<bool> RemoveModule<T>()
|
public async Task<bool> RemoveModuleAsync<T>()
|
||||||
{
|
{
|
||||||
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
||||||
try
|
try
|
||||||
@@ -217,9 +217,9 @@ namespace Discord.Commands
|
|||||||
return SearchResult.FromError(CommandError.UnknownCommand, "Unknown command.");
|
return SearchResult.FromError(CommandError.UnknownCommand, "Unknown command.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<IResult> Execute(CommandContext context, int argPos, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
public Task<IResult> ExecuteAsync(CommandContext context, int argPos, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
||||||
=> Execute(context, context.Message.Content.Substring(argPos), dependencyMap, multiMatchHandling);
|
=> ExecuteAsync(context, context.Message.Content.Substring(argPos), dependencyMap, multiMatchHandling);
|
||||||
public async Task<IResult> Execute(CommandContext context, string input, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
public async Task<IResult> ExecuteAsync(CommandContext context, string input, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
||||||
{
|
{
|
||||||
dependencyMap = dependencyMap ?? DependencyMap.Empty;
|
dependencyMap = dependencyMap ?? DependencyMap.Empty;
|
||||||
|
|
||||||
@@ -230,7 +230,7 @@ namespace Discord.Commands
|
|||||||
var commands = searchResult.Commands;
|
var commands = searchResult.Commands;
|
||||||
for (int i = commands.Count - 1; i >= 0; i--)
|
for (int i = commands.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
var preconditionResult = await commands[i].CheckPreconditions(context, dependencyMap).ConfigureAwait(false);
|
var preconditionResult = await commands[i].CheckPreconditionsAsync(context, dependencyMap).ConfigureAwait(false);
|
||||||
if (!preconditionResult.IsSuccess)
|
if (!preconditionResult.IsSuccess)
|
||||||
{
|
{
|
||||||
if (commands.Count == 1)
|
if (commands.Count == 1)
|
||||||
@@ -239,7 +239,7 @@ namespace Discord.Commands
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var parseResult = await commands[i].Parse(context, searchResult, preconditionResult).ConfigureAwait(false);
|
var parseResult = await commands[i].ParseAsync(context, searchResult, preconditionResult).ConfigureAwait(false);
|
||||||
if (!parseResult.IsSuccess)
|
if (!parseResult.IsSuccess)
|
||||||
{
|
{
|
||||||
if (parseResult.Error == CommandError.MultipleMatches)
|
if (parseResult.Error == CommandError.MultipleMatches)
|
||||||
|
|||||||
@@ -40,16 +40,20 @@ namespace Discord.Commands
|
|||||||
RunMode = builder.RunMode;
|
RunMode = builder.RunMode;
|
||||||
Priority = builder.Priority;
|
Priority = builder.Priority;
|
||||||
|
|
||||||
Aliases = module.Aliases.Permutate(builder.Aliases, (first, second) => first + " " + second).ToImmutableArray();
|
if (module.Aliases.Count != 0)
|
||||||
|
Aliases = module.Aliases.Permutate(builder.Aliases, (first, second) => first + " " + second).ToImmutableArray();
|
||||||
|
else
|
||||||
|
Aliases = builder.Aliases.ToImmutableArray();
|
||||||
|
|
||||||
Preconditions = builder.Preconditions.ToImmutableArray();
|
Preconditions = builder.Preconditions.ToImmutableArray();
|
||||||
|
|
||||||
Parameters = builder.Parameters.Select(x => x.Build(this, service)).ToImmutableArray();
|
Parameters = builder.Parameters.Select(x => x.Build(this)).ToImmutableArray();
|
||||||
HasVarArgs = builder.Parameters.Count > 0 ? builder.Parameters[builder.Parameters.Count - 1].Multiple : false;
|
HasVarArgs = builder.Parameters.Count > 0 ? builder.Parameters[builder.Parameters.Count - 1].IsMultiple : false;
|
||||||
|
|
||||||
_action = builder.Callback;
|
_action = builder.Callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<PreconditionResult> CheckPreconditions(CommandContext context, IDependencyMap map = null)
|
public async Task<PreconditionResult> CheckPreconditionsAsync(CommandContext context, IDependencyMap map = null)
|
||||||
{
|
{
|
||||||
if (map == null)
|
if (map == null)
|
||||||
map = DependencyMap.Empty;
|
map = DependencyMap.Empty;
|
||||||
@@ -71,7 +75,7 @@ namespace Discord.Commands
|
|||||||
return PreconditionResult.FromSuccess();
|
return PreconditionResult.FromSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ParseResult> Parse(CommandContext context, SearchResult searchResult, PreconditionResult? preconditionResult = null)
|
public async Task<ParseResult> ParseAsync(CommandContext context, SearchResult searchResult, PreconditionResult? preconditionResult = null)
|
||||||
{
|
{
|
||||||
if (!searchResult.IsSuccess)
|
if (!searchResult.IsSuccess)
|
||||||
return ParseResult.FromError(searchResult);
|
return ParseResult.FromError(searchResult);
|
||||||
@@ -114,9 +118,9 @@ namespace Discord.Commands
|
|||||||
paramList[i] = parseResult.ParamValues[i].Values.First().Value;
|
paramList[i] = parseResult.ParamValues[i].Values.First().Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Execute(context, argList, paramList, map);
|
return ExecuteAsync(context, argList, paramList, map);
|
||||||
}
|
}
|
||||||
public async Task<ExecuteResult> Execute(CommandContext context, IEnumerable<object> argList, IEnumerable<object> paramList, IDependencyMap map)
|
public async Task<ExecuteResult> ExecuteAsync(CommandContext context, IEnumerable<object> argList, IEnumerable<object> paramList, IDependencyMap map)
|
||||||
{
|
{
|
||||||
if (map == null)
|
if (map == null)
|
||||||
map = DependencyMap.Empty;
|
map = DependencyMap.Empty;
|
||||||
@@ -163,7 +167,7 @@ namespace Discord.Commands
|
|||||||
|
|
||||||
if (HasVarArgs)
|
if (HasVarArgs)
|
||||||
{
|
{
|
||||||
var func = _arrayConverters.GetOrAdd(Parameters[Parameters.Count - 1].ParameterType, t =>
|
var func = _arrayConverters.GetOrAdd(Parameters[Parameters.Count - 1].Type, t =>
|
||||||
{
|
{
|
||||||
var method = _convertParamsMethod.MakeGenericMethod(t);
|
var method = _convertParamsMethod.MakeGenericMethod(t);
|
||||||
return (Func<IEnumerable<object>, object>)method.CreateDelegate(typeof(Func<IEnumerable<object>, object>));
|
return (Func<IEnumerable<object>, object>)method.CreateDelegate(typeof(Func<IEnumerable<object>, object>));
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using System;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
@@ -41,11 +40,11 @@ namespace Discord.Commands
|
|||||||
Stack<ModuleBuilder> builderStack = new Stack<ModuleBuilder>();
|
Stack<ModuleBuilder> builderStack = new Stack<ModuleBuilder>();
|
||||||
builderStack.Push(builder);
|
builderStack.Push(builder);
|
||||||
|
|
||||||
ModuleBuilder parent = builder.ParentModule;
|
ModuleBuilder parent = builder.Parent;
|
||||||
while (parent != null)
|
while (parent != null)
|
||||||
{
|
{
|
||||||
builderStack.Push(parent);
|
builderStack.Push(parent);
|
||||||
parent = parent.ParentModule;
|
parent = parent.Parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (builderStack.Count() > 0)
|
while (builderStack.Count() > 0)
|
||||||
@@ -82,7 +81,7 @@ namespace Discord.Commands
|
|||||||
while (parent != null)
|
while (parent != null)
|
||||||
{
|
{
|
||||||
result.AddRange(parent.Preconditions);
|
result.AddRange(parent.Preconditions);
|
||||||
parent = parent.ParentModule;
|
parent = parent.Parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -16,11 +16,11 @@ namespace Discord.Commands
|
|||||||
|
|
||||||
Name = builder.Name;
|
Name = builder.Name;
|
||||||
Summary = builder.Summary;
|
Summary = builder.Summary;
|
||||||
IsOptional = builder.Optional;
|
IsOptional = builder.IsOptional;
|
||||||
IsRemainder = builder.Remainder;
|
IsRemainder = builder.IsRemainder;
|
||||||
IsMultiple = builder.Multiple;
|
IsMultiple = builder.IsMultiple;
|
||||||
|
|
||||||
ParameterType = builder.ParameterType;
|
Type = builder.ParameterType;
|
||||||
DefaultValue = builder.DefaultValue;
|
DefaultValue = builder.DefaultValue;
|
||||||
|
|
||||||
_reader = builder.TypeReader;
|
_reader = builder.TypeReader;
|
||||||
@@ -32,7 +32,7 @@ namespace Discord.Commands
|
|||||||
public bool IsOptional { get; }
|
public bool IsOptional { get; }
|
||||||
public bool IsRemainder { get; }
|
public bool IsRemainder { get; }
|
||||||
public bool IsMultiple { get; }
|
public bool IsMultiple { get; }
|
||||||
public Type ParameterType { get; }
|
public Type Type { get; }
|
||||||
public object DefaultValue { get; }
|
public object DefaultValue { get; }
|
||||||
|
|
||||||
public async Task<TypeReaderResult> Parse(CommandContext context, string input)
|
public async Task<TypeReaderResult> Parse(CommandContext context, string input)
|
||||||
|
|||||||
@@ -1,95 +1,39 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Discord.Commands
|
namespace Discord.Commands
|
||||||
{
|
{
|
||||||
internal class CommandMap
|
internal class CommandMap
|
||||||
{
|
{
|
||||||
static readonly char[] _whitespaceChars = new char[] { ' ', '\r', '\n' };
|
private readonly CommandMapNode _root;
|
||||||
private readonly object _lockObj = new object();
|
private static readonly string[] _blankAliases = new[] { "" };
|
||||||
|
|
||||||
private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;
|
|
||||||
|
|
||||||
public CommandMap()
|
public CommandMap()
|
||||||
{
|
{
|
||||||
_nodes = new ConcurrentDictionary<string, CommandMapNode>();
|
_root = new CommandMapNode("");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddCommand(CommandInfo command)
|
public void AddCommand(CommandInfo command)
|
||||||
{
|
{
|
||||||
foreach (string text in command.Aliases)
|
foreach (string text in GetAliases(command))
|
||||||
{
|
_root.AddCommand(text, 0, command);
|
||||||
int nextSpace = NextWhitespace(text);
|
|
||||||
string name;
|
|
||||||
|
|
||||||
if (nextSpace == -1)
|
|
||||||
name = text;
|
|
||||||
else
|
|
||||||
name = text.Substring(0, nextSpace);
|
|
||||||
|
|
||||||
lock (_lockObj)
|
|
||||||
{
|
|
||||||
var nextNode = _nodes.GetOrAdd(name, x => new CommandMapNode(x));
|
|
||||||
nextNode.AddCommand(nextSpace == -1 ? "" : text, nextSpace + 1, command);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
public void RemoveCommand(CommandInfo command)
|
public void RemoveCommand(CommandInfo command)
|
||||||
{
|
{
|
||||||
foreach (string text in command.Aliases)
|
foreach (string text in GetAliases(command))
|
||||||
{
|
_root.RemoveCommand(text, 0, command);
|
||||||
int nextSpace = NextWhitespace(text);
|
|
||||||
string name;
|
|
||||||
|
|
||||||
if (nextSpace == -1)
|
|
||||||
name = text;
|
|
||||||
else
|
|
||||||
name = text.Substring(0, nextSpace);
|
|
||||||
|
|
||||||
lock (_lockObj)
|
|
||||||
{
|
|
||||||
CommandMapNode nextNode;
|
|
||||||
if (_nodes.TryGetValue(name, out nextNode))
|
|
||||||
{
|
|
||||||
nextNode.RemoveCommand(nextSpace == -1 ? "" : text, nextSpace + 1, command);
|
|
||||||
if (nextNode.IsEmpty)
|
|
||||||
_nodes.TryRemove(name, out nextNode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<CommandInfo> GetCommands(string text)
|
public IEnumerable<CommandInfo> GetCommands(string text)
|
||||||
{
|
{
|
||||||
int nextSpace = NextWhitespace(text);
|
return _root.GetCommands(text, 0);
|
||||||
string name;
|
|
||||||
|
|
||||||
if (nextSpace == -1)
|
|
||||||
name = text;
|
|
||||||
else
|
|
||||||
name = text.Substring(0, nextSpace);
|
|
||||||
|
|
||||||
lock (_lockObj)
|
|
||||||
{
|
|
||||||
CommandMapNode nextNode;
|
|
||||||
if (_nodes.TryGetValue(name, out nextNode))
|
|
||||||
return nextNode.GetCommands(text, nextSpace + 1);
|
|
||||||
else
|
|
||||||
return Enumerable.Empty<CommandInfo>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int NextWhitespace(string text)
|
private IReadOnlyList<string> GetAliases(CommandInfo command)
|
||||||
{
|
{
|
||||||
int lowest = int.MaxValue;
|
var aliases = command.Aliases;
|
||||||
for (int i = 0; i < _whitespaceChars.Length; i++)
|
if (aliases.Count == 0)
|
||||||
{
|
return _blankAliases;
|
||||||
int index = text.IndexOf(_whitespaceChars[i]);
|
return aliases;
|
||||||
if (index != -1 && index < lowest)
|
|
||||||
lowest = index;
|
|
||||||
}
|
|
||||||
return (lowest != int.MaxValue) ? lowest : -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Concurrent;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
|
|
||||||
@@ -6,6 +7,8 @@ namespace Discord.Commands
|
|||||||
{
|
{
|
||||||
internal class CommandMapNode
|
internal class CommandMapNode
|
||||||
{
|
{
|
||||||
|
private static readonly char[] _whitespaceChars = new char[] { ' ', '\r', '\n' };
|
||||||
|
|
||||||
private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;
|
private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;
|
||||||
private readonly string _name;
|
private readonly string _name;
|
||||||
private readonly object _lockObj = new object();
|
private readonly object _lockObj = new object();
|
||||||
@@ -22,13 +25,17 @@ namespace Discord.Commands
|
|||||||
|
|
||||||
public void AddCommand(string text, int index, CommandInfo command)
|
public void AddCommand(string text, int index, CommandInfo command)
|
||||||
{
|
{
|
||||||
int nextSpace = text.IndexOf(' ', index);
|
int nextSpace = NextWhitespace(text, index);
|
||||||
string name;
|
string name;
|
||||||
|
|
||||||
lock (_lockObj)
|
lock (_lockObj)
|
||||||
{
|
{
|
||||||
if (text == "")
|
if (text == "")
|
||||||
|
{
|
||||||
|
if (_name == "")
|
||||||
|
throw new InvalidOperationException("Cannot add commands to the root node.");
|
||||||
_commands = _commands.Add(command);
|
_commands = _commands.Add(command);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (nextSpace == -1)
|
if (nextSpace == -1)
|
||||||
@@ -43,7 +50,7 @@ namespace Discord.Commands
|
|||||||
}
|
}
|
||||||
public void RemoveCommand(string text, int index, CommandInfo command)
|
public void RemoveCommand(string text, int index, CommandInfo command)
|
||||||
{
|
{
|
||||||
int nextSpace = text.IndexOf(' ', index);
|
int nextSpace = NextWhitespace(text, index);
|
||||||
string name;
|
string name;
|
||||||
|
|
||||||
lock (_lockObj)
|
lock (_lockObj)
|
||||||
@@ -70,7 +77,7 @@ namespace Discord.Commands
|
|||||||
|
|
||||||
public IEnumerable<CommandInfo> GetCommands(string text, int index)
|
public IEnumerable<CommandInfo> GetCommands(string text, int index)
|
||||||
{
|
{
|
||||||
int nextSpace = text.IndexOf(' ', index);
|
int nextSpace = NextWhitespace(text, index);
|
||||||
string name;
|
string name;
|
||||||
|
|
||||||
var commands = _commands;
|
var commands = _commands;
|
||||||
@@ -92,5 +99,17 @@ namespace Discord.Commands
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int NextWhitespace(string text, int startIndex)
|
||||||
|
{
|
||||||
|
int lowest = int.MaxValue;
|
||||||
|
for (int i = 0; i < _whitespaceChars.Length; i++)
|
||||||
|
{
|
||||||
|
int index = text.IndexOf(_whitespaceChars[i], startIndex);
|
||||||
|
if (index != -1 && index < lowest)
|
||||||
|
lowest = index;
|
||||||
|
}
|
||||||
|
return (lowest != int.MaxValue) ? lowest : -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,47 +5,51 @@ namespace Discord
|
|||||||
internal static class Preconditions
|
internal static class Preconditions
|
||||||
{
|
{
|
||||||
//Objects
|
//Objects
|
||||||
public static void NotNull<T>(T obj, string name, string msg = null) where T : class { if (obj == null) throw new ArgumentNullException(name); }
|
public static void NotNull<T>(T obj, string name, string msg = null) where T : class { if (obj == null) throw CreateNotNullException(name, msg); }
|
||||||
public static void NotNull<T>(Optional<T> obj, string name, string msg = null) where T : class { if (obj.IsSpecified && obj.Value == null) throw new ArgumentNullException(name); }
|
public static void NotNull<T>(Optional<T> obj, string name, string msg = null) where T : class { if (obj.IsSpecified && obj.Value == null) throw CreateNotNullException(name, msg); }
|
||||||
|
|
||||||
|
private static ArgumentNullException CreateNotNullException(string name, string msg)
|
||||||
|
{
|
||||||
|
if (msg == null) return new ArgumentNullException(name);
|
||||||
|
else return new ArgumentNullException(name, msg);
|
||||||
|
}
|
||||||
|
|
||||||
//Strings
|
//Strings
|
||||||
public static void NotEmpty(string obj, string name, string msg = null) { if (obj.Length == 0) throw new ArgumentException("Argument cannot be empty.", name); }
|
public static void NotEmpty(string obj, string name, string msg = null) { if (obj.Length == 0) throw CreateNotEmptyException(name, msg); }
|
||||||
public static void NotEmpty(Optional<string> obj, string name, string msg = null) { if (obj.IsSpecified && obj.Value.Length == 0) throw new ArgumentException("Argument cannot be empty.", name); }
|
public static void NotEmpty(Optional<string> obj, string name, string msg = null) { if (obj.IsSpecified && obj.Value.Length == 0) throw CreateNotEmptyException(name, msg); }
|
||||||
public static void NotNullOrEmpty(string obj, string name, string msg = null)
|
public static void NotNullOrEmpty(string obj, string name, string msg = null)
|
||||||
{
|
{
|
||||||
if (obj == null)
|
if (obj == null) throw CreateNotNullException(name, msg);
|
||||||
throw new ArgumentNullException(name);
|
if (obj.Length == 0) throw CreateNotEmptyException(name, msg);
|
||||||
if (obj.Length == 0)
|
|
||||||
throw new ArgumentException("Argument cannot be empty.", name);
|
|
||||||
}
|
}
|
||||||
public static void NotNullOrEmpty(Optional<string> obj, string name, string msg = null)
|
public static void NotNullOrEmpty(Optional<string> obj, string name, string msg = null)
|
||||||
{
|
{
|
||||||
if (obj.IsSpecified)
|
if (obj.IsSpecified)
|
||||||
{
|
{
|
||||||
if (obj.Value == null)
|
if (obj.Value == null) throw CreateNotNullException(name, msg);
|
||||||
throw new ArgumentNullException(name);
|
if (obj.Value.Length == 0) throw CreateNotEmptyException(name, msg);
|
||||||
if (obj.Value.Length == 0)
|
|
||||||
throw new ArgumentException("Argument cannot be empty.", name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static void NotNullOrWhitespace(string obj, string name, string msg = null)
|
public static void NotNullOrWhitespace(string obj, string name, string msg = null)
|
||||||
{
|
{
|
||||||
if (obj == null)
|
if (obj == null) throw CreateNotNullException(name, msg);
|
||||||
throw new ArgumentNullException(name);
|
if (obj.Trim().Length == 0) throw CreateNotEmptyException(name, msg);
|
||||||
if (obj.Trim().Length == 0)
|
|
||||||
throw new ArgumentException("Argument cannot be blank.", name);
|
|
||||||
}
|
}
|
||||||
public static void NotNullOrWhitespace(Optional<string> obj, string name, string msg = null)
|
public static void NotNullOrWhitespace(Optional<string> obj, string name, string msg = null)
|
||||||
{
|
{
|
||||||
if (obj.IsSpecified)
|
if (obj.IsSpecified)
|
||||||
{
|
{
|
||||||
if (obj.Value == null)
|
if (obj.Value == null) throw CreateNotNullException(name, msg);
|
||||||
throw new ArgumentNullException(name);
|
if (obj.Value.Trim().Length == 0) throw CreateNotEmptyException(name, msg);
|
||||||
if (obj.Value.Trim().Length == 0)
|
|
||||||
throw new ArgumentException("Argument cannot be blank.", name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ArgumentException CreateNotEmptyException(string name, string msg)
|
||||||
|
{
|
||||||
|
if (msg == null) return new ArgumentException(name, "Argument cannot be blank.");
|
||||||
|
else return new ArgumentException(name, msg);
|
||||||
|
}
|
||||||
|
|
||||||
//Numerics
|
//Numerics
|
||||||
public static void NotEqual(sbyte obj, sbyte value, string name, string msg = null) { if (obj == value) throw CreateNotEqualException(name, msg, value); }
|
public static void NotEqual(sbyte obj, sbyte value, string name, string msg = null) { if (obj == value) throw CreateNotEqualException(name, msg, value); }
|
||||||
public static void NotEqual(byte obj, byte value, string name, string msg = null) { if (obj == value) throw CreateNotEqualException(name, msg, value); }
|
public static void NotEqual(byte obj, byte value, string name, string msg = null) { if (obj == value) throw CreateNotEqualException(name, msg, value); }
|
||||||
@@ -82,10 +86,8 @@ namespace Discord
|
|||||||
|
|
||||||
private static ArgumentException CreateNotEqualException<T>(string name, string msg, T value)
|
private static ArgumentException CreateNotEqualException<T>(string name, string msg, T value)
|
||||||
{
|
{
|
||||||
if (msg == null)
|
if (msg == null) return new ArgumentException($"Value may not be equal to {value}", name);
|
||||||
throw new ArgumentException($"Value may not be equal to {value}", name);
|
else return new ArgumentException(msg, name);
|
||||||
else
|
|
||||||
throw new ArgumentException(msg, name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void AtLeast(sbyte obj, sbyte value, string name, string msg = null) { if (obj < value) throw CreateAtLeastException(name, msg, value); }
|
public static void AtLeast(sbyte obj, sbyte value, string name, string msg = null) { if (obj < value) throw CreateAtLeastException(name, msg, value); }
|
||||||
@@ -107,10 +109,8 @@ namespace Discord
|
|||||||
|
|
||||||
private static ArgumentException CreateAtLeastException<T>(string name, string msg, T value)
|
private static ArgumentException CreateAtLeastException<T>(string name, string msg, T value)
|
||||||
{
|
{
|
||||||
if (msg == null)
|
if (msg == null) return new ArgumentException($"Value must be at least {value}", name);
|
||||||
throw new ArgumentException($"Value must be at least {value}", name);
|
else return new ArgumentException(msg, name);
|
||||||
else
|
|
||||||
throw new ArgumentException(msg, name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void GreaterThan(sbyte obj, sbyte value, string name, string msg = null) { if (obj <= value) throw CreateGreaterThanException(name, msg, value); }
|
public static void GreaterThan(sbyte obj, sbyte value, string name, string msg = null) { if (obj <= value) throw CreateGreaterThanException(name, msg, value); }
|
||||||
@@ -132,10 +132,8 @@ namespace Discord
|
|||||||
|
|
||||||
private static ArgumentException CreateGreaterThanException<T>(string name, string msg, T value)
|
private static ArgumentException CreateGreaterThanException<T>(string name, string msg, T value)
|
||||||
{
|
{
|
||||||
if (msg == null)
|
if (msg == null) return new ArgumentException($"Value must be greater than {value}", name);
|
||||||
throw new ArgumentException($"Value must be greater than {value}", name);
|
else return new ArgumentException(msg, name);
|
||||||
else
|
|
||||||
throw new ArgumentException(msg, name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void AtMost(sbyte obj, sbyte value, string name, string msg = null) { if (obj > value) throw CreateAtMostException(name, msg, value); }
|
public static void AtMost(sbyte obj, sbyte value, string name, string msg = null) { if (obj > value) throw CreateAtMostException(name, msg, value); }
|
||||||
@@ -157,10 +155,8 @@ namespace Discord
|
|||||||
|
|
||||||
private static ArgumentException CreateAtMostException<T>(string name, string msg, T value)
|
private static ArgumentException CreateAtMostException<T>(string name, string msg, T value)
|
||||||
{
|
{
|
||||||
if (msg == null)
|
if (msg == null) return new ArgumentException($"Value must be at most {value}", name);
|
||||||
throw new ArgumentException($"Value must be at most {value}", name);
|
else return new ArgumentException(msg, name);
|
||||||
else
|
|
||||||
throw new ArgumentException(msg, name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LessThan(sbyte obj, sbyte value, string name, string msg = null) { if (obj >= value) throw CreateLessThanException(name, msg, value); }
|
public static void LessThan(sbyte obj, sbyte value, string name, string msg = null) { if (obj >= value) throw CreateLessThanException(name, msg, value); }
|
||||||
@@ -182,10 +178,8 @@ namespace Discord
|
|||||||
|
|
||||||
private static ArgumentException CreateLessThanException<T>(string name, string msg, T value)
|
private static ArgumentException CreateLessThanException<T>(string name, string msg, T value)
|
||||||
{
|
{
|
||||||
if (msg == null)
|
if (msg == null) return new ArgumentException($"Value must be less than {value}", name);
|
||||||
throw new ArgumentException($"Value must be less than {value}", name);
|
else return new ArgumentException(msg, name);
|
||||||
else
|
|
||||||
throw new ArgumentException(msg, name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user