Skip to content

Commit

Permalink
refactor: use Application.Quitting instead of SentryMonoBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Mar 4, 2022
1 parent 3706e34 commit 3dfea1c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 44 deletions.
11 changes: 3 additions & 8 deletions src/Sentry.Unity.Native/SentryNative.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;

namespace Sentry.Unity.Native
{
Expand All @@ -17,11 +17,11 @@ public static void Configure(SentryUnityOptions options)
if (options.WindowsNativeSupportEnabled)
{
SentryNativeBridge.Init(options);
SentryMonoBehaviour.AttachOnApplicationQuittingOverride(() =>
ApplicationAdapter.Instance.Quitting += () =>
{
options?.DiagnosticLogger?.LogDebug("Closing the sentry-native SDK");
SentryNativeBridge.Close();
});
};
options.ScopeObserver = new NativeScopeObserver(options);
options.EnableScopeSync = true;
// options.CrashedLastRun = () =>
Expand All @@ -48,9 +48,4 @@ public static void Configure(SentryUnityOptions options)
}
}
}

internal class SentryMonoBehaviour : SentryMonoBehaviourInternal
{
public static void AttachOnApplicationQuittingOverride(Action action) => AttachOnApplicationQuitting(action);
}
}
3 changes: 2 additions & 1 deletion src/Sentry.Unity/Integrations/IApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ internal interface IApplication
RuntimePlatform Platform { get; }
}

internal sealed class ApplicationAdapter : IApplication
/// Semi-internal class to be used by other Sentry.Unity assemblies
public sealed class ApplicationAdapter : IApplication
{
public static readonly ApplicationAdapter Instance = new();

Expand Down
36 changes: 2 additions & 34 deletions src/Sentry.Unity/SentryMonoBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ internal partial class SentryMonoBehaviour
/// </summary>
public event Action? ApplicationPausing;

/// <summary>
/// Hook to receive an event when the application is quitting.
/// <remarks>
/// This is not guaranteed to be called on all platforms. See Unity docs for more details:
/// https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html
/// </remarks>
/// </summary>
public event Action? ApplicationQuitting;

// Keeping internal track of running state because OnApplicationPause and OnApplicationFocus get called during startup and would fire false resume events
private bool _isRunning = true;

Expand Down Expand Up @@ -125,31 +116,8 @@ internal void OnApplicationFocus(bool hasFocus)
}
}

private void OnApplicationQuit()
{
try
{
ApplicationQuitting?.Invoke();
}
catch (Exception e)
{
Debug.LogWarningFormat("ApplicationQuitting.Invoke() threw an exception %s", e.ToString());
}

// The GameObject has to destroy itself since it was created with HideFlags.HideAndDontSave
Destroy(gameObject);
}
}

/// <summary>
/// Sentry-Unity SDK internal class for other Sentry.Unity.* assemblies.
/// You should not use this directly in your application.
/// </summary>
public class SentryMonoBehaviourInternal
{
/// This is `protected internal` to be semi-hidden from users.
protected internal static void AttachOnApplicationQuitting(Action action) =>
SentryMonoBehaviour.Instance.ApplicationQuitting += action;
// The GameObject has to destroy itself since it was created with HideFlags.HideAndDontSave
private void OnApplicationQuit() => Destroy(gameObject);
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Sentry.Unity/SentryUnity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.ComponentModel;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;

namespace Sentry.Unity
{
Expand Down Expand Up @@ -32,7 +33,7 @@ public static void Init(SentryUnityOptions sentryUnityOptions)
{
sentryUnityOptions.DiagnosticLogger?.LogDebug(sentryUnityOptions.ToString());
var sentryDotNet = SentrySdk.Init(sentryUnityOptions);
SentryMonoBehaviour.Instance.ApplicationQuitting += () =>
ApplicationAdapter.Instance.Quitting += () =>
{
sentryUnityOptions.DiagnosticLogger?.LogDebug("Closing the sentry-dotnet SDK");
sentryDotNet.Dispose();
Expand Down

0 comments on commit 3dfea1c

Please sign in to comment.