@@ -68,6 +68,7 @@ private long lastUpdated
6868 private bool isFetching = false ;
6969 private bool isParsing = false ;
7070 private bool canRefetch => ! ( isFetching || isParsing ) ;
71+ private string statusMessage ;
7172
7273 private int tab ;
7374
@@ -80,7 +81,7 @@ public static void ShowWindow()
8081 Vector2 scrollPos = Vector2 . zero ;
8182 private void OnGUI ( )
8283 {
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 ) ) ) ) ) ;
8485 scrollPos = GUILayout . BeginScrollView ( scrollPos ) ;
8586 tab = GUILayout . Toolbar ( tab , new string [ ] { "GitHub" , "Commits" } ) ;
8687 if ( tab == 0 )
@@ -120,7 +121,7 @@ private void OnGUI()
120121 EditorGUILayout . LabelField ( "Release date: " + DateTime . Parse ( DateTime . Parse ( releases [ i ] . published_at ) . ToString ( ) ) , EditorStyles . miniBoldLabel ) ;
121122
122123 if ( currentVersion != releases [ i ] . tag_name && GUILayout . Button ( "Install" ) )
123- InstallRelease ( i ) ;
124+ EditorCoroutine . Start ( InstallRelease ( i ) ) ;
124125
125126 EditorGUI . indentLevel -- ;
126127 }
@@ -129,19 +130,20 @@ private void OnGUI()
129130 }
130131 else if ( tab == 1 )
131132 {
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 ) ;
133134 }
134135 GUILayout . EndScrollView ( ) ;
135136 GUILayout . EndArea ( ) ;
136137
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 ) ) ) ) ) ;
138139
139140 string lastUpdatedString = lastUpdated == 0 ? "Never" : new DateTime ( lastUpdated ) . ToShortTimeString ( ) ;
140141 GUILayout . Label ( "Last checked: " + lastUpdatedString , EditorStyles . centeredGreyMiniLabel ) ;
141142
142- string fetchButton = isFetching ? "Fetching..." : isParsing ? "Parsing..." : "Fetch releases" ;
143- if ( GUILayout . Button ( fetchButton ) && canRefetch )
143+ if ( canRefetch && GUILayout . Button ( "Fetch releases" ) )
144144 EditorCoroutine . Start ( GetReleases ( ) ) ;
145+ if ( ! string . IsNullOrEmpty ( statusMessage ) )
146+ GUILayout . Label ( statusMessage , EditorStyles . centeredGreyMiniLabel ) ;
145147 if ( GUILayout . Button ( "Reset defaults" ) )
146148 {
147149 releases = new GithubRelease [ 0 ] ;
@@ -158,93 +160,147 @@ private void OnGUI()
158160 Repaint ( ) ;
159161 }
160162
161- private void InstallRelease ( int index )
163+ private IEnumerator InstallRelease ( int index )
162164 {
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 ;
163174 for ( int i = 0 ; i < releases [ index ] . assets . Length ; i ++ )
164175 {
165176 WWW www = new WWW ( releases [ index ] . assets [ i ] . browser_download_url ) ;
166177 while ( ! www . isDone && string . IsNullOrEmpty ( www . error ) )
167178 {
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 ;
169181 }
170182
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 ) ;
173200
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+ }
175207
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+ }
178210 }
179211
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
181216 AssetDatabase . Refresh ( ) ;
182217 }
183218
184219
185- IEnumerator GetReleases ( )
220+ private IEnumerator GetReleases ( )
186221 {
187222 lastUpdated = DateTime . Now . Ticks ;
188223
189224 WWW www = new WWW ( "https://api.github.com/repos/TwoTenPvP/MLAPI/releases" ) ;
190225 isFetching = true ;
191226 while ( ! www . isDone && string . IsNullOrEmpty ( www . error ) )
192227 {
228+ statusMessage = "Fetching releases " + www . progress + "%" ;
193229 yield return null ;
194230 }
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- }
223231
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 )
227239 yield return null ;
228- }
240+ statusMessage = "" ;
229241 }
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+ }
230272
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+ }
233279
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+ }
241282
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 ++ )
243287 {
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+ }
245299 }
300+
301+ statusMessage = "" ;
302+ isParsing = false ;
246303 }
247- isParsing = false ;
248304 }
249305
250306 public class EditorCoroutine
0 commit comments