Repository navigation
Expand file tree
/
Copy pathConfigSettingEntry.cs
More file actions
226 lines (192 loc) · 8.72 KB
/
Copy pathConfigSettingEntry.cs
File metadata and controls
226 lines (192 loc) · 8.72 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// 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;
using UnityEngine;
namespace ConfigurationManager
{
internal sealed class ConfigSettingEntry : SettingEntryBase
{
public ConfigEntryBase Entry { get; }
private readonly ConfigSynchronizationInfo synchronizationInfo;
private readonly List<DynamicAttributeSource> dynamicAttributeSources = new List<DynamicAttributeSource>();
private int synchronizationFrame = -1;
private ConfigSynchronizationState synchronizationState;
private readonly GUIContent synchronizationContent = new GUIContent();
private bool lastServerControlled;
private Color lastSymbolColor;
internal bool HasDynamicAttributes => dynamicAttributeSources.Count != 0;
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);
InitializeDynamicAttributeSources(entry.Description?.Tags);
synchronizationInfo = ConfigSynchronizationInfo.Create(entry);
}
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);
var maxProp = t.GetProperty(nameof(AcceptableValueRange<bool>.MaxValue), BindingFlags.Instance | BindingFlags.Public);
if (minProp != null && maxProp != null)
AcceptableValueRange = new KeyValuePair<object, object>(minProp.GetValue(values, null), maxProp.GetValue(values, null));
}
}
private void InitializeDynamicAttributeSources(object[] tags)
{
if (tags == null)
return;
foreach (object tag in tags)
{
if (tag == null)
continue;
Type tagType = tag.GetType();
if (tagType.Name != "ConfigurationManagerAttributes")
continue;
PropertyInfo readOnlyProperty = tagType.GetProperty(nameof(ReadOnly), BindingFlags.Instance | BindingFlags.Public);
FieldInfo readOnlyField = tagType.GetField(nameof(ReadOnly), BindingFlags.Instance | BindingFlags.Public);
PropertyInfo browsableProperty = tagType.GetProperty(nameof(Browsable), BindingFlags.Instance | BindingFlags.Public);
FieldInfo browsableField = tagType.GetField(nameof(Browsable), BindingFlags.Instance | BindingFlags.Public);
if (readOnlyProperty != null || readOnlyField != null || browsableProperty != null || browsableField != null)
{
dynamicAttributeSources.Add(new DynamicAttributeSource(
tag,
tagType,
readOnlyProperty,
readOnlyField,
browsableProperty,
browsableField));
}
}
}
internal override bool RefreshDynamicAttributes()
{
bool? previousReadOnly = ReadOnly;
bool? previousBrowsable = Browsable;
foreach (DynamicAttributeSource source in dynamicAttributeSources)
{
try
{
if (source.TryGetReadOnly(out bool readOnly))
ReadOnly = readOnly;
if (source.TryGetBrowsable(out bool browsable))
Browsable = browsable;
}
catch (Exception ex)
{
ConfigurationManager.LogInfo($"Failed to refresh dynamic attributes from {source.SourceType.FullName} - {ex.Message}");
}
}
return previousBrowsable != Browsable ||
(previousReadOnly != ReadOnly && ConfigurationManager._readOnlyStyle?.Value == ConfigurationManager.ReadOnlyStyle.Hidden);
}
private sealed class DynamicAttributeSource
{
private readonly object source;
private readonly PropertyInfo readOnlyProperty;
private readonly FieldInfo readOnlyField;
private readonly PropertyInfo browsableProperty;
private readonly FieldInfo browsableField;
internal DynamicAttributeSource(
object source,
Type sourceType,
PropertyInfo readOnlyProperty,
FieldInfo readOnlyField,
PropertyInfo browsableProperty,
FieldInfo browsableField)
{
this.source = source;
SourceType = sourceType;
this.readOnlyProperty = readOnlyProperty;
this.readOnlyField = readOnlyField;
this.browsableProperty = browsableProperty;
this.browsableField = browsableField;
}
internal Type SourceType { get; }
internal bool TryGetReadOnly(out bool value)
{
return TryGetBoolean(readOnlyProperty, readOnlyField, out value);
}
internal bool TryGetBrowsable(out bool value)
{
return TryGetBoolean(browsableProperty, browsableField, out value);
}
private bool TryGetBoolean(PropertyInfo property, FieldInfo field, out bool value)
{
object memberValue = property != null ? property.GetValue(source, null) : field?.GetValue(source);
if (memberValue is bool booleanValue)
{
value = booleanValue;
return true;
}
value = false;
return false;
}
}
public override Type SettingType => Entry.SettingType;
public override object Get()
{
return Entry.BoxedValue;
}
protected override void SetValue(object newVal)
{
Entry.BoxedValue = newVal;
}
internal ConfigSynchronizationState GetSynchronizationState()
{
if (synchronizationFrame != Time.frameCount)
{
synchronizationState = synchronizationInfo.GetState();
synchronizationFrame = Time.frameCount;
}
return synchronizationState;
}
internal GUIContent GetSynchronizationContent(ConfigSynchronizationState state, Color symbolColor)
{
if (string.IsNullOrEmpty(synchronizationContent.text) || lastServerControlled != state.IsServerControlled || !lastSymbolColor.Equals(symbolColor))
{
lastServerControlled = state.IsServerControlled;
lastSymbolColor = symbolColor;
synchronizationContent.text = $"<color=#{ColorUtility.ToHtmlStringRGBA(symbolColor)}>{(state.IsServerControlled ? "S" : "C")}</color>";
}
if (synchronizationContent.tooltip != state.Tooltip)
synchronizationContent.tooltip = state.Tooltip;
return synchronizationContent;
}
internal bool ToggleSynchronizationPolicy()
{
// This is only a presentation cache, never an authorization cache.
synchronizationFrame = -1;
return synchronizationInfo.TogglePolicy();
}
internal bool ShouldBeHidden()
{
return ConfigurationManager.hiddenSettings.Value.Contains($"{PluginInfo.GUID}={Entry.Definition.Section}={Entry.Definition.Key}");
}
}
}