-
Notifications
You must be signed in to change notification settings - Fork 4
/
Tools.cs
214 lines (202 loc) · 8.7 KB
/
Tools.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace StripSymmetry
{
/// <summary>
/// Various handy functions.
/// </summary>
public static class Tools
{
/// <summary>
/// Clamps the exponentValue <paramref name="x"/> between <paramref name="min"/> and <paramref name="max"/>.
/// </summary>
/// <param name="x">The exponentValue to start out with.</param>
/// <param name="min">The minimum exponentValue to clamp to.</param>
/// <param name="max">The maximum exponentValue to clamp to.</param>
/// <returns>The exponentValue closest to <paramref name="x"/> that's no less than <paramref name="min"/> and no more than <paramref name="max"/>.</returns>
public static float Clamp(float x, float min, float max)
{
return x < min ? min : x > max ? max : x;
}
/// <summary>
/// Gets the exponentValue in <paramref name="values"/> that's closest to <paramref name="x"/>.
/// </summary>
/// <param name="x">The exponentValue to find.</param>
/// <param name="values">The values to look through.</param>
/// <returns>The exponentValue in <paramref name="values"/> that's closest to <paramref name="x"/>.</returns>
public static float Closest(float x, IEnumerable<float> values)
{
var minDistance = float.PositiveInfinity;
var result = float.NaN;
foreach (var value in values)
{
var tmpDistance = Math.Abs(value - x);
if (tmpDistance < minDistance)
{
result = value;
minDistance = tmpDistance;
}
}
return result;
}
/// <summary>
/// Finds the index of the exponentValue in <paramref name="values"/> that's closest to <paramref name="x"/>.
/// </summary>
/// <param name="x">The exponentValue to find.</param>
/// <param name="values">The values to look through.</param>
/// <returns>The index of the exponentValue in <paramref name="values"/> that's closest to <paramref name="x"/>.</returns>
public static int ClosestIndex(float x, IEnumerable<float> values)
{
var minDistance = float.PositiveInfinity;
int result = 0;
int idx = 0;
foreach (var value in values)
{
var tmpDistance = Math.Abs(value - x);
if (tmpDistance < minDistance)
{
result = idx;
minDistance = tmpDistance;
}
idx++;
}
return result;
}
/// <summary>
/// Writes destination log message to output_log.txt.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="args">The arguments to the format.</param>
public static void Logf(string format, params object[] args)
{
Debug.Log("[TweakScale] " + string.Format(format, args.Select(a => a.PreFormat()).ToArray()));
}
/// <summary>
/// Writes destination log message to output_log.txt.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="args">The arguments to the format.</param>
public static void LogWf(string format, params object[] args)
{
Debug.LogWarning("[TweakScale Warning] " + string.Format(format, args.Select(a => a.PreFormat()).ToArray()));
}
/// <summary>
/// Formats certain types to make them more readable.
/// </summary>
/// <param name="obj">The object to format.</param>
/// <returns>A more readable representation of <paramref name="obj"/>>.</returns>
public static object PreFormat(this object obj)
{
if (obj == null)
{
return "null";
}
if (obj is IEnumerable)
{
if (obj.GetType().GetMethod("ToString", new Type[] { }).IsOverride())
{
var e = obj as IEnumerable;
return string.Format("[{0}]", string.Join(", ", e.Cast<object>().Select(a => a.PreFormat().ToString()).ToArray()));
}
}
return obj;
}
/// <summary>
/// Reads destination exponentValue from the ConfigNode and magically converts it to the type you ask. Tested for float, boolean and double[]. Anything else is at your own risk.
/// </summary>
/// <typeparam name="T">The type to convert to. Usually inferred from <paramref name="defaultValue"/>.</typeparam>
/// <param name="config">ScaleType node from which to read values.</param>
/// <param name="name">Name of the ConfigNode's field.</param>
/// <param name="defaultValue">The exponentValue to use when the ConfigNode doesn't contain what we want.</param>
/// <returns>The exponentValue in the ConfigNode, or <paramref name="defaultValue"/> if no decent exponentValue is found there.</returns>
public static T ConfigValue<T>(ConfigNode config, string name, T defaultValue)
{
if (!config.HasValue(name))
{
return defaultValue;
}
string cfgValue = config.GetValue(name);
try
{
var result = ConvertEx.ChangeType<T>(cfgValue);
return result;
}
catch (Exception ex)
{
if (ex is InvalidCastException || ex is FormatException || ex is OverflowException || ex is ArgumentNullException)
{
LogWf("Failed to convert string value \"{0}\" to type {1}", cfgValue, typeof(T).Name);
return defaultValue;
}
throw;
}
}
/// <summary>
/// Fetches the the comma-delimited string exponentValue by the name <paramref name="name"/> from the node <paramref name="config"/> and converts it into an array of <typeparamref name="T"/>s.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="config">The node to fetch values from.</param>
/// <param name="name">The name of the exponentValue to fetch.</param>
/// <param name="defaultValue">The exponentValue to return if the exponentValue does not exist, or cannot be converted to <typeparamref name="T"/>s.</param>
/// <returns>An array containing the elements of the string exponentValue as <typeparamref name="T"/>s.</returns>
public static T[] ConfigValue<T>(ConfigNode config, string name, T[] defaultValue)
{
if (!config.HasValue(name))
{
return defaultValue;
}
return ConvertString(config.GetValue(name), defaultValue);
}
/// <summary>
/// Converts destination comma-delimited string into an array of <typeparamref name="T"/>s.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">A comma-delimited list of values.</param>
/// <param name="defaultValue">The exponentValue to return if the list does not hold valid values.</param>
/// <returns>An arra</returns>
public static T[] ConvertString<T>(string value, T[] defaultValue)
{
try
{
return value.Split(',').Select(ConvertEx.ChangeType<T>).ToArray();
}
catch (Exception ex)
{
if (!(ex is InvalidCastException) && !(ex is FormatException) && !(ex is OverflowException) &&
!(ex is ArgumentNullException))
throw;
LogWf("Failed to convert string value \"{0}\" to type {1}", value, typeof(T).Name);
return defaultValue;
}
}
/// <summary>
/// Gets all types defined in all loaded assemblies.
/// </summary>
public static IEnumerable<Type> GetAllTypes()
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
Type[] types;
try
{
types = assembly.GetTypes();
}
catch (Exception)
{
types = Type.EmptyTypes;
}
foreach (var type in types)
{
yield return type;
}
}
}
public static bool HasParent(this Part p)
{
return (object)p.parent != null;
}
}
}