9
9
10
10
namespace GitHub . Unity
11
11
{
12
- internal sealed class ApplicationCache : ScriptableObject
13
- //, ISerializationCallbackReceiver
12
+ internal sealed class ApplicationCache : ScriptableObject , ISerializationCallbackReceiver
14
13
{
15
14
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 ; } }
16
21
17
22
public static ApplicationCache Instance {
18
23
get {
19
- return instance ?? ( instance = CreateInstance ( ) ) ;
24
+ return instance ?? CreateApplicationCache ( EntryPoint . Environment ) ;
20
25
}
21
26
}
22
27
23
- private static ApplicationCache CreateInstance ( )
28
+ private static ApplicationCache CreateApplicationCache ( IEnvironment environment )
24
29
{
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" ;
31
31
32
- if ( System . IO . File . Exists ( GetCachePath ( ) ) )
32
+ if ( System . IO . File . Exists ( cachePath ) )
33
33
{
34
34
Debug . Log ( "Loading from cache" ) ;
35
35
36
- var objects = UnityEditorInternal . InternalEditorUtility . LoadSerializedFileAndForget ( GetCachePath ( ) ) ;
36
+ var objects = UnityEditorInternal . InternalEditorUtility . LoadSerializedFileAndForget ( cachePath ) ;
37
37
if ( objects . Any ( ) )
38
38
{
39
- var applicationCache = objects [ 0 ] as ApplicationCache ;
40
- if ( applicationCache != null )
39
+ instance = objects [ 0 ] as ApplicationCache ;
40
+ if ( instance != null )
41
41
{
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 ;
44
46
}
45
47
}
46
48
}
47
49
48
50
Debug . Log ( "Creating instance" ) ;
49
- var createdInstance = CreateInstance < ApplicationCache > ( ) ;
50
- createdInstance . Initialize ( ) ;
51
-
52
- return createdInstance ;
51
+ instance = CreateInstance < ApplicationCache > ( ) ;
52
+ return instance . Initialize ( ) ;
53
53
}
54
54
55
- [ SerializeField ] public bool Initialized ;
56
-
57
- [ SerializeField ] public string CreatedDate ;
58
-
59
- public void Initialize ( )
55
+ private ApplicationCache Initialize ( )
60
56
{
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 ;
67
60
}
68
61
69
- private static string GetCachePath ( )
62
+ private void Flush ( )
70
63
{
71
- return Application . dataPath + "/../Temp/github_cache.yaml" ;
64
+ UnityEditorInternal . InternalEditorUtility . SaveToSerializedFileAndForget ( new Object [ ] { this } , cachePath , true ) ;
72
65
}
73
66
74
- private void OnDisable ( )
67
+ void ISerializationCallbackReceiver . OnBeforeSerialize ( )
75
68
{
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 ( ) ) ;
81
70
}
82
71
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
+ }
91
76
}
92
77
93
78
[ InitializeOnLoad ]
94
79
class EntryPoint : ScriptableObject
95
80
{
96
81
private static ILogging logger ;
97
- private static bool cctorCalled = false ;
98
82
99
83
private static ApplicationManager appManager ;
100
84
@@ -108,15 +92,6 @@ static EntryPoint()
108
92
return ;
109
93
}
110
94
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
-
120
95
ServicePointManager . ServerCertificateValidationCallback = ServerCertificateValidationCallback ;
121
96
EditorApplication . update += Initialize ;
122
97
}
@@ -127,8 +102,15 @@ private static void Initialize()
127
102
EditorApplication . update -= Initialize ;
128
103
129
104
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
+
132
114
//try
133
115
//{
134
116
// if (logPath.FileExists())
@@ -138,11 +120,15 @@ private static void Initialize()
138
120
//}
139
121
//catch
140
122
//{}
123
+
141
124
Logging . LoggerFactory = s => new FileLogAdapter ( logPath , s ) ;
142
125
logger = Logging . GetLogger < EntryPoint > ( ) ;
126
+
143
127
Logging . Info ( "Initializing GitHub for Unity version " + ApplicationInfo . Version ) ;
144
128
145
129
ApplicationManager . Run ( ) ;
130
+
131
+ //Logging.Trace("ApplicationCache: " + ApplicationCache.Instance.CreatedDate);
146
132
}
147
133
148
134
private static bool ServerCertificateValidationCallback ( object sender , X509Certificate certificate ,
0 commit comments