-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
105 lines (89 loc) · 2.75 KB
/
Program.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
using System.Diagnostics;
using System.Reflection;
namespace Hyperstellar;
public static class Program
{
private static void DefaultTryForeverExceptionFunc(Exception ex)
{
ex.Demystify();
Console.WriteLine(ex);
}
internal static string GetExceptionStackTraceString(Exception ex)
{
string msg = "";
EnhancedStackTrace stackTrace = new(ex);
for (int i = 0; i < stackTrace.FrameCount; i++)
{
StackFrame frame = stackTrace.GetFrame(i);
string methodMsg = "???::??";
MethodBase? method = frame.GetMethod();
if (method != null)
{
string typeMsg = "<Unknown type>";
Type? type = method.DeclaringType;
if (type != null)
{
typeMsg = type.FullName ?? $"???.{type.Name}";
}
methodMsg = typeMsg + "::" + method.Name;
}
string fileNameMsg = $"{frame.GetFileName()?.Split("/").Last() ?? "??.cs"}";
string lineNumberMsg = $"{frame.GetFileLineNumber()}";
string columnNumberMsg = $"{frame.GetFileColumnNumber()}";
string frameMsg = $"at {methodMsg}() in {fileNameMsg}:{lineNumberMsg}.{columnNumberMsg}";
msg += $"{frameMsg}\n";
}
return msg;
}
internal static async Task<T> TryUntilAsync<T>(Func<Task<T>> tryFunc, Func<Exception, Task>? repeatFunc = null,
int msRetryWaitInterval = 0)
{
while (true)
{
try
{
return await tryFunc();
}
catch (Exception ex)
{
if (repeatFunc != null)
{
await repeatFunc(ex);
}
else
{
DefaultTryForeverExceptionFunc(ex);
}
await Task.Delay(msRetryWaitInterval);
}
}
}
internal static async Task TryUntilAsync(Func<Task> tryFunc, Func<Exception, Task>? repeatFunc = null,
bool runForever = false)
{
while (true)
{
try
{
await tryFunc();
if (!runForever)
{
return;
}
}
catch (Exception ex)
{
if (repeatFunc != null)
{
await repeatFunc(ex);
}
else
{
DefaultTryForeverExceptionFunc(ex);
}
}
}
}
public static async Task Main() =>
await Task.WhenAll(Discord.Dc.InitAsync(), Clash.Coc.s_initTask, Sql.Db.InitAsync());
}