Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json for Sentry config #80

Merged
merged 17 commits into from
Mar 30, 2021
Merged
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

This file was deleted.

This file was deleted.

202 changes: 0 additions & 202 deletions src/Sentry.Unity.Editor/AndroidManifestConfiguration.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/Sentry.Unity.Editor/SentryTestWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public static SentryTestWindow Open()
public void Dispose()
{
Close(); // calls 'OnLostFocus' implicitly
AssetDatabase.DeleteAsset(SentryOptionsAssetPath);
File.Delete(SentryOptionsAssetPath);
AssetDatabase.Refresh();
}
}
}
110 changes: 93 additions & 17 deletions src/Sentry.Unity.Editor/SentryWindow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using Sentry.Extensibility;
using UnityEditor;
using UnityEngine;

Expand All @@ -11,9 +12,11 @@ public class SentryWindow : EditorWindow
public static SentryWindow OpenSentryWindow()
=> (SentryWindow)GetWindow(typeof(SentryWindow));

protected virtual string SentryOptionsAssetName { get; } = "SentryOptions";
protected virtual string SentryOptionsAssetName { get; } = UnitySentryOptions.ConfigName;

protected string SentryOptionsAssetPath => $"Assets/Resources/Sentry/{SentryOptionsAssetName}.asset";
// Will be used only from Unity Editor
protected string SentryOptionsAssetPath
=> $"{Application.dataPath}/Resources/{UnitySentryOptions.ConfigRootFolder}/{SentryOptionsAssetName}.json";

public UnitySentryOptions Options { get; set; } = null!; // Set by OnEnable()

Expand All @@ -23,22 +26,26 @@ private void OnEnable()
{
SetTitle();

Options = AssetDatabase.LoadAssetAtPath<UnitySentryOptions>(SentryOptionsAssetPath);
if (Options is null)
TryCreateSentryFolder();

Options = LoadUnitySentryOptions();

TryCopyLinkXml(Options.Logger);
}

private UnitySentryOptions LoadUnitySentryOptions()
{
if (File.Exists(SentryOptionsAssetPath))
{
Options = CreateInstance<UnitySentryOptions>();
if (!AssetDatabase.IsValidFolder("Assets/Resources"))
{
AssetDatabase.CreateFolder("Assets", "Resources");
}
if (!AssetDatabase.IsValidFolder("Assets/Resources/Sentry"))
{
AssetDatabase.CreateFolder("Assets/Resources", "Sentry");
}
AssetDatabase.CreateAsset(Options, SentryOptionsAssetPath);
return UnitySentryOptions.LoadFromUnity();
}

EditorUtility.SetDirty(Options);
var unitySentryOptions = new UnitySentryOptions { Enabled = true };
unitySentryOptions
.TryAttachLogger()
.SaveToUnity(SentryOptionsAssetPath);

return unitySentryOptions;
}

private void SetTitle()
Expand Down Expand Up @@ -90,8 +97,8 @@ private void ValidateDsn()
public void OnLostFocus()
{
Validate();
AssetDatabase.SaveAssets();
// TODO: This should be gone

Options.SaveToUnity(SentryOptionsAssetPath);
AssetDatabase.Refresh();
}

Expand Down Expand Up @@ -155,6 +162,75 @@ private void OnGUI()
// project = EditorGUILayout.TextField("Project", project);
// EditorGUILayout.EndToggleGroup();
}

/// <summary>
/// Creates Sentry folder for storing its configs - Assets/Resources/Sentry
/// </summary>
private static void TryCreateSentryFolder()
{
// TODO: revise, 'Resources' is a special Unity folder which is created by default. Not sure this check is needed.
if (!AssetDatabase.IsValidFolder("Assets/Resources"))
{
AssetDatabase.CreateFolder("Assets", "Resources");
}
if (!AssetDatabase.IsValidFolder($"Assets/Resources/{UnitySentryOptions.ConfigRootFolder}"))
{
AssetDatabase.CreateFolder("Assets/Resources", UnitySentryOptions.ConfigRootFolder);
}
}

/// <summary>
/// Find and copy 'link.xml' into current Unity project for IL2CPP builds
/// </summary>
private static void TryCopyLinkXml(IDiagnosticLogger? logger)
{
const string linkXmlFileName = "link.xml";

var linkXmlPath = $"{Application.dataPath}/Resources/{UnitySentryOptions.ConfigRootFolder}/{linkXmlFileName}";
if (File.Exists(linkXmlPath))
{
return;
}

logger?.Log(SentryLevel.Debug, $"'{linkXmlFileName}' is not found. Creating one!");

var linkPath = GetLinkXmlPath(linkXmlFileName);
if (linkPath == null)
{
logger?.Log(SentryLevel.Fatal, $"Couldn't locate '{linkXmlFileName}' in 'Packages'.");
return;
}

var linkXmlAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(linkPath);
File.WriteAllBytes(linkXmlPath, linkXmlAsset.bytes);
}

/// <summary>
/// Get Unity path to 'link.xml' file from `Packages` folder.
///
/// Release UPM:
/// Given: link.xml
/// Returns: Packages/io.sentry.unity/Runtime/link.xml
///
/// Dev UPM:
/// Given: link.xml
/// Returns: Packages/io.sentry.unity.dev/Runtime/link.xml
/// </summary>
private static string? GetLinkXmlPath(string linkXmlFileName)
{
var assetIds = AssetDatabase.FindAssets(UnitySentryOptions.PackageName, new [] { "Packages" });
for (var i = 0; i < assetIds.Length; i++)
{
var assetName = AssetDatabase.GUIDToAssetPath(assetIds[i]);
if (assetName.Contains("Runtime"))
{
var linkFolderPath = Path.GetDirectoryName(assetName)!;
return Path.Combine(linkFolderPath, linkXmlFileName);
}
}

return null;
}
}

public readonly struct ValidationError
Expand Down
Loading