-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathSentryUnitySdk.cs
More file actions
201 lines (173 loc) · 7.3 KB
/
SentryUnitySdk.cs
File metadata and controls
201 lines (173 loc) · 7.3 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
using System;
using System.IO;
using System.Threading.Tasks;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;
using UnityEngine;
namespace Sentry.Unity;
internal class SentryUnitySdk
{
private readonly SentryUnityOptions _options;
private IDisposable _dotnetSdk = null!;
private FileStream? LockFile;
private SentryUnitySdk(SentryUnityOptions options)
{
_options = options;
}
internal static SentryUnitySdk? Init(SentryUnityOptions options)
{
var unitySdk = new SentryUnitySdk(options);
options.SetupUnityLogging();
if (!options.ShouldInitializeSdk())
{
return null;
}
MainThreadData.CollectData();
// Some integrations are controlled through a flag and opt-in. Adding these integrations late so we have equal
// behaviour whether the options got created through the ScriptableObject or the SDK gets manually initialized
AddIntegrations(options);
SetUpWindowsPlayerCaching(unitySdk, options);
ConfigureUnsupportedPlatformFallbacks(options);
unitySdk._dotnetSdk = Sentry.SentrySdk.Init(options);
// We can safely call this during initialization. If the SDK self-initialized we're right on time. If the SDK
// was initialized manually, the RuntimeOnLoad attributes already triggered, making this call a no-op.
StartupTracingIntegration.StartTracing();
if (options.NativeContextWriter is { } contextWriter)
{
Sentry.SentrySdk.CurrentHub.ConfigureScope((scope) =>
{
var task = Task.Run(() => contextWriter.Write(scope)).ContinueWith(t =>
{
if (t.Exception is not null)
{
options.DiagnosticLogger?.LogWarning(
"Failed to synchronize scope to the native SDK: {0}", t.Exception);
}
});
});
}
ApplicationAdapter.Instance.Quitting += unitySdk.Close;
return unitySdk;
}
public void Close()
{
_options.DiagnosticLogger?.LogDebug("Closing the sentry-dotnet SDK");
try
{
ApplicationAdapter.Instance.Quitting -= Close;
_options.NativeSupportCloseCallback?.Invoke();
_options.NativeSupportCloseCallback = null;
_dotnetSdk.Dispose();
}
catch (Exception ex)
{
_options.DiagnosticLogger?.Log(SentryLevel.Warning,
"Exception while closing the .NET SDK.", ex);
}
try
{
// We don't really need to close, Windows would release the lock anyway, but let's be nice.
LockFile?.Close();
}
catch (Exception ex)
{
_options.DiagnosticLogger?.Log(SentryLevel.Warning,
"Exception while releasing the lockfile on the config directory.", ex);
}
}
public SentrySdk.CrashedLastRun CrashedLastRun()
{
if (_options.CrashedLastRun is null)
{
_options.DiagnosticLogger?.LogDebug("The SDK does not have a 'CrashedLastRun' set. " +
"This might be due to a missing or disabled native integration.");
return SentrySdk.CrashedLastRun.Unknown;
}
return _options.CrashedLastRun.Invoke()
? SentrySdk.CrashedLastRun.Crashed
: SentrySdk.CrashedLastRun.DidNotCrash;
}
public void CaptureFeedback(string message, string? email, string? name, bool addScreenshot)
{
if (string.IsNullOrWhiteSpace(message))
{
_options.LogError("To submit a feedback, you must provide a message.");
return;
}
var hint = addScreenshot
? SentryHint.WithAttachments(
new SentryAttachment(
AttachmentType.Default,
new ByteAttachmentContent(SentryScreenshot.Capture(_options)),
"screenshot.jpg",
"image/jpeg"))
: null;
Sentry.SentrySdk.CurrentHub.CaptureFeedback(message, email, name, hint: hint);
}
internal static void SetUpWindowsPlayerCaching(SentryUnitySdk unitySdk, SentryUnityOptions options)
{
// On Windows-Standalone, we disable cache dir in case multiple app instances run over the same path.
// Note: we cannot use a named Mutex, because Unity doesn't support it. Instead, we create a file with `FileShare.None`.
// https://forum.unity.com/threads/unsupported-internal-call-for-il2cpp-mutex-createmutex_internal-named-mutexes-are-not-supported.387334/
if (ApplicationAdapter.Instance.Platform is not RuntimePlatform.WindowsPlayer ||
options.CacheDirectoryPath is null)
{
return;
}
try
{
unitySdk.LockFile = new FileStream(Path.Combine(options.CacheDirectoryPath, "sentry-unity.lock"), FileMode.OpenOrCreate,
FileAccess.ReadWrite, FileShare.None);
}
catch (Exception ex)
{
options.DiagnosticLogger?.LogWarning("An exception was thrown while trying to " +
"acquire a lockfile on the config directory: .NET event cache will be disabled.", ex);
options.CacheDirectoryPath = null;
options.AutoSessionTracking = false;
}
}
internal static void AddIntegrations(SentryUnityOptions options)
{
if (options.AttachViewHierarchy)
{
options.AddEventProcessor(new ViewHierarchyEventProcessor(options));
}
if (!ApplicationAdapter.Instance.IsEditor && options.AttachScreenshot)
{
options.AddEventProcessor(new ScreenshotEventProcessor(options));
}
if (!ApplicationAdapter.Instance.IsEditor &&
options.UnityInfo.IL2CPP &&
options.Il2CppLineNumberSupportEnabled)
{
if (options.UnityInfo.Il2CppMethods is not null)
{
options.AddExceptionProcessor(new UnityIl2CppEventExceptionProcessor(options));
}
else
{
options.DiagnosticLogger?.LogWarning("Failed to find required IL2CPP methods - Skipping line number support");
}
}
}
internal static void ConfigureUnsupportedPlatformFallbacks(SentryUnityOptions options)
{
if (!options.UnityInfo.IsKnownPlatform())
{
options.DisableFileWrite = true;
// Requires file access, see https://github.com/getsentry/sentry-unity/issues/290#issuecomment-1163608988
if (options.AutoSessionTracking)
{
options.DiagnosticLogger?.LogDebug("Platform support for automatic session tracking is unknown: disabling.");
options.AutoSessionTracking = false;
}
// This is only provided on a best-effort basis for other than the explicitly supported platforms.
if (options.BackgroundWorker is null)
{
options.DiagnosticLogger?.LogDebug("Platform support for background thread execution is unknown: using WebBackgroundWorker.");
options.BackgroundWorker = new WebBackgroundWorker(options, SentryMonoBehaviour.Instance);
}
}
}
}