2121 * THE SOFTWARE.
2222 */
2323
24+ using System ;
2425using System . Collections . Generic ;
2526using System . Linq ;
2627using System . Reflection ;
2728using CarterGames . Shared . NotionData ;
2829using CarterGames . Shared . NotionData . Editor ;
2930using CarterGames . NotionData . Filters ;
31+ using Newtonsoft . Json . Linq ;
3032using UnityEditor ;
3133using UnityEngine ;
3234
@@ -41,12 +43,23 @@ public sealed class DownloadAllHandler : EditorWindow
4143 | Fields
4244 ───────────────────────────────────────────────────────────────────────────────────────────────────────────── */
4345
44- private static bool haltOnDownload = true ;
46+ private static bool haltOnError = true ;
4547 private static List < NdAsset > toProcess ;
4648 private static int TotalToProcessed = 0 ;
4749 private static int TotalProcessed = 0 ;
4850 private static bool hasErrorOnDownload ;
4951 private static List < NotionRequestError > silencedErrors ;
52+
53+ /* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
54+ | Properties
55+ ───────────────────────────────────────────────────────────────────────────────────────────────────────────── */
56+
57+ public static bool IsDownloadingAllAssets { get ; private set ; }
58+
59+ public static float AllDownloadProgress01 => TotalProcessed / TotalToProcessed ;
60+
61+ private static NdAsset TargetAsset { get ; set ; }
62+ private static SerializedObject TargetAssetObject { get ; set ; }
5063
5164 /* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
5265 | Menu Item
@@ -55,7 +68,7 @@ public sealed class DownloadAllHandler : EditorWindow
5568 [ MenuItem ( "Tools/Carter Games/Notion Data/Update All Notion Data" , priority = 21 ) ]
5669 private static void DownloadAll ( )
5770 {
58- haltOnDownload = true ;
71+ haltOnError = true ;
5972 TotalToProcessed = 0 ;
6073 TotalProcessed = 0 ;
6174 hasErrorOnDownload = false ;
@@ -69,7 +82,7 @@ private static void DownloadAll()
6982 }
7083 else
7184 {
72- var window = GetWindow < DownloadAllHandler > ( true , "Download Notion Data" ) ;
85+ var window = GetWindow < DownloadAllHandler > ( true , "Download All Notion Data" ) ;
7386 window . minSize = new Vector2 ( 400 , 150 ) ;
7487 window . maxSize = new Vector2 ( 400 , 150 ) ;
7588 }
@@ -92,7 +105,7 @@ private void OnGUI()
92105 EditorGUILayout . LabelField ( "Settings" , EditorStyles . boldLabel ) ;
93106 GeneralUtilEditor . DrawHorizontalGUILine ( ) ;
94107
95- haltOnDownload = EditorGUILayout . Toggle ( new GUIContent ( "Halt download on error:" ) , haltOnDownload ) ;
108+ haltOnError = EditorGUILayout . Toggle ( new GUIContent ( "Halt download on error:" ) , haltOnError ) ;
96109
97110 GUILayout . Space ( 1.5f ) ;
98111 EditorGUILayout . EndVertical ( ) ;
@@ -106,17 +119,24 @@ private void OnGUI()
106119 {
107120 if ( Application . internetReachability == NetworkReachability . NotReachable )
108121 {
109- EditorUtility . DisplayDialog ( "Standalone Notion Data" , "You cannot download data while offline." ,
122+ EditorUtility . DisplayDialog ( "Download All Notion Data" , "You cannot download data while offline." ,
110123 "Continue" ) ;
111124 return ;
112125 }
113126
114- toProcess = NotionDataAccessor . GetAllAssets ( ) ;
127+ NdAssetIndexHandler . UpdateIndex ( ) ;
128+
129+ toProcess = NotionDataAccessor . GetAllAssets ( ) . Where ( t => t . GetType ( ) != typeof ( EditorOnlyNdAsset ) )
130+ . ToList ( ) ;
131+
115132 TotalToProcessed = toProcess . Count ;
116133 TotalProcessed = 0 ;
117134 hasErrorOnDownload = false ;
118135 silencedErrors ??= new List < NotionRequestError > ( ) ;
119136 silencedErrors . Clear ( ) ;
137+ TargetAsset = null ;
138+
139+ IsDownloadingAllAssets = true ;
120140
121141 ProcessNextAsset ( ) ;
122142 }
@@ -130,74 +150,146 @@ private void OnGUI()
130150
131151 private static void ProcessNextAsset ( )
132152 {
133- if ( toProcess . Count <= 0 )
153+ if ( TotalProcessed == TotalToProcessed )
134154 {
135155 OnAllDownloadCompleted ( ) ;
136156 return ;
137157 }
138158
139- var asset = toProcess . First ( ) ;
140- toProcess . RemoveAt ( 0 ) ;
141-
142- var assetObject = new SerializedObject ( asset ) ;
143-
144- if ( assetObject . Fp ( "databaseApiKey" ) == null )
159+ try
145160 {
146- ProcessNextAsset ( ) ;
147- return ;
148- }
149-
150- TotalProcessed ++ ;
161+ TargetAsset = toProcess [ TotalProcessed ] ;
162+ TargetAssetObject = new SerializedObject ( TargetAsset ) ;
163+
164+ if ( TargetAssetObject . Fp ( "databaseApiKey" ) == null )
165+ {
166+ TotalProcessed ++ ;
167+ ProcessNextAsset ( ) ;
168+ Debug . LogWarning ( $ "No api key assigned to asset { TargetAssetObject . targetObject . name } , skipping it.") ;
169+ return ;
170+ }
151171
152- var databaseId = assetObject . Fp ( "linkToDatabase" ) . stringValue . Split ( '/' ) . Last ( ) . Split ( '?' ) . First ( ) ;
172+ var databaseId = TargetAssetObject . Fp ( "linkToDatabase" ) . stringValue . Split ( '/' ) . Last ( ) . Split ( '?' ) . First ( ) ;
153173
154- NotionApiRequestHandler . DataReceived . Remove ( OnAssetDownloadComplete ) ;
155- NotionApiRequestHandler . DataReceived . Add ( OnAssetDownloadComplete ) ;
174+ EditorUtility . DisplayProgressBar ( "Download All Notion Data" , $ "Downloading { TargetAsset . name } ", AllDownloadProgress01 ) ;
156175
157- NotionApiRequestHandler . RequestError . Remove ( OnAssetDownloadComplete ) ;
158- NotionApiRequestHandler . RequestError . Add ( OnAssetDownloadComplete ) ;
159-
160- EditorUtility . DisplayProgressBar ( "Standalone Notion Data" , $ "Downloading { asset . name } ", ( float ) TotalProcessed / TotalToProcessed ) ;
161-
162- NotionApiRequestHandler . ResetRequestData ( ) ;
163-
164- var filters = ( NotionFilterContainer ) assetObject . GetType ( ) . BaseType !
165- . GetField ( "filters" , BindingFlags . NonPublic | BindingFlags . Instance )
166- ! . GetValue ( assetObject . targetObject ) ;
167-
168- var requestData = new NotionRequestData ( asset , databaseId , assetObject . Fp ( "databaseApiKey" ) . stringValue , assetObject . Fp ( "sortProperties" ) . ToSortPropertyArray ( ) , filters , true ) ;
169- NotionApiRequestHandler . WebRequestPostWithAuth ( requestData ) ;
176+ NotionFilterContainer filters = null ;
177+
178+ if ( TargetAssetObject . GetType ( ) . BaseType !
179+ . GetField ( "filters" , BindingFlags . NonPublic | BindingFlags . Instance ) != null )
180+ {
181+ filters = ( NotionFilterContainer ) TargetAssetObject . GetType ( ) . BaseType !
182+ . GetField ( "filters" , BindingFlags . NonPublic | BindingFlags . Instance )
183+ ! . GetValue ( TargetAssetObject . targetObject ) ;
184+ }
185+
186+
187+ var requestData = new NotionRequestData ( TargetAsset , databaseId , TargetAssetObject . Fp ( "databaseApiKey" ) . stringValue , TargetAssetObject . Fp ( "sortProperties" ) . ToSortPropertyArray ( ) , filters , true ) ;
188+
189+ NotionApiRequestManager . RunRequest ( requestData , OnAssetDownloadComplete , OnAssetDownloadComplete ) ;
190+ }
191+ catch ( Exception e )
192+ {
193+ Debug . LogError ( e ) ;
194+ EditorUtility . ClearProgressBar ( ) ;
195+ IsDownloadingAllAssets = false ;
196+ throw ;
197+ }
170198 }
171199
172200
173201 private static void OnAssetDownloadComplete ( NotionRequestResult result )
174202 {
203+ NotionDatabaseQueryResult queryResult ;
204+
205+ try
206+ {
207+ queryResult = NotionDownloadParser . Parse ( result . Data ) ;
208+ }
209+ catch ( Exception e )
210+ {
211+ Debug . LogError ( "Parsing of downloaded data failed. See message below for more information:" ) ;
212+ Debug . LogError ( e ) ;
213+ EditorUtility . ClearProgressBar ( ) ;
214+ throw ;
215+ }
216+
217+ try
218+ {
219+ TargetAsset . GetType ( ) . BaseType . GetMethod ( "Apply" , BindingFlags . NonPublic | BindingFlags . Instance )
220+ ? . Invoke ( TargetAssetObject . targetObject , new object [ ] { queryResult } ) ;
221+
222+ EditorUtility . ClearProgressBar ( ) ;
223+
224+ if ( ! result . SilentResponse )
225+ {
226+ EditorUtility . DisplayDialog ( "Notion Data Download" , "Download completed & parsed successfully" , "Continue" ) ;
227+ }
228+ }
229+ catch ( Exception e )
230+ {
231+ if ( ! haltOnError )
232+ {
233+ hasErrorOnDownload = true ;
234+ silencedErrors . Add ( new NotionRequestError ( TargetAsset , new JObject ( )
235+ {
236+ [ "message" ] = $ "Applying values from parsed data failed. See message below for more information: { e } "
237+ } ) ) ;
238+ }
239+ else
240+ {
241+ Debug . LogError ( "Applying values from parsed data failed. See message below for more information:" ) ;
242+ Debug . LogError ( e ) ;
243+
244+ EditorUtility . ClearProgressBar ( ) ;
245+
246+ if ( ! result . SilentResponse )
247+ {
248+ EditorUtility . DisplayDialog ( "Notion Data Download" , "Download completed, but failed to parse all properties. See console for more information." , "Continue" ) ;
249+ }
250+ }
251+ }
252+
253+ TargetAssetObject . ApplyModifiedProperties ( ) ;
254+ TargetAssetObject . Update ( ) ;
255+
256+ TotalProcessed ++ ;
175257 ProcessNextAsset ( ) ;
176258 }
177259
178260
179261 private static void OnAssetDownloadComplete ( NotionRequestError error )
180262 {
181- if ( ! haltOnDownload )
263+ TotalProcessed ++ ;
264+
265+ if ( ! haltOnError )
182266 {
183267 hasErrorOnDownload = true ;
184268 silencedErrors . Add ( error ) ;
269+
270+ TargetAssetObject . ApplyModifiedProperties ( ) ;
271+ TargetAssetObject . Update ( ) ;
272+
185273 ProcessNextAsset ( ) ;
186274 return ;
187275 }
188276
189277 EditorUtility . ClearProgressBar ( ) ;
190- EditorUtility . DisplayDialog ( "Standalone Notion Data" , $ "Download failed due to an error:\n { error . Asset . name } \n { error . Error } \n { error . Message } ", "Continue" ) ;
278+ EditorUtility . DisplayDialog ( "Download All Notion Data" , $ "Download failed due to an error:\n { error . Asset . name } \n { error . Error } \n { error . Message } ", "Continue" ) ;
279+
280+ TargetAssetObject . ApplyModifiedProperties ( ) ;
281+ TargetAssetObject . Update ( ) ;
191282 }
192283
193284
194285 private static void OnAllDownloadCompleted ( )
195286 {
196287 EditorUtility . ClearProgressBar ( ) ;
288+ IsDownloadingAllAssets = false ;
197289
198290 if ( hasErrorOnDownload )
199291 {
200- if ( EditorUtility . DisplayDialog ( "Standalone Notion Data" ,
292+ if ( EditorUtility . DisplayDialog ( "Download All Notion Data" ,
201293 "Download completed with errors.\n See console for errors." , "Continue" ) )
202294 {
203295 foreach ( var error in silencedErrors )
@@ -208,8 +300,14 @@ private static void OnAllDownloadCompleted()
208300 }
209301 else
210302 {
211- EditorUtility . DisplayDialog ( "Standalone Notion Data" , "Download completed." , "Continue" ) ;
303+ EditorUtility . DisplayDialog ( "Download All Notion Data" , "Download completed." , "Continue" ) ;
212304 }
213305 }
306+
307+
308+ public static void SetProgressMessage ( string message )
309+ {
310+ EditorUtility . DisplayProgressBar ( "Download All Notion Data" , message , AllDownloadProgress01 ) ;
311+ }
214312 }
215313}
0 commit comments