forked from Biotronic/TweakScale
-
Notifications
You must be signed in to change notification settings - Fork 23
/
MemberUpdater.cs
250 lines (233 loc) · 8.24 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Reflection;
using System.Collections.Generic;
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 = "";
int numFields = fields.Count;
for (int i = 0; i < numFields; i++)
{
ConcatSafely("field", () => { return fields[i].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)
{
// handle special cases
if ((obj is PartModule) && (name == "inputResources" || name == "outputResources"))
return Create((obj as PartModule).resHandler, name);
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(int))
{
Set((int)Math.Round((int)(newValue) * scale));
}
else if (MemberType == typeof(Vector3))
{
Set((Vector3)newValue * (float)scale);
}
else if (MemberType == typeof(FloatCurve))
{
var curve = (newValue as FloatCurve).Curve;
var tmp = new AnimationCurve();
for (int i = 0; i < curve.length; i++)
{
var k = curve.keys[i];
k.value *= (float)scale;
k.inTangent *= (float)scale;
k.outTangent *= (float)scale;
tmp.AddKey(k);
}
(Value as FloatCurve).Curve = tmp;
}
else if (MemberType == typeof(List<ResourceRatio>))
{
var l = (newValue as List<ResourceRatio>);
//List<ResourceRatio> l2 = new List<ResourceRatio>();
for (int i=0; i<l.Count; i++)
{
var tmp = l[i];
tmp.Ratio *= scale;
(Value as List<ResourceRatio>)[i] = tmp;
}
}
else if (MemberType == typeof(ConversionRecipe))
{
var l = (newValue as ConversionRecipe);
ScaleResourceList(l.Inputs, scale);
ScaleResourceList(l.Outputs, scale);
ScaleResourceList(l.Requirements, scale);
}
}
public void ScaleResourceList(List<ResourceRatio> l, double scale)
{
for (int i = 0; i < l.Count; i++)
{
var tmp = l[i];
tmp.Ratio *= scale;
l[i] = tmp;
}
}
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;
}
}
}
}