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

Commit 6e14fc2

Browse files
authored
Merge pull request #318 from github/enhancement/application-cache
Application Cache
2 parents da3c355 + 522799f commit 6e14fc2

File tree

3 files changed

+89
-21
lines changed

3 files changed

+89
-21
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using UnityEditorInternal;
5+
using UnityEngine;
6+
using Object = UnityEngine.Object;
7+
8+
namespace GitHub.Unity
9+
{
10+
sealed class ApplicationCache : ScriptableObject, ISerializationCallbackReceiver
11+
{
12+
private static ApplicationCache instance;
13+
private static string cachePath;
14+
15+
[SerializeField] private bool firstRun = true;
16+
public bool FirstRun
17+
{
18+
get { return firstRun; }
19+
private set
20+
{
21+
firstRun = value;
22+
Flush();
23+
}
24+
}
25+
26+
[SerializeField] private string createdDate;
27+
public string CreatedDate
28+
{
29+
get { return createdDate; }
30+
}
31+
32+
public static ApplicationCache Instance
33+
{
34+
get { return instance ?? CreateApplicationCache(EntryPoint.Environment); }
35+
}
36+
37+
private static ApplicationCache CreateApplicationCache(IEnvironment environment)
38+
{
39+
cachePath = environment.UnityProjectPath + "/Temp/github_cache.yaml";
40+
41+
if (File.Exists(cachePath))
42+
{
43+
var objects = InternalEditorUtility.LoadSerializedFileAndForget(cachePath);
44+
if (objects.Any())
45+
{
46+
instance = objects[0] as ApplicationCache;
47+
if (instance != null)
48+
{
49+
if (instance.FirstRun)
50+
{
51+
instance.FirstRun = false;
52+
}
53+
54+
return instance;
55+
}
56+
}
57+
}
58+
59+
instance = CreateInstance<ApplicationCache>();
60+
instance.createdDate = DateTime.Now.ToLongTimeString();
61+
instance.Flush();
62+
63+
return instance;
64+
}
65+
66+
private void Flush()
67+
{
68+
InternalEditorUtility.SaveToSerializedFileAndForget(new Object[] { this }, cachePath, true);
69+
}
70+
71+
void ISerializationCallbackReceiver.OnBeforeSerialize()
72+
{
73+
}
74+
75+
void ISerializationCallbackReceiver.OnAfterDeserialize()
76+
{
77+
}
78+
}
79+
}

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.IO;
2-
using System.Net;
1+
using System.Net;
32
using System.Net.Security;
43
using System.Security.Cryptography.X509Certificates;
54
using UnityEditor;
@@ -11,7 +10,6 @@ namespace GitHub.Unity
1110
class EntryPoint : ScriptableObject
1211
{
1312
private static ILogging logger;
14-
private static bool cctorCalled = false;
1513

1614
private static ApplicationManager appManager;
1715

@@ -25,13 +23,6 @@ static EntryPoint()
2523
return;
2624
}
2725

28-
if (cctorCalled)
29-
{
30-
return;
31-
}
32-
cctorCalled = true;
33-
Logging.Info("Initializing GitHub for Unity version " + ApplicationInfo.Version);
34-
3526
ServicePointManager.ServerCertificateValidationCallback = ServerCertificateValidationCallback;
3627
EditorApplication.update += Initialize;
3728
}
@@ -42,19 +33,16 @@ private static void Initialize()
4233
EditorApplication.update -= Initialize;
4334

4435
var logPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)
45-
.ToNPath().Combine(ApplicationInfo.ApplicationName, "github-unity.log");
46-
Logging.Info("Initializing GitHub for Unity log file: " + logPath);
47-
//try
48-
//{
49-
// if (logPath.FileExists())
50-
// {
51-
// logPath.Move(logPath.Parent.Combine(string.Format("github-unity-{0}.log"), System.DateTime.Now.ToString("s")));
52-
// }
53-
//}
54-
//catch
55-
//{}
36+
.ToNPath().Combine(ApplicationInfo.ApplicationName, "github-unity.log");
37+
38+
if (ApplicationCache.Instance.FirstRun)
39+
{
40+
Logging.Info("Initializing GitHub for Unity version " + ApplicationInfo.Version);
41+
}
42+
5643
Logging.LoggerFactory = s => new FileLogAdapter(logPath, s);
5744
logger = Logging.GetLogger<EntryPoint>();
45+
5846
Logging.Info("Initializing GitHub for Unity version " + ApplicationInfo.Version);
5947

6048
ApplicationManager.Run();

src/UnityExtension/Assets/Editor/GitHub.Unity/GitHub.Unity.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
</Reference>
7373
</ItemGroup>
7474
<ItemGroup>
75+
<Compile Include="ApplicationCache.cs" />
7576
<Compile Include="ApplicationManager.cs" />
7677
<Compile Include="Extensions\ActionExtensions.cs" />
7778
<Compile Include="Misc\RepositoryInitializer.cs" />

0 commit comments

Comments
 (0)