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
65 changes: 65 additions & 0 deletions samples/unity-of-bugs/Assets/Resources/Sentry/link.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<linker>
<assembly fullname="Sentry" preserve="all"/>
<assembly fullname="Sentry.Unity" preserve="all"/>
<assembly fullname="System.Text.Json" preserve="all"/>

<assembly fullname="System">
<type fullname="System.ComponentModel.TypeConverter" preserve="all"/>
<type fullname="System.ComponentModel.ArrayConverter" preserve="all"/>
<type fullname="System.ComponentModel.BaseNumberConverter" preserve="all"/>
<type fullname="System.ComponentModel.BooleanConverter" preserve="all"/>
<type fullname="System.ComponentModel.ByteConverter" preserve="all"/>
<type fullname="System.ComponentModel.CharConverter" preserve="all"/>
<type fullname="System.ComponentModel.CollectionConverter" preserve="all"/>
<type fullname="System.ComponentModel.ComponentConverter" preserve="all"/>
<type fullname="System.ComponentModel.CultureInfoConverter" preserve="all"/>
<type fullname="System.ComponentModel.DateTimeConverter" preserve="all"/>
<type fullname="System.ComponentModel.DecimalConverter" preserve="all"/>
<type fullname="System.ComponentModel.DoubleConverter" preserve="all"/>
<type fullname="System.ComponentModel.EnumConverter" preserve="all"/>
<type fullname="System.ComponentModel.ExpandableObjectConverter" preserve="all"/>
<type fullname="System.ComponentModel.Int16Converter" preserve="all"/>
<type fullname="System.ComponentModel.Int32Converter" preserve="all"/>
<type fullname="System.ComponentModel.Int64Converter" preserve="all"/>
<type fullname="System.ComponentModel.NullableConverter" preserve="all"/>
<type fullname="System.ComponentModel.SByteConverter" preserve="all"/>
<type fullname="System.ComponentModel.SingleConverter" preserve="all"/>
<type fullname="System.ComponentModel.StringConverter" preserve="all"/>
<type fullname="System.ComponentModel.TimeSpanConverter" preserve="all"/>
<type fullname="System.ComponentModel.UInt16Converter" preserve="all"/>
<type fullname="System.ComponentModel.UInt32Converter" preserve="all"/>
<type fullname="System.ComponentModel.UInt64Converter" preserve="all"/>
</assembly>


<!--https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.lambdaexpression.name?view=netframework-4.7.2&viewFallbackFrom=netframework-2.0-->
<assembly fullname="System.Core">
<type fullname="System.Linq.Expressions" preserve="all"/>
<type fullname="System.Linq.Expressions.*" preserve="all"/>
<type fullname="System.Linq.Expressions.Interpreter.LightLambda" preserve="all"/>
<type fullname="System.Linq.Expressions.LambdaExpression" preserve="all"/>
</assembly>
<assembly fullname="System.Linq.Expressions">
<type fullname="System.Linq.Expressions" preserve="all"/>
<type fullname="System.Linq.Expressions.*" preserve="all"/>
<type fullname="System.Linq.Expressions.Interpreter.LightLambda" preserve="all"/>
<type fullname="System.Linq.Expressions.LambdaExpression" preserve="all"/>
</assembly>
<assembly fullname="System.Runtime">
<type fullname="System.Exception" preserve="all"/>
<type fullname="System.Exception" preserve="all"/>
<type fullname="System.Diagnostics.StackTrace" preserve="all"/>
</assembly>
<assembly fullname="mscorlib">
<type fullname="System.Exception" preserve="all"/>
<type fullname="System.Diagnostics.StackTrace" preserve="all"/>
</assembly>
<assembly fullname="netstandard">
<type fullname="System.Linq.Expressions" preserve="all"/>
<type fullname="System.Linq.Expressions.*" preserve="all"/>
<type fullname="System.Linq.Expressions.Interpreter.LightLambda" preserve="all"/>
<type fullname="System.Linq.Expressions.LambdaExpression" preserve="all"/>
<type fullname="System.Exception" preserve="all"/>
<type fullname="System.Diagnostics.StackTrace" preserve="all"/>
</assembly>
</linker>
7 changes: 7 additions & 0 deletions samples/unity-of-bugs/Assets/Resources/Sentry/link.xml.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 45 additions & 5 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 System.Text.Json;
using UnityEditor;
using UnityEngine;

Expand All @@ -15,16 +16,27 @@ public static SentryWindow OpenSentryWindow()

protected string SentryOptionsAssetPath => $"Assets/Resources/Sentry/{SentryOptionsAssetName}.asset";

protected string SentryOptionsJsonPath => $"Sentry/SentryOptionsJson";

