|
| 1 | +using BepInEx; |
| 2 | +using BepInEx.Configuration; |
| 3 | +using System; |
| 4 | +using System.Collections; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | + |
| 9 | +namespace DistanceModConfigurationManager |
| 10 | +{ |
| 11 | + internal sealed class ConfigSettingEntry : SettingEntryBase |
| 12 | + { |
| 13 | + public ConfigEntryBase Entry { get; } |
| 14 | + |
| 15 | + public ConfigSettingEntry(ConfigEntryBase entry, BaseUnityPlugin owner) |
| 16 | + { |
| 17 | + Entry = entry; |
| 18 | + |
| 19 | + DispName = entry.Definition.Key; |
| 20 | + Category = entry.Definition.Section; |
| 21 | + Description = entry.Description?.Description; |
| 22 | + |
| 23 | + var converter = TomlTypeConverter.GetConverter(entry.SettingType); |
| 24 | + if (converter != null) |
| 25 | + { |
| 26 | + ObjToStr = o => converter.ConvertToString(o, entry.SettingType); |
| 27 | + StrToObj = s => converter.ConvertToObject(s, entry.SettingType); |
| 28 | + } |
| 29 | + |
| 30 | + var values = entry.Description?.AcceptableValues; |
| 31 | + if (values != null) |
| 32 | + GetAcceptableValues(values); |
| 33 | + |
| 34 | + DefaultValue = entry.DefaultValue; |
| 35 | + |
| 36 | + SetFromAttributes(entry.Description?.Tags, owner); |
| 37 | + } |
| 38 | + |
| 39 | + private void GetAcceptableValues(AcceptableValueBase values) |
| 40 | + { |
| 41 | + var t = values.GetType(); |
| 42 | + var listProp = t.GetProperty(nameof(AcceptableValueList<bool>.AcceptableValues), BindingFlags.Instance | BindingFlags.Public); |
| 43 | + if (listProp != null) |
| 44 | + { |
| 45 | + AcceptableValues = ((IEnumerable)listProp.GetValue(values, null)).Cast<object>().ToArray(); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + var minProp = t.GetProperty(nameof(AcceptableValueRange<bool>.MinValue), BindingFlags.Instance | BindingFlags.Public); |
| 50 | + var maxProp = t.GetProperty(nameof(AcceptableValueRange<bool>.MaxValue), BindingFlags.Instance | BindingFlags.Public); |
| 51 | + if (minProp != null && maxProp != null) |
| 52 | + { |
| 53 | + AcceptableValueRange = new KeyValuePair<object, object>(minProp.GetValue(values, null), maxProp.GetValue(values, null)); |
| 54 | + ShowRangeAsPercent = (AcceptableValueRange.Key.Equals(0) || AcceptableValueRange.Key.Equals(1)) && AcceptableValueRange.Value.Equals(100) || AcceptableValueRange.Key.Equals(0f) && AcceptableValueRange.Value.Equals(1f); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public override Type SettingType => Entry.SettingType; |
| 60 | + |
| 61 | + public override object Get() |
| 62 | + { |
| 63 | + return Entry.BoxedValue; |
| 64 | + } |
| 65 | + |
| 66 | + protected override void SetValue(object newVal) |
| 67 | + { |
| 68 | + Entry.BoxedValue = newVal; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments