@@ -6,8 +6,9 @@ import { EventHandler } from '../core/event-handler.js';
6
6
import { findAvailableLocale } from '../i18n/utils.js' ;
7
7
8
8
import { ABSOLUTE_URL } from './constants.js' ;
9
- import { AssetVariants } from './asset-variants .js' ;
9
+ import { AssetFile } from './asset-file .js' ;
10
10
import { getApplication } from '../framework/globals.js' ;
11
+ import { http } from '../net/http.js' ;
11
12
12
13
// auto incrementing number for asset ids
13
14
var assetIdCounter = - 1 ;
@@ -82,8 +83,6 @@ class Asset extends EventHandler {
82
83
this . tags = new Tags ( this ) ;
83
84
this . _preload = false ;
84
85
85
- this . variants = new AssetVariants ( this ) ;
86
-
87
86
this . _file = null ;
88
87
this . _data = data || { } ;
89
88
this . options = options || { } ;
@@ -169,7 +168,7 @@ class Asset extends EventHandler {
169
168
* var img = "<img src='" + assets[0].getFileUrl() + "'>";
170
169
*/
171
170
getFileUrl ( ) {
172
- var file = this . getPreferredFile ( ) ;
171
+ var file = this . file ;
173
172
174
173
if ( ! file || ! file . url )
175
174
return null ;
@@ -188,44 +187,6 @@ class Asset extends EventHandler {
188
187
return url ;
189
188
}
190
189
191
- getPreferredFile ( ) {
192
- if ( ! this . file )
193
- return null ;
194
-
195
- if ( this . type === 'texture' || this . type === 'textureatlas' || this . type === 'bundle' ) {
196
- var app = this . registry ?. _loader ?. _app || getApplication ( ) ;
197
- var device = app ?. graphicsDevice ;
198
- if ( device ) {
199
- for ( var i = 0 , len = VARIANT_DEFAULT_PRIORITY . length ; i < len ; i ++ ) {
200
- var variant = VARIANT_DEFAULT_PRIORITY [ i ] ;
201
- // if the device supports the variant
202
- if ( ! device [ VARIANT_SUPPORT [ variant ] ] ) continue ;
203
-
204
- // if the variant exists in the asset then just return it
205
- if ( this . file . variants [ variant ] ) {
206
- return this . file . variants [ variant ] ;
207
- }
208
-
209
- // if the variant does not exist but the asset is in a bundle
210
- // and the bundle contain assets with this variant then return the default
211
- // file for the asset
212
- if ( app . enableBundles ) {
213
- var bundles = app . bundles . listBundlesForAsset ( this ) ;
214
- if ( ! bundles ) continue ;
215
-
216
- for ( var j = 0 , len2 = bundles . length ; j < len2 ; j ++ ) {
217
- if ( bundles [ j ] . file && bundles [ j ] . file . variants && bundles [ j ] . file . variants [ variant ] ) {
218
- return this . file ;
219
- }
220
- }
221
- }
222
- }
223
- }
224
- }
225
-
226
- return this . file ;
227
- }
228
-
229
190
/**
230
191
* @private
231
192
* @function
@@ -369,54 +330,42 @@ class Asset extends EventHandler {
369
330
}
370
331
371
332
set file ( value ) {
372
- // fire change event when the file changes
373
- // so that we reload it if necessary
374
- // set/unset file property of file hash been changed
375
- var key ;
376
- var valueAsBool = ! ! value ;
377
- var fileAsBool = ! ! this . _file ;
378
- if ( valueAsBool !== fileAsBool || ( value && this . _file && value . hash !== this . _file ) ) {
379
- if ( value ) {
380
- if ( ! this . _file )
381
- this . _file = { } ;
382
-
383
- this . _file . url = value . url ;
384
- this . _file . filename = value . filename ;
385
- this . _file . hash = value . hash ;
386
- this . _file . size = value . size ;
387
- this . _file . variants = this . variants ;
388
- this . _file . contents = value . contents ;
389
-
390
- if ( value . hasOwnProperty ( 'variants' ) ) {
391
- this . variants . clear ( ) ;
392
-
393
- if ( value . variants ) {
394
- for ( key in value . variants ) {
395
- if ( ! value . variants [ key ] )
396
- continue ;
397
-
398
- this . variants [ key ] = value . variants [ key ] ;
333
+ // if value contains variants, choose the correct variant first
334
+ if ( value && value . variants && [ 'texture' , 'textureatlas' , 'bundle' ] . indexOf ( this . type ) !== - 1 ) {
335
+ // search for active variant
336
+ const app = this . registry ?. _loader ?. _app || getApplication ( ) ;
337
+ const device = app ?. graphicsDevice ;
338
+ if ( device ) {
339
+ for ( let i = 0 , len = VARIANT_DEFAULT_PRIORITY . length ; i < len ; i ++ ) {
340
+ const variant = VARIANT_DEFAULT_PRIORITY [ i ] ;
341
+ // if the device supports the variant
342
+ if ( value . variants [ variant ] && device [ VARIANT_SUPPORT [ variant ] ] ) {
343
+ value = value . variants [ variant ] ;
344
+ break ;
345
+ }
346
+
347
+ // if the variant does not exist but the asset is in a bundle
348
+ // and the bundle contain assets with this variant then return the default
349
+ // file for the asset
350
+ if ( app . enableBundles ) {
351
+ const bundles = app . bundles . listBundlesForAsset ( this ) ;
352
+ if ( bundles && bundles . find ( ( b ) => {
353
+ return b ?. file ?. variants [ variant ] ;
354
+ } ) ) {
355
+ break ;
399
356
}
400
357
}
401
358
}
402
-
403
- this . fire ( 'change' , this , 'file' , this . _file , this . _file ) ;
404
- this . reload ( ) ;
405
- } else {
406
- this . _file = null ;
407
- this . variants . clear ( ) ;
408
359
}
409
- } else if ( value && this . _file && value . hasOwnProperty ( 'variants' ) ) {
410
- this . variants . clear ( ) ;
360
+ }
411
361
412
- if ( value . variants ) {
413
- for ( key in value . variants ) {
414
- if ( ! value . variants [ key ] )
415
- continue ;
362
+ const oldFile = this . _file ;
363
+ const newFile = value ? new AssetFile ( value . url , value . filename , value . hash , value . size , value . opt , value . contents ) : null ;
416
364
417
- this . variants [ key ] = value . variants [ key ] ;
418
- }
419
- }
365
+ if ( ! ! newFile !== ! ! oldFile || ( newFile && ! newFile . equals ( oldFile ) ) ) {
366
+ this . _file = newFile ;
367
+ this . fire ( 'change' , this , 'file' , newFile , oldFile ) ;
368
+ this . reload ( ) ;
420
369
}
421
370
}
422
371
@@ -487,6 +436,35 @@ class Asset extends EventHandler {
487
436
this . registry . _loader . patch ( this , this . registry ) ;
488
437
}
489
438
}
439
+
440
+ /**
441
+ * @private
442
+ * @function
443
+ * @name Asset#fetchArrayBuffer
444
+ * @description Helper function to resolve asset file data and return the contents as an
445
+ * ArrayBuffer. If the asset file contents are present, that is returned. Otherwise the file
446
+ * data is be downloaded via http.
447
+ * @param {string } loadUrl - The URL as passed into the handler
448
+ * @param {callbacks.ResourceLoader } callback - The callback function to receive results.
449
+ * @param {Asset } [asset] - The asset
450
+ * @param {number } maxRetries - Number of retries if http download is required
451
+ */
452
+ static fetchArrayBuffer ( loadUrl , callback , asset , maxRetries = 0 ) {
453
+ if ( asset ?. file ?. contents ) {
454
+ // asset file contents were provided
455
+ setTimeout ( ( ) => {
456
+ callback ( null , asset . file . contents ) ;
457
+ } ) ;
458
+ } else {
459
+ // asset contents must be downloaded
460
+ http . get ( loadUrl , {
461
+ cache : true ,
462
+ responseType : 'arraybuffer' ,
463
+ retry : maxRetries > 0 ,
464
+ maxRetries : maxRetries
465
+ } , callback ) ;
466
+ }
467
+ }
490
468
}
491
469
492
470
export { Asset } ;
0 commit comments