|
1 | | -namespace Obsidian.API.Commands.ArgumentParsers; |
| 1 | +using System.Numerics; |
2 | 2 |
|
3 | | -public sealed class SignedIntArgumentParser : BaseArgumentParser<int> |
| 3 | +namespace Obsidian.API.Commands.ArgumentParsers; |
| 4 | + |
| 5 | +public abstract partial class NumericArgumentParser<TNumber> : BaseArgumentParser<TNumber> where TNumber : struct, |
| 6 | + IConvertible, |
| 7 | + IMinMaxValue<TNumber>, |
| 8 | + INumber<TNumber> |
4 | 9 | { |
5 | | - public SignedIntArgumentParser() : base(3, "brigadier:integer") { } |
6 | | - public override bool TryParseArgument(string input, CommandContext ctx, out int result) |
7 | | - => int.TryParse(input, out result); |
| 10 | + private static Type NumberType => typeof(TNumber); |
| 11 | + public NumberFlags Flags { get; private set; } |
| 12 | + |
| 13 | + public TNumber Min { get; } |
| 14 | + |
| 15 | + public TNumber Max { get; } |
| 16 | + |
| 17 | + protected NumericArgumentParser(TNumber min, TNumber max) |
| 18 | + { |
| 19 | + this.Min = min; |
| 20 | + this.Max = max; |
| 21 | + |
| 22 | + if (min != TNumber.MinValue) |
| 23 | + this.Flags |= NumberFlags.HasMinValue; |
| 24 | + if (max != TNumber.MaxValue) |
| 25 | + this.Flags |= NumberFlags.HasMaxValue; |
| 26 | + } |
| 27 | + |
| 28 | + public override void Write(INetStreamWriter writer) |
| 29 | + { |
| 30 | + base.Write(writer); |
| 31 | + |
| 32 | + writer.WriteByte((sbyte)this.Flags); |
| 33 | + |
| 34 | + this.WriteNumbers(writer); |
| 35 | + } |
| 36 | + |
| 37 | + private void WriteNumbers(INetStreamWriter writer) |
| 38 | + { |
| 39 | + if (NumberType == typeof(int)) |
| 40 | + this.WriteAsInt(writer); |
| 41 | + else if (NumberType == typeof(double)) |
| 42 | + this.WriteAsDouble(writer); |
| 43 | + else if (NumberType == typeof(float)) |
| 44 | + this.WriteAsSingle(writer); |
| 45 | + else if (NumberType == typeof(long)) |
| 46 | + this.WriteAsLong(writer); |
| 47 | + } |
8 | 48 | } |
9 | 49 |
|
10 | | -public sealed class BoolArgumentParser : BaseArgumentParser<bool> |
| 50 | +[ArgumentParser("brigadier:integer")] |
| 51 | +public sealed partial class SignedIntArgumentParser : NumericArgumentParser<int> |
11 | 52 | { |
12 | | - public BoolArgumentParser() : base(0, "brigadier:bool") { } |
13 | | - public override bool TryParseArgument(string input, CommandContext ctx, out bool result) |
14 | | - => bool.TryParse(input, out result); |
| 53 | + public SignedIntArgumentParser() : base(int.MinValue, int.MaxValue) { } |
| 54 | + public SignedIntArgumentParser(int min, int max) : base(min, max) { } |
| 55 | + |
| 56 | + public override bool TryParseArgument(string input, CommandContext ctx, out int result) |
| 57 | + => int.TryParse(input, out result); |
15 | 58 | } |
16 | 59 |
|
17 | | -public sealed class UnsignedByteArgumentParser : BaseArgumentParser<byte> |
| 60 | +[ArgumentParser("brigadier:integer")] |
| 61 | +public sealed partial class UnsignedByteArgumentParser : NumericArgumentParser<byte> |
18 | 62 | { |
19 | | - public UnsignedByteArgumentParser() : base(3, "brigadier:integer") { } |
| 63 | + public UnsignedByteArgumentParser() : base(byte.MinValue, byte.MaxValue) { } |
| 64 | + public UnsignedByteArgumentParser(byte min, byte max) : base(min, max) { } |
| 65 | + |
20 | 66 | public override bool TryParseArgument(string input, CommandContext ctx, out byte result) |
21 | 67 | => byte.TryParse(input, out result); |
22 | 68 | } |
23 | 69 |
|
24 | | -public sealed class SignedByteArgumentParser : BaseArgumentParser<sbyte> |
| 70 | +[ArgumentParser("brigadier:integer")] |
| 71 | +public sealed partial class SignedByteArgumentParser : NumericArgumentParser<sbyte> |
25 | 72 | { |
26 | | - public SignedByteArgumentParser() : base(3, "brigadier:integer") { } |
| 73 | + public SignedByteArgumentParser() : base(sbyte.MinValue, sbyte.MaxValue) { } |
| 74 | + public SignedByteArgumentParser(sbyte min, sbyte max) : base(min, max) { } |
| 75 | + |
27 | 76 | public override bool TryParseArgument(string input, CommandContext ctx, out sbyte result) |
28 | 77 | => sbyte.TryParse(input, out result); |
29 | 78 | } |
30 | 79 |
|
31 | | -public sealed class SignedShortArgumentParser : BaseArgumentParser<short> |
| 80 | +[ArgumentParser("brigadier:integer")] |
| 81 | +public sealed partial class SignedShortArgumentParser : NumericArgumentParser<short> |
32 | 82 | { |
33 | | - public SignedShortArgumentParser() : base(3, "brigadier:integer") { } |
| 83 | + public SignedShortArgumentParser() : base(short.MinValue, short.MaxValue) { } |
| 84 | + public SignedShortArgumentParser(short min, short max) : base(min, max) { } |
| 85 | + |
34 | 86 | public override bool TryParseArgument(string input, CommandContext ctx, out short result) |
35 | 87 | => short.TryParse(input, out result); |
36 | 88 | } |
37 | 89 |
|
38 | | -public sealed class UnsignedShortArgumentParser : BaseArgumentParser<ushort> |
| 90 | +[ArgumentParser("brigadier:integer")] |
| 91 | +public sealed partial class UnsignedShortArgumentParser : NumericArgumentParser<ushort> |
39 | 92 | { |
40 | | - public UnsignedShortArgumentParser() : base(3, "brigadier:integer") { } |
| 93 | + public UnsignedShortArgumentParser() : base(ushort.MinValue, ushort.MaxValue) { } |
| 94 | + public UnsignedShortArgumentParser(ushort min, ushort max) : base(min, max) { } |
| 95 | + |
41 | 96 | public override bool TryParseArgument(string input, CommandContext ctx, out ushort result) |
42 | 97 | => ushort.TryParse(input, out result); |
43 | 98 | } |
44 | 99 |
|
45 | | -public sealed class UnsignedIntArgumentParser : BaseArgumentParser<uint> |
| 100 | +[ArgumentParser("brigadier:integer")] |
| 101 | +public sealed partial class UnsignedIntArgumentParser : NumericArgumentParser<uint> |
46 | 102 | { |
47 | | - public UnsignedIntArgumentParser() : base(3, "brigadier:integer") { } |
| 103 | + public UnsignedIntArgumentParser() : base(uint.MinValue, uint.MaxValue) { } |
| 104 | + public UnsignedIntArgumentParser(uint min, uint max) : base(min, max) { } |
| 105 | + |
48 | 106 | public override bool TryParseArgument(string input, CommandContext ctx, out uint result) |
49 | 107 | => uint.TryParse(input, out result); |
50 | 108 | } |
51 | 109 |
|
52 | | -public sealed class SignedLongArgumentParser : BaseArgumentParser<long> |
| 110 | +[ArgumentParser("brigadier:long")] |
| 111 | +public sealed partial class SignedLongArgumentParser : NumericArgumentParser<long> |
53 | 112 | { |
54 | | - public SignedLongArgumentParser() : base(3, "brigadier:long") { } |
| 113 | + public SignedLongArgumentParser() : base(long.MinValue, long.MaxValue) { } |
| 114 | + public SignedLongArgumentParser(long min, long max) : base(min, max) { } |
| 115 | + |
55 | 116 | public override bool TryParseArgument(string input, CommandContext ctx, out long result) |
56 | 117 | => long.TryParse(input, out result); |
57 | 118 | } |
58 | 119 |
|
59 | | -public sealed class UnsignedLongArgumentParser : BaseArgumentParser<ulong> |
| 120 | +[ArgumentParser("brigadier:long")] |
| 121 | +public sealed partial class UnsignedLongArgumentParser : NumericArgumentParser<ulong> |
60 | 122 | { |
61 | | - public UnsignedLongArgumentParser() : base(4, "brigadier:long") { } |
| 123 | + public UnsignedLongArgumentParser() : base(ulong.MinValue, ulong.MaxValue) { } |
| 124 | + public UnsignedLongArgumentParser(ulong min, ulong max) : base(min, max) { } |
| 125 | + |
62 | 126 | public override bool TryParseArgument(string input, CommandContext ctx, out ulong result) |
63 | 127 | => ulong.TryParse(input, out result); |
64 | 128 | } |
65 | 129 |
|
66 | | -public sealed class FloatArgumentParser : BaseArgumentParser<float> |
| 130 | +[ArgumentParser("brigadier:float")] |
| 131 | +public sealed partial class FloatArgumentParser : NumericArgumentParser<float> |
67 | 132 | { |
68 | | - public FloatArgumentParser() : base(1, "brigadier:float") { } |
| 133 | + public FloatArgumentParser() : base(float.MinValue, float.MaxValue) { } |
| 134 | + public FloatArgumentParser(float min, float max) : base(min, max) { } |
| 135 | + |
69 | 136 | public override bool TryParseArgument(string input, CommandContext ctx, out float result) |
70 | 137 | => float.TryParse(input, out result); |
71 | 138 | } |
72 | | -public sealed class DoubleArgumentParser : BaseArgumentParser<double> |
| 139 | + |
| 140 | +[ArgumentParser("brigadier:double")] |
| 141 | +public sealed partial class DoubleArgumentParser : NumericArgumentParser<double> |
73 | 142 | { |
74 | | - public DoubleArgumentParser() : base(2, "brigadier:double") { } |
| 143 | + public DoubleArgumentParser() : base(double.MinValue, double.MaxValue) { } |
| 144 | + public DoubleArgumentParser(double min, double max) : base(min, max) { } |
| 145 | + |
75 | 146 | public override bool TryParseArgument(string input, CommandContext ctx, out double result) |
76 | 147 | => double.TryParse(input, out result); |
77 | 148 | } |
78 | | -public sealed class DecimalArgumentParser : BaseArgumentParser<decimal> |
| 149 | + |
| 150 | +[ArgumentParser("brigadier:integer")] |
| 151 | +public sealed partial class DecimalArgumentParser : NumericArgumentParser<decimal> |
79 | 152 | { |
80 | | - public DecimalArgumentParser() : base(3, "brigadier:integer") { } |
| 153 | + public DecimalArgumentParser() : base(decimal.MinValue, decimal.MaxValue) { } |
| 154 | + public DecimalArgumentParser(decimal min, decimal max) : base(min, max) { } |
| 155 | + |
81 | 156 | public override bool TryParseArgument(string input, CommandContext ctx, out decimal result) |
82 | 157 | => decimal.TryParse(input, out result); |
83 | 158 | } |
| 159 | + |
| 160 | +[Flags] |
| 161 | +public enum NumberFlags : byte |
| 162 | +{ |
| 163 | + None, |
| 164 | + HasMinValue = 1, |
| 165 | + HasMaxValue |
| 166 | +} |
0 commit comments