generated from vrchat-community/template-package
-
Notifications
You must be signed in to change notification settings - Fork 18
/
ErrorReport.cs
360 lines (308 loc) · 11.2 KB
/
ErrorReport.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
#region
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.ExceptionServices;
using JetBrains.Annotations;
using nadena.dev.ndmf.localization;
using nadena.dev.ndmf.runtime;
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
#endregion
namespace nadena.dev.ndmf
{
#region
using UnityObject = Object;
#endregion
internal class NullScope : IDisposable
{
public void Dispose()
{
}
}
internal class ErrorReportScope : IDisposable
{
private ErrorReport _report;
public ErrorReportScope(ErrorReport report)
{
_report = ErrorReport.CurrentReport;
ErrorReport.CurrentReport = report;
}
public void Dispose()
{
ErrorReport.CurrentReport = _report;
}
}
internal class ReferenceStackScope : IDisposable
{
private ErrorReport _report;
private int _stackDepth;
public ReferenceStackScope(ErrorReport report)
{
_report = report;
_stackDepth = ErrorReport.ReferenceStack.Count;
}
public void Dispose()
{
ErrorReport.ReferenceStack.RemoveRange(_stackDepth, ErrorReport.ReferenceStack.Count - _stackDepth);
}
}
internal class RestoreContextScope : IDisposable
{
private ErrorReport _report;
private ErrorContext _priorContext;
public RestoreContextScope(ErrorReport report)
{
this._report = report;
this._priorContext = report.CurrentContext;
}
public void Dispose()
{
_report.CurrentContext = _priorContext;
}
}
/// <summary>
/// Contains any errors or warnings issued during a single build operation.
/// </summary>
public sealed class ErrorReport
{
internal static List<ErrorReport> Reports = new List<ErrorReport>();
internal static ErrorReport CurrentReport = null;
internal static int editorFrame = 0;
internal static List<ObjectReference> ReferenceStack = new List<ObjectReference>();
private ErrorReport(string avatarName, string avatarPath)
{
AvatarName = avatarName;
AvatarRootPath = avatarPath;
Errors = ImmutableList<ErrorContext>.Empty;
}
/// <summary>
/// The name of the avatar being processed
/// </summary>
public string AvatarName { get; }
/// <summary>
/// The path (from the scene root) of the avatar being processed
/// </summary>
public string AvatarRootPath { get; }
/// <summary>
/// A list of reported errors.
/// </summary>
public ImmutableList<ErrorContext> Errors { get; private set; }
internal ErrorContext CurrentContext = new ErrorContext();
private HashSet<Exception> ReportedExceptions = new HashSet<Exception>();
internal static ErrorReport Create(GameObject root, bool isClone)
{
if (Time.frameCount != editorFrame)
{
editorFrame = Time.frameCount;
Reports.Clear();
}
var name = root.name;
if (isClone && name.EndsWith("(Clone)"))
{
name = name.Substring(0, name.Length - 7);
}
var path = RuntimeUtil.RelativePath(null, root);
if (isClone && path.EndsWith("(Clone)"))
{
path = path.Substring(0, path.Length - 7);
}
var report = new ErrorReport(name, path);
Reports.Add(report);
return report;
}
internal void AddError(IError error)
{
var context = CurrentContext;
context.TheError = error;
Errors = Errors.Add(context);
}
/// <summary>
/// Adds an error to the currently active error report. If no error report is active, the error will simply be
/// logged to the debug log.
/// </summary>
/// <param name="error"></param>
public static void ReportError(IError error)
{
if (error is StackTraceError e)
{
Debug.LogException(e.Exception);
}
else
{
Debug.LogWarning("[NDMF] Error Reported: " + error.ToMessage());
}
var contextObjs = ReferenceStack.ToList();
contextObjs.Reverse();
foreach (var context in contextObjs)
{
if (context != null) error.AddReference(context);
}
CurrentReport?.AddError(error);
}
/// <summary>
/// Helper to report a SimpleError.
/// </summary>
/// <param name="localizer">The Localizer used to look up translations</param>
/// <param name="errorSeverity">The severity of the error</param>
/// <param name="key">The prefix used to find localization keys</param>
/// <param name="args">Inline substitutions and unity objects to reference from the error</param>
public static void ReportError(Localizer localizer, ErrorSeverity errorSeverity, string key,
params object[] args)
{
ReportError(new InlineError(localizer, errorSeverity, key, args));
}
/// <summary>
/// Helper to report an exception. This will generate an error of InternalError severity.
/// </summary>
/// <param name="e">Exception to report</param>
/// <param name="additionalStackTrace">Additional information to append to the stack trace</param>
public static void ReportException(Exception e, string additionalStackTrace = null)
{
var report = CurrentReport;
Exception e_ = e;
while (e_ != null && report != null)
{
if (report.ReportedExceptions.Contains(e_)) return;
e_ = e_.InnerException;
}
ReportError(new StackTraceError(e, additionalStackTrace));
report?.ReportedExceptions?.Add(e);
}
/// <summary>
/// Attempts to find the original avatar that generated the report.
/// </summary>
/// <param name="av">The avatar root</param>
/// <returns>true if the avatar was found, otherwise false</returns>
public bool TryResolveAvatar(out GameObject av)
{
var scene = SceneManager.GetActiveScene();
var firstPathElement = AvatarRootPath.Split('/')[0];
var remaining = firstPathElement == AvatarRootPath ? null : AvatarRootPath.Substring(firstPathElement.Length + 1);
foreach (var obj in scene.GetRootGameObjects())
{
if (obj.name == firstPathElement)
{
if (remaining == null)
{
av = obj;
return true;
}
else
{
av = obj.transform.Find(remaining)?.gameObject;
return av != null;
}
}
}
av = null;
return false;
}
/// <summary>
/// Returns a disposable scope, within which all errors will reference a specific UnityObject.
/// </summary>
/// <param name="obj">The object to reference (can be null)</param>
/// <returns>A disposable that will remove the object from the current scope when disposed.</returns>
public static IDisposable WithContextObject([CanBeNull] UnityObject obj)
{
if (obj == null || CurrentReport == null) return new NullScope();
var scope = new ReferenceStackScope(CurrentReport);
ReferenceStack.Add(ObjectRegistry.GetReference(obj));
return scope;
}
/// <summary>
/// Executes a function, within which any errors will reference a specific UnityObject.
/// Thrown exceptions will automatically be logged.
/// </summary>
/// <param name="obj">The object to reference</param>
/// <param name="func">The function to invoke</param>
/// <returns>The return value of func()</returns>
public static T WithContextObject<T>([CanBeNull] UnityObject obj, Func<T> func)
{
using (WithContextObject(obj))
{
try
{
return func();
}
catch (Exception e)
{
ReportException(e);
throw e;
}
}
}
/// <summary>
/// Executes a function, within which any errors will reference a specific UnityObject.
/// Thrown exceptions will automatically be logged.
/// </summary>
/// <param name="obj">The object to reference</param>
/// <param name="func">The function to invoke</param>
/// <returns>The return value of func()</returns>
public static void WithContextObject([CanBeNull] UnityObject obj, Action action)
{
using (WithContextObject(obj))
{
try
{
action();
}
catch (Exception e)
{
ReportException(e);
throw;
}
}
}
internal IDisposable WithContext(PluginBase thePlugin)
{
var scope = new RestoreContextScope(this);
CurrentContext.Plugin = thePlugin;
return scope;
}
internal IDisposable WithContextPassName(string name)
{
var scope = new RestoreContextScope(this);
CurrentContext.PassName = name;
return scope;
}
internal IDisposable WithExtensionContextTrace(Type extensionContext)
{
var scope = new RestoreContextScope(this);
CurrentContext.ExtensionContext = extensionContext;
return scope;
}
/// <summary>
/// Runs the given action, capturing all errors and returning any errors generated.
/// Intended for unit testing only.
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
public static List<ErrorContext> CaptureErrors(Action action)
{
var report = new ErrorReport("test avatar", "test avatar");
using (new ErrorReportScope(report))
{
try
{
action();
}
catch (Exception e)
{
ReportException(e);
}
}
return report.Errors.ToList();
}
/// <summary>
/// Clears all error reports.
/// </summary>
public static void Clear()
{
Reports.Clear();
CurrentReport = null;
}
}
}