using System;
using System.Linq;
namespace BepInEx.Configuration
{
///
/// Specify the list of acceptable values for a setting.
///
public sealed class AcceptableValueList : AcceptableValueBase where T : IEquatable
{
///
/// List of values that a setting can take.
///
public T[] AcceptableValues { get; }
///
/// Specify the list of acceptable values for a setting.
/// If the setting does not equal any of the values, it will be set to the first one.
///
public AcceptableValueList(params T[] acceptableValues) : base(typeof(T))
{
if (acceptableValues == null) throw new ArgumentNullException(nameof(acceptableValues));
if (acceptableValues.Length == 0) throw new ArgumentException("At least one acceptable value is needed", nameof(acceptableValues));
AcceptableValues = acceptableValues;
}
///
public override object Clamp(object value)
{
if (IsValid(value))
return value;
return AcceptableValues[0];
}
///
public override bool IsValid(object value)
{
return value is T v && AcceptableValues.Any(x => x.Equals(v));
}
///
public override string ToSerializedString()
{
return "# Acceptable values: " + string.Join(", ", AcceptableValues.Select(x => x.ToString()).ToArray());
}
}
}