You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ParameterBuilder.cs 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Collections.Generic;
  5. namespace Discord.Commands.Builders
  6. {
  7. public class ParameterBuilder
  8. {
  9. private readonly List<ParameterPreconditionAttribute> _preconditions;
  10. private readonly List<Attribute> _attributes;
  11. public CommandBuilder Command { get; }
  12. public string Name { get; internal set; }
  13. public Type ParameterType { get; internal set; }
  14. public TypeReader TypeReader { get; set; }
  15. public bool IsOptional { get; set; }
  16. public bool IsRemainder { get; set; }
  17. public bool IsMultiple { get; set; }
  18. public object DefaultValue { get; set; }
  19. public string Summary { get; set; }
  20. public IReadOnlyList<ParameterPreconditionAttribute> Preconditions => _preconditions;
  21. public IReadOnlyList<Attribute> Attributes => _attributes;
  22. //Automatic
  23. internal ParameterBuilder(CommandBuilder command)
  24. {
  25. _preconditions = new List<ParameterPreconditionAttribute>();
  26. _attributes = new List<Attribute>();
  27. Command = command;
  28. }
  29. //User-defined
  30. internal ParameterBuilder(CommandBuilder command, string name, Type type)
  31. : this(command)
  32. {
  33. Discord.Preconditions.NotNull(name, nameof(name));
  34. Name = name;
  35. SetType(type);
  36. }
  37. internal void SetType(Type type)
  38. {
  39. TypeReader = GetReader(type);
  40. if (type.GetTypeInfo().IsValueType)
  41. DefaultValue = Activator.CreateInstance(type);
  42. else if (type.IsArray)
  43. type = ParameterType.GetElementType();
  44. ParameterType = type;
  45. }
  46. private TypeReader GetReader(Type type)
  47. {
  48. var commands = Command.Module.Service;
  49. if (type.GetTypeInfo().GetCustomAttribute<NamedArgumentTypeAttribute>() != null)
  50. {
  51. IsRemainder = true;
  52. var reader = commands.GetTypeReaders(type)?.FirstOrDefault().Value;
  53. if (reader == null)
  54. {
  55. Type readerType;
  56. try
  57. {
  58. readerType = typeof(NamedArgumentTypeReader<>).MakeGenericType(new[] { type });
  59. }
  60. catch (ArgumentException ex)
  61. {
  62. throw new InvalidOperationException($"Parameter type '{type.Name}' for command '{Command.Name}' must be a class with a public parameterless constructor to use as a NamedArgumentType.", ex);
  63. }
  64. reader = (TypeReader)Activator.CreateInstance(readerType, new[] { commands });
  65. commands.AddTypeReader(type, reader);
  66. }
  67. return reader;
  68. }
  69. var readers = commands.GetTypeReaders(type);
  70. if (readers != null)
  71. return readers.FirstOrDefault().Value;
  72. else
  73. return commands.GetDefaultTypeReader(type);
  74. }
  75. public ParameterBuilder WithSummary(string summary)
  76. {
  77. Summary = summary;
  78. return this;
  79. }
  80. public ParameterBuilder WithDefault(object defaultValue)
  81. {
  82. DefaultValue = defaultValue;
  83. return this;
  84. }
  85. public ParameterBuilder WithIsOptional(bool isOptional)
  86. {
  87. IsOptional = isOptional;
  88. return this;
  89. }
  90. public ParameterBuilder WithIsRemainder(bool isRemainder)
  91. {
  92. IsRemainder = isRemainder;
  93. return this;
  94. }
  95. public ParameterBuilder WithIsMultiple(bool isMultiple)
  96. {
  97. IsMultiple = isMultiple;
  98. return this;
  99. }
  100. public ParameterBuilder AddAttributes(params Attribute[] attributes)
  101. {
  102. _attributes.AddRange(attributes);
  103. return this;
  104. }
  105. public ParameterBuilder AddPrecondition(ParameterPreconditionAttribute precondition)
  106. {
  107. _preconditions.Add(precondition);
  108. return this;
  109. }
  110. internal ParameterInfo Build(CommandInfo info)
  111. {
  112. if ((TypeReader ?? (TypeReader = GetReader(ParameterType))) == null)
  113. throw new InvalidOperationException($"No type reader found for type {ParameterType.Name}, one must be specified");
  114. return new ParameterInfo(this, info, Command.Module.Service);
  115. }
  116. }
  117. }