-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAcceptableValueListSorted.cs
65 lines (58 loc) · 2.28 KB
/
AcceptableValueListSorted.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using BepInEx.Configuration;
using System;
namespace LCMaxSoundFix
{
public class AcceptableValueListSorted<T> : AcceptableValueList<T> where T : IEquatable<T>, IComparable<T>
{
public AcceptableValueListSorted(params T[] acceptableValues)
: base(acceptableValues)
{
Array.Sort(AcceptableValues);
}
public override object Clamp(object value)
{
if (value is IComparable<T>)
{
int closestIndex = 0;
T comparableValue = (T)value;
for (int i = 0; i < AcceptableValues.Length; ++i)
{
int comp = AcceptableValues[i].CompareTo(comparableValue);
if (comp == 0)
{
// We found a perfect match!
closestIndex = i;
break;
}
else if (comp < 0)
{
closestIndex = i;
}
else if (comp > 0)
{
if (i == 0)
{
// We are at the start of the array, so we cannot compare to the previous value.
closestIndex = i;
break;
}
// The dynamic keyword isn't supported by IL2CPP, so check the type manually...
if (typeof(T) == typeof(int))
{
int currDiff = Math.Abs(Convert.ToInt32(AcceptableValues[i]) - (int)value);
int prevDiff = Math.Abs(Convert.ToInt32(AcceptableValues[i - 1]) - (int)value);
// Returns the closest value in the list.
closestIndex = currDiff < prevDiff ? i : i - 1;
break;
}
// By default, return the previous value.
break;
}
}
return AcceptableValues[closestIndex];
}
// Returns the matching value or the first value by default.
return base.Clamp(value);
}
}
}