using System;
namespace BepInEx.Configuration
{
///
/// Specify the range of acceptable values for a setting.
///
public class AcceptableValueRange : AcceptableValueBase where T : IComparable
{
/// Lowest acceptable value
/// Highest acceptable value
public AcceptableValueRange(T minValue, T maxValue) : base(typeof(T))
{
if (maxValue == null)
throw new ArgumentNullException(nameof(maxValue));
if (minValue == null)
throw new ArgumentNullException(nameof(minValue));
if (minValue.CompareTo(maxValue) >= 0)
throw new ArgumentException($"{nameof(minValue)} has to be lower than {nameof(maxValue)}");
MinValue = minValue;
MaxValue = maxValue;
}
///
/// Lowest acceptable value
///
public virtual T MinValue { get; }
///
/// Highest acceptable value
///
public virtual T MaxValue { get; }
///
public override object Clamp(object value)
{
if (MinValue.CompareTo(value) > 0)
return MinValue;
if (MaxValue.CompareTo(value) < 0)
return MaxValue;
return value;
}
///
public override bool IsValid(object value)
{
return MinValue.CompareTo(value) <= 0 && MaxValue.CompareTo(value) >= 0;
}
///
public override string ToDescriptionString()
{
return $"# Acceptable value range: From {MinValue} to {MaxValue}";
}
}
}