@@ -8,7 +8,7 @@ use crate::{
8
8
Asset , AssetLoadError , AssetServer , Assets , Handle , UntypedAssetId , UntypedHandle ,
9
9
} ;
10
10
use bevy_ecs:: world:: World ;
11
- use bevy_utils:: { BoxedFuture , HashMap , HashSet } ;
11
+ use bevy_utils:: { BoxedFuture , CowArc , HashMap , HashSet } ;
12
12
use downcast_rs:: { impl_downcast, Downcast } ;
13
13
use futures_lite:: AsyncReadExt ;
14
14
use ron:: error:: SpannedError ;
@@ -143,7 +143,7 @@ pub struct LoadedAsset<A: Asset> {
143
143
pub ( crate ) value : A ,
144
144
pub ( crate ) dependencies : HashSet < UntypedAssetId > ,
145
145
pub ( crate ) loader_dependencies : HashMap < AssetPath < ' static > , AssetHash > ,
146
- pub ( crate ) labeled_assets : HashMap < String , LabeledAsset > ,
146
+ pub ( crate ) labeled_assets : HashMap < CowArc < ' static , str > , LabeledAsset > ,
147
147
pub ( crate ) meta : Option < Box < dyn AssetMetaDyn > > ,
148
148
}
149
149
@@ -175,7 +175,7 @@ pub struct ErasedLoadedAsset {
175
175
pub ( crate ) value : Box < dyn AssetContainer > ,
176
176
pub ( crate ) dependencies : HashSet < UntypedAssetId > ,
177
177
pub ( crate ) loader_dependencies : HashMap < AssetPath < ' static > , AssetHash > ,
178
- pub ( crate ) labeled_assets : HashMap < String , LabeledAsset > ,
178
+ pub ( crate ) labeled_assets : HashMap < CowArc < ' static , str > , LabeledAsset > ,
179
179
pub ( crate ) meta : Option < Box < dyn AssetMetaDyn > > ,
180
180
}
181
181
@@ -214,13 +214,16 @@ impl ErasedLoadedAsset {
214
214
}
215
215
216
216
/// Returns the [`ErasedLoadedAsset`] for the given label, if it exists.
217
- pub fn get_labeled ( & self , label : & str ) -> Option < & ErasedLoadedAsset > {
218
- self . labeled_assets . get ( label) . map ( |a| & a. asset )
217
+ pub fn get_labeled (
218
+ & self ,
219
+ label : impl Into < CowArc < ' static , str > > ,
220
+ ) -> Option < & ErasedLoadedAsset > {
221
+ self . labeled_assets . get ( & label. into ( ) ) . map ( |a| & a. asset )
219
222
}
220
223
221
224
/// Iterate over all labels for "labeled assets" in the loaded asset
222
225
pub fn iter_labels ( & self ) -> impl Iterator < Item = & str > {
223
- self . labeled_assets . keys ( ) . map ( |s| s . as_str ( ) )
226
+ self . labeled_assets . keys ( ) . map ( |s| & * * s )
224
227
}
225
228
}
226
229
@@ -269,7 +272,7 @@ pub struct LoadContext<'a> {
269
272
dependencies : HashSet < UntypedAssetId > ,
270
273
/// Direct dependencies used by this loader.
271
274
loader_dependencies : HashMap < AssetPath < ' static > , AssetHash > ,
272
- labeled_assets : HashMap < String , LabeledAsset > ,
275
+ labeled_assets : HashMap < CowArc < ' static , str > , LabeledAsset > ,
273
276
}
274
277
275
278
impl < ' a > LoadContext < ' a > {
@@ -362,9 +365,10 @@ impl<'a> LoadContext<'a> {
362
365
/// See [`AssetPath`] for more on labeled assets.
363
366
pub fn add_loaded_labeled_asset < A : Asset > (
364
367
& mut self ,
365
- label : String ,
368
+ label : impl Into < CowArc < ' static , str > > ,
366
369
loaded_asset : LoadedAsset < A > ,
367
370
) -> Handle < A > {
371
+ let label = label. into ( ) ;
368
372
let loaded_asset: ErasedLoadedAsset = loaded_asset. into ( ) ;
369
373
let labeled_path = self . asset_path . with_label ( label. clone ( ) ) ;
370
374
let handle = self
@@ -383,9 +387,9 @@ impl<'a> LoadContext<'a> {
383
387
/// Returns `true` if an asset with the label `label` exists in this context.
384
388
///
385
389
/// See [`AssetPath`] for more on labeled assets.
386
- pub fn has_labeled_asset ( & self , label : & str ) -> bool {
387
- let path = self . asset_path . with_label ( label) ;
388
- self . asset_server . get_handle_untyped ( path) . is_some ( )
390
+ pub fn has_labeled_asset < ' b > ( & self , label : impl Into < CowArc < ' b , str > > ) -> bool {
391
+ let path = self . asset_path . with_label ( label. into ( ) ) ;
392
+ self . asset_server . get_handle_untyped ( & path) . is_some ( )
389
393
}
390
394
391
395
/// "Finishes" this context by populating the final [`Asset`] value (and the erased [`AssetMeta`] value, if it exists).
@@ -406,7 +410,7 @@ impl<'a> LoadContext<'a> {
406
410
}
407
411
408
412
/// Gets the source asset path for this load context.
409
- pub fn asset_path ( & self ) -> & AssetPath {
413
+ pub fn asset_path ( & self ) -> & AssetPath < ' static > {
410
414
& self . asset_path
411
415
}
412
416
@@ -432,7 +436,7 @@ impl<'a> LoadContext<'a> {
432
436
let mut bytes = Vec :: new ( ) ;
433
437
reader. read_to_end ( & mut bytes) . await ?;
434
438
self . loader_dependencies
435
- . insert ( AssetPath :: new ( path. to_owned ( ) , None ) , hash) ;
439
+ . insert ( AssetPath :: from_path ( path. to_owned ( ) ) , hash) ;
436
440
Ok ( bytes)
437
441
}
438
442
@@ -461,14 +465,12 @@ impl<'a> LoadContext<'a> {
461
465
path : impl Into < AssetPath < ' b > > ,
462
466
settings : impl Fn ( & mut S ) + Send + Sync + ' static ,
463
467
) -> Handle < A > {
464
- let path = path. into ( ) . to_owned ( ) ;
468
+ let path = path. into ( ) ;
465
469
let handle = if self . should_load_dependencies {
466
470
self . asset_server . load_with_settings ( path. clone ( ) , settings)
467
471
} else {
468
- self . asset_server . get_or_create_path_handle (
469
- path. clone ( ) ,
470
- Some ( loader_settings_meta_transform ( settings) ) ,
471
- )
472
+ self . asset_server
473
+ . get_or_create_path_handle ( path, Some ( loader_settings_meta_transform ( settings) ) )
472
474
} ;
473
475
self . dependencies . insert ( handle. id ( ) . untyped ( ) ) ;
474
476
handle
@@ -477,8 +479,11 @@ impl<'a> LoadContext<'a> {
477
479
/// Returns a handle to an asset of type `A` with the label `label`. This [`LoadContext`] must produce an asset of the
478
480
/// given type and the given label or the dependencies of this asset will never be considered "fully loaded". However you
479
481
/// can call this method before _or_ after adding the labeled asset.
480
- pub fn get_label_handle < A : Asset > ( & mut self , label : & str ) -> Handle < A > {
481
- let path = self . asset_path . with_label ( label) . to_owned ( ) ;
482
+ pub fn get_label_handle < ' b , A : Asset > (
483
+ & mut self ,
484
+ label : impl Into < CowArc < ' b , str > > ,
485
+ ) -> Handle < A > {
486
+ let path = self . asset_path . with_label ( label) ;
482
487
let handle = self . asset_server . get_or_create_path_handle :: < A > ( path, None ) ;
483
488
self . dependencies . insert ( handle. id ( ) . untyped ( ) ) ;
484
489
handle
@@ -498,36 +503,37 @@ impl<'a> LoadContext<'a> {
498
503
& mut self ,
499
504
path : impl Into < AssetPath < ' b > > ,
500
505
) -> Result < ErasedLoadedAsset , LoadDirectError > {
501
- let path = path. into ( ) ;
506
+ let path = path. into ( ) . into_owned ( ) ;
502
507
let to_error = |e : AssetLoadError | -> LoadDirectError {
503
508
LoadDirectError {
504
- dependency : path. to_owned ( ) ,
509
+ dependency : path. clone ( ) ,
505
510
error : e,
506
511
}
507
512
} ;
508
- let ( meta, loader, mut reader) = self
509
- . asset_server
510
- . get_meta_loader_and_reader ( & path)
511
- . await
512
- . map_err ( to_error) ?;
513
- let loaded_asset = self
514
- . asset_server
515
- . load_with_meta_loader_and_reader (
516
- & path,
517
- meta,
518
- & * loader,
519
- & mut * reader,
520
- false ,
521
- self . populate_hashes ,
522
- )
523
- . await
524
- . map_err ( to_error) ?;
513
+ let loaded_asset = {
514
+ let ( meta, loader, mut reader) = self
515
+ . asset_server
516
+ . get_meta_loader_and_reader ( & path)
517
+ . await
518
+ . map_err ( to_error) ?;
519
+ self . asset_server
520
+ . load_with_meta_loader_and_reader (
521
+ & path,
522
+ meta,
523
+ & * loader,
524
+ & mut * reader,
525
+ false ,
526
+ self . populate_hashes ,
527
+ )
528
+ . await
529
+ . map_err ( to_error) ?
530
+ } ;
525
531
let info = loaded_asset
526
532
. meta
527
533
. as_ref ( )
528
534
. and_then ( |m| m. processed_info ( ) . as_ref ( ) ) ;
529
535
let hash = info. map ( |i| i. full_hash ) . unwrap_or ( Default :: default ( ) ) ;
530
- self . loader_dependencies . insert ( path. to_owned ( ) , hash) ;
536
+ self . loader_dependencies . insert ( path, hash) ;
531
537
Ok ( loaded_asset)
532
538
}
533
539
}
0 commit comments