diff --git a/Directory.Build.props b/Directory.Build.props index bae8e87f0..f292f011d 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -42,6 +42,10 @@ + + + + diff --git a/src/Sentry.Unity.Native/SentryNative.cs b/src/Sentry.Unity.Native/SentryNative.cs index df29f170e..1ecf08c23 100644 --- a/src/Sentry.Unity.Native/SentryNative.cs +++ b/src/Sentry.Unity.Native/SentryNative.cs @@ -37,6 +37,12 @@ internal static void Configure(SentryUnityOptions options, RuntimePlatform platf return; } + if (options.DisableFileWrite) + { + Logger?.LogWarning("Native support disabled because it requires file writing."); + return; + } + try { if (!SentryNativeBridge.Init(options)) @@ -70,17 +76,17 @@ internal static void Configure(SentryUnityOptions options, RuntimePlatform platf // Note: we must actually call the function now and on every other call use the value we get here. // Additionally, we cannot call this multiple times for the same directory, because the result changes // on subsequent runs. Therefore, we cache the value during the whole runtime of the application. - var cacheDirectory = SentryNativeBridge.GetCacheDirectory(options); + var databasePath = SentryNativeBridge.GetDatabasePath(options); var crashedLastRun = false; // In the event the SDK is re-initialized with a different path on disk, we need to track which ones were already read // Similarly we need to cache the value of each call since a subsequent call would return a different value // as the file used on disk to mark it as crashed is deleted after we read it. lock (PerDirectoryCrashInfo) { - if (!PerDirectoryCrashInfo.TryGetValue(cacheDirectory, out crashedLastRun)) + if (!PerDirectoryCrashInfo.TryGetValue(databasePath, out crashedLastRun)) { crashedLastRun = SentryNativeBridge.HandleCrashedLastRun(options); - PerDirectoryCrashInfo.Add(cacheDirectory, crashedLastRun); + PerDirectoryCrashInfo.Add(databasePath, crashedLastRun); Logger? .LogDebug("Native SDK reported: 'crashedLastRun': '{0}'", crashedLastRun); diff --git a/src/Sentry.Unity.Native/SentryNativeBridge.cs b/src/Sentry.Unity.Native/SentryNativeBridge.cs index f3d018e33..81ac5c721 100644 --- a/src/Sentry.Unity.Native/SentryNativeBridge.cs +++ b/src/Sentry.Unity.Native/SentryNativeBridge.cs @@ -2,6 +2,7 @@ using System.IO; using System.Runtime.InteropServices; using Sentry.Extensibility; +using Sentry.Unity.Integrations; using UnityEngine; using AOT; @@ -68,21 +69,21 @@ is RuntimePlatform.LinuxPlayer or RuntimePlatform.LinuxServer sentry_options_set_attach_screenshot(cOptions, options.AttachScreenshot ? 1 : 0); } - var dir = GetCacheDirectory(options); + var databasePath = GetDatabasePath(options); #if SENTRY_NATIVE_SWITCH - Logger?.LogDebug("Setting CacheDirectoryPath: {0}", dir); - sentry_options_set_database_path(cOptions, dir); + Logger?.LogDebug("Setting DatabasePath: {0}", databasePath); + sentry_options_set_database_path(cOptions, databasePath); #else // Note: don't use RuntimeInformation.IsOSPlatform - it will report windows on WSL. if (IsWindows) { - Logger?.LogDebug("Setting CacheDirectoryPath on Windows: {0}", dir); - sentry_options_set_database_pathw(cOptions, dir); + Logger?.LogDebug("Setting DatabasePath on Windows: {0}", databasePath); + sentry_options_set_database_pathw(cOptions, databasePath); } else { - Logger?.LogDebug("Setting CacheDirectoryPath: {0}", dir); - sentry_options_set_database_path(cOptions, dir); + Logger?.LogDebug("Setting DatabasePath: {0}", databasePath); + sentry_options_set_database_path(cOptions, databasePath); } #endif @@ -111,17 +112,15 @@ internal static bool HandleCrashedLastRun(SentryUnityOptions options) return result; } - internal static string GetCacheDirectory(SentryUnityOptions options) + internal static string GetDatabasePath(SentryUnityOptions options, IApplication? application = null) { - if (options.CacheDirectoryPath is null) + if (options.CacheDirectoryPath is not null) { - // same as the default of sentry-native - return Path.Combine(Directory.GetCurrentDirectory(), ".sentry-native"); - } - else - { - return Path.Combine(options.CacheDirectoryPath, "SentryNative"); + return Path.Combine(options.CacheDirectoryPath, ".sentry-native"); } + + application ??= ApplicationAdapter.Instance; + return Path.Combine(application.PersistentDataPath, ".sentry-native"); } internal static void ReinstallBackend() => sentry_reinstall_backend(); diff --git a/src/Sentry.Unity.Native/SentryNativeSwitch.cs b/src/Sentry.Unity.Native/SentryNativeSwitch.cs index 0f18277b2..6fab13909 100644 --- a/src/Sentry.Unity.Native/SentryNativeSwitch.cs +++ b/src/Sentry.Unity.Native/SentryNativeSwitch.cs @@ -89,6 +89,10 @@ internal static void Configure(SentryUnityOptions options, RuntimePlatform platf return; } + // Setting CacheDirectoryPath here so that GetDatabasePath() finds it in step 1 of its + // cascading resolution. On Switch, DisableFileWrite is true, which prevents the .NET + // CachingTransport from using this path for offline caching — it only serves as the + // base for the native database path. Logger?.LogDebug("Setting native cache directory: {0}", cachePath); options.CacheDirectoryPath = cachePath;