Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition="!$(MSBuildProjectName.Contains('Editor'))">
<ProjectReference Include="$(RepoRoot)src/sentry-dotnet/src/Sentry.Analyzers/Sentry.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="all" />
</ItemGroup>

<!-- Add reference once we figure out where the DLL is (find Unity version and install location) -->
<Target Name="ReferenceUnity" DependsOnTargets="FindUnity" BeforeTargets="BeforeResolveReferences">
<Error Condition="'$(UnityManagedPath)' == ''" Text="'UnityManagedPath' not defined. Can't find UnityEngine.dll." />
Expand Down
12 changes: 9 additions & 3 deletions src/Sentry.Unity.Native/SentryNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 14 additions & 15 deletions src/Sentry.Unity.Native/SentryNativeBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Runtime.InteropServices;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;
using UnityEngine;
using AOT;

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/Sentry.Unity.Native/SentryNativeSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading