Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 01749ef

Browse files
committed
Grab unity version and access ApplicationCache only on the main thread
ApplicationCache is not threadsafe, if it gets instantiated on the wrong thread Unity will throw.
1 parent 5c3da92 commit 01749ef

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

src/GitHub.Api/ApplicationManagerBase.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,32 @@ private async Task<NPath> LookForGitInstallationPath()
147147
return await GitClient.FindGitInstallation().SafeAwait();
148148
}
149149

150+
protected void SetupMetrics(string unityVersion, bool firstRun)
151+
{
152+
Logger.Trace("Setup metrics");
153+
154+
var usagePath = Environment.UserCachePath.Combine("usage.json");
155+
156+
string id;
157+
if (!UserSettings.Exists("Id"))
158+
{
159+
id = Guid.NewGuid().ToString();
160+
UserSettings.Set("Id", id);
161+
}
162+
else
163+
{
164+
id = UserSettings.Get("Id");
165+
}
166+
167+
UsageTracker = new UsageTracker(usagePath, id, unityVersion);
168+
UsageTracker.Enabled = UserSettings.Get("MetricsEnabled", true);
169+
170+
if (firstRun)
171+
{
172+
UsageTracker.IncrementLaunchCount();
173+
}
174+
}
175+
150176
private bool disposed = false;
151177
protected virtual void Dispose(bool disposing)
152178
{

src/GitHub.Api/Metrics/UsageTracker.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ class UsageTracker : IUsageTracker
1313
private static IMetricsService metricsService;
1414

1515
private readonly NPath storePath;
16-
private readonly string userTrackingId;
16+
private readonly string id;
17+
private readonly string unityVersion;
1718

1819
private bool firstRun = true;
1920
private Timer timer;
2021

21-
public UsageTracker(NPath storePath, string userTrackingId)
22+
public UsageTracker(NPath storePath, string id, string unityVersion)
2223
{
23-
this.userTrackingId = userTrackingId;
24-
Logger.Trace("Tracking Id:{0}", userTrackingId);
25-
24+
this.id = id;
2625
this.storePath = storePath;
26+
this.unityVersion = unityVersion;
2727

28+
Logger.Trace("id:{0}", id);
2829
RunTimer();
2930
}
3031

src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationManager.cs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public override ITask Run()
3131
Utility.Initialize();
3232

3333
return base.Run()
34-
.Then(_ => {
35-
SetupUserTracking();
36-
})
3734
.ThenInUI(_ =>
3835
{
3936
Logger.Debug("Run");
@@ -45,8 +42,10 @@ public override ITask Run()
4542
if (view != null)
4643
view.Initialize(this);
4744

48-
//logger.Debug("Application Restarted");
49-
}).Start();
45+
return new { Version = Application.unityVersion, FirstRun = ApplicationCache.Instance.FirstRun };
46+
})
47+
.Then((s, x) => SetupMetrics(x.Version, x.FirstRun))
48+
.Start();
5049
}
5150

5251

@@ -142,32 +141,6 @@ protected override void Dispose(bool disposing)
142141
}
143142
}
144143

145-
private void SetupUserTracking()
146-
{
147-
Logger.Trace("Setup User Tracking");
148-
149-
var usagePath = Environment.UserCachePath.Combine("github-unity-usage.json");
150-
151-
string userTrackingId;
152-
if (!UserSettings.Exists("UserTrackingId"))
153-
{
154-
userTrackingId = Guid.NewGuid().ToString();
155-
UserSettings.Set("UserTrackingId", userTrackingId);
156-
}
157-
else
158-
{
159-
userTrackingId = UserSettings.Get("UserTrackingId");
160-
}
161-
162-
UsageTracker = new UsageTracker(usagePath, userTrackingId);
163-
UsageTracker.Enabled = UserSettings.Get("UserTrackingEnabled", true);
164-
165-
if (ApplicationCache.Instance.FirstRun)
166-
{
167-
UsageTracker.IncrementLaunchCount();
168-
}
169-
}
170-
171144
public override IProcessEnvironment GitEnvironment { get { return Platform.GitEnvironment; } }
172145
}
173146
}

0 commit comments

Comments
 (0)