summaryrefslogtreecommitdiff
path: root/Client/Assembly-CSharp/Extensions.cs
blob: a653cea27091404044637ab6908aab40be4374d9 (plain)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public static class Extensions
{
	private static string[] ByteHex = (from x in Enumerable.Range(0, 256)
	select x.ToString("X2")).ToArray<string>();

	public static void TrimEnd(this StringBuilder self)
	{
		for (int i = self.Length - 1; i >= 0; i--)
		{
			char c = self[i];
			if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
			{
				break;
			}
			int length = self.Length;
			self.Length = length - 1;
		}
	}

	public static void AddUnique<T>(this IList<T> self, T item)
	{
		if (!self.Contains(item))
		{
			self.Add(item);
		}
	}

	public static string ToTextColor(this Color c)
	{
		return string.Concat(new string[]
		{
			"[",
			Extensions.ByteHex[(int)((byte)(c.r * 255f))],
			Extensions.ByteHex[(int)((byte)(c.g * 255f))],
			Extensions.ByteHex[(int)((byte)(c.b * 255f))],
			Extensions.ByteHex[(int)((byte)(c.a * 255f))],
			"]"
		});
	}

	public static int ToInteger(this Color c, bool alpha)
	{
		if (alpha)
		{
			return (int)((byte)(c.r * 256f)) << 24 | (int)((byte)(c.g * 256f)) << 16 | (int)((byte)(c.b * 256f)) << 8 | (int)((byte)(c.a * 256f));
		}
		return (int)((byte)(c.r * 256f)) << 16 | (int)((byte)(c.g * 256f)) << 8 | (int)((byte)(c.b * 256f));
	}

	public static bool HasAnyBit(this int self, int bit)
	{
		return (self & bit) != 0;
	}

	public static bool HasAnyBit(this byte self, byte bit)
	{
		return (self & bit) > 0;
	}

	public static bool HasBit(this byte self, byte bit)
	{
		return (self & bit) == bit;
	}

	public static int BitCount(this byte self)
	{
		int num = 0;
		for (int i = 0; i < 8; i++)
		{
			if ((1 << i & (int)self) != 0)
			{
				num++;
			}
		}
		return num;
	}

	public static int IndexOf<T>(this T[] self, T item) where T : class
	{
		for (int i = 0; i < self.Length; i++)
		{
			if (self[i] == item)
			{
				return i;
			}
		}
		return -1;
	}

	public static int IndexOfMin<T>(this T[] self, Func<T, float> comparer)
	{
		float num = float.MaxValue;
		int result = -1;
		for (int i = 0; i < self.Length; i++)
		{
			float num2 = comparer(self[i]);
			if (num2 <= num)
			{
				result = i;
				num = num2;
			}
		}
		return result;
	}

	public static int IndexOfMax<T>(this T[] self, Func<T, int> comparer, out bool tie)
	{
		tie = false;
		int num = int.MinValue;
		int result = -1;
		for (int i = 0; i < self.Length; i++)
		{
			int num2 = comparer(self[i]);
			if (num2 > num)
			{
				result = i;
				num = num2;
				tie = false;
			}
			else if (num2 == num)
			{
				tie = true;
				result = -1;
			}
		}
		return result;
	}

	public static void SetAll<T>(this IList<T> self, T value)
	{
		for (int i = 0; i < self.Count; i++)
		{
			self[i] = value;
		}
	}

	public static void AddAll<T>(this List<T> self, IList<T> other)
	{
		int num = self.Count + other.Count;
		if (self.Capacity < num)
		{
			self.Capacity = num;
		}
		for (int i = 0; i < other.Count; i++)
		{
			self.Add(other[i]);
		}
	}

	public static void RemoveDupes<T>(this IList<T> self) where T : class
	{
		for (int i = 0; i < self.Count; i++)
		{
			T t = self[i];
			for (int j = self.Count - 1; j > i; j--)
			{
				if (self[j] == t)
				{
					self.RemoveAt(j);
				}
			}
		}
	}

	public static void Shuffle<T>(this IList<T> self)
	{
		for (int i = 0; i < self.Count - 1; i++)
		{
			T value = self[i];
			int index = UnityEngine.Random.Range(i, self.Count);
			self[i] = self[index];
			self[index] = value;
		}
	}

	public static void Shuffle<T>(this System.Random r, IList<T> self)
	{
		for (int i = 0; i < self.Count; i++)
		{
			T value = self[i];
			int index = r.Next(self.Count);
			self[i] = self[index];
			self[index] = value;
		}
	}

	public static T[] RandomSet<T>(this IList<T> self, int length)
	{
		T[] array = new T[length];
		self.RandomFill(array);
		return array;
	}

	public static void RandomFill<T>(this IList<T> self, T[] target)
	{
		HashSet<int> hashSet = new HashSet<int>();
		for (int i = 0; i < target.Length; i++)
		{
			int num;
			do
			{
				num = self.RandomIdx<T>();
			}
			while (hashSet.Contains(num));
			target[i] = self[num];
			hashSet.Add(num);
			if (hashSet.Count == self.Count)
			{
				return;
			}
		}
	}

	public static int RandomIdx<T>(this IList<T> self)
	{
		return UnityEngine.Random.Range(0, self.Count);
	}

	public static int RandomIdx<T>(this IEnumerable<T> self)
	{
		return UnityEngine.Random.Range(0, self.Count<T>());
	}

	public static T Random<T>(this IEnumerable<T> self)
	{
		return self.ToArray<T>().Random<T>();
	}

	public static T Random<T>(this IList<T> self)
	{
		if (self.Count > 0)
		{
			return self[UnityEngine.Random.Range(0, self.Count)];
		}
		return default(T);
	}

	public static Vector2 Div(this Vector2 a, Vector2 b)
	{
		return new Vector2(a.x / b.x, a.y / b.y);
	}

	public static Vector2 Mul(this Vector2 a, Vector2 b)
	{
		return new Vector2(a.x * b.x, a.y * b.y);
	}

	public static Vector3 Mul(this Vector3 a, Vector3 b)
	{
		return new Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
	}

	public static Vector3 Inv(this Vector3 a)
	{
		return new Vector3(1f / a.x, 1f / a.y, 1f / a.z);
	}

	public static Rect Lerp(this Rect source, Rect target, float t)
	{
		return new Rect
		{
			position = Vector2.Lerp(source.position, target.position, t),
			size = Vector2.Lerp(source.size, target.size, t)
		};
	}

	public static void ForEach<T>(this IList<T> self, Action<T> todo)
	{
		for (int i = 0; i < self.Count; i++)
		{
			todo(self[i]);
		}
	}

	public static T Max<T>(this IList<T> self, Func<T, float> comparer)
	{
		T t = self.First<T>();
		float num = comparer(t);
		for (int i = 0; i < self.Count; i++)
		{
			T t2 = self[i];
			float num2 = comparer(t2);
			if (num < num2 || (num == num2 && UnityEngine.Random.value > 0.5f))
			{
				num = num2;
				t = t2;
			}
		}
		return t;
	}

	public static T Max<T>(this IList<T> self, Func<T, decimal> comparer)
	{
		T t = self.First<T>();
		decimal d = comparer(t);
		for (int i = 0; i < self.Count; i++)
		{
			T t2 = self[i];
			decimal num = comparer(t2);
			if (d < num || (d == num && UnityEngine.Random.value > 0.5f))
			{
				d = num;
				t = t2;
			}
		}
		return t;
	}

	public static int Wrap(this int self, int max)
	{
		if (self >= 0)
		{
			return self % max;
		}
		return (self + -(self / max) * max + max) % max;
	}

	public static int IndexOf<T>(this T[] self, Predicate<T> pred)
	{
		for (int i = 0; i < self.Length; i++)
		{
			if (pred(self[i]))
			{
				return i;
			}
		}
		return -1;
	}

	public static Vector2 MapToRectangle(this Vector2 del, Vector2 widthAndHeight)
	{
		del = del.normalized;
		if (Mathf.Abs(del.x) > Mathf.Abs(del.y))
		{
			return new Vector2(Mathf.Sign(del.x) * widthAndHeight.x, del.y * widthAndHeight.y / 0.70710677f);
		}
		return new Vector2(del.x * widthAndHeight.x / 0.70710677f, Mathf.Sign(del.y) * widthAndHeight.y);
	}

	public static float AngleSignedRad(this Vector2 vector1, Vector2 vector2)
	{
		return Mathf.Atan2(vector2.y, vector2.x) - Mathf.Atan2(vector1.y, vector1.x);
	}

	public static float AngleSigned(this Vector2 vector1, Vector2 vector2)
	{
		return vector1.AngleSignedRad(vector2) * 57.29578f;
	}

	public static Vector2 Rotate(this Vector2 self, float degrees)
	{
		float f = 0.017453292f * degrees;
		float num = Mathf.Cos(f);
		float num2 = Mathf.Sin(f);
		return new Vector2(self.x * num - num2 * self.y, self.x * num2 + num * self.y);
	}

	public static Vector3 RotateY(this Vector3 self, float degrees)
	{
		float f = 0.017453292f * degrees;
		float num = Mathf.Cos(f);
		float num2 = Mathf.Sin(f);
		return new Vector3(self.x * num - num2 * self.z, self.y, self.x * num2 + num * self.z);
	}

	public static bool TryToEnum<TEnum>(this string strEnumValue, out TEnum enumValue)
	{
		enumValue = default(TEnum);
		if (!Enum.IsDefined(typeof(TEnum), strEnumValue))
		{
			return false;
		}
		enumValue = (TEnum)((object)Enum.Parse(typeof(TEnum), strEnumValue));
		return true;
	}

	public static TEnum ToEnum<TEnum>(this string strEnumValue)
	{
		if (!Enum.IsDefined(typeof(TEnum), strEnumValue))
		{
			return default(TEnum);
		}
		return (TEnum)((object)Enum.Parse(typeof(TEnum), strEnumValue));
	}

	public static TEnum ToEnum<TEnum>(this string strEnumValue, TEnum defaultValue)
	{
		if (!Enum.IsDefined(typeof(TEnum), strEnumValue))
		{
			return defaultValue;
		}
		return (TEnum)((object)Enum.Parse(typeof(TEnum), strEnumValue));
	}

	public static bool IsNullOrWhiteSpace(this string s)
	{
		if (s == null)
		{
			return true;
		}
		return !s.Any((char c) => !char.IsWhiteSpace(c));
	}
}