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

Commit 6c1ab1a

Browse files
Application Cache progress
1 parent 861196a commit 6c1ab1a

File tree

1 file changed

+45
-59
lines changed

1 file changed

+45
-59
lines changed

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

Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,92 +9,76 @@
99

1010
namespace GitHub.Unity
1111
{
12-
internal sealed class ApplicationCache : ScriptableObject
13-
//, ISerializationCallbackReceiver
12+
internal sealed class ApplicationCache : ScriptableObject, ISerializationCallbackReceiver
1413
{
1514
private static ApplicationCache instance;
15+
private static string cachePath;
16+
17+
[SerializeField] private bool firstRun = true;
18+
public bool FirstRun { get { return firstRun; } private set { firstRun = value; Flush(); } }
19+
[SerializeField] private string createdDate;
20+
public string CreatedDate { get { return createdDate; } }
1621

1722
public static ApplicationCache Instance {
1823
get {
19-
return instance ?? (instance = CreateInstance());
24+
return instance ?? CreateApplicationCache(EntryPoint.Environment);
2025
}
2126
}
2227

23-
private static ApplicationCache CreateInstance()
28+
private static ApplicationCache CreateApplicationCache(IEnvironment environment)
2429
{
25-
var foundInstance = FindObjectOfType<ApplicationCache>();
26-
if (foundInstance != null)
27-
{
28-
Debug.Log("Instance Found");
29-
return foundInstance;
30-
}
30+
cachePath = environment.UnityProjectPath + "/Temp/github_cache.yaml";
3131

32-
if (System.IO.File.Exists(GetCachePath()))
32+
if (System.IO.File.Exists(cachePath))
3333
{
3434
Debug.Log("Loading from cache");
3535

36-
var objects = UnityEditorInternal.InternalEditorUtility.LoadSerializedFileAndForget(GetCachePath());
36+
var objects = UnityEditorInternal.InternalEditorUtility.LoadSerializedFileAndForget(cachePath);
3737
if (objects.Any())
3838
{
39-
var applicationCache = objects[0] as ApplicationCache;
40-
if (applicationCache != null)
39+
instance = objects[0] as ApplicationCache;
40+
if (instance != null)
4141
{
42-
Debug.Log("Loading from cache successful");
43-
return applicationCache;
42+
Debug.LogFormat("Loading from cache successful {0}", instance);
43+
if (instance.FirstRun)
44+
instance.FirstRun = false;
45+
return instance;
4446
}
4547
}
4648
}
4749

4850
Debug.Log("Creating instance");
49-
var createdInstance = CreateInstance<ApplicationCache>();
50-
createdInstance.Initialize();
51-
52-
return createdInstance;
51+
instance = CreateInstance<ApplicationCache>();
52+
return instance.Initialize();
5353
}
5454

55-
[SerializeField] public bool Initialized;
56-
57-
[SerializeField] public string CreatedDate;
58-
59-
public void Initialize()
55+
private ApplicationCache Initialize()
6056
{
61-
if (!Initialized)
62-
{
63-
Debug.Log("Initializing");
64-
Initialized = true;
65-
CreatedDate = DateTime.Now.ToLongTimeString();
66-
}
57+
createdDate = DateTime.Now.ToLongTimeString();
58+
Flush();
59+
return this;
6760
}
6861

69-
private static string GetCachePath()
62+
private void Flush()
7063
{
71-
return Application.dataPath + "/../Temp/github_cache.yaml";
64+
UnityEditorInternal.InternalEditorUtility.SaveToSerializedFileAndForget(new Object[] { this }, cachePath, true);
7265
}
7366

74-
private void OnDisable()
67+
void ISerializationCallbackReceiver.OnBeforeSerialize()
7568
{
76-
Debug.Log("ApplicationCache OnDisable");
77-
if (instance != null)
78-
{
79-
UnityEditorInternal.InternalEditorUtility.SaveToSerializedFileAndForget(new Object[] { instance }, GetCachePath(), true);
80-
}
69+
Debug.LogFormat("ApplicationCache OnBeforeSerialize {0} {1}", firstRun, GetInstanceID());
8170
}
8271

83-
// public void OnBeforeSerialize()
84-
// {
85-
// Debug.Log("ApplicationCache OnBeforeSerialize");
86-
// }
87-
//
88-
// public void OnAfterDeserialize()
89-
// {
90-
// }
72+
void ISerializationCallbackReceiver.OnAfterDeserialize()
73+
{
74+
Debug.LogFormat("ApplicationCache OnAfterDeserialize {0} {1}", firstRun, GetInstanceID());
75+
}
9176
}
9277

9378
[InitializeOnLoad]
9479
class EntryPoint : ScriptableObject
9580
{
9681
private static ILogging logger;
97-
private static bool cctorCalled = false;
9882

9983
private static ApplicationManager appManager;
10084

@@ -108,15 +92,6 @@ static EntryPoint()
10892
return;
10993
}
11094

111-
if (cctorCalled)
112-
{
113-
return;
114-
}
115-
cctorCalled = true;
116-
Logging.Info("Initializing GitHub for Unity version " + ApplicationInfo.Version);
117-
118-
Logging.Trace("ApplicationCache: " + ApplicationCache.Instance.CreatedDate);
119-
12095
ServicePointManager.ServerCertificateValidationCallback = ServerCertificateValidationCallback;
12196
EditorApplication.update += Initialize;
12297
}
@@ -127,8 +102,15 @@ private static void Initialize()
127102
EditorApplication.update -= Initialize;
128103

129104
var logPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)
130-
.ToNPath().Combine(ApplicationInfo.ApplicationName, "github-unity.log");
131-
Logging.Info("Initializing GitHub for Unity log file: " + logPath);
105+
.ToNPath().Combine(ApplicationInfo.ApplicationName, "github-unity.log");
106+
107+
if (ApplicationCache.Instance.FirstRun)
108+
{
109+
Logging.Info("Initializing GitHub for Unity version " + ApplicationInfo.Version);
110+
//Logging.Info("ApplicationCache: " + ApplicationCache.Instance.CreatedDate);
111+
Logging.Info("Initializing GitHub for Unity log file: " + logPath);
112+
}
113+
132114
//try
133115
//{
134116
// if (logPath.FileExists())
@@ -138,11 +120,15 @@ private static void Initialize()
138120
//}
139121
//catch
140122
//{}
123+
141124
Logging.LoggerFactory = s => new FileLogAdapter(logPath, s);
142125
logger = Logging.GetLogger<EntryPoint>();
126+
143127
Logging.Info("Initializing GitHub for Unity version " + ApplicationInfo.Version);
144128

145129
ApplicationManager.Run();
130+
131+
//Logging.Trace("ApplicationCache: " + ApplicationCache.Instance.CreatedDate);
146132
}
147133

148134
private static bool ServerCertificateValidationCallback(object sender, X509Certificate certificate,

0 commit comments

Comments
 (0)