// Will be used only from Unity Editor
protected string SentryOptionsJsonPathFull => $"{Application.dataPath}/Resources/{SentryOptionsJsonPath}.json";

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

public event Action<ValidationError> OnValidationError = _ => { };

private readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

private void OnEnable()
{
SetTitle();

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

/*if (Options is null)
{
Options = CreateInstance<UnitySentryOptions>();
if (!AssetDatabase.IsValidFolder("Assets/Resources"))
Expand All @@ -38,7 +50,28 @@ private void OnEnable()
AssetDatabase.CreateAsset(Options, SentryOptionsAssetPath);
}

EditorUtility.SetDirty(Options);
EditorUtility.SetDirty(Options);*/
}

private UnitySentryOptionsJson LoadSentryJsonConfig()
{
if (!File.Exists(SentryOptionsJsonPathFull))
{
var unitySentryOptionsJson = new UnitySentryOptionsJson
{
Dsn = "https://94677106febe46b88b9b9ae5efd18a00@o447951.ingest.sentry.io/5439417",
Enabled = true
};
var emptyOptions = JsonSerializer.Serialize(unitySentryOptionsJson, _jsonOptions);
File.WriteAllText(SentryOptionsJsonPathFull, emptyOptions);

// Must be called, otherwise Unity won't be able to load it with the next call. *.meta should be created for new file
AssetDatabase.Refresh();
}

// We should use `TextAsset` for read-only access in runtime. It's platform agnostic.
var sentryOptionsTextAsset = Resources.Load<TextAsset>(SentryOptionsJsonPath);
return JsonSerializer.Deserialize<UnitySentryOptionsJson>(sentryOptionsTextAsset.text, _jsonOptions)!;
}

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

// Saving `UnitySentryOptions` and not `UnitySentryOptionsJson` for OnGUI backward-compat
var text = JsonSerializer.Serialize(Options, _jsonOptions);
File.WriteAllText(SentryOptionsJsonPathFull, text);
// Write is not enough for Unity, must update its asset database.
AssetDatabase.Refresh();

/*AssetDatabase.SaveAssets();
// TODO: This should be gone
AssetDatabase.Refresh();*/
}

// ReSharper disable once UnusedMember.Local
Expand Down
15 changes: 13 additions & 2 deletions src/Sentry.Unity/SentryInitialization.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using UnityEngine;
using UnityEngine.SceneManagement;
using CompressionLevel = System.IO.Compression.CompressionLevel;
Expand All @@ -26,15 +28,24 @@ public static class SentryInitialization
internal static LogTimeDebounce LogTimeDebounce = new(TimeSpan.FromSeconds(1));

internal static bool IsInit { get; private set; }
private static readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

private static string SentryOptionsJsonPath => $"Sentry/SentryOptionsJson";

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void Init()
{
if (!(Resources.Load("Sentry/SentryOptions") is UnitySentryOptions options))
var sentryOptionsTextAsset = Resources.Load<TextAsset>(SentryOptionsJsonPath);
var optionsJson = JsonSerializer.Deserialize<UnitySentryOptionsJson>(sentryOptionsTextAsset.text, _jsonOptions)!;
var options = optionsJson.ToUnitySentryOptions();
/*if (!(Resources.Load("Sentry/SentryOptions") is UnitySentryOptions options))
{
Debug.LogWarning("Sentry Options asset not found. Did you configure it on Component/Sentry?");
return;
}
}*/

if (!options.Enabled)
{
Expand Down
12 changes: 12 additions & 0 deletions src/Sentry.Unity/UnitySentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ public enum SentryUnityCompression
Fastest = 2,
NoCompression = 3
}

// We need to think carefully about what types should be placed here, because of ILL2CPP stripping
public sealed class UnitySentryOptionsJson
{
public bool Enabled { get; set; }

public string Dsn { get; set; }

public UnitySentryOptions ToUnitySentryOptions()
=> new () { Enabled = Enabled, Dsn = Dsn };
}

[Serializable]
public sealed class UnitySentryOptions : ScriptableObject
{
Expand Down
3 changes: 3 additions & 0 deletions src/Sentry.Unity/link.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<linker>
<assembly fullname="Sentry" preserve="all"/>
<assembly fullname="Sentry.Unity" preserve="all"/>
<assembly fullname="System.Text.Json" preserve="all"/>

<assembly fullname="System">
<type fullname="System.ComponentModel.TypeConverter" preserve="all"/>
Expand Down Expand Up @@ -29,6 +31,7 @@
<type fullname="System.ComponentModel.UInt64Converter" preserve="all"/>
</assembly>


<!--https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.lambdaexpression.name?view=netframework-4.7.2&viewFallbackFrom=netframework-2.0-->
<assembly fullname="System.Core">
<type fullname="System.Linq.Expressions" preserve="all"/>
Expand Down