|
1 |
| -using System.IO; |
| 1 | +using System; |
| 2 | +using System.Linq; |
2 | 3 | using System.Net;
|
3 | 4 | using System.Net.Security;
|
4 | 5 | using System.Security.Cryptography.X509Certificates;
|
5 | 6 | using UnityEditor;
|
6 | 7 | using UnityEngine;
|
| 8 | +using Object = UnityEngine.Object; |
7 | 9 |
|
8 | 10 | namespace GitHub.Unity
|
9 | 11 | {
|
| 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 | + |
10 | 83 | [InitializeOnLoad]
|
11 | 84 | class EntryPoint : ScriptableObject
|
12 | 85 | {
|
|
0 commit comments