AcceptableValueRange.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. namespace BepInEx.Configuration
  3. {
  4. /// <summary>
  5. /// Specify the range of acceptable values for a setting.
  6. /// </summary>
  7. public class AcceptableValueRange<T> : AcceptableValueBase where T : IComparable
  8. {
  9. /// <param name="minValue">Lowest acceptable value</param>
  10. /// <param name="maxValue">Highest acceptable value</param>
  11. public AcceptableValueRange(T minValue, T maxValue) : base(typeof(T))
  12. {
  13. if (maxValue == null)
  14. throw new ArgumentNullException(nameof(maxValue));
  15. if (minValue == null)
  16. throw new ArgumentNullException(nameof(minValue));
  17. if (minValue.CompareTo(maxValue) >= 0)
  18. throw new ArgumentException($"{nameof(minValue)} has to be lower than {nameof(maxValue)}");
  19. MinValue = minValue;
  20. MaxValue = maxValue;
  21. }
  22. /// <summary>
  23. /// Lowest acceptable value
  24. /// </summary>
  25. public virtual T MinValue { get; }
  26. /// <summary>
  27. /// Highest acceptable value
  28. /// </summary>
  29. public virtual T MaxValue { get; }
  30. /// <inheritdoc />
  31. public override object Clamp(object value)
  32. {
  33. if (MinValue.CompareTo(value) > 0)
  34. return MinValue;
  35. if (MaxValue.CompareTo(value) < 0)
  36. return MaxValue;
  37. return value;
  38. }
  39. /// <inheritdoc />
  40. public override bool IsValid(object value)
  41. {
  42. return MinValue.CompareTo(value) <= 0 && MaxValue.CompareTo(value) >= 0;
  43. }
  44. /// <inheritdoc />
  45. public override string ToDescriptionString()
  46. {
  47. return $"# Acceptable value range: From {MinValue} to {MaxValue}";
  48. }
  49. }
  50. }