Skip to content

Commit 288badc

Browse files
authored
.NET 3.5 backward compatibility (#93)
* Backward compatibility * Annotations test isolation * Removed not used anymore variable * Version bump * iOS Support * Safe enum comparision x64 vs x86 * variable name improvements * Version bump
1 parent e8c46c2 commit 288badc

14 files changed

+90
-82
lines changed

Runtime/BacktraceClient.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Backtrace.Unity
2424
/// </summary>
2525
public class BacktraceClient : MonoBehaviour, IBacktraceClient
2626
{
27-
public const string VERSION = "3.5.0";
27+
public const string VERSION = "3.5.1-preview.1";
2828

2929
public BacktraceConfiguration Configuration;
3030

@@ -656,7 +656,10 @@ private void OnDestroy()
656656
Application.logMessageReceivedThreaded -= HandleUnityBackgroundException;
657657
#if UNITY_ANDROID || UNITY_IOS
658658
Application.lowMemory -= HandleLowMemory;
659-
_nativeClient?.Disable();
659+
if (_nativeClient != null)
660+
{
661+
_nativeClient.Disable();
662+
}
660663
#endif
661664
}
662665

Runtime/BacktraceDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ public bool EnableBreadcrumbsSupport()
729729
{
730730
return false;
731731
}
732-
return Breadcrumbs.EnableBreadcrumbs(Configuration.BacktraceBreadcrumbsLevel, Configuration.LogLevel);
732+
return _breadcrumbs.EnableBreadcrumbs(Configuration.BacktraceBreadcrumbsLevel, Configuration.LogLevel);
733733
}
734734
}
735735
}

Runtime/Extensions/EnumExtensions.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,21 @@ internal static bool HasFlag(this Enum variable, Enum value)
1212
{
1313
throw new ArgumentException("The checked flag is not from the same type as the checked variable.");
1414
}
15+
// Get the type code of the enumeration
16+
var typeCode = variable.GetTypeCode();
1517

16-
Convert.ToUInt64(value);
17-
ulong num = Convert.ToUInt64(value);
18-
ulong num2 = Convert.ToUInt64(variable);
18+
// If the underlying type of the flag is signed
19+
if (typeCode == TypeCode.SByte || typeCode == TypeCode.Int16 || typeCode == TypeCode.Int32 || typeCode == TypeCode.Int64)
20+
{
21+
return (Convert.ToInt64(variable) & Convert.ToInt64(value)) != 0;
22+
}
1923

20-
return (num2 & num) == num;
24+
// If the underlying type of the flag is unsigned
25+
if (typeCode == TypeCode.Byte || typeCode == TypeCode.UInt16 || typeCode == TypeCode.UInt32 || typeCode == TypeCode.UInt64)
26+
{
27+
return (Convert.ToUInt64(variable) & Convert.ToUInt64(value)) != 0;
28+
}
29+
return false;
2130
}
2231
#endif
2332
}

Runtime/Extensions/StreamExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ namespace Backtrace.Unity.Extensions
66
internal static class StreamExtensions
77
{
88
#if !(NET_STANDARD_2_0 && NET_4_6)
9+
// We pick a value that is the largest multiple of 4096 that is still smaller than the large object heap threshold (85K).
10+
// The CopyTo/CopyToAsync buffer is short-lived and is likely to be collected at Gen0, and it offers a significant
11+
// improvement in Copy performance.
12+
private const int _DefaultCopyBufferSize = 81920;
13+
914
public static void CopyTo(this Stream original, Stream destination)
1015
{
1116
if (destination == null)
@@ -29,7 +34,7 @@ public static void CopyTo(this Stream original, Stream destination)
2934
throw new NotSupportedException("NotSupportedException destination");
3035
}
3136

32-
byte[] array = new byte[4096];
37+
byte[] array = new byte[_DefaultCopyBufferSize];
3338
int count;
3439
while ((count = original.Read(array, 0, array.Length)) != 0)
3540
{

Runtime/Model/JsonData/Annotations.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Globalization;
66
using UnityEngine;
77
using UnityEngine.SceneManagement;
8+
9+
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Backtrace.Unity.Tests.Runtime")]
810
namespace Backtrace.Unity.Model.JsonData
911
{
1012
/// <summary>
@@ -15,14 +17,24 @@ public class Annotations
1517
/// <summary>
1618
/// Backward compatibility helper
1719
/// </summary>
18-
private static Dictionary<string, string> _environmentVariablesCache;
20+
internal static Dictionary<string, string> _environmentVariablesCache;
21+
22+
/// <summary>
23+
/// Determinate if static helper should load environment variables or not.
24+
/// </summary>
25+
internal static bool VariablesLoaded;
26+
27+
/// <summary>
28+
/// Loaded environment variables
29+
/// </summary>
1930
public static Dictionary<string, string> EnvironmentVariablesCache
2031
{
2132
get
2233
{
23-
if (_environmentVariablesCache == null)
34+
if (VariablesLoaded == false)
2435
{
2536
_environmentVariablesCache = SetEnvironmentVariables();
37+
VariablesLoaded = true;
2638
}
2739
return _environmentVariablesCache;
2840
}
@@ -31,24 +43,19 @@ public static Dictionary<string, string> EnvironmentVariablesCache
3143
_environmentVariablesCache = value;
3244
}
3345
}
46+
3447
/// <summary>
35-
/// Backward compatibility helper
48+
/// Backward compatibility - local reference to environment variables
3649
/// </summary>
37-
private Dictionary<string, string> _environmentVariables;
38-
3950
public Dictionary<string, string> EnvironmentVariables
4051
{
4152
get
4253
{
43-
if (_environmentVariables == null)
44-
{
45-
_environmentVariables = EnvironmentVariablesCache;
46-
}
47-
return _environmentVariables;
54+
return EnvironmentVariablesCache;
4855
}
4956
set
5057
{
51-
_environmentVariables = value;
58+
EnvironmentVariablesCache = value;
5259
}
5360
}
5461
/// <summary>

Runtime/Native/Android/NativeClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ private void SetDefaultAttributeMaps()
109109
/// <summary>
110110
/// Anr watcher object
111111
/// </summary>
112+
#pragma warning disable IDE0052 // Remove unread private members
112113
private AndroidJavaObject _anrWatcher;
114+
#pragma warning restore IDE0052 // Remove unread private members
113115

114116
private bool _captureNativeCrashes = false;
115117
private readonly bool _handlerANR = false;

Runtime/Native/iOS/NativeClient.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,26 @@ public void GetAttributes(IDictionary<string, string> result)
125125
{
126126
return;
127127
}
128-
GetNativeAttributes(out IntPtr pUnmanagedArray, out int keysCount);
128+
IntPtr pUnmanagedArray;
129+
int keysCount;
130+
GetNativeAttributes(out pUnmanagedArray, out keysCount);
129131

130132
// calculate struct size for current OS.
131133
// We multiply by 2 because Entry struct has two pointers
132134
var structSize = IntPtr.Size * 2;
135+
const int x86StructSize = 4;
133136
for (int i = 0; i < keysCount; i++)
134137
{
135-
var address = pUnmanagedArray + i * structSize;
136-
Entry entry = Marshal.PtrToStructure<Entry>(address);
138+
IntPtr address;
139+
if (structSize == x86StructSize)
140+
{
141+
address = new IntPtr(pUnmanagedArray.ToInt32() + i * structSize);
142+
}
143+
else
144+
{
145+
address = new IntPtr(pUnmanagedArray.ToInt64() + i + structSize);
146+
}
147+
Entry entry = (Entry)Marshal.PtrToStructure(address, typeof(Entry));
137148
result[entry.Key] = entry.Value;
138149
}
139150

Runtime/Services/BacktraceDatabaseContext.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ public void Dispose()
275275
/// </summary>
276276
public void Clear()
277277
{
278-
var records = BatchRetry.SelectMany(n => n.Value);
279278
TotalRecords = 0;
280279
TotalSize = 0;
281280
//clear all existing batches

Runtime/Services/BacktraceDatabaseFileContext.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,10 @@ public void RemoveOrphaned(IEnumerable<BacktraceDatabaseRecord> existingRecords)
167167
file.Delete();
168168
}
169169
}
170-
#pragma warning disable CS0168
171170
catch (Exception e)
172171
{
173-
#if DEBUG
174-
Debug.Log(e.ToString());
175-
#endif
176-
Debug.LogWarning(string.Format("Cannot remove file in path: {0}", file.FullName));
172+
Debug.LogWarning(string.Format("Cannot remove file in path: {0}. Reason: {1}", file.FullName, e.Message));
177173
}
178-
#pragma warning restore CS0168
179174
}
180175
}
181176

Tests/Runtime/ClientSendTests.cs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void Setup()
2929
public void Cleanup()
3030
{
3131
client.RequestHandler = null;
32+
Annotations.VariablesLoaded = false;
3233
}
3334

3435
[UnityTest]
@@ -73,31 +74,6 @@ public IEnumerator SendReport_MessageReport_ValidSend()
7374
yield return null;
7475
}
7576

76-
[UnityTest]
77-
public IEnumerator PiiTests_ShouldRemoveEnvironmentVariables_AnnotationsShouldntBeAvailable()
78-
{
79-
var trigger = false;
80-
var exception = new Exception("custom exception message");
81-
client.BeforeSend = (BacktraceData data) =>
82-
{
83-
Assert.IsNotNull(data.Annotation.EnvironmentVariables);
84-
data.Annotation.EnvironmentVariables = null;
85-
return data;
86-
};
87-
88-
client.RequestHandler = (string url, BacktraceData data) =>
89-
{
90-
trigger = true;
91-
Assert.IsNull(data.Annotation.EnvironmentVariables);
92-
return new BacktraceResult();
93-
};
94-
client.Send(exception);
95-
96-
yield return new WaitForEndOfFrame();
97-
Assert.IsTrue(trigger);
98-
yield return null;
99-
}
100-
10177
[UnityTest]
10278
public IEnumerator PiiTests_ShouldChangeApplicationDataPath_ApplicationDataPathDoesntHaveUserNameAnymore()
10379
{
@@ -178,5 +154,30 @@ public IEnumerator PiiTests_ShouldRemoveEnvironmentVariableValue_IntegrationShou
178154
Assert.IsTrue(trigger);
179155
yield return null;
180156
}
157+
158+
[UnityTest]
159+
public IEnumerator PiiTests_ShouldRemoveEnvironmentVariables_AnnotationsShouldntBeAvailable()
160+
{
161+
var trigger = false;
162+
var exception = new Exception("custom exception message");
163+
client.BeforeSend = (BacktraceData data) =>
164+
{
165+
Assert.IsNotNull(data.Annotation.EnvironmentVariables);
166+
data.Annotation.EnvironmentVariables = null;
167+
return data;
168+
};
169+
170+
client.RequestHandler = (string url, BacktraceData data) =>
171+
{
172+
trigger = true;
173+
Assert.IsNull(data.Annotation.EnvironmentVariables);
174+
return new BacktraceResult();
175+
};
176+
client.Send(exception);
177+
178+
yield return new WaitForEndOfFrame();
179+
Assert.IsTrue(trigger);
180+
yield return null;
181+
}
181182
}
182183
}

0 commit comments

Comments
 (0)