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

Commit 6d19ff7

Browse files
Initial ApplicationCache object
1 parent 85913ad commit 6d19ff7

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

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

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,85 @@
1-
using System.IO;
1+
using System;
2+
using System.Linq;
23
using System.Net;
34
using System.Net.Security;
45
using System.Security.Cryptography.X509Certificates;
56
using UnityEditor;
67
using UnityEngine;
8+
using Object = UnityEngine.Object;
79

810
namespace GitHub.Unity
911
{
12+
[InitializeOnLoad]
13+
internal sealed class ApplicationCache : ScriptableObject
14+
{
15+
static ApplicationCache()
16+
{
17+
cachePath = Application.dataPath + "/../Temp/github_cache.yaml";
18+
}
19+
20+
private static ApplicationCache instance;
21+
private static string cachePath;
22+
23+
public static ApplicationCache Instance {
24+
get {
25+
return instance ?? (instance = CreateInstance());
26+
}
27+
}
28+
29+
private static ApplicationCache CreateInstance()
30+
{
31+
var foundInstance = FindObjectOfType<ApplicationCache>();
32+
if (foundInstance != null)
33+
{
34+
Debug.Log("Instance Found");
35+
return foundInstance;
36+
}
37+
38+
if (System.IO.File.Exists(cachePath))
39+
{
40+
Debug.Log("Loading from cache");
41+
42+
var objects = UnityEditorInternal.InternalEditorUtility.LoadSerializedFileAndForget(cachePath);
43+
if (objects.Any())
44+
{
45+
var applicationCache = objects[0] as ApplicationCache;
46+
if (applicationCache != null)
47+
{
48+
Debug.Log("Loading from cache successful");
49+
return applicationCache;
50+
}
51+
}
52+
}
53+
54+
Debug.Log("Creating instance");
55+
var createdInstance = CreateInstance<ApplicationCache>();
56+
createdInstance.Initialize();
57+
58+
return createdInstance;
59+
}
60+
61+
[SerializeField] public bool Initialized;
62+
63+
[SerializeField] public string CreatedDate;
64+
65+
public void Initialize()
66+
{
67+
if (!Initialized)
68+
{
69+
Debug.Log("Initializing");
70+
Initialized = true;
71+
CreatedDate = DateTime.Now.ToLongTimeString();
72+
}
73+
}
74+
75+
private void OnDisable()
76+
{
77+
Debug.Log("ApplicationCache Disable");
78+
79+
UnityEditorInternal.InternalEditorUtility.SaveToSerializedFileAndForget(new Object[] { Instance }, cachePath, true);
80+
}
81+
}
82+
1083
[InitializeOnLoad]
1184
class EntryPoint : ScriptableObject
1285
{

0 commit comments

Comments
 (0)