-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathUnityEventProcessor.cs
More file actions
139 lines (118 loc) · 4.35 KB
/
Copy pathUnityEventProcessor.cs
File metadata and controls
139 lines (118 loc) · 4.35 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
using System;
using Sentry.Extensibility;
using Sentry.Protocol;
using Sentry.Unity.Integrations;
using UnityEngine;
using DeviceOrientation = Sentry.Protocol.DeviceOrientation;
namespace Sentry.Unity;
internal class UnityEventProcessor :
ISentryEventProcessor,
ISentryTransactionProcessor
{
private readonly SentryUnityOptions _sentryOptions;
private readonly ISentryUnityInfo _unityInfo;
private readonly ISceneManager _sceneManager;
private readonly IApplication _application;
public UnityEventProcessor(SentryUnityOptions sentryOptions, ISentryUnityInfo unityInfo, IApplication? application = null, ISceneManager? sceneManager = null)
{
_sentryOptions = sentryOptions;
_unityInfo = unityInfo;
_application = application ?? ApplicationAdapter.Instance;
_sceneManager = sceneManager ?? SceneManagerAdapter.Instance;
}
public SentryTransaction Process(SentryTransaction transaction)
{
SetEventContext(transaction);
return transaction;
}
public SentryEvent Process(SentryEvent @event)
{
SetEventContext(@event);
@event.ServerName = null;
return @event;
}
private void SetEventContext(IEventLike sentryEvent)
{
try
{
PopulateApp(sentryEvent.Contexts.App);
PopulateDevice(sentryEvent.Contexts.Device);
// The Unity context should get set in the UnityScopeIntegration automatically sets it when it gets registered
sentryEvent.Contexts.TryGetValue(Protocol.Unity.Type, out var contextObject);
if (contextObject is not Protocol.Unity unityContext)
{
unityContext = new Protocol.Unity();
sentryEvent.Contexts.Add(Protocol.Unity.Type, unityContext);
}
PopulateUnity(unityContext);
// Populating the SDK Integrations here (for now) instead of UnityScopeIntegration because it cannot be guaranteed
// that it got added last or that there was not an integration added at a later point
PopulateSdkIntegrations(sentryEvent.Sdk);
}
catch (Exception exception)
{
_sentryOptions.LogError(exception, "{0} processing failed.", nameof(SentryEvent));
}
}
private void PopulateApp(App app)
{
if (!MainThreadData.IsMainThread())
{
return;
}
// The Profiler returns '0' if it is not available
var totalAllocatedMemory = UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong();
if (totalAllocatedMemory > 0)
{
app.Memory = totalAllocatedMemory;
}
}
private void PopulateDevice(Device device)
{
if (!MainThreadData.IsMainThread())
{
return;
}
device.BatteryStatus = SystemInfo.batteryStatus.ToString();
var batteryLevel = SystemInfo.batteryLevel;
if (batteryLevel > 0.0)
{
device.BatteryLevel = (short?)(batteryLevel * 100);
}
switch (Input.deviceOrientation)
{
case UnityEngine.DeviceOrientation.Portrait:
case UnityEngine.DeviceOrientation.PortraitUpsideDown:
device.Orientation = DeviceOrientation.Portrait;
break;
case UnityEngine.DeviceOrientation.LandscapeLeft:
case UnityEngine.DeviceOrientation.LandscapeRight:
device.Orientation = DeviceOrientation.Landscape;
break;
case UnityEngine.DeviceOrientation.FaceUp:
case UnityEngine.DeviceOrientation.FaceDown:
// TODO: Add to protocol?
break;
}
}
private void PopulateUnity(Protocol.Unity unity)
{
unity.IsMainThread = MainThreadData.IsMainThread();
if (!MainThreadData.IsMainThread())
{
return;
}
if (_application.IsEditor || _unityInfo.IL2CPP)
{
// Currently an IL2CPP only feature: see https://github.com/getsentry/sentry-unity/issues/2181
unity.ActiveSceneName = _sceneManager.GetActiveScene().Name;
}
}
private void PopulateSdkIntegrations(SdkVersion sdkVersion)
{
foreach (var integrationName in _sentryOptions.SdkIntegrationNames)
{
sdkVersion.AddIntegration(integrationName);
}
}
}