-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathSentryWindow.cs
More file actions
248 lines (204 loc) · 7.75 KB
/
SentryWindow.cs
File metadata and controls
248 lines (204 loc) · 7.75 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
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
using System;
using System.IO;
using Sentry.Extensibility;
using UnityEditor;
using UnityEngine;
namespace Sentry.Unity.Editor.ConfigurationWindow;
public class SentryWindow : EditorWindow
{
private const string LinkXmlPath = "Assets/Plugins/Sentry/link.xml";
public const string EditorMenuPath = "Tools -> Sentry";
[MenuItem("Tools/Sentry")]
public static void OnMenuClick()
{
if (Wizard.InProgress)
{
Debug.Log("Wizard in progress, ignoring Tools/Sentry menu click");
return;
}
if (Instance is null && IsFirstLoad && EditorUtility.DisplayDialog("Start a setup wizard?",
"It looks like you're setting up Sentry for the first time in this project.\n\n" +
"Would you like to start a setup wizard to connect to sentry.io?", "Start wizard", "I'll set it up manually"))
{
Wizard.Start(CreateLogger());
}
else
{
OpenSentryWindow();
}
}
public static void OpenSentryWindow()
{
Instance = GetWindow<SentryWindow>();
Instance.minSize = new Vector2(800, 550);
}
public static SentryWindow? Instance;
protected static string SentryOptionsAssetName { get; set; } = ScriptableSentryUnityOptions.ConfigName;
protected static string SentryCliAssetName { get; } = SentryCliOptions.ConfigName;
public ScriptableSentryUnityOptions Options { get; private set; } = null!; // Set by OnEnable()
public SentryCliOptions CliOptions { get; private set; } = null!; // Set by OnEnable()
public static Texture2D? ErrorIcon { get; private set; }
public event Action<ValidationError> OnValidationError = _ => { };
private int _currentTab = 0;
private Vector2 _scrollPosition = Vector2.zero;
private readonly string[] _tabs =
{
"Core",
"Logging",
"Enrichment",
"Transport",
"Advanced",
"Options Config",
"Debug Symbols"
};
private IDiagnosticLogger _logger;
private static string OptionsPath => ScriptableSentryUnityOptions.GetConfigPath(SentryOptionsAssetName);
private static string CliOptionsPath => SentryCliOptions.GetConfigPath(SentryCliAssetName);
public SentryWindow()
{
_logger = CreateLogger();
}
private static IDiagnosticLogger CreateLogger() =>
new UnityLogger(new SentryOptions() { Debug = SentryPackageInfo.IsDevPackage });
void OnDestroy()
{
Instance = null;
}
private void OnEnable()
{
// Note: these are not allowed to be called in constructors
SetTitle(this);
Options = SentryScriptableObject.CreateOrLoad<ScriptableSentryUnityOptions>(OptionsPath);
CliOptions = SentryScriptableObject.CreateOrLoad<SentryCliOptions>(CliOptionsPath);
ErrorIcon = EditorGUIUtility.Load("icons/console.erroricon.png") as Texture2D;
}
internal static void SaveWizardResult(WizardConfiguration config)
{
var options = SentryScriptableObject.CreateOrLoad<ScriptableSentryUnityOptions>(OptionsPath);
var cliOptions = SentryScriptableObject.CreateOrLoad<SentryCliOptions>(CliOptionsPath);
options.Dsn = config.Dsn;
cliOptions.UploadSymbols = !string.IsNullOrWhiteSpace(config.Token);
cliOptions.Auth = config.Token;
cliOptions.Project = config.ProjectSlug;
EditorUtility.SetDirty(options);
EditorUtility.SetDirty(cliOptions);
AssetDatabase.SaveAssets();
}
private static bool IsFirstLoad =>
!File.Exists(ScriptableSentryUnityOptions.GetConfigPath(SentryOptionsAssetName)) &&
!File.Exists(SentryCliOptions.GetConfigPath(SentryCliAssetName));
// ReSharper disable once UnusedMember.Local
private void OnGUI()
{
EditorGUILayout.Space();
GUILayout.Label("SDK Options", EditorStyles.boldLabel);
Options.Enabled = EditorGUILayout.ToggleLeft(
new GUIContent("Enable Sentry", "Controls if the SDK should initialize itself or not."),
Options.Enabled);
EditorGUILayout.Space();
EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, 1), Color.gray);
EditorGUILayout.Space();
var selectedTab = GUILayout.Toolbar(_currentTab, _tabs);
if (selectedTab != _currentTab)
{
// Edge-case: Lose focus so currently selected fields don't "bleed" through like DSN -> Override Release
GUI.FocusControl(null);
_currentTab = selectedTab;
}
EditorGUI.BeginDisabledGroup(!Options.Enabled);
EditorGUILayout.Space();
EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, 1), Color.gray);
EditorGUILayout.Space();
_scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition);
switch (_currentTab)
{
case 0:
CoreTab.Display(Options);
break;
case 1:
LoggingTab.Display(Options);
break;
case 2:
EnrichmentTab.Display(Options);
break;
case 3:
TransportTab.Display(Options);
break;
case 4:
AdvancedTab.Display(Options, CliOptions);
break;
case 5:
OptionsConfigurationTab.Display(Options);
break;
case 6:
DebugSymbolsTab.Display(CliOptions);
break;
default:
break;
}
EditorGUILayout.EndScrollView();
EditorGUI.EndDisabledGroup();
}
private void OnLostFocus()
{
// Make sure the actual config asset exists before validating/saving. Crashes the editor otherwise.
if (!File.Exists(ScriptableSentryUnityOptions.GetConfigPath(SentryOptionsAssetName)))
{
_logger.LogWarning("Options could not been saved. The configuration asset is missing.");
return;
}
Validate();
EditorUtility.SetDirty(Options);
EditorUtility.SetDirty(CliOptions);
AssetDatabase.SaveAssets();
}
private void Validate()
{
if (!Options.Enabled)
{
return;
}
ValidateDsn();
}
internal void ValidateDsn()
{
if (string.IsNullOrWhiteSpace(Options.Dsn))
{
return;
}
if (Uri.IsWellFormedUriString(Options.Dsn, UriKind.Absolute))
{
return;
}
var fullFieldName = $"{nameof(Options)}.{nameof(Options.Dsn)}";
var validationError = new ValidationError(fullFieldName, "Invalid DSN format. Expected a URL.");
OnValidationError(validationError);
_logger.LogWarning(validationError.ToString());
}
internal static void SetTitle(EditorWindow window, string title = "Sentry", string description = "Sentry SDK options")
{
var isDarkMode = EditorGUIUtility.isProSkin;
var texture = new Texture2D(16, 16);
using var memStream = new MemoryStream();
using var stream = window.GetType().Assembly
.GetManifestResourceStream(
$"Sentry.Unity.Editor.Resources.SentryLogo{(isDarkMode ? "Light" : "Dark")}.png");
stream.CopyTo(memStream);
stream.Flush();
memStream.Position = 0;
texture.LoadImage(memStream.ToArray());
window.titleContent = new GUIContent(title, texture, description);
}
}
public readonly struct ValidationError
{
public readonly string PropertyName;
public readonly string Reason;
public ValidationError(string propertyName, string reason)
{
PropertyName = propertyName;
Reason = reason;
}
public override string ToString()
=> $"[{PropertyName}] Reason: {Reason}";
}