forked from Biotronic/TweakScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemberUpdater.cs
196 lines (181 loc) · 6.15 KB
/
MemberUpdater.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
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
using System;
using System.Reflection;
using UnityEngine;
namespace TweakScale
{
public class MemberUpdater
{
private readonly object _object;
private readonly FieldInfo _field;
private readonly PropertyInfo _property;
private readonly UI_FloatRange _floatRange;
private const BindingFlags LookupFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
static void ConcatSafely(string name, Func<string> a, ref string result)
{
try
{
result += ' ' + name + " = ";
result += a() + ' ';
}
catch
{
result += "EXCEPTION";
}
}
public static MemberUpdater Create(object obj, string name)
{
if (obj == null || name == null)
{
return null;
}
var objectType = obj.GetType();
var field = objectType.GetField(name, LookupFlags);
var property = objectType.GetProperty(name, LookupFlags);
UI_FloatRange floatRange = null;
BaseFieldList fields;
if (obj is PartModule)
{
fields = (obj as PartModule).Fields;
try
{
var fieldData = fields[name];
if ((object)fieldData != null)
{
var ctrl = fieldData.uiControlEditor;
if (ctrl is UI_FloatRange)
{
floatRange = ctrl as UI_FloatRange;
}
}
}
catch (Exception)
{
string debuglog = "===============================================\r\n";
ConcatSafely("objectType", () => { return objectType.ToString(); }, ref debuglog);
ConcatSafely("name", () => { return name; }, ref debuglog);
ConcatSafely("fields.ReflectType", () => { return fields.ReflectedType.ToString().ToString(); }, ref debuglog);
ConcatSafely("fields.Count", () => { return fields.Count.ToString(); }, ref debuglog);
ConcatSafely("fields.Count",
() =>
{
string acc = "";
foreach (var f in fields)
ConcatSafely("field", () => {return f.ToString_rec(1); }, ref acc);
return acc;
}, ref debuglog);
Tools.LogWf(debuglog);
}
}
if (property != null && property.GetIndexParameters().Length > 0)
{
Tools.LogWf("Property {0} on {1} requires indices, which TweakScale currently does not support.", name, objectType.Name);
return null;
}
if (field == null && property == null)
{
Tools.LogWf("No valid member found for {0} in {1}", name, objectType.Name);
return null;
}
return new MemberUpdater(obj, field, property, floatRange);
}
private MemberUpdater(object obj, FieldInfo field, PropertyInfo property, UI_FloatRange floatRange)
{
_object = obj;
_field = field;
_property = property;
_floatRange = floatRange;
}
public object Value
{
get
{
if (_field != null)
{
return _field.GetValue(_object);
}
if (_property != null)
{
return _property.GetValue(_object, null);
}
return null;
}
}
public Type MemberType
{
get
{
if (_field != null)
{
return _field.FieldType;
}
if (_property != null)
{
return _property.PropertyType;
}
return null;
}
}
public void Set(object value)
{
if (value.GetType() != MemberType && MemberType.GetInterface("IConvertible") != null &&
value.GetType().GetInterface("IConvertible") != null)
{
value = Convert.ChangeType(value, MemberType);
}
if (_field != null)
{
_field.SetValue(_object, value);
}
else if (_property != null)
{
_property.SetValue(_object, value, null);
}
}
public void Scale(double scale, MemberUpdater source)
{
if (_field == null && _property == null)
{
return;
}
var newValue = source.Value;
if (MemberType == typeof(float))
{
RescaleFloatRange((float)scale);
Set((float)newValue * (float)scale);
}
else if (MemberType == typeof(double))
{
RescaleFloatRange((float)scale);
Set((double)newValue * scale);
}
else if (MemberType == typeof(Vector3))
{
Set((Vector3)newValue * (float)scale);
}
}
private void RescaleFloatRange(float factor)
{
if ((object)_floatRange == null)
{
return;
}
_floatRange.maxValue *= factor;
_floatRange.minValue *= factor;
_floatRange.stepIncrement *= factor;
}
public string Name {
get
{
if (_field != null)
{
return _field.Name;
}
if (_property != null)
{
return _property.Name;
}
return null;
}
}
}
}