forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
1996 lines (1797 loc) · 76.8 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! In the context of game development, an "asset" is a piece of content that is loaded from disk and displayed in the game.
//! Typically, these are authored by artists and designers (in contrast to code),
//! are relatively large in size, and include everything from textures and models to sounds and music to levels and scripts.
//!
//! This presents two main challenges:
//! - Assets take up a lot of memory; simply storing a copy for each instance of an asset in the game would be prohibitively expensive.
//! - Loading assets from disk is slow, and can cause long load times and delays.
//!
//! These problems play into each other, for if assets are expensive to store in memory,
//! then larger game worlds will need to load them from disk as needed, ideally without a loading screen.
//!
//! As is common in Rust, non-blocking asset loading is done using `async`, with background tasks used to load assets while the game is running.
//! Bevy coordinates these tasks using the [`AssetServer`] resource, storing each loaded asset in a strongly-typed [`Assets<T>`] collection (also a resource).
//! [`Handle`]s serve as an id-based reference to entries in the [`Assets`] collection, allowing them to be cheaply shared between systems,
//! and providing a way to initialize objects (generally entities) before the required assets are loaded.
//! In short: [`Handle`]s are not the assets themselves, they just tell how to look them up!
//!
//! ## Loading assets
//!
//! The [`AssetServer`] is the main entry point for loading assets.
//! Typically, you'll use the [`AssetServer::load`] method to load an asset from disk, which returns a [`Handle`].
//! Note that this method does not attempt to reload the asset if it has already been loaded: as long as at least one handle has not been dropped,
//! calling [`AssetServer::load`] on the same path will return the same handle.
//! The handle that's returned can be used to instantiate various [`Component`]s that require asset data to function,
//! which will then be spawned into the world as part of an entity.
//!
//! To avoid assets "popping" into existence, you may want to check that all of the required assets are loaded before transitioning to a new scene.
//! This can be done by checking the [`LoadState`] of the asset handle using [`AssetServer::is_loaded_with_dependencies`],
//! which will be `true` when the asset is ready to use.
//!
//! Keep track of what you're waiting on by using a [`HashSet`] of asset handles or similar data structure,
//! which iterate over and poll in your update loop, and transition to the new scene once all assets are loaded.
//! Bevy's built-in states system can be very helpful for this!
//!
//! # Modifying entities that use assets
//!
//! If we later want to change the asset data a given component uses (such as changing an entity's material), we have three options:
//!
//! 1. Change the handle stored on the responsible component to the handle of a different asset
//! 2. Despawn the entity and spawn a new one with the new asset data.
//! 3. Use the [`Assets`] collection to directly modify the current handle's asset data
//!
//! The first option is the most common: just query for the component that holds the handle, and mutate it, pointing to the new asset.
//! Check how the handle was passed in to the entity when it was spawned: if a mesh-related component required a handle to a mesh asset,
//! you'll need to find that component via a query and change the handle to the new mesh asset.
//! This is so commonly done that you should think about strategies for how to store and swap handles in your game.
//!
//! The second option is the simplest, but can be slow if done frequently,
//! and can lead to frustrating bugs as references to the old entity (such as what is targeting it) and other data on the entity are lost.
//! Generally, this isn't a great strategy.
//!
//! The third option has different semantics: rather than modifying the asset data for a single entity, it modifies the asset data for *all* entities using this handle.
//! While this might be what you want, it generally isn't!
//!
//! # Hot reloading assets
//!
//! Bevy supports asset hot reloading, allowing you to change assets on disk and see the changes reflected in your game without restarting.
//! When enabled, any changes to the underlying asset file will be detected by the [`AssetServer`], which will then reload the asset,
//! mutating the asset data in the [`Assets`] collection and thus updating all entities that use the asset.
//! While it has limited uses in published games, it is very useful when developing, as it allows you to iterate quickly.
//!
//! To enable asset hot reloading on desktop platforms, enable `bevy`'s `file_watcher` cargo feature.
//! To toggle it at runtime, you can use the `watch_for_changes_override` field in the [`AssetPlugin`] to enable or disable hot reloading.
//!
//! # Procedural asset creation
//!
//! Not all assets are loaded from disk: some are generated at runtime, such as procedural materials, sounds or even levels.
//! After creating an item of a type that implements [`Asset`], you can add it to the [`Assets`] collection using [`Assets::add`].
//! Once in the asset collection, this data can be operated on like any other asset.
//!
//! Note that, unlike assets loaded from a file path, no general mechanism currently exists to deduplicate procedural assets:
//! calling [`Assets::add`] for every entity that needs the asset will create a new copy of the asset for each entity,
//! quickly consuming memory.
//!
//! ## Handles and reference counting
//!
//! [`Handle`] (or their untyped counterpart [`UntypedHandle`]) are used to reference assets in the [`Assets`] collection,
//! and are the primary way to interact with assets in Bevy.
//! As a user, you'll be working with handles a lot!
//!
//! The most important thing to know about handles is that they are reference counted: when you clone a handle, you're incrementing a reference count.
//! When the object holding the handle is dropped (generally because an entity was despawned), the reference count is decremented.
//! When the reference count hits zero, the asset it references is removed from the [`Assets`] collection.
//!
//! This reference counting is a simple, largely automatic way to avoid holding onto memory for game objects that are no longer in use.
//! However, it can lead to surprising behavior if you're not careful!
//!
//! There are two categories of problems to watch out for:
//! - never dropping a handle, causing the asset to never be removed from memory
//! - dropping a handle too early, causing the asset to be removed from memory while it's still in use
//!
//! The first problem is less critical for beginners, as for tiny games, you can often get away with simply storing all of the assets in memory at once,
//! and loading them all at the start of the game.
//! As your game grows, you'll need to be more careful about when you load and unload assets,
//! segmenting them by level or area, and loading them on-demand.
//! This problem generally arises when handles are stored in a persistent "collection" or "manifest" of possible objects (generally in a resource),
//! which is convenient for easy access and zero-latency spawning, but can result in high but stable memory usage.
//!
//! The second problem is more concerning, and looks like your models or textures suddenly disappearing from the game.
//! Debugging reveals that the *entities* are still there, but nothing is rendering!
//! This is because the assets were removed from memory while they were still in use.
//! You were probably too aggressive with the use of weak handles (which don't increment the reference count of the asset): think through the lifecycle of your assets carefully!
//! As soon as an asset is loaded, you must ensure that at least one strong handle is held to it until all matching entities are out of sight of the player.
//!
//! # Asset dependencies
//!
//! Some assets depend on other assets to be loaded before they can be loaded themselves.
//! For example, a 3D model might require both textures and meshes to be loaded,
//! or a 2D level might require a tileset to be loaded.
//!
//! The assets that are required to load another asset are called "dependencies".
//! An asset is only considered fully loaded when it and all of its dependencies are loaded.
//! Asset dependencies can be declared when implementing the [`Asset`] trait by implementing the [`VisitAssetDependencies`] trait,
//! and the `#[dependency]` attribute can be used to automatically derive this implementation.
//!
//! # Custom asset types
//!
//! While Bevy comes with implementations for a large number of common game-oriented asset types (often behind off-by-default feature flags!),
//! implementing a custom asset type can be useful when dealing with unusual, game-specific, or proprietary formats.
//!
//! Defining a new asset type is as simple as implementing the [`Asset`] trait.
//! This requires [`TypePath`] for metadata about the asset type,
//! and [`VisitAssetDependencies`] to track asset dependencies.
//! In simple cases, you can derive [`Asset`] and [`Reflect`] and be done with it: the required supertraits will be implemented for you.
//!
//! With a new asset type in place, we now need to figure out how to load it.
//! While [`AssetReader`](io::AssetReader) describes strategies to read asset bytes from various sources,
//! [`AssetLoader`] is the trait that actually turns those into your desired in-memory format.
//! Generally, (only) [`AssetLoader`] needs to be implemented for custom assets, as the [`AssetReader`](io::AssetReader) implementations are provided by Bevy.
//!
//! However, [`AssetLoader`] shouldn't be implemented for your asset type directly: instead, this is implemented for a "loader" type
//! that can store settings and any additional data required to load your asset, while your asset type is used as the [`AssetLoader::Asset`] associated type.
//! As the trait documentation explains, this allows various [`AssetLoader::Settings`] to be used to configure the loader.
//!
//! After the loader is implemented, it needs to be registered with the [`AssetServer`] using [`App::register_asset_loader`](AssetApp::register_asset_loader).
//! Once your asset type is loaded, you can use it in your game like any other asset type!
//!
//! If you want to save your assets back to disk, you should implement [`AssetSaver`](saver::AssetSaver) as well.
//! This trait mirrors [`AssetLoader`] in structure, and works in tandem with [`AssetWriter`](io::AssetWriter), which mirrors [`AssetReader`](io::AssetReader).
#![expect(missing_docs, reason = "Not all docs are written yet, see #3492.")]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc(
html_logo_url = "https://bevyengine.org/assets/icon.png",
html_favicon_url = "https://bevyengine.org/assets/icon.png"
)]
#![no_std]
extern crate alloc;
extern crate std;
// Required to make proc macros work in bevy itself.
extern crate self as bevy_asset;
pub mod io;
pub mod meta;
pub mod processor;
pub mod saver;
pub mod transformer;
/// The asset prelude.
///
/// This includes the most common types in this crate, re-exported for your convenience.
pub mod prelude {
#[doc(hidden)]
pub use crate::asset_changed::AssetChanged;
#[doc(hidden)]
pub use crate::{
Asset, AssetApp, AssetEvent, AssetId, AssetMode, AssetPlugin, AssetServer, Assets,
DirectAssetAccessExt, Handle, UntypedHandle,
};
}
mod asset_changed;
mod assets;
mod direct_access_ext;
mod event;
mod folder;
mod handle;
mod id;
mod loader;
mod loader_builders;
mod path;
mod reflect;
mod render_asset;
mod server;
pub use assets::*;
pub use bevy_asset_macros::Asset;
pub use direct_access_ext::DirectAssetAccessExt;
pub use event::*;
pub use folder::*;
pub use futures_lite::{AsyncReadExt, AsyncWriteExt};
pub use handle::*;
pub use id::*;
pub use loader::*;
pub use loader_builders::{
Deferred, DynamicTyped, Immediate, NestedLoader, StaticTyped, UnknownTyped,
};
pub use path::*;
pub use reflect::*;
pub use render_asset::*;
pub use server::*;
/// Rusty Object Notation, a crate used to serialize and deserialize bevy assets.
pub use ron;
pub use uuid;
use crate::{
io::{embedded::EmbeddedAssetRegistry, AssetSourceBuilder, AssetSourceBuilders, AssetSourceId},
processor::{AssetProcessor, Process},
};
use alloc::{
string::{String, ToString},
sync::Arc,
vec::Vec,
};
use bevy_app::{App, Plugin, PostUpdate, PreUpdate};
use bevy_ecs::prelude::Component;
use bevy_ecs::{
reflect::AppTypeRegistry,
schedule::{IntoScheduleConfigs, SystemSet},
world::FromWorld,
};
use bevy_platform::collections::HashSet;
use bevy_reflect::{FromReflect, GetTypeRegistration, Reflect, TypePath};
use core::any::TypeId;
use tracing::error;
#[cfg(all(feature = "file_watcher", not(feature = "multi_threaded")))]
compile_error!(
"The \"file_watcher\" feature for hot reloading requires the \
\"multi_threaded\" feature to be functional.\n\
Consider either disabling the \"file_watcher\" feature or enabling \"multi_threaded\""
);
/// Provides "asset" loading and processing functionality. An [`Asset`] is a "runtime value" that is loaded from an [`AssetSource`],
/// which can be something like a filesystem, a network, etc.
///
/// Supports flexible "modes", such as [`AssetMode::Processed`] and
/// [`AssetMode::Unprocessed`] that enable using the asset workflow that best suits your project.
///
/// [`AssetSource`]: io::AssetSource
pub struct AssetPlugin {
/// The default file path to use (relative to the project root) for unprocessed assets.
pub file_path: String,
/// The default file path to use (relative to the project root) for processed assets.
pub processed_file_path: String,
/// If set, will override the default "watch for changes" setting. By default "watch for changes" will be `false` unless
/// the `watch` cargo feature is set. `watch` can be enabled manually, or it will be automatically enabled if a specific watcher
/// like `file_watcher` is enabled.
///
/// Most use cases should leave this set to [`None`] and enable a specific watcher feature such as `file_watcher` to enable
/// watching for dev-scenarios.
pub watch_for_changes_override: Option<bool>,
/// The [`AssetMode`] to use for this server.
pub mode: AssetMode,
/// How/If asset meta files should be checked.
pub meta_check: AssetMetaCheck,
/// How to handle load requests of files that are outside the approved directories.
///
/// Approved folders are [`AssetPlugin::file_path`] and the folder of each
/// [`AssetSource`](io::AssetSource). Subfolders within these folders are also valid.
pub unapproved_path_mode: UnapprovedPathMode,
}
/// Determines how to react to attempts to load assets not inside the approved folders.
///
/// Approved folders are [`AssetPlugin::file_path`] and the folder of each
/// [`AssetSource`](io::AssetSource). Subfolders within these folders are also valid.
///
/// It is strongly discouraged to use [`Allow`](UnapprovedPathMode::Allow) if your
/// app will include scripts or modding support, as it could allow allow arbitrary file
/// access for malicious code.
///
/// See [`AssetPath::is_unapproved`](crate::AssetPath::is_unapproved)
#[derive(Clone, Default)]
pub enum UnapprovedPathMode {
/// Unapproved asset loading is allowed. This is strongly discouraged.
Allow,
/// Fails to load any asset that is is unapproved, unless an override method is used, like
/// [`AssetServer::load_override`].
Deny,
/// Fails to load any asset that is is unapproved.
#[default]
Forbid,
}
/// Controls whether or not assets are pre-processed before being loaded.
///
/// This setting is controlled by setting [`AssetPlugin::mode`].
///
/// When building on web, asset preprocessing can cause problems due to the lack of filesystem access.
/// See [bevy#10157](https://github.com/bevyengine/bevy/issues/10157) for context.
#[derive(Debug)]
pub enum AssetMode {
/// Loads assets from their [`AssetSource`]'s default [`AssetReader`] without any "preprocessing".
///
/// [`AssetReader`]: io::AssetReader
/// [`AssetSource`]: io::AssetSource
Unprocessed,
/// Assets will be "pre-processed". This enables assets to be imported / converted / optimized ahead of time.
///
/// Assets will be read from their unprocessed [`AssetSource`] (defaults to the `assets` folder),
/// processed according to their [`AssetMeta`], and written to their processed [`AssetSource`] (defaults to the `imported_assets/Default` folder).
///
/// By default, this assumes the processor _has already been run_. It will load assets from their final processed [`AssetReader`].
///
/// When developing an app, you should enable the `asset_processor` cargo feature, which will run the asset processor at startup. This should generally
/// be used in combination with the `file_watcher` cargo feature, which enables hot-reloading of assets that have changed. When both features are enabled,
/// changes to "original/source assets" will be detected, the asset will be re-processed, and then the final processed asset will be hot-reloaded in the app.
///
/// [`AssetMeta`]: meta::AssetMeta
/// [`AssetSource`]: io::AssetSource
/// [`AssetReader`]: io::AssetReader
Processed,
}
/// Configures how / if meta files will be checked. If an asset's meta file is not checked, the default meta for the asset
/// will be used.
#[derive(Debug, Default, Clone)]
pub enum AssetMetaCheck {
/// Always check if assets have meta files. If the meta does not exist, the default meta will be used.
#[default]
Always,
/// Only look up meta files for the provided paths. The default meta will be used for any paths not contained in this set.
Paths(HashSet<AssetPath<'static>>),
/// Never check if assets have meta files and always use the default meta. If meta files exist, they will be ignored and the default meta will be used.
Never,
}
impl Default for AssetPlugin {
fn default() -> Self {
Self {
mode: AssetMode::Unprocessed,
file_path: Self::DEFAULT_UNPROCESSED_FILE_PATH.to_string(),
processed_file_path: Self::DEFAULT_PROCESSED_FILE_PATH.to_string(),
watch_for_changes_override: None,
meta_check: AssetMetaCheck::default(),
unapproved_path_mode: UnapprovedPathMode::default(),
}
}
}
impl AssetPlugin {
const DEFAULT_UNPROCESSED_FILE_PATH: &'static str = "assets";
/// NOTE: this is in the Default sub-folder to make this forward compatible with "import profiles"
/// and to allow us to put the "processor transaction log" at `imported_assets/log`
const DEFAULT_PROCESSED_FILE_PATH: &'static str = "imported_assets/Default";
}
impl Plugin for AssetPlugin {
fn build(&self, app: &mut App) {
let embedded = EmbeddedAssetRegistry::default();
{
let mut sources = app
.world_mut()
.get_resource_or_init::<AssetSourceBuilders>();
sources.init_default_source(
&self.file_path,
(!matches!(self.mode, AssetMode::Unprocessed))
.then_some(self.processed_file_path.as_str()),
);
embedded.register_source(&mut sources);
}
{
let mut watch = cfg!(feature = "watch");
if let Some(watch_override) = self.watch_for_changes_override {
watch = watch_override;
}
match self.mode {
AssetMode::Unprocessed => {
let mut builders = app.world_mut().resource_mut::<AssetSourceBuilders>();
let sources = builders.build_sources(watch, false);
app.insert_resource(AssetServer::new_with_meta_check(
sources,
AssetServerMode::Unprocessed,
self.meta_check.clone(),
watch,
self.unapproved_path_mode.clone(),
));
}
AssetMode::Processed => {
#[cfg(feature = "asset_processor")]
{
let mut builders = app.world_mut().resource_mut::<AssetSourceBuilders>();
let processor = AssetProcessor::new(&mut builders);
let mut sources = builders.build_sources(false, watch);
sources.gate_on_processor(processor.data.clone());
// the main asset server shares loaders with the processor asset server
app.insert_resource(AssetServer::new_with_loaders(
sources,
processor.server().data.loaders.clone(),
AssetServerMode::Processed,
AssetMetaCheck::Always,
watch,
self.unapproved_path_mode.clone(),
))
.insert_resource(processor)
.add_systems(bevy_app::Startup, AssetProcessor::start);
}
#[cfg(not(feature = "asset_processor"))]
{
let mut builders = app.world_mut().resource_mut::<AssetSourceBuilders>();
let sources = builders.build_sources(false, watch);
app.insert_resource(AssetServer::new_with_meta_check(
sources,
AssetServerMode::Processed,
AssetMetaCheck::Always,
watch,
self.unapproved_path_mode.clone(),
));
}
}
}
}
app.insert_resource(embedded)
.init_asset::<LoadedFolder>()
.init_asset::<LoadedUntypedAsset>()
.init_asset::<()>()
.add_event::<UntypedAssetLoadFailedEvent>()
.configure_sets(PreUpdate, TrackAssets.after(handle_internal_asset_events))
// `handle_internal_asset_events` requires the use of `&mut World`,
// and as a result has ambiguous system ordering with all other systems in `PreUpdate`.
// This is virtually never a real problem: asset loading is async and so anything that interacts directly with it
// needs to be robust to stochastic delays anyways.
.add_systems(PreUpdate, handle_internal_asset_events.ambiguous_with_all())
.register_type::<AssetPath>();
}
}
/// Declares that this type is an asset,
/// which can be loaded and managed by the [`AssetServer`] and stored in [`Assets`] collections.
///
/// Generally, assets are large, complex, and/or expensive to load from disk, and are often authored by artists or designers.
///
/// [`TypePath`] is largely used for diagnostic purposes, and should almost always be implemented by deriving [`Reflect`] on your type.
/// [`VisitAssetDependencies`] is used to track asset dependencies, and an implementation is automatically generated when deriving [`Asset`].
#[diagnostic::on_unimplemented(
message = "`{Self}` is not an `Asset`",
label = "invalid `Asset`",
note = "consider annotating `{Self}` with `#[derive(Asset)]`"
)]
pub trait Asset: VisitAssetDependencies + TypePath + Send + Sync + 'static {}
/// A trait for components that can be used as asset identifiers, e.g. handle wrappers.
pub trait AsAssetId: Component {
/// The underlying asset type.
type Asset: Asset;
/// Retrieves the asset id from this component.
fn as_asset_id(&self) -> AssetId<Self::Asset>;
}
/// This trait defines how to visit the dependencies of an asset.
/// For example, a 3D model might require both textures and meshes to be loaded.
///
/// Note that this trait is automatically implemented when deriving [`Asset`].
pub trait VisitAssetDependencies {
fn visit_dependencies(&self, visit: &mut impl FnMut(UntypedAssetId));
}
impl<A: Asset> VisitAssetDependencies for Handle<A> {
fn visit_dependencies(&self, visit: &mut impl FnMut(UntypedAssetId)) {
visit(self.id().untyped());
}
}
impl<A: Asset> VisitAssetDependencies for Option<Handle<A>> {
fn visit_dependencies(&self, visit: &mut impl FnMut(UntypedAssetId)) {
if let Some(handle) = self {
visit(handle.id().untyped());
}
}
}
impl VisitAssetDependencies for UntypedHandle {
fn visit_dependencies(&self, visit: &mut impl FnMut(UntypedAssetId)) {
visit(self.id());
}
}
impl VisitAssetDependencies for Option<UntypedHandle> {
fn visit_dependencies(&self, visit: &mut impl FnMut(UntypedAssetId)) {
if let Some(handle) = self {
visit(handle.id());
}
}
}
impl<A: Asset> VisitAssetDependencies for Vec<Handle<A>> {
fn visit_dependencies(&self, visit: &mut impl FnMut(UntypedAssetId)) {
for dependency in self {
visit(dependency.id().untyped());
}
}
}
impl VisitAssetDependencies for Vec<UntypedHandle> {
fn visit_dependencies(&self, visit: &mut impl FnMut(UntypedAssetId)) {
for dependency in self {
visit(dependency.id());
}
}
}
/// Adds asset-related builder methods to [`App`].
pub trait AssetApp {
/// Registers the given `loader` in the [`App`]'s [`AssetServer`].
fn register_asset_loader<L: AssetLoader>(&mut self, loader: L) -> &mut Self;
/// Registers the given `processor` in the [`App`]'s [`AssetProcessor`].
fn register_asset_processor<P: Process>(&mut self, processor: P) -> &mut Self;
/// Registers the given [`AssetSourceBuilder`] with the given `id`.
///
/// Note that asset sources must be registered before adding [`AssetPlugin`] to your application,
/// since registered asset sources are built at that point and not after.
fn register_asset_source(
&mut self,
id: impl Into<AssetSourceId<'static>>,
source: AssetSourceBuilder,
) -> &mut Self;
/// Sets the default asset processor for the given `extension`.
fn set_default_asset_processor<P: Process>(&mut self, extension: &str) -> &mut Self;
/// Initializes the given loader in the [`App`]'s [`AssetServer`].
fn init_asset_loader<L: AssetLoader + FromWorld>(&mut self) -> &mut Self;
/// Initializes the given [`Asset`] in the [`App`] by:
/// * Registering the [`Asset`] in the [`AssetServer`]
/// * Initializing the [`AssetEvent`] resource for the [`Asset`]
/// * Adding other relevant systems and resources for the [`Asset`]
/// * Ignoring schedule ambiguities in [`Assets`] resource. Any time a system takes
/// mutable access to this resource this causes a conflict, but they rarely actually
/// modify the same underlying asset.
fn init_asset<A: Asset>(&mut self) -> &mut Self;
/// Registers the asset type `T` using `[App::register]`,
/// and adds [`ReflectAsset`] type data to `T` and [`ReflectHandle`] type data to [`Handle<T>`] in the type registry.
///
/// This enables reflection code to access assets. For detailed information, see the docs on [`ReflectAsset`] and [`ReflectHandle`].
fn register_asset_reflect<A>(&mut self) -> &mut Self
where
A: Asset + Reflect + FromReflect + GetTypeRegistration;
/// Preregisters a loader for the given extensions, that will block asset loads until a real loader
/// is registered.
fn preregister_asset_loader<L: AssetLoader>(&mut self, extensions: &[&str]) -> &mut Self;
}
impl AssetApp for App {
fn register_asset_loader<L: AssetLoader>(&mut self, loader: L) -> &mut Self {
self.world()
.resource::<AssetServer>()
.register_loader(loader);
self
}
fn register_asset_processor<P: Process>(&mut self, processor: P) -> &mut Self {
if let Some(asset_processor) = self.world().get_resource::<AssetProcessor>() {
asset_processor.register_processor(processor);
}
self
}
fn register_asset_source(
&mut self,
id: impl Into<AssetSourceId<'static>>,
source: AssetSourceBuilder,
) -> &mut Self {
let id = AssetSourceId::from_static(id);
if self.world().get_resource::<AssetServer>().is_some() {
error!("{} must be registered before `AssetPlugin` (typically added as part of `DefaultPlugins`)", id);
}
{
let mut sources = self
.world_mut()
.get_resource_or_init::<AssetSourceBuilders>();
sources.insert(id, source);
}
self
}
fn set_default_asset_processor<P: Process>(&mut self, extension: &str) -> &mut Self {
if let Some(asset_processor) = self.world().get_resource::<AssetProcessor>() {
asset_processor.set_default_processor::<P>(extension);
}
self
}
fn init_asset_loader<L: AssetLoader + FromWorld>(&mut self) -> &mut Self {
let loader = L::from_world(self.world_mut());
self.register_asset_loader(loader)
}
fn init_asset<A: Asset>(&mut self) -> &mut Self {
let assets = Assets::<A>::default();
self.world()
.resource::<AssetServer>()
.register_asset(&assets);
if self.world().contains_resource::<AssetProcessor>() {
let processor = self.world().resource::<AssetProcessor>();
// The processor should have its own handle provider separate from the Asset storage
// to ensure the id spaces are entirely separate. Not _strictly_ necessary, but
// desirable.
processor
.server()
.register_handle_provider(AssetHandleProvider::new(
TypeId::of::<A>(),
Arc::new(AssetIndexAllocator::default()),
));
}
self.insert_resource(assets)
.allow_ambiguous_resource::<Assets<A>>()
.add_event::<AssetEvent<A>>()
.add_event::<AssetLoadFailedEvent<A>>()
.register_type::<Handle<A>>()
.add_systems(
PostUpdate,
Assets::<A>::asset_events
.run_if(Assets::<A>::asset_events_condition)
.in_set(AssetEvents),
)
.add_systems(PreUpdate, Assets::<A>::track_assets.in_set(TrackAssets))
}
fn register_asset_reflect<A>(&mut self) -> &mut Self
where
A: Asset + Reflect + FromReflect + GetTypeRegistration,
{
let type_registry = self.world().resource::<AppTypeRegistry>();
{
let mut type_registry = type_registry.write();
type_registry.register::<A>();
type_registry.register::<Handle<A>>();
type_registry.register_type_data::<A, ReflectAsset>();
type_registry.register_type_data::<Handle<A>, ReflectHandle>();
}
self
}
fn preregister_asset_loader<L: AssetLoader>(&mut self, extensions: &[&str]) -> &mut Self {
self.world_mut()
.resource_mut::<AssetServer>()
.preregister_loader::<L>(extensions);
self
}
}
/// A system set that holds all "track asset" operations.
#[derive(SystemSet, Hash, Debug, PartialEq, Eq, Clone)]
pub struct TrackAssets;
/// A system set where events accumulated in [`Assets`] are applied to the [`AssetEvent`] [`Events`] resource.
///
/// [`Events`]: bevy_ecs::event::Events
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub struct AssetEvents;
#[cfg(test)]
mod tests {
use crate::{
folder::LoadedFolder,
handle::Handle,
io::{
gated::{GateOpener, GatedReader},
memory::{Dir, MemoryAssetReader},
AssetReader, AssetReaderError, AssetSource, AssetSourceId, Reader,
},
loader::{AssetLoader, LoadContext},
Asset, AssetApp, AssetEvent, AssetId, AssetLoadError, AssetLoadFailedEvent, AssetPath,
AssetPlugin, AssetServer, Assets, LoadState, UnapprovedPathMode,
};
use alloc::{
boxed::Box,
format,
string::{String, ToString},
sync::Arc,
vec,
vec::Vec,
};
use bevy_app::{App, TaskPoolPlugin, Update};
use bevy_ecs::{
event::EventCursor,
prelude::*,
schedule::{LogLevel, ScheduleBuildSettings},
};
use bevy_platform::collections::HashMap;
use bevy_reflect::TypePath;
use core::time::Duration;
use serde::{Deserialize, Serialize};
use std::path::Path;
use thiserror::Error;
#[derive(Asset, TypePath, Debug, Default)]
pub struct CoolText {
pub text: String,
pub embedded: String,
#[dependency]
pub dependencies: Vec<Handle<CoolText>>,
#[dependency]
pub sub_texts: Vec<Handle<SubText>>,
}
#[derive(Asset, TypePath, Debug)]
pub struct SubText {
text: String,
}
#[derive(Serialize, Deserialize)]
pub struct CoolTextRon {
text: String,
dependencies: Vec<String>,
embedded_dependencies: Vec<String>,
sub_texts: Vec<String>,
}
#[derive(Default)]
pub struct CoolTextLoader;
#[derive(Error, Debug)]
pub enum CoolTextLoaderError {
#[error("Could not load dependency: {dependency}")]
CannotLoadDependency { dependency: AssetPath<'static> },
#[error("A RON error occurred during loading")]
RonSpannedError(#[from] ron::error::SpannedError),
#[error("An IO error occurred during loading")]
Io(#[from] std::io::Error),
}
impl AssetLoader for CoolTextLoader {
type Asset = CoolText;
type Settings = ();
type Error = CoolTextLoaderError;
async fn load(
&self,
reader: &mut dyn Reader,
_settings: &Self::Settings,
load_context: &mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
let mut ron: CoolTextRon = ron::de::from_bytes(&bytes)?;
let mut embedded = String::new();
for dep in ron.embedded_dependencies {
let loaded = load_context
.loader()
.immediate()
.load::<CoolText>(&dep)
.await
.map_err(|_| Self::Error::CannotLoadDependency {
dependency: dep.into(),
})?;
let cool = loaded.get();
embedded.push_str(&cool.text);
}
Ok(CoolText {
text: ron.text,
embedded,
dependencies: ron
.dependencies
.iter()
.map(|p| load_context.load(p))
.collect(),
sub_texts: ron
.sub_texts
.drain(..)
.map(|text| load_context.add_labeled_asset(text.clone(), SubText { text }))
.collect(),
})
}
fn extensions(&self) -> &[&str] {
&["cool.ron"]
}
}
/// A dummy [`CoolText`] asset reader that only succeeds after `failure_count` times it's read from for each asset.
#[derive(Default, Clone)]
pub struct UnstableMemoryAssetReader {
pub attempt_counters: Arc<std::sync::Mutex<HashMap<Box<Path>, usize>>>,
pub load_delay: Duration,
memory_reader: MemoryAssetReader,
failure_count: usize,
}
impl UnstableMemoryAssetReader {
pub fn new(root: Dir, failure_count: usize) -> Self {
Self {
load_delay: Duration::from_millis(10),
memory_reader: MemoryAssetReader { root },
attempt_counters: Default::default(),
failure_count,
}
}
}
impl AssetReader for UnstableMemoryAssetReader {
async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
self.memory_reader.is_directory(path).await
}
async fn read_directory<'a>(
&'a self,
path: &'a Path,
) -> Result<Box<bevy_asset::io::PathStream>, AssetReaderError> {
self.memory_reader.read_directory(path).await
}
async fn read_meta<'a>(
&'a self,
path: &'a Path,
) -> Result<impl Reader + 'a, AssetReaderError> {
self.memory_reader.read_meta(path).await
}
async fn read<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
let attempt_number = {
let mut attempt_counters = self.attempt_counters.lock().unwrap();
if let Some(existing) = attempt_counters.get_mut(path) {
*existing += 1;
*existing
} else {
attempt_counters.insert(path.into(), 1);
1
}
};
if attempt_number <= self.failure_count {
let io_error = std::io::Error::new(
std::io::ErrorKind::ConnectionRefused,
format!(
"Simulated failure {attempt_number} of {}",
self.failure_count
),
);
let wait = self.load_delay;
return async move {
std::thread::sleep(wait);
Err(AssetReaderError::Io(io_error.into()))
}
.await;
}
self.memory_reader.read(path).await
}
}
fn test_app(dir: Dir) -> (App, GateOpener) {
let mut app = App::new();
let (gated_memory_reader, gate_opener) = GatedReader::new(MemoryAssetReader { root: dir });
app.register_asset_source(
AssetSourceId::Default,
AssetSource::build().with_reader(move || Box::new(gated_memory_reader.clone())),
)
.add_plugins((TaskPoolPlugin::default(), AssetPlugin::default()));
(app, gate_opener)
}
pub fn run_app_until(app: &mut App, mut predicate: impl FnMut(&mut World) -> Option<()>) {
for _ in 0..LARGE_ITERATION_COUNT {
app.update();
if predicate(app.world_mut()).is_some() {
return;
}
}
panic!("Ran out of loops to return `Some` from `predicate`");
}
const LARGE_ITERATION_COUNT: usize = 10000;
fn get<A: Asset>(world: &World, id: AssetId<A>) -> Option<&A> {
world.resource::<Assets<A>>().get(id)
}
#[derive(Resource, Default)]
struct StoredEvents(Vec<AssetEvent<CoolText>>);
fn store_asset_events(
mut reader: EventReader<AssetEvent<CoolText>>,
mut storage: ResMut<StoredEvents>,
) {
storage.0.extend(reader.read().cloned());
}
#[test]
fn load_dependencies() {
// The particular usage of GatedReader in this test will cause deadlocking if running single-threaded
#[cfg(not(feature = "multi_threaded"))]
panic!("This test requires the \"multi_threaded\" feature, otherwise it will deadlock.\ncargo test --package bevy_asset --features multi_threaded");
let dir = Dir::default();
let a_path = "a.cool.ron";
let a_ron = r#"
(
text: "a",
dependencies: [
"foo/b.cool.ron",
"c.cool.ron",
],
embedded_dependencies: [],
sub_texts: [],
)"#;
let b_path = "foo/b.cool.ron";
let b_ron = r#"
(
text: "b",
dependencies: [],
embedded_dependencies: [],
sub_texts: [],
)"#;
let c_path = "c.cool.ron";
let c_ron = r#"
(
text: "c",
dependencies: [
"d.cool.ron",
],
embedded_dependencies: ["a.cool.ron", "foo/b.cool.ron"],
sub_texts: ["hello"],
)"#;
let d_path = "d.cool.ron";
let d_ron = r#"
(
text: "d",
dependencies: [],
embedded_dependencies: [],
sub_texts: [],
)"#;
dir.insert_asset_text(Path::new(a_path), a_ron);
dir.insert_asset_text(Path::new(b_path), b_ron);
dir.insert_asset_text(Path::new(c_path), c_ron);
dir.insert_asset_text(Path::new(d_path), d_ron);
#[derive(Resource)]
struct IdResults {
b_id: AssetId<CoolText>,
c_id: AssetId<CoolText>,
d_id: AssetId<CoolText>,
}
let (mut app, gate_opener) = test_app(dir);
app.init_asset::<CoolText>()
.init_asset::<SubText>()
.init_resource::<StoredEvents>()
.register_asset_loader(CoolTextLoader)
.add_systems(Update, store_asset_events);
let asset_server = app.world().resource::<AssetServer>().clone();
let handle: Handle<CoolText> = asset_server.load(a_path);
let a_id = handle.id();
app.update();
{
let a_text = get::<CoolText>(app.world(), a_id);
let (a_load, a_deps, a_rec_deps) = asset_server.get_load_states(a_id).unwrap();
assert!(a_text.is_none(), "a's asset should not exist yet");
assert!(a_load.is_loading());
assert!(a_deps.is_loading());
assert!(a_rec_deps.is_loading());
}
// Allow "a" to load ... wait for it to finish loading and validate results
// Dependencies are still gated so they should not be loaded yet
gate_opener.open(a_path);
run_app_until(&mut app, |world| {
let a_text = get::<CoolText>(world, a_id)?;
let (a_load, a_deps, a_rec_deps) = asset_server.get_load_states(a_id).unwrap();
assert_eq!(a_text.text, "a");
assert_eq!(a_text.dependencies.len(), 2);
assert!(a_load.is_loaded());
assert!(a_deps.is_loading());
assert!(a_rec_deps.is_loading());
let b_id = a_text.dependencies[0].id();
let b_text = get::<CoolText>(world, b_id);
let (b_load, b_deps, b_rec_deps) = asset_server.get_load_states(b_id).unwrap();
assert!(b_text.is_none(), "b component should not exist yet");
assert!(b_load.is_loading());
assert!(b_deps.is_loading());
assert!(b_rec_deps.is_loading());
let c_id = a_text.dependencies[1].id();
let c_text = get::<CoolText>(world, c_id);
let (c_load, c_deps, c_rec_deps) = asset_server.get_load_states(c_id).unwrap();
assert!(c_text.is_none(), "c component should not exist yet");
assert!(c_load.is_loading());
assert!(c_deps.is_loading());
assert!(c_rec_deps.is_loading());
Some(())
});
// Allow "b" to load ... wait for it to finish loading and validate results
// "c" should not be loaded yet
gate_opener.open(b_path);
run_app_until(&mut app, |world| {