forked from aedenthorn/ValheimMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigSettingEntry.cs
76 lines (64 loc) · 2.8 KB
/
ConfigSettingEntry.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
66
67
68
69
70
71
72
73
74
75
76
// Based on code made by MarC0 / ManlyMarco
// Copyright 2018 GNU General Public License v3.0
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BepInEx;
using BepInEx.Configuration;
namespace ConfigurationManager
{
internal sealed class ConfigSettingEntry : SettingEntryBase
{
public ConfigEntryBase Entry { get; }
public ConfigSettingEntry(ConfigEntryBase entry, BaseUnityPlugin owner)
{
Entry = entry;
DispName = entry.Definition.Key;
Category = entry.Definition.Section;
Description = entry.Description?.Description;
var converter = TomlTypeConverter.GetConverter(entry.SettingType);
if (converter != null)
{
ObjToStr = o => converter.ConvertToString(o, entry.SettingType);
StrToObj = s => converter.ConvertToObject(s, entry.SettingType);
}
var values = entry.Description?.AcceptableValues;
if (values != null)
GetAcceptableValues(values);
DefaultValue = entry.DefaultValue;
SetFromAttributes(entry.Description?.Tags, owner);
}
private void GetAcceptableValues(AcceptableValueBase values)
{
var t = values.GetType();
var listProp = t.GetProperty(nameof(AcceptableValueList<bool>.AcceptableValues), BindingFlags.Instance | BindingFlags.Public);
if (listProp != null)
{
AcceptableValues = ((IEnumerable)listProp.GetValue(values, null)).Cast<object>().ToArray();
}
else
{
var minProp = t.GetProperty(nameof(AcceptableValueRange<bool>.MinValue), BindingFlags.Instance | BindingFlags.Public);
if (minProp != null)
{
var maxProp = t.GetProperty(nameof(AcceptableValueRange<bool>.MaxValue), BindingFlags.Instance | BindingFlags.Public);
if (maxProp == null) throw new ArgumentNullException(nameof(maxProp));
AcceptableValueRange = new KeyValuePair<object, object>(minProp.GetValue(values, null), maxProp.GetValue(values, null));
ShowRangeAsPercent = (AcceptableValueRange.Key.Equals(0) || AcceptableValueRange.Key.Equals(1)) && AcceptableValueRange.Value.Equals(100) ||
AcceptableValueRange.Key.Equals(0f) && AcceptableValueRange.Value.Equals(1f);
}
}
}
public override Type SettingType => Entry.SettingType;
public override object Get()
{
return Entry.BoxedValue;
}
protected override void SetValue(object newVal)
{
Entry.BoxedValue = newVal;
}
}
}