Skip to content

Commit

Permalink
feat: add support for Unity 2019 (#72)
Browse files Browse the repository at this point in the history
* feat: support Unity 2019

Fixes #71

* chore: fix unit tests

* Reformating null operators for c# downgrade

* chore: clean up sample code

Co-authored-by: Zoolouie <allo1877@colorado.edu>
  • Loading branch information
bobbyg603 and Zoolouie authored May 16, 2022
1 parent 9bc9cbb commit b75d6c9
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 85 deletions.
3 changes: 0 additions & 3 deletions Runtime/BugSplat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public bool CaptureScreenshots
/// <summary>
/// Determines whether BugSplat should post exceptions when user is in the Unity editor.
/// </summary>
///
public bool PostExceptionsInEditor
{
get
Expand All @@ -94,8 +93,6 @@ public bool PostExceptionsInEditor
/// <summary>
/// A guard that prevents Exceptions from being posted in rapid succession and must be able to handle null - defaults to 1 crash every 10 seconds.
/// </summary>
///
// TODO can we be more explicit that the Exception might be null with the Type here?
public Func<Exception, bool> ShouldPostException
{
get
Expand Down
29 changes: 17 additions & 12 deletions Runtime/Client/UnityWebClient.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine.Networking;
#if UNITY_2020_1_OR_NEWER
using static UnityEngine.Networking.UnityWebRequest;
#endif

namespace BugSplatUnity.Runtime.Client
{
internal interface IDownloadHandler
{
string text { get; }
string Text { get; }
}

internal interface IUnityWebRequest
{
UnityWebRequestAsyncOperation SendWebRequest();
Result result { get; }
string error { get; }
long responseCode { get; }
IDownloadHandler downloadHandler { get; }
bool Success { get; }
string Error { get; }
long ResponseCode { get; }
IDownloadHandler DownloadHandler { get; }
}

internal interface IUnityWebClient
Expand All @@ -35,10 +36,14 @@ public IUnityWebRequest Post(string url, Dictionary<string, string> formData)

internal class WrappedUnityWebRequest: IUnityWebRequest
{
public Result result => _request.result;
public string error => _request.error;
public long responseCode => _request.responseCode;
public IDownloadHandler downloadHandler => new WrappedDownloadHandler(_request.downloadHandler);
#if UNITY_2020_1_OR_NEWER
public bool Success => _request.result == UnityWebRequest.Result.Success;
#else
public bool Success => !_request.isHttpError && !_request.isNetworkError;
#endif
public string Error => _request.error;
public long ResponseCode => _request.responseCode;
public IDownloadHandler DownloadHandler => new WrappedDownloadHandler(_request.downloadHandler);

private readonly UnityWebRequest _request;

Expand All @@ -55,7 +60,7 @@ public UnityWebRequestAsyncOperation SendWebRequest()

internal class WrappedDownloadHandler : IDownloadHandler
{
public string text => _downloadHandler.text;
public string Text => _downloadHandler.text;

private readonly DownloadHandler _downloadHandler;

Expand Down
11 changes: 5 additions & 6 deletions Runtime/Client/WebGLExceptionClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using BugSplatDotNetStandard;
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Expand Down Expand Up @@ -34,7 +33,7 @@ public IEnumerator Post(Exception ex, IReportPostOptions options = null)

private IEnumerator PostException(string exception, IReportPostOptions options = null)
{
options ??= new ReportPostOptions();
options = options ?? new ReportPostOptions();

var url = $"https://{_database}.bugsplat.com/post/dotnetstandard/";
var formData = new Dictionary<string, string>()
Expand All @@ -53,13 +52,13 @@ private IEnumerator PostException(string exception, IReportPostOptions options =
var request = UnityWebClient.Post(url, formData);
yield return request.SendWebRequest();

if (request.result != UnityWebRequest.Result.Success)
if (!request.Success)
{
Debug.LogError($"BugSplat error: Could not post exception {request.error}");
Debug.LogError($"BugSplat error: Could not post exception {request.Error}");
yield break;
}

Debug.Log($"BugSplat info: status {request.responseCode}\n {request.downloadHandler.text}");
Debug.Log($"BugSplat info: status {request.ResponseCode}\n {request.DownloadHandler.Text}");
}
}
}
14 changes: 7 additions & 7 deletions Runtime/ReportPostOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace BugSplatUnity
{
public interface IReportPostOptions
{
public List<FileInfo> AdditionalAttachments { get; }
public List<FormDataParam> AdditionalFormDataParams { get; }
public string Description { get; set; }
public string Email { get; set; }
public string Key { get; set; }
public string User { get; set; }
public int CrashTypeId { get; set; }
List<FileInfo> AdditionalAttachments { get; }
List<FormDataParam> AdditionalFormDataParams { get; }
string Description { get; set; }
string Email { get; set; }
string Key { get; set; }
string User { get; set; }
int CrashTypeId { get; set; }
}

public class FormDataParam : IFormDataParam
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Reporter/DotNetStandardExceptionReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public IEnumerator Post(Exception exception, IReportPostOptions options = null,
yield break;
}

options ??= new ReportPostOptions();
options = options ?? new ReportPostOptions();
options.SetNullOrEmptyValues(_clientSettings);
options.CrashTypeId = (int)BugSplatDotNetStandard.BugSplat.ExceptionTypeId.Unity;

Expand Down
4 changes: 2 additions & 2 deletions Runtime/Reporter/ReportUploadGuardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace BugSplatUnity.Runtime.Reporter
{
internal interface IReportUploadGuardService
{
public bool ShouldPostLogMessage(LogType type);
public bool ShouldPostException(Exception exception);
bool ShouldPostLogMessage(LogType type);
bool ShouldPostException(Exception exception);
}

internal class ReportUploadGuardService : IReportUploadGuardService
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Reporter/WebGLReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public IEnumerator Post(Exception ex, IReportPostOptions options = null, Action
yield break;
}

options ??= new ReportPostOptions();
options = options ?? new ReportPostOptions();
options.SetNullOrEmptyValues(ClientSettings);
options.CrashTypeId = (int)BugSplatDotNetStandard.BugSplat.ExceptionTypeId.Unity;

Expand Down
4 changes: 2 additions & 2 deletions Runtime/Reporter/WindowsReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public IEnumerator PostAllCrashes(IReportPostOptions options = null, Action<List

public IEnumerator PostCrash(IDirectoryInfo crashFolder, IReportPostOptions options = null, Action<HttpResponseMessage> callback = null)
{
options ??= new ReportPostOptions();
options = options ?? new ReportPostOptions();

if (!crashFolder.Exists)
{
Expand Down Expand Up @@ -143,7 +143,7 @@ public IEnumerator PostMostRecentCrash(IReportPostOptions options = null, Action

public IEnumerator Post(FileInfo minidump, IReportPostOptions options = null, Action<HttpResponseMessage> callback = null)
{
options ??= new ReportPostOptions();
options = options ?? new ReportPostOptions();
options.SetNullOrEmptyValues(_clientSettings);

yield return Task.Run(
Expand Down
14 changes: 7 additions & 7 deletions Runtime/Util/WrappedDirectoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ namespace BugSplatUnity.Runtime.Util
{
internal interface IDirectoryInfoFactory
{
public IDirectoryInfo CreateDirectoryInfo(string path);
IDirectoryInfo CreateDirectoryInfo(string path);
}

internal interface IDirectoryInfo
{
public IDirectoryInfo[] GetDirectories();
public FileInfo[] GetFiles();
public bool Exists { get; }
public string FullName { get; }
public DateTime LastWriteTime { get; }
public string Name { get; }
IDirectoryInfo[] GetDirectories();
FileInfo[] GetFiles();
bool Exists { get; }
string FullName { get; }
DateTime LastWriteTime { get; }
string Name { get; }
}

class WrappedDirectoryInfo : IDirectoryInfo
Expand Down
3 changes: 0 additions & 3 deletions Samples~/my-unity-crasher/Scripts/BugSplatLayoutButtons.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

Expand All @@ -10,7 +8,6 @@ public class BugSplatLayoutButtons : MonoBehaviour
private readonly Color bugSplatGreen = new Color32(74, 235, 195, 255);
private readonly Color bugSplatBlue = new Color32(58, 163, 255, 255);


void Start()
{
var images = transform.GetComponentsInChildren<Image>();
Expand Down
64 changes: 32 additions & 32 deletions Samples~/my-unity-crasher/Scripts/Crasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,61 +23,61 @@ void Start()
#endif
}

private void generateSampleStackFramesAndThrow()
public void Event_ForceCrash(ForcedCrashCategory category)
{
sampleStackFrame0();
Utils.ForceCrash(category);
}

private void sampleStackFrame0()
public void Event_CatchExceptionThenPostNewBugSplat()
{
sampleStackFrame1();
try
{
GenerateSampleStackFramesAndThrow();
}
catch (Exception ex)
{
var options = new ReportPostOptions()
{
Description = "a new description"
};

StartCoroutine(bugsplat.Post(ex, options, ExceptionCallback));
}
}

private void sampleStackFrame1()
public void Event_ThrowException()
{
sampleStackFrame2();
GenerateSampleStackFramesAndThrow();
}

private void sampleStackFrame2()
private void GenerateSampleStackFramesAndThrow()
{
throwException();
SampleStackFrame0();
}

private void throwException()
private void SampleStackFrame0()
{
throw new Exception("BugSplat rocks!");
SampleStackFrame1();
}

public void Event_ForceCrash(ForcedCrashCategory category)
private void SampleStackFrame1()
{
Utils.ForceCrash(category);
SampleStackFrame2();
}

public void Event_CatchExceptionThenPostNewBugSplat()
private void SampleStackFrame2()
{
try
{
generateSampleStackFramesAndThrow();
}
catch (Exception ex)
{
var options = new ReportPostOptions()
{
Description = "a new description"
};

static void callback()
{
Debug.Log($"Exception post callback!");
};
ThrowException();
}

StartCoroutine(bugsplat.Post(ex, options, callback));
}
private void ThrowException()
{
throw new Exception("BugSplat rocks!");
}

public void Event_ThrowException()
static void ExceptionCallback()
{
generateSampleStackFramesAndThrow();
Debug.Log($"Exception post callback!");
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions Tests/BugSplat.Unity.RuntimeTests.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"references": [
"BugSplat.Unity.Runtime"
],
"includePlatforms": [
"Editor"
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
Expand Down
14 changes: 7 additions & 7 deletions Tests/Runtime/Client/Fakes/FakeUnityWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public IUnityWebRequest Post(string url, Dictionary<string, string> formData)

class FakeUnityWebRequest : IUnityWebRequest
{
public UnityWebRequest.Result result { get; set; } = UnityWebRequest.Result.Success;
public string error { get; set; } = string.Empty;
public long responseCode { get; set; } = 200;
public IDownloadHandler downloadHandler { get; set; } = new FakeDownloadHandler(string.Empty);
public bool Success { get; set; } = true;
public string Error { get; set; } = string.Empty;
public long ResponseCode { get; set; } = 200;
public IDownloadHandler DownloadHandler { get; set; } = new FakeDownloadHandler(string.Empty);

public UnityWebRequestAsyncOperation SendWebRequest()
{
Expand All @@ -47,11 +47,11 @@ public UnityWebRequestAsyncOperation SendWebRequest()

class FakeDownloadHandler : IDownloadHandler
{
public string text { get; }
public string Text { get; }

public FakeDownloadHandler(string txt)
public FakeDownloadHandler(string text)
{
text = txt;
Text = text;
}
}

Expand Down

0 comments on commit b75d6c9

Please sign in to comment.