forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrashReportHelper.cs
More file actions
197 lines (179 loc) · 8.39 KB
/
Copy pathCrashReportHelper.cs
File metadata and controls
197 lines (179 loc) · 8.39 KB
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
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Stride.Core.Assets.Editor.Components.Transactions;
using Stride.Core.Assets.Editor.ViewModel;
using Stride.Core.Extensions;
using Stride.Core.Transactions;
using Stride.Core.Windows;
using Stride.Assets;
using Stride.Core.Presentation.Services;
using Stride.Editor.CrashReport;
using Stride.Graphics;
using DialogResult = System.Windows.Forms.DialogResult;
using Stride.GameStudio.AssetsEditors;
using Stride.Core.Assets.Editor.Services;
namespace Stride.GameStudio.Helpers
{
public static class CrashReportHelper
{
private const int DebugVersion = 4;
public static void SendReport(string exceptionMessage, int crashLocation, string[] logs, string threadName)
{
var crashReport = new CrashReportData
{
["Application"] = "GameStudio",
["StrideVersion"] = StrideVersion.NuGetVersion,
["GameStudioVersion"] = DebugVersion.ToString(),
["ThreadName"] = string.IsNullOrEmpty(threadName) ? "" : threadName,
#if DEBUG
["CrashLocation"] = crashLocation.ToString(),
["ProcessID"] = Environment.ProcessId.ToString()
#endif
};
try
{
// Add session-specific information in this try/catch block
var gameSettingsAsset = SessionViewModel.Instance?.CurrentProject?.Package.GetGameSettingsAsset();
if (gameSettingsAsset != null)
{
crashReport["DefaultGraphicProfile"] = gameSettingsAsset.GetOrCreate<RenderingSettings>().DefaultGraphicsProfile.ToString();
}
}
catch (Exception e)
{
e.Ignore();
}
// opened assets
try
{
if (SessionViewModel.Instance?.ServiceProvider.TryGet<IAssetEditorsManager>() is AssetEditorsManager manager)
{
var sb = new StringBuilder();
foreach (var asset in manager.GetCurrentlyOpenedAssets())
{
sb.AppendLine($"{asset.Id}:{asset.Name} ({asset.TypeDisplayName})");
}
crashReport["OpenedAssets"] = sb.ToString();
}
}
catch (Exception e)
{
e.Ignore();
}
// action history
try
{
// Add session-specific information in this try/catch block
var actionsViewModel = SessionViewModel.Instance?.ActionHistory;
if (actionsViewModel != null)
{
var actions = actionsViewModel.Transactions.ToList();
var sb = new StringBuilder();
for (var i = Math.Max(0, actions.Count - 5); i < actions.Count; ++i)
{
ExpandAction(actions[i], sb, 4);
}
crashReport["LastActions"] = sb.ToString();
}
}
catch (Exception e)
{
e.Ignore();
}
// transaction in progress
try
{
// Add session-specific information in this try/catch block
var actionService = SessionViewModel.Instance?.UndoRedoService;
if (actionService != null && actionService.TransactionInProgress)
{
// FIXME: expose some readonly properties/methods from ITransactionStack or ITransaction to reduce reflection
var stackField = typeof(UndoRedoService).GetField("stack", BindingFlags.Instance | BindingFlags.NonPublic);
if (stackField != null)
{
var transactionsInProgressField = typeof(ITransactionStack).Assembly.GetType("Stride.Core.Transactions.TransactionStack")?.GetField("transactionsInProgress", BindingFlags.Instance | BindingFlags.NonPublic);
if (transactionsInProgressField != null)
{
var stack = stackField.GetValue(actionService);
if (transactionsInProgressField.GetValue(stack) is IEnumerable<IReadOnlyTransaction> transactionsInProgress)
{
var sb = new StringBuilder();
sb.AppendLine("Transactions in progress:");
foreach (var transaction in transactionsInProgress)
{
PrintTransaction(transaction, sb, 4);
}
crashReport["TransactionInProgress"] = sb.ToString();
}
}
}
}
}
catch (Exception e)
{
e.Ignore();
}
crashReport["CurrentDirectory"] = Environment.CurrentDirectory;
crashReport["CommandArgs"] = string.Join(" ", AppHelper.GetCommandLineArgs());
crashReport["OsVersion"] = $"{System.Runtime.InteropServices.RuntimeInformation.OSDescription} {(Environment.Is64BitOperatingSystem ? "x64" : "x86")}";
crashReport["ProcessorCount"] = Environment.ProcessorCount.ToString();
crashReport["Exception"] = exceptionMessage;
var videoConfig = AppHelper.GetVideoConfig();
foreach (var conf in videoConfig)
{
crashReport.Data.Add((conf.Key, conf.Value));
}
var nonFatalReport = new StringBuilder();
for (var index = 0; index < logs.Length; index++)
{
var log = logs[index];
nonFatalReport.AppendFormat($"{index + 1}: {log}\r\n");
}
crashReport["Log"] = nonFatalReport.ToString();
// Try to anonymize reports
// It also makes it easier to copy and paste paths
for (var i = 0; i < crashReport.Data.Count; i++)
{
var data = crashReport.Data[i].Item2;
data = Regex.Replace(data, Regex.Escape(Environment.GetEnvironmentVariable("USERPROFILE")), Regex.Escape("%USERPROFILE%"), RegexOptions.IgnoreCase);
data = Regex.Replace(data, $@"\b{Regex.Escape(Environment.GetEnvironmentVariable("USERNAME"))}\b", Regex.Escape("%USERNAME%"), RegexOptions.IgnoreCase);
crashReport.Data[i] = (crashReport.Data[i].Item1, data);
}
var reporter = new CrashReportWindow(crashReport, "Stride GameStudio");
var result = reporter.ShowDialog();
}
private static void ExpandAction(TransactionViewModel actionItem, StringBuilder sb, int increment)
{
sb.AppendLine($"* {(actionItem.IsDone ? "+" : "-")}[{actionItem.Name}]");
var memberInfo = typeof(TransactionViewModel).GetField("transaction", BindingFlags.Instance | BindingFlags.NonPublic);
if (memberInfo != null)
{
var transaction = (IReadOnlyTransaction)memberInfo.GetValue(actionItem);
PrintTransaction(transaction, sb, increment);
}
}
private static void PrintOperation(Operation operation, StringBuilder stringBuilder, int increment, int offset)
{
if (operation is IReadOnlyTransaction transaction)
{
PrintTransaction(transaction, stringBuilder, increment, offset);
return;
}
stringBuilder.Append("".PadLeft(offset) + "*");
stringBuilder.AppendLine($" {operation}");
}
private static void PrintTransaction(IReadOnlyTransaction transaction, StringBuilder stringBuilder, int increment, int offset = 0)
{
foreach (var operation in transaction.Operations)
{
PrintOperation(operation, stringBuilder, increment, offset + increment);
}
}
}
}