forked from allista/AT_Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiscUtils.cs
631 lines (575 loc) · 21.4 KB
/
MiscUtils.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2015 Allis Tauri
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Diagnostics;
using UnityEngine;
using Object = UnityEngine.Object;
namespace AT_Utils
{
public static partial class Utils
{
public static readonly ResourceInfo ElectricCharge = new ResourceInfo("ElectricCharge");
private static readonly Dictionary<string, int> _layers = new Dictionary<string, int>();
public static int GetLayer(string name)
{
if(_layers.TryGetValue(name, out var layer))
return layer;
layer = 1 << LayerMask.NameToLayer(name);
_layers[name] = layer;
return layer;
}
public static int GetLayers(params string[] names)
{
var layers = 0;
for(int i = 0, len = names.Length; i < len; i++)
layers |= GetLayer(names[i]);
return layers;
}
/// <summary>
/// The camel case components matching regexp.
/// From: http://stackoverflow.com/questions/155303/net-how-can-you-split-a-caps-delimited-string-into-an-array
/// </summary>
private const string CamelCaseRegexp = "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))";
private static readonly Regex CCR = new Regex(CamelCaseRegexp);
public static string ParseCamelCase(string s) => CCR.Replace(s, "$1 ");
public static readonly char[] Delimiters = { ' ', '\t', ',', ';' };
public static readonly char[] Whitespace = { ' ', '\t' };
public static readonly char[] Semicolon = { ';' };
public static readonly char[] Comma = { ',' };
public static string[] ParseLine(string line, char[] delimiters, bool trim = true)
{
if(string.IsNullOrEmpty(line))
return new string[] { };
var array = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
if(!trim)
return array;
for(int i = 0, len = array.Length; i < len; i++)
array[i] = array[i].Trim();
return array;
}
public static void StopTimeWarp(bool instant = false)
{
if(TimeWarp.fetch == null)
return;
TimeWarp.fetch.CancelAutoWarp();
TimeWarp.SetRate(0, instant);
}
public static string formatVeryBigValue(float value, string unit, string format = "F1")
{
string mod;
if(value > 1e24)
{
value /= 1e24f;
mod = "Y";
}
else if(value > 1e21)
{
value /= 1e21f;
mod = "Z";
}
else if(value > 1e18)
{
value /= 1e18f;
mod = "E";
}
else if(value > 1e15)
{
value /= 1e15f;
mod = "P";
}
else if(value > 1e12)
{
value /= 1e12f;
mod = "T";
}
else
return formatBigValue(value, unit, format);
return value.ToString(format) + mod + unit;
}
public static string formatBigValue(float value, string unit, string format = "F1")
{
var mod = "";
if(value > 1e9)
{
value /= 1e9f;
mod = "G";
}
else if(value > 1e6)
{
value /= 1e6f;
mod = "M";
}
else if(value > 1e3)
{
value /= 1e3f;
mod = "k";
}
return value.ToString(format) + mod + unit;
}
public static string formatSmallValue(float value, string unit, string format = "F1")
{
var mod = "";
if(value < 1)
{
if(value > 1e-3)
{
value *= 1e3f;
mod = "m";
}
else if(value > 1e-6)
{
value *= 1e6f;
mod = "μ";
}
else if(value > 1e-9)
{
value *= 1e9f;
mod = "n";
}
}
return value.ToString(format) + mod + unit;
}
public static string formatMass(float mass)
{
if(mass >= 0.1f)
return mass.ToString("n2") + "t";
if(mass >= 0.001f)
return (mass * 1e3f).ToString("n1") + "kg";
return (mass * 1e6f).ToString("n0") + "g";
}
public static string formatVolume(double volume)
{
if(volume < 1f)
return (volume * 1e3f).ToString("n0") + "L";
return volume.ToString("n1") + "m3";
}
public static string formatUnits(float units)
{
units = Mathf.Abs(units);
if(units >= 1f)
return units.ToString("n2") + "u";
if(units >= 1e-3f)
return (units * 1e3f).ToString("n1") + "mu";
if(units >= 1e-6f)
return (units * 1e6f).ToString("n1") + "μu";
if(units >= 1e-9f)
return (units * 1e9f).ToString("n1") + "nu";
if(units >= 1e-13f) //to fully use the last digit
return (units * 1e12f).ToString("n1") + "pu";
return "0.0u"; // effectively zero
}
private readonly struct _DateTime
{
public readonly int seconds, minutes, hours, days, years;
public _DateTime(double time, int year_len, int day_len)
{
years = (int)(time / year_len);
time -= years * year_len;
int secs = (int)time;
seconds = secs % 60;
minutes = secs / 60 % 60;
hours = secs / 3600 % (day_len / 3600);
days = secs / day_len;
}
}
public static string formatTimeDelta(double delta)
{
var dt = new _DateTime(delta, KSPUtil.dateTimeFormatter.Year, KSPUtil.dateTimeFormatter.Day);
if(dt.years > 0)
return $"{dt.years}y {dt.days,3}d {dt.hours,2}:{dt.minutes:00}:{dt.seconds:00}";
if(dt.days > 0)
return $"{dt.days,3}d {dt.hours,2}:{dt.minutes:00}:{dt.seconds:00}";
return $"{dt.hours,2}:{dt.minutes:00}:{dt.seconds:00}";
}
public static string formatDimensions(Vector3 size) => $"{size.x:F2}m x {size.y:F2}m x {size.z:F2}m";
public static string formatVector(Vector3 v) => $"({v.x}, {v.y}, {v.z}); |v| = {v.magnitude}";
public static string formatVector(Vector3d v) => $"({v.x}, {v.y}, {v.z}); |v| = {v.magnitude}";
public static string formatComponents(Vector3 v) =>
$"[{v.x: 0.000;-0.000; 0.000;}, {v.y: 0.000;-0.000; 0.000;}, {v.z: 0.000;-0.000; 0.000;}]";
public static string formatCB(CelestialBody cb)
{
if(cb == null)
return "Body: null";
return Utils.Format(
"Body: {}\n" + "\trotation: {} s\n" + "\tradius: {}\n" + "\trot angle {} deg",
cb.bodyName,
cb.rotationPeriod,
formatBigValue((float)cb.Radius, "m"),
cb.rotationAngle);
}
public static string formatOrbit(Orbit o)
{
return Utils.Format(
"{}\n"
+ "PeA: {} m\n"
+ "ApA: {} m\n"
+ "PeR: {} m\n"
+ "ApR: {} m\n"
+ "SMA: {} m\n"
+ "SmA: {} m\n"
+ "Ecc: {}\n"
+ "Inc: {} deg\n"
+ "LAN: {} deg\n"
+ "MA: {} rad\n"
+ "TA: {} rad\n"
+ "AoP: {} deg\n"
+ "Period: {} s\n"
+ "epoch: {}\n"
+ "T@epoch: {} s\n"
+ "T: {} s\n"
+ "T2Pe {} s\n"
+ "T2Ap {} s\n"
+ "StartUT {} StartTrans: {}\n"
+ "EndUT {}, EndTrans: {}\n"
+ "Vel: {} m/s\n"
+ "Pos: {} m\n",
formatCB(o.referenceBody),
o.PeA,
o.ApA,
o.PeR,
o.ApR,
o.semiMajorAxis,
o.semiMinorAxis,
o.eccentricity,
o.inclination,
o.LAN,
o.meanAnomaly,
o.trueAnomaly,
o.argumentOfPeriapsis,
o.period,
o.epoch,
o.ObTAtEpoch,
o.ObT,
o.timeToPe,
o.timeToAp,
o.StartUT,
o.patchStartTransition,
o.EndUT,
o.patchEndTransition,
formatVector(o.vel),
formatVector(o.pos));
}
public static string formatPatches(Orbit o, string tag)
{
var with_tag = !string.IsNullOrEmpty(tag);
var ret = with_tag
? Format("===================== {} : {} =======================\n{}",
tag,
Planetarium.GetUniversalTime(),
o)
: formatOrbit(o);
ret += "\n";
if(o.nextPatch != null
&& o.nextPatch.referenceBody != null
&& o.patchEndTransition != Orbit.PatchTransitionType.FINAL)
ret += formatPatches(o.nextPatch, "");
if(with_tag)
ret += "===================================================================\n";
return ret;
}
public static string formatBounds(Bounds b, string name = "") =>
$"Bounds: {name}\nCenter: {b.center}\nExtents: {b.extents}\nMin: {b.min}\nMax: {b.max}\nVolume: {b.size.x * b.size.y * b.size.z}";
public static string formatException(Exception ex) => $"{ex.Message}\n{ex.Source}\n{ex.StackTrace}";
public static string Format(string s, params object[] args)
{
if(args == null || args.Length == 0)
return s;
convert_args(args);
for(int i = 0, argsLength = args.Length; i < argsLength; i++)
{
var ind = s.IndexOf("{}", StringComparison.InvariantCulture);
if(ind >= 0)
s = $"{s.Substring(0, ind)}{{{i}}}{s.Substring(ind + 2)}";
else
s += string.Format(" arg{0}: {{{0}}}", i);
}
return string.Format(s.Replace("{}", "[no arg]"), args);
}
private static void convert_args(object[] args)
{
for(int i = 0, argsL = args.Length; i < argsL; i++)
{
var arg = args[i];
switch(arg)
{
case string _:
continue;
case null:
args[i] = "null";
break;
case float f:
args[i] = f.ToString("R");
break;
case double d:
args[i] = d.ToString("R");
break;
case Vector3 vector3:
args[i] = formatVector(vector3);
break;
case Vector3d vector3d:
args[i] = formatVector(vector3d);
break;
case CelestialBody body:
args[i] = formatCB(body);
break;
case Orbit orbit:
args[i] = formatOrbit(orbit);
break;
case Bounds bounds:
args[i] = formatBounds(bounds);
break;
case Exception exc:
args[i] = formatException(exc);
break;
case IConfigNode node:
args[i] = node.ToConfigString();
break;
case Transform t:
args[i] = $"{t.name}: pos {t.position}, rot {t.rotation.eulerAngles}";
break;
case Object obj:
args[i] = obj.GetID();
break;
case IEnumerable enumerable:
{
var arr = enumerable.Cast<object>().ToArray();
convert_args(arr);
args[i] = string.Join("\n",
$"Count: {arr.Length}",
"[",
string.Join(",\n", arr.Cast<string>().ToArray()),
"]");
break;
}
default:
args[i] = arg.ToString();
break;
}
}
}
static string prepare_message(string msg)
{
var mod_name = "AT_Utils";
var stack = new StackTrace(2);
var frames = stack.GetFrames();
if(frames != null)
{
foreach(var f in frames)
{
var method = f.GetMethod();
if(log_re.IsMatch(method.Name))
continue;
if(method.DeclaringType != null)
mod_name = method.DeclaringType.Assembly.GetName().Name;
break;
}
}
#if DEBUG
UnityEngine.Debug.Log(stack);
#endif
return $"[{mod_name}: {DateTime.Now:HH:mm:ss.fff} {Time.frameCount}] {msg}";
}
static readonly Regex log_re = new Regex("[Ll]og");
public static void Log(string msg)
{
msg = prepare_message(msg);
UnityEngine.Debug.Log(msg);
#if DEBUG
BackupLogger.LogRaw(msg);
#endif
}
public static void Log(string msg, params object[] args)
{
if(args.Length > 0)
{
convert_args(args);
msg = Format(msg, args);
}
Log(msg);
}
public static void Debug(string msg)
{
#if DEBUG
Log($"DEBUG: {msg}");
#endif
}
public static void Debug(string msg, params object[] args)
{
#if DEBUG
Log($"DEBUG: {msg}", args);
#endif
}
public static void Info(string msg) => Log($"INFO: {msg}");
public static void Info(string msg, params object[] args) => Log($"INFO: {msg}", args);
public static void Warning(string msg) => Log($"WARNING: {msg}");
public static void Warning(string msg, params object[] args) => Log($"WARNING: {msg}", args);
public static void Error(string msg) => Log($"ERROR: {msg}");
public static void Error(string msg, params object[] args) => Log($"ERROR: {msg}", args);
public static void Log2File(string filename, string msg, params object[] args)
{
using(var f = new StreamWriter(filename, true))
{
if(args.Length > 0)
{
convert_args(args);
msg = Format(msg, args);
}
f.WriteLine(prepare_message(msg));
}
}
public static bool PartIsPurchased(string name)
{
if(PartLoader.Instance == null)
return false;
var info = PartLoader.getPartInfoByName(name);
return info != null && PartIsPurchased(info);
}
public static bool PartIsPurchased(AvailablePart info)
{
return (HighLogic.CurrentGame != null
&& (HighLogic.CurrentGame.Mode == Game.Modes.SANDBOX
|| ResearchAndDevelopment.PartModelPurchased(info)));
}
public static Vector3[] BoundCorners(Bounds b)
{
var corners = new Vector3[8];
var min = b.min;
var max = b.max;
corners[0] = new Vector3(min.x, min.y, min.z); //left-bottom-back
corners[1] = new Vector3(min.x, min.y, max.z); //left-bottom-front
corners[2] = new Vector3(min.x, max.y, min.z); //left-top-back
corners[3] = new Vector3(min.x, max.y, max.z); //left-top-front
corners[4] = new Vector3(max.x, min.y, min.z); //right-bottom-back
corners[5] = new Vector3(max.x, min.y, max.z); //right-bottom-front
corners[6] = new Vector3(max.x, max.y, min.z); //right-top-back
corners[7] = new Vector3(max.x, max.y, max.z); //right-top-front
return corners;
}
public static int[] BoundTriangles()
{
return new[] {
0, 1, 2, 2, 1, 3, //left
3, 1, 7, 7, 1, 5, //front
5, 4, 7, 7, 4, 6, //right
6, 4, 2, 2, 4, 0, //back
2, 6, 3, 3, 6, 7, //top
0, 4, 1, 1, 4, 5, //bottom
};
}
public static Vector3[] BoundCorners(Vector3 center, Vector3 size)
{
var b = new Bounds(center, size);
return BoundCorners(b);
}
//KSP-provided System.dll declares Path.Combine(strin[]) as internal O_o
public static string PathChain(params string[] paths)
{
var path = "";
for(int p = 0, len = paths.Length; p < len; p++)
path = Path.Combine(path, paths[p]);
return path;
}
//sound (from the KAS mod; KAS_Shared class)
public static bool createFXSound(Part part, FXGroup group, string sndPath, bool loop, float maxDistance = 30f)
{
group.audio = part.gameObject.AddComponent<AudioSource>();
group.audio.volume = GameSettings.SHIP_VOLUME;
group.audio.rolloffMode = AudioRolloffMode.Logarithmic;
group.audio.dopplerLevel = 0f;
group.audio.maxDistance = maxDistance;
group.audio.loop = loop;
group.audio.playOnAwake = false;
if(GameDatabase.Instance.ExistsAudioClip(sndPath))
{
group.audio.clip = GameDatabase.Instance.GetAudioClip(sndPath);
return true;
}
Message(10, "Sound file : {0} has not been found, please check your Hangar installation", sndPath);
return false;
}
public static void SaveGame(string name, bool with_message = true)
{
var game = HighLogic.CurrentGame.Updated();
game.startScene = GameScenes.FLIGHT;
GamePersistence.SaveGame(game, name, HighLogic.SaveFolder, SaveMode.OVERWRITE);
if(with_message)
Message("Game saved as: {0}", name);
}
// ReSharper disable once IteratorNeverReturns
public static IEnumerator<YieldInstruction> SlowUpdate(Action action, float period = 1)
{
while(true)
{
action();
yield return new WaitForSeconds(period);
}
}
public static bool NameMatches(string name, IList<string> list)
{
if(string.IsNullOrEmpty(name))
return false;
for(int j = 0, count = list.Count; j < count; j++)
{
var lname = list[j];
if(string.IsNullOrEmpty(lname))
continue;
if(name.IndexOf(lname, StringComparison.OrdinalIgnoreCase) >= 0)
return true;
}
return false;
}
public static Collider AddCollider(this MeshFilter mesh, bool isTrigger = false)
{
var col = mesh.GetComponent<Collider>();
if(col == null || !col.isTrigger)
{
var collider = mesh.gameObject.AddComponent<MeshCollider>();
collider.sharedMesh = mesh.sharedMesh;
collider.convex = true;
collider.isTrigger = isTrigger;
col = collider;
}
col.enabled = true;
return col;
}
}
public static class WaitWithPhysics
{
public static void DelayPhysicsForSeconds(float dt)
{
OrbitPhysicsManager.HoldVesselUnpack(Mathf.CeilToInt(dt / TimeWarp.fixedDeltaTime) + 1);
}
public static WaitForSeconds ForSeconds(float dt)
{
DelayPhysicsForSeconds(dt);
return new WaitForSeconds(dt);
}
public static WaitForFixedUpdate ForFixedUpdate()
{
OrbitPhysicsManager.HoldVesselUnpack(2);
return new WaitForFixedUpdate();
}
public static YieldInstruction ForNextUpdate()
{
DelayPhysicsForSeconds(TimeWarp.deltaTime);
return null;
}
}
public class ListDict<K, V> : Dictionary<K, List<V>>
{
public void Add(K key, V value)
{
if(TryGetValue(key, out var lst))
lst.Add(value);
else
this[key] = new List<V> { value };
}
}
}