@@ -68,6 +68,7 @@ private long lastUpdated
68
68
private bool isFetching = false ;
69
69
private bool isParsing = false ;
70
70
private bool canRefetch => ! ( isFetching || isParsing ) ;
71
+ private string statusMessage ;
71
72
72
73
private int tab ;
73
74
@@ -80,7 +81,7 @@ public static void ShowWindow()
80
81
Vector2 scrollPos = Vector2 . zero ;
81
82
private void OnGUI ( )
82
83
{
83
- GUILayout . BeginArea ( new Rect ( 5 , 0 , position . width - 5 , position . height - 60 ) ) ;
84
+ GUILayout . BeginArea ( new Rect ( 5 , 0 , position . width - 5 , position . height - ( 40 + ( ( string . IsNullOrEmpty ( statusMessage ) ? 0 : 20 ) + ( canRefetch ? 20 : 0 ) ) ) ) ) ;
84
85
scrollPos = GUILayout . BeginScrollView ( scrollPos ) ;
85
86
tab = GUILayout . Toolbar ( tab , new string [ ] { "GitHub" , "Commits" } ) ;
86
87
if ( tab == 0 )
@@ -120,7 +121,7 @@ private void OnGUI()
120
121
EditorGUILayout . LabelField ( "Release date: " + DateTime . Parse ( DateTime . Parse ( releases [ i ] . published_at ) . ToString ( ) ) , EditorStyles . miniBoldLabel ) ;
121
122
122
123
if ( currentVersion != releases [ i ] . tag_name && GUILayout . Button ( "Install" ) )
123
- InstallRelease ( i ) ;
124
+ EditorCoroutine . Start ( InstallRelease ( i ) ) ;
124
125
125
126
EditorGUI . indentLevel -- ;
126
127
}
@@ -129,19 +130,20 @@ private void OnGUI()
129
130
}
130
131
else if ( tab == 1 )
131
132
{
132
- EditorGUILayout . LabelField ( "Not yet implemented. The rest API for AppVeyor is proper garbage and is needed to grab the artifact download URLs" , EditorStyles . wordWrappedLabel ) ;
133
+ EditorGUILayout . LabelField ( "Not yet implemented. The rest API for AppVeyor is proper garbage and is needed to grab the artifact download URLs" , EditorStyles . wordWrappedMiniLabel ) ;
133
134
}
134
135
GUILayout . EndScrollView ( ) ;
135
136
GUILayout . EndArea ( ) ;
136
137
137
- GUILayout . BeginArea ( new Rect ( 5 , position . height - 60 , position . width - 5 , 60 ) ) ;
138
+ GUILayout . BeginArea ( new Rect ( 5 , position . height - ( 40 + ( ( string . IsNullOrEmpty ( statusMessage ) ? 0 : 20 ) + ( canRefetch ? 20 : 0 ) ) ) , position . width - 5 , ( 60 + ( ( string . IsNullOrEmpty ( statusMessage ) ? 0 : 20 ) + ( canRefetch ? 20 : 0 ) ) ) ) ) ;
138
139
139
140
string lastUpdatedString = lastUpdated == 0 ? "Never" : new DateTime ( lastUpdated ) . ToShortTimeString ( ) ;
140
141
GUILayout . Label ( "Last checked: " + lastUpdatedString , EditorStyles . centeredGreyMiniLabel ) ;
141
142
142
- string fetchButton = isFetching ? "Fetching..." : isParsing ? "Parsing..." : "Fetch releases" ;
143
- if ( GUILayout . Button ( fetchButton ) && canRefetch )
143
+ if ( canRefetch && GUILayout . Button ( "Fetch releases" ) )
144
144
EditorCoroutine . Start ( GetReleases ( ) ) ;
145
+ if ( ! string . IsNullOrEmpty ( statusMessage ) )
146
+ GUILayout . Label ( statusMessage , EditorStyles . centeredGreyMiniLabel ) ;
145
147
if ( GUILayout . Button ( "Reset defaults" ) )
146
148
{
147
149
releases = new GithubRelease [ 0 ] ;
@@ -158,93 +160,147 @@ private void OnGUI()
158
160
Repaint ( ) ;
159
161
}
160
162
161
- private void InstallRelease ( int index )
163
+ private IEnumerator InstallRelease ( int index )
162
164
{
165
+ statusMessage = "Cleaning lib folder" ;
166
+ yield return null ;
167
+
168
+ if ( Directory . Exists ( Application . dataPath + "/MLAPI/Lib/" ) )
169
+ Directory . Delete ( Application . dataPath + "/MLAPI/Lib/" , true ) ;
170
+
171
+ Directory . CreateDirectory ( Application . dataPath + "/MLAPI/Lib/" ) ;
172
+
173
+ bool downloadFail = false ;
163
174
for ( int i = 0 ; i < releases [ index ] . assets . Length ; i ++ )
164
175
{
165
176
WWW www = new WWW ( releases [ index ] . assets [ i ] . browser_download_url ) ;
166
177
while ( ! www . isDone && string . IsNullOrEmpty ( www . error ) )
167
178
{
168
- EditorGUI . ProgressBar ( new Rect ( 5 , position . height - 60 , position . width , 20 ) , www . progress , "Installing " + i + "/" + releases [ index ] . assets . Length ) ;
179
+ statusMessage = "Downloading " + releases [ index ] . assets [ i ] . name + "(" + ( i + 1 ) + "/" + releases [ index ] . assets . Length + ") " + www . progress + "%" ;
180
+ yield return null ;
169
181
}
170
182
171
- if ( ! Directory . Exists ( Application . dataPath + "/MLAPI/Lib/" ) )
172
- Directory . CreateDirectory ( Application . dataPath + "/MLAPI/Lib/" ) ;
183
+ if ( ! string . IsNullOrEmpty ( www . error ) )
184
+ {
185
+ //Some kind of error
186
+ downloadFail = true ;
187
+ statusMessage = "Failed to download asset " + releases [ index ] . assets [ i ] . name + ". Error: " + www . error ;
188
+ double startTime = EditorApplication . timeSinceStartup ;
189
+ //Basically = yield return new WaitForSeconds(5);
190
+ while ( EditorApplication . timeSinceStartup - startTime <= 5f )
191
+ yield return null ;
192
+ statusMessage = "" ;
193
+ }
194
+ else
195
+ {
196
+ statusMessage = "Writing " + releases [ index ] . assets [ i ] . name + " to disk" ;
197
+ yield return null ;
198
+
199
+ File . WriteAllBytes ( Application . dataPath + "/MLAPI/Lib/" + releases [ index ] . assets [ i ] . name , www . bytes ) ;
173
200
174
- File . WriteAllBytes ( Application . dataPath + "/MLAPI/Lib/" + releases [ index ] . assets [ i ] . name , www . bytes ) ;
201
+ if ( releases [ index ] . assets [ i ] . name . EndsWith ( ".unitypackage" ) )
202
+ {
203
+ statusMessage = "Installing " + releases [ index ] . assets [ i ] . name ;
204
+ yield return null ;
205
+ AssetDatabase . ImportPackage ( Application . dataPath + "/MLAPI/Lib/" + releases [ index ] . assets [ i ] . name , false ) ;
206
+ }
175
207
176
- if ( releases [ index ] . assets [ i ] . name . EndsWith ( ".unitypackage" ) )
177
- AssetDatabase . ImportPackage ( Application . dataPath + "/MLAPI/Lib/" + releases [ index ] . assets [ i ] . name , false ) ;
208
+ yield return null ;
209
+ }
178
210
}
179
211
180
- currentVersion = releases [ index ] . tag_name ;
212
+ yield return null ;
213
+ statusMessage = "" ;
214
+ if ( ! downloadFail )
215
+ currentVersion = releases [ index ] . tag_name ; //Only set this if there was no fail. This is to allow them to still retry the download
181
216
AssetDatabase . Refresh ( ) ;
182
217
}
183
218
184
219
185
- IEnumerator GetReleases ( )
220
+ private IEnumerator GetReleases ( )
186
221
{
187
222
lastUpdated = DateTime . Now . Ticks ;
188
223
189
224
WWW www = new WWW ( "https://api.github.com/repos/TwoTenPvP/MLAPI/releases" ) ;
190
225
isFetching = true ;
191
226
while ( ! www . isDone && string . IsNullOrEmpty ( www . error ) )
192
227
{
228
+ statusMessage = "Fetching releases " + www . progress + "%" ;
193
229
yield return null ;
194
230
}
195
- isFetching = false ;
196
- isParsing = true ;
197
- string json = www . text ;
198
-
199
- //This makes it from a json array to the individual objects in the array.
200
- //The JSON serializer cant take arrays. We have to split it up outselves.
201
- List < string > releasesJson = new List < string > ( ) ;
202
- int depth = 0 ;
203
- StringBuilder builder = new StringBuilder ( ) ;
204
- for ( int i = 1 ; i < json . Length - 1 ; i ++ )
205
- {
206
- if ( json [ i ] == '[' )
207
- depth ++ ;
208
- else if ( json [ i ] == ']' )
209
- depth -- ;
210
- else if ( json [ i ] == '{' )
211
- depth ++ ;
212
- else if ( json [ i ] == '}' )
213
- depth -- ;
214
-
215
- if ( ( depth == 0 && json [ i ] != ',' ) || depth > 0 )
216
- builder . Append ( json [ i ] ) ;
217
-
218
- if ( depth == 0 && json [ i ] == ',' )
219
- {
220
- releasesJson . Add ( builder . ToString ( ) ) ;
221
- builder . Length = 0 ;
222
- }
223
231
224
- //Parse in smaller batches
225
- if ( i % ( json . Length / 30 ) == 0 )
226
- {
232
+ if ( ! string . IsNullOrEmpty ( www . error ) )
233
+ {
234
+ //Some kind of error
235
+ statusMessage = "Failed to fetch releases. Error: " + www . error ;
236
+ double startTime = EditorApplication . timeSinceStartup ;
237
+ //Basically = yield return new WaitForSeconds(5);
238
+ while ( EditorApplication . timeSinceStartup - startTime <= 5f )
227
239
yield return null ;
228
- }
240
+ statusMessage = "" ;
229
241
}
242
+ else
243
+ {
244
+ isFetching = false ;
245
+ isParsing = true ;
246
+ string json = www . text ;
247
+
248
+ //This makes it from a json array to the individual objects in the array.
249
+ //The JSON serializer cant take arrays. We have to split it up outselves.
250
+ List < string > releasesJson = new List < string > ( ) ;
251
+ int depth = 0 ;
252
+ StringBuilder builder = new StringBuilder ( ) ;
253
+ for ( int i = 1 ; i < json . Length - 1 ; i ++ )
254
+ {
255
+ if ( json [ i ] == '[' )
256
+ depth ++ ;
257
+ else if ( json [ i ] == ']' )
258
+ depth -- ;
259
+ else if ( json [ i ] == '{' )
260
+ depth ++ ;
261
+ else if ( json [ i ] == '}' )
262
+ depth -- ;
263
+
264
+ if ( ( depth == 0 && json [ i ] != ',' ) || depth > 0 )
265
+ builder . Append ( json [ i ] ) ;
266
+
267
+ if ( depth == 0 && json [ i ] == ',' )
268
+ {
269
+ releasesJson . Add ( builder . ToString ( ) ) ;
270
+ builder . Length = 0 ;
271
+ }
230
272
231
- releases = new GithubRelease [ releasesJson . Count ] ;
232
- foldoutStatus = new bool [ releasesJson . Count ] ;
273
+ //Parse in smaller batches
274
+ if ( i % ( json . Length / 100 ) == 0 )
275
+ {
276
+ statusMessage = "Splitting JSON " + ( i / ( float ) json . Length ) * 100f + "%" ;
277
+ yield return null ;
278
+ }
233
279
234
- for ( int i = 0 ; i < releasesJson . Count ; i ++ )
235
- {
236
- releases [ i ] = JsonUtility . FromJson < GithubRelease > ( releasesJson [ i ] ) ;
237
- if ( i == 0 )
238
- foldoutStatus [ i ] = true ;
239
- else
240
- foldoutStatus [ i ] = false ;
280
+ statusMessage = "" ;
281
+ }
241
282
242
- if ( i % ( releasesJson . Count / 30f ) == 0 )
283
+ releases = new GithubRelease [ releasesJson . Count ] ;
284
+ foldoutStatus = new bool [ releasesJson . Count ] ;
285
+
286
+ for ( int i = 0 ; i < releasesJson . Count ; i ++ )
243
287
{
244
- yield return null ;
288
+ releases [ i ] = JsonUtility . FromJson < GithubRelease > ( releasesJson [ i ] ) ;
289
+ if ( i == 0 )
290
+ foldoutStatus [ i ] = true ;
291
+ else
292
+ foldoutStatus [ i ] = false ;
293
+
294
+ if ( i % ( releasesJson . Count / 30f ) == 0 )
295
+ {
296
+ yield return null ;
297
+ statusMessage = "Parsing JSON " + ( i / ( float ) releasesJson . Count ) * 100f + "%" ;
298
+ }
245
299
}
300
+
301
+ statusMessage = "" ;
302
+ isParsing = false ;
246
303
}
247
- isParsing = false ;
248
304
}
249
305
250
306
public class EditorCoroutine
0 commit comments