@@ -29,6 +29,28 @@ public SkyboxInfoRequest([JsonProperty("request")] SkyboxInfo skyboxInfo)
29
29
public SkyboxInfo SkyboxInfo { get ; }
30
30
}
31
31
32
+ [ Preserve ]
33
+ private class SkyboxOperation
34
+ {
35
+ [ Preserve ]
36
+ [ JsonConstructor ]
37
+ public SkyboxOperation (
38
+ [ JsonProperty ( "success" ) ] string success ,
39
+ [ JsonProperty ( "error" ) ] string error )
40
+ {
41
+ Success = success ;
42
+ Error = error ;
43
+ }
44
+
45
+ [ Preserve ]
46
+ [ JsonProperty ( "success" ) ]
47
+ public string Success { get ; }
48
+
49
+ [ Preserve ]
50
+ [ JsonProperty ( "Error" ) ]
51
+ public string Error { get ; }
52
+ }
53
+
32
54
public SkyboxEndpoint ( BlockadeLabsClient client ) : base ( client ) { }
33
55
34
56
protected override string Root => string . Empty ;
@@ -40,8 +62,7 @@ public SkyboxEndpoint(BlockadeLabsClient client) : base(client) { }
40
62
/// <returns>A list of <see cref="SkyboxStyle"/>s.</returns>
41
63
public async Task < IReadOnlyList < SkyboxStyle > > GetSkyboxStylesAsync ( CancellationToken cancellationToken = default )
42
64
{
43
- var endpoint = GetUrl ( "skybox/styles" ) ;
44
- var response = await Rest . GetAsync ( endpoint , parameters : new RestParameters ( client . DefaultRequestHeaders ) , cancellationToken ) ;
65
+ var response = await Rest . GetAsync ( GetUrl ( "skybox/styles" ) , parameters : new RestParameters ( client . DefaultRequestHeaders ) , cancellationToken ) ;
45
66
response . Validate ( ) ;
46
67
return JsonConvert . DeserializeObject < IReadOnlyList < SkyboxStyle > > ( response . Body , client . JsonSerializationOptions ) ;
47
68
}
@@ -102,39 +123,36 @@ public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, i
102
123
103
124
while ( ! cancellationToken . IsCancellationRequested )
104
125
{
105
- await Task . Delay ( pollingInterval ?? 3 * 1000 , cancellationToken )
126
+ await Task . Delay ( pollingInterval ?? 3 * 1000 , CancellationToken . None )
106
127
. ConfigureAwait ( true ) ; // Configure await to make sure we're still in Unity context
107
- skyboxInfo = await GetSkyboxInfoAsync ( skyboxInfo , cancellationToken ) ;
128
+ skyboxInfo = await GetSkyboxInfoAsync ( skyboxInfo , CancellationToken . None ) ;
108
129
109
- if ( skyboxInfo . Status is "pending" or "processing" )
130
+ if ( skyboxInfo . Status is Status . Pending or Status . Processing or Status . Dispatched )
110
131
{
111
132
continue ;
112
133
}
113
134
114
135
break ;
115
136
}
116
137
117
- if ( skyboxInfo . Status != "complete" )
138
+ if ( cancellationToken . IsCancellationRequested )
118
139
{
119
- throw new Exception ( $ "Failed to generate skybox! { skyboxInfo . Id } -> { skyboxInfo . Status } \n Error: { skyboxInfo . ErrorMessage } \n { skyboxInfo } ") ;
140
+ var cancelResult = await CancelSkyboxGenerationAsync ( skyboxInfo , CancellationToken . None ) ;
141
+
142
+ if ( ! cancelResult )
143
+ {
144
+ throw new Exception ( $ "Failed to cancel generation for { skyboxInfo . Id } ") ;
145
+ }
120
146
}
121
147
122
- var downloadTasks = new List < Task > ( 2 )
148
+ cancellationToken . ThrowIfCancellationRequested ( ) ;
149
+
150
+ if ( skyboxInfo . Status != Status . Complete )
123
151
{
124
- Task . Run ( async ( ) =>
125
- {
126
- skyboxInfo . MainTexture = await Rest . DownloadTextureAsync ( skyboxInfo . MainTextureUrl , parameters : null , cancellationToken : cancellationToken ) ;
127
- } , cancellationToken ) ,
128
- Task . Run ( async ( ) =>
129
- {
130
- if ( ! string . IsNullOrWhiteSpace ( skyboxInfo . DepthTextureUrl ) )
131
- {
132
- skyboxInfo . DepthTexture = await Rest . DownloadTextureAsync ( skyboxInfo . DepthTextureUrl , parameters : null , cancellationToken : cancellationToken ) ;
133
- }
134
- } , cancellationToken )
135
- } ;
136
-
137
- await Task . WhenAll ( downloadTasks ) ;
152
+ throw new Exception ( $ "Failed to generate skybox! { skyboxInfo . Id } -> { skyboxInfo . Status } \n Error: { skyboxInfo . ErrorMessage } \n { skyboxInfo } ") ;
153
+ }
154
+
155
+ await skyboxInfo . LoadTexturesAsync ( cancellationToken ) ;
138
156
return skyboxInfo ;
139
157
}
140
158
@@ -150,5 +168,85 @@ public async Task<SkyboxInfo> GetSkyboxInfoAsync(int id, CancellationToken cance
150
168
response . Validate ( ) ;
151
169
return JsonConvert . DeserializeObject < SkyboxInfoRequest > ( response . Body , client . JsonSerializationOptions ) . SkyboxInfo ;
152
170
}
171
+
172
+ /// <summary>
173
+ /// Deletes a skybox by id.
174
+ /// </summary>
175
+ /// <param name="id">The id of the skybox.</param>
176
+ /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
177
+ /// <returns>True, if skybox was successfully deleted.</returns>
178
+ public async Task < bool > DeleteSkyboxAsync ( int id , CancellationToken cancellationToken = default )
179
+ {
180
+ var response = await Rest . DeleteAsync ( GetUrl ( $ "imagine/deleteImagine/{ id } ") , new RestParameters ( client . DefaultRequestHeaders ) , cancellationToken ) ;
181
+ response . Validate ( ) ;
182
+ var skyboxOp = JsonConvert . DeserializeObject < SkyboxOperation > ( response . Body , client . JsonSerializationOptions ) ;
183
+
184
+ const string successStatus = "Item deleted successfully" ;
185
+
186
+ if ( skyboxOp is not { Success : successStatus } )
187
+ {
188
+ throw new Exception ( $ "Failed to cancel generation for skybox { id } !\n { skyboxOp ? . Error } ") ;
189
+ }
190
+
191
+ return skyboxOp . Success . Equals ( successStatus ) ;
192
+ }
193
+
194
+ /// <summary>
195
+ /// Gets the previously generated skyboxes.
196
+ /// </summary>
197
+ /// <param name="parameters">Optional, <see cref="SkyboxHistoryParameters"/>.</param>
198
+ /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
199
+ /// <returns><see cref="SkyboxHistory"/>.</returns>
200
+ public async Task < SkyboxHistory > GetSkyboxHistoryAsync ( SkyboxHistoryParameters parameters = null , CancellationToken cancellationToken = default )
201
+ {
202
+ var historyRequest = parameters ?? new SkyboxHistoryParameters ( ) ;
203
+ var response = await Rest . GetAsync ( GetUrl ( $ "imagine/myRequests{ historyRequest } ") , parameters : new RestParameters ( client . DefaultRequestHeaders ) , cancellationToken ) ;
204
+ response . Validate ( ) ;
205
+ return JsonConvert . DeserializeObject < SkyboxHistory > ( response . Body , client . JsonSerializationOptions ) ;
206
+ }
207
+
208
+ /// <summary>
209
+ /// Cancels a pending skybox generation request by id.
210
+ /// </summary>
211
+ /// <param name="id">The id of the skybox.</param>
212
+ /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
213
+ /// <returns>True, if generation was cancelled.</returns>
214
+ public async Task < bool > CancelSkyboxGenerationAsync ( int id , CancellationToken cancellationToken = default )
215
+ {
216
+ var response = await Rest . DeleteAsync ( GetUrl ( $ "imagine/requests/{ id } ") , new RestParameters ( client . DefaultRequestHeaders ) , cancellationToken ) ;
217
+ response . Validate ( ) ;
218
+ var skyboxOp = JsonConvert . DeserializeObject < SkyboxOperation > ( response . Body , client . JsonSerializationOptions ) ;
219
+
220
+ if ( skyboxOp is not { Success : "true" } )
221
+ {
222
+ throw new Exception ( $ "Failed to cancel generation for skybox { id } !\n { skyboxOp ? . Error } ") ;
223
+ }
224
+
225
+ return skyboxOp . Success . Equals ( "true" ) ;
226
+ }
227
+
228
+ /// <summary>
229
+ /// Cancels ALL pending skybox generation requests.
230
+ /// </summary>
231
+ /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
232
+ public async Task < bool > CancelAllPendingSkyboxGenerationsAsync ( CancellationToken cancellationToken = default )
233
+ {
234
+ var response = await Rest . DeleteAsync ( GetUrl ( "imagine/requests/pending" ) , new RestParameters ( client . DefaultRequestHeaders ) , cancellationToken ) ;
235
+ response . Validate ( ) ;
236
+ var skyboxOp = JsonConvert . DeserializeObject < SkyboxOperation > ( response . Body , client . JsonSerializationOptions ) ;
237
+
238
+ if ( skyboxOp is not { Success : "true" } )
239
+ {
240
+ if ( skyboxOp != null &&
241
+ skyboxOp . Error . Contains ( "You don't have any pending" ) )
242
+ {
243
+ return false ;
244
+ }
245
+
246
+ throw new Exception ( $ "Failed to cancel all pending skybox generations!\n { skyboxOp ? . Error } ") ;
247
+ }
248
+
249
+ return skyboxOp . Success . Equals ( "true" ) ;
250
+ }
153
251
}
154
252
}
0 commit comments