Skip to content

Commit d455238

Browse files
authored
fix event not sent issue on unity mobile devices (#10)
* fix user prefs compatibility issue * add link.xml
1 parent bba511d commit d455238

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

Analytics-CSharp/Analytics-CSharp.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
<RepositoryUrl>https://github.com/segmentio/Analytics-CSharp</RepositoryUrl>
1616
<RepositoryType>git</RepositoryType>
1717
</PropertyGroup>
18-
18+
1919
<ItemGroup>
2020
<None Include="..\LICENSE" Pack="true" PackagePath="LICENSE" />
21+
<None Include="..\README.md" Pack="true" PackagePath="README.md" />
22+
</ItemGroup>
23+
<ItemGroup>
24+
<EmbeddedResource Include="Resources\link.xml">
25+
<LogicalName>Segment.Analytics.CSharp.xml</LogicalName>
26+
</EmbeddedResource>
2127
</ItemGroup>
2228
<ItemGroup>
2329
<None Remove="Segment\" />

Analytics-CSharp/Resources/link.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<linker>
2+
<assembly fullname="Segment.Analytics.CSharp" preserve="all" />
3+
<assembly fullname="Coroutine.NET" preserve="all" />
4+
<assembly fullname="Newtonsoft.Json" preserve="all" />
5+
<assembly fullname="Serialization.NET" preserve="all" />
6+
<assembly fullname="Sovran.NET" preserve="all" />
7+
<assembly fullname="System.Core">
8+
<type fullname="System.Linq.Expressions.Interpreter.LightLambda" preserve="all" />
9+
</assembly>
10+
</linker>
11+

Analytics-CSharp/Segment/Analytics/Utilities/UserPrefs.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Runtime.Serialization;
54
using System.Threading;
65
using System.Threading.Tasks;
76
using Segment.Concurrent;
7+
using Segment.Serialization;
88

99
namespace Segment.Analytics.Utilities
1010
{
@@ -67,9 +67,9 @@ public int GetInt(string key, int defaultValue = -1)
6767

6868
try
6969
{
70-
ret = (int)cache[key];
70+
ret = Convert.ToInt32(cache[key]);
7171
}
72-
catch
72+
catch
7373
{
7474
ret = defaultValue;
7575
}
@@ -88,7 +88,7 @@ public float GetFloat(string key, float defaultValue = -1.0f)
8888

8989
try
9090
{
91-
ret = (float)cache[key];
91+
ret = float.Parse(Convert.ToString(cache[key]));
9292
}
9393
catch
9494
{
@@ -109,7 +109,7 @@ public string GetString(string key, string defaultValue = null)
109109

110110
try
111111
{
112-
ret = (string)cache[key];
112+
ret = Convert.ToString(cache[key]);
113113
}
114114
catch
115115
{
@@ -195,21 +195,24 @@ private void WriteToFile(long memoryEpochSnapshot)
195195
}
196196
}
197197

198+
FileStream fs = null;
198199
try
199200
{
200-
var fs = _file.Open(FileMode.Create);
201-
var serializer = new DataContractSerializer(cache.GetType());
202-
serializer.WriteObject(fs, cache);
201+
fs = _file.Open(FileMode.Create);
202+
var json = JsonUtility.ToJson(cache);
203+
var bytes = json.GetBytes();
204+
fs.Write(bytes, 0, bytes.Length);
203205
fs.Close();
204-
206+
205207
// successfully updated file, can safely delete backup and return
206208
_backupFile.Delete();
207209
diskEpoch = memoryEpoch;
208-
210+
209211
return;
210212
}
211213
catch (Exception e)
212214
{
215+
fs?.Close();
213216
Analytics.logger?.LogError(e, "Error encountered updating file.");
214217
}
215218

@@ -263,31 +266,25 @@ private void LoadFromDisk()
263266
}
264267
}
265268

266-
FileStream fs = null;
267269
Dictionary<string, object> dict = null;
268270

269271
try
270272
{
271-
fs = _file.Open(FileMode.OpenOrCreate);
272-
if (fs.Length == 0)
273+
var json = File.ReadAllText(_file.FullName);
274+
if (string.IsNullOrEmpty(json))
273275
{
274276
dict = new Dictionary<string, object>();
275277
}
276278
else
277279
{
278-
var deserializer = new DataContractSerializer(cache.GetType());
279-
dict = (Dictionary<string, object>) deserializer.ReadObject(fs);
280+
dict = JsonUtility.FromJson<Dictionary<string, object>>(json);
280281
}
281282
}
282283
catch (Exception e)
283284
{
284285
Analytics.logger?.LogError(e, "Error on deserializing cached user prefs.");
285286
thrown = e;
286287
}
287-
finally
288-
{
289-
fs?.Close();
290-
}
291288

292289
lock (mutex)
293290
{

0 commit comments

Comments
 (0)