forked from Biotronic/TweakScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScaleExponents.cs
356 lines (324 loc) · 13.1 KB
/
ScaleExponents.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TweakScale
{
[KSPAddon(KSPAddon.Startup.MainMenu, false)]
class ScaleExponentsLoader : RescalableRegistratorAddon
{
public override void OnStart()
{
ScaleExponents.LoadGlobalExponents();
}
}
public class ScaleExponents
{
private string _id;
private string _name;
private Dictionary<string, Tuple<string, bool>> _exponents;
private Dictionary<string, ScaleExponents> _children;
private static Dictionary<string, ScaleExponents> globalList;
private static bool globalListLoaded = false;
private const string exponentConfigName = "TWEAKSCALEEXPONENTS";
private static bool IsExponentBlock(ConfigNode node)
{
return node.name == exponentConfigName || node.name == "MODULE";
}
/// <summary>
/// Load all TWEAKSCALEEXPONENTS that are globally defined.
/// </summary>
public static void LoadGlobalExponents()
{
if (!globalListLoaded)
{
var tmp = GameDatabase.Instance.GetConfigs(exponentConfigName)
.Select(a => new ScaleExponents(a.config));
globalList = tmp
.GroupBy(a => a._id)
.Select(a => a.Aggregate((b, c) => Merge(b, c)))
.ToDictionary(a => a._id, a => a);
globalListLoaded = true;
}
}
/// <summary>
/// Creates modules copy of the ScaleExponents.
/// </summary>
/// <returns>A copy of the object on which the function is called.</returns>
private ScaleExponents Clone()
{
return new ScaleExponents(this);
}
private ScaleExponents(ScaleExponents source)
{
_id = source._id;
if (source == null)
{
_exponents = new Dictionary<string, Tuple<string, bool>>();
_children = new Dictionary<string, ScaleExponents>();
}
else
{
_exponents = source._exponents.Clone();
_children = source
._children
.Select(a => new KeyValuePair<string, ScaleExponents>(a.Key, a.Value.Clone()))
.ToDictionary(a => a.Key, a => a.Value);
}
}
private ScaleExponents(ConfigNode node, ScaleExponents source = null)
{
_id = IsExponentBlock(node) ? node.GetValue("name") : node.name;
_name = node.GetValue("name");
if (_id == null)
{
_id = "";
}
if (IsExponentBlock(node))
{
if (string.IsNullOrEmpty(_id))
{
_id = "Part";
_name = "Part";
}
}
_exponents = new Dictionary<string, Tuple<string, bool>>();
_children = new Dictionary<string, ScaleExponents>();
foreach (var value in node.values.OfType<ConfigNode.Value>().Where(a=>a.name != "name"))
{
if (value.name.StartsWith("!"))
{
_exponents[value.name.Substring(1)] = Tuple.Create(value.value, true);
}
else
{
_exponents[value.name] = Tuple.Create(value.value, false);
}
}
foreach (var childNode in node.nodes.OfType<ConfigNode>())
{
_children[childNode.name] = new ScaleExponents(childNode);
}
if (source != null)
{
Merge(this, source);
}
}
/// <summary>
/// Merge two ScaleExponents. All the values in <paramref name="source"/> that are not already present in <paramref name="destination"/> will be added to <paramref name="destination"/>
/// </summary>
/// <param name="destination">The ScaleExponents to update.</param>
/// <param name="source">The ScaleExponents to add to <paramref name="destination"/></param>
/// <returns>The updated exponentValue of <paramref name="destination"/>. Note that this exponentValue is also changed, so using the return exponentValue is not necessary.</returns>
public static ScaleExponents Merge(ScaleExponents destination, ScaleExponents source)
{
if (destination._id != source._id)
{
Tools.LogWf("Wrong merge target! A name {0}, B name {1}", destination._id, source._id);
}
foreach (var value in source._exponents)
{
if (!destination._exponents.ContainsKey(value.Key))
{
destination._exponents[value.Key] = value.Value;
}
}
foreach (var child in source._children)
{
if (destination._children.ContainsKey(child.Key))
{
Merge(destination._children[child.Key], child.Value);
}
else
{
destination._children[child.Key] = child.Value.Clone();
}
}
return destination;
}
/// <summary>
/// The names of all exponents this ScaleExponents set covers.
/// </summary>
public IEnumerable<string> Exponents
{
get
{
return _exponents.Keys;
}
}
/// <summary>
/// Rescales destination exponentValue according to its associated exponent.
/// </summary>
/// <param name="currentValue">The current exponentValue.</param>
/// <param name="baseValue">The unscaled exponentValue, gotten from the prefab.</param>
/// <param name="name">The name of the field.</param>
/// <param name="factor">The rescaling factor.</param>
/// <param name="relative">Whether to use relative or absolute scaling.</param>
/// <returns>The rescaled exponentValue.</returns>
public double Rescale(double currentValue, double baseValue, string name, ScalingFactor factor, bool relative = false)
{
if (!_exponents.ContainsKey(name))
{
Tools.LogWf("No exponent found for {0}.{1}", this._id, name);
return currentValue;
}
var exponentValue = _exponents[name].Item1;
if (exponentValue.Contains(','))
{
if (factor.index == -1)
{
Tools.LogWf("Value list used for freescale part exponent field {0}: {1}", name, exponentValue);
return currentValue;
}
var values = Tools.ConvertString(exponentValue, new double[] { });
if (values.Length == 0)
{
Tools.LogWf("No valid values found for {0}: {1}", name, exponentValue);
}
if (values.Length <= factor.index)
{
Tools.LogWf("Too few values given for {0}. Expected at least {1}, got {2}: {3}", name, factor.index + 1, values.Length, exponentValue);
}
return values[factor.index];
}
else
{
double exponent;
if (double.TryParse(exponentValue, out exponent))
{
if (relative)
{
return currentValue * Math.Pow(factor.relative.linear, exponent);
}
return baseValue * Math.Pow(factor.absolute.linear, exponent);
}
return currentValue;
}
}
/// <summary>
/// Rescale the field of <paramref name="obj"/> according to the exponents of the ScaleExponents and <paramref name="factor"/>.
/// </summary>
/// <param name="obj">The object to rescale.</param>
/// <param name="baseObj">The corresponding object in the prefab.</param>
/// <param name="factor">The new scale.</param>
public void UpdateFields(object obj, object baseObj, ScalingFactor factor)
{
if (obj is PartModule && obj.GetType().Name != _id)
{
Tools.LogWf("This ScaleExponent is intended for {0}, not {1}", _id, obj.GetType().Name);
return;
}
if (obj is IEnumerable)
{
UpdateEnumerable((IEnumerable)obj, (IEnumerable)baseObj, factor);
return;
}
foreach (var fieldName in Exponents)
{
var baseObjTmp = baseObj;
if (_exponents[fieldName].Item2)
{
baseObjTmp = null;
}
var value = FieldChanger<double>.CreateFromName(obj, fieldName);
if (value == null)
{
continue;
}
if (baseObjTmp == null)
{
// No prefab from which to grab values. Use relative scaling.
value.Value = Rescale(value.Value, value.Value, fieldName, factor, relative: true);
}
else
{
var baseValue = FieldChanger<double>.CreateFromName(baseObj, fieldName);
value.Value = Rescale(value.Value, baseValue.Value, fieldName, factor);
}
}
foreach (var _child in _children)
{
string childName = _child.Key;
var childObjField = FieldChanger<object>.CreateFromName(obj, childName);
if (childObjField != null)
{
var baseChildObjField = FieldChanger<object>.CreateFromName(baseObj, childName);
_child.Value.UpdateFields(childObjField.Value, baseChildObjField.Value, factor);
}
}
}
/// <summary>
/// For IEnumerables (arrays, lists, etc), we want to update the items, not the list.
/// </summary>
/// <param name="obj">The list whose items we want to update.</param>
/// <param name="baseObj">The corresponding list in the prefab.</param>
/// <param name="factor">The scaling factor.</param>
private void UpdateEnumerable(IEnumerable obj, IEnumerable baseObj, ScalingFactor factor)
{
IEnumerable other = baseObj;
if (baseObj == null || obj.StupidCount() != baseObj.StupidCount())
{
other = ((object)null).Repeat().Take(obj.StupidCount());
}
foreach (var item in obj.Zip(other))
{
if (!string.IsNullOrEmpty(_name) && _name != "*") // Operate on specific elements, not all.
{
var childName = item.Item1.GetType().GetField("name");
if (childName != null)
{
if (childName.FieldType != typeof(string) || (string)childName.GetValue(item.Item1) != _name)
{
continue;
}
}
}
UpdateFields(item.Item1, item.Item2, factor);
}
}
public static void UpdateObject(Part part, Part basePart, Dictionary<string, ScaleExponents> exps, ScalingFactor factor)
{
if (exps.ContainsKey("Part"))
{
exps["Part"].UpdateFields(part, basePart, factor);
}
var modulesAndExponents = part.Modules.Cast<PartModule>().Zip(basePart.Modules.Cast<PartModule>()).Join(exps, modules => modules.Item1.moduleName, exponents => exponents.Key, (modules, exponent) => Tuple.Create(modules, exponent.Value)).ToArray();
foreach (var modExp in modulesAndExponents)
{
modExp.Item2.UpdateFields(modExp.Item1.Item1, modExp.Item1.Item2, factor);
}
}
public static Dictionary<string, ScaleExponents> CreateExponentsForModule(ConfigNode node, Dictionary<string, ScaleExponents> parent)
{
var local = node.nodes
.OfType<ConfigNode>()
.Where(IsExponentBlock)
.Select(a => new ScaleExponents(a))
.ToDictionary(a => a._id);
foreach (var pExp in parent.Values)
{
if (local.ContainsKey(pExp._id))
{
Merge(local[pExp._id], pExp);
}
else
{
local[pExp._id] = pExp;
}
}
foreach (var gExp in globalList.Values)
{
if (local.ContainsKey(gExp._id))
{
Merge(local[gExp._id], gExp);
}
else
{
local[gExp._id] = gExp;
}
}
return local;
}
}
}