Use cfg attribute to filter supported extensions (#2297)

When implementing `AssetLoader ` you need to specify which File extensions are supported by that loader.
Currently, Bevy always says it supports extensions that actually require activating a Feature beforehand.

This PR adds cf attributes, so Bevy only tries to load those Extensions whose Features were activated.

This prevents Bevy from Panicking and reports such a warning:
```
Jun 02 23:05:57.139  WARN bevy_asset::asset_server: no `AssetLoader` found for the following extension: ogg
```

This also fixes the Bug, that the `png Feature had to be activated even if you wanted to load a different image format.

Fixes #640
This commit is contained in:
MinerSebas 2021-06-03 19:58:08 +00:00
parent c4b8210a7c
commit 4fed2ee858
4 changed files with 41 additions and 5 deletions

View File

@ -30,7 +30,16 @@ impl AssetLoader for Mp3Loader {
} }
fn extensions(&self) -> &[&str] { fn extensions(&self) -> &[&str] {
&["mp3", "flac", "wav", "ogg"] &[
#[cfg(feature = "mp3")]
"mp3",
#[cfg(feature = "flac")]
"flac",
#[cfg(feature = "wav")]
"wav",
#[cfg(feature = "vorbis")]
"ogg",
]
} }
} }

View File

@ -23,11 +23,13 @@ impl Plugin for AudioPlugin {
fn build(&self, app: &mut AppBuilder) { fn build(&self, app: &mut AppBuilder) {
app.init_non_send_resource::<AudioOutput<AudioSource>>() app.init_non_send_resource::<AudioOutput<AudioSource>>()
.add_asset::<AudioSource>() .add_asset::<AudioSource>()
.init_asset_loader::<Mp3Loader>()
.init_resource::<Audio<AudioSource>>() .init_resource::<Audio<AudioSource>>()
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
play_queued_audio_system::<AudioSource>.exclusive_system(), play_queued_audio_system::<AudioSource>.exclusive_system(),
); );
#[cfg(any(feature = "mp3", feature = "flac", feature = "wav", feature = "vorbis"))]
app.init_asset_loader::<Mp3Loader>();
} }
} }

View File

@ -58,7 +58,13 @@ use renderer::{AssetRenderResourceBindings, RenderResourceBindings, RenderResour
use shader::ShaderLoader; use shader::ShaderLoader;
#[cfg(feature = "hdr")] #[cfg(feature = "hdr")]
use texture::HdrTextureLoader; use texture::HdrTextureLoader;
#[cfg(feature = "png")] #[cfg(any(
feature = "png",
feature = "dds",
feature = "tga",
feature = "jpeg",
feature = "bmp"
))]
use texture::ImageTextureLoader; use texture::ImageTextureLoader;
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)] #[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
@ -97,7 +103,13 @@ impl Default for RenderPlugin {
impl Plugin for RenderPlugin { impl Plugin for RenderPlugin {
fn build(&self, app: &mut AppBuilder) { fn build(&self, app: &mut AppBuilder) {
#[cfg(feature = "png")] #[cfg(any(
feature = "png",
feature = "dds",
feature = "tga",
feature = "jpeg",
feature = "bmp"
))]
{ {
app.init_asset_loader::<ImageTextureLoader>(); app.init_asset_loader::<ImageTextureLoader>();
} }

View File

@ -8,7 +8,20 @@ use thiserror::Error;
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct ImageTextureLoader; pub struct ImageTextureLoader;
const FILE_EXTENSIONS: &[&str] = &["png", "dds", "tga", "jpg", "jpeg", "bmp"]; const FILE_EXTENSIONS: &[&str] = &[
#[cfg(feature = "png")]
"png",
#[cfg(feature = "dds")]
"dds",
#[cfg(feature = "tga")]
"tga",
#[cfg(feature = "jpeg")]
"jpg",
#[cfg(feature = "jpeg")]
"jpeg",
#[cfg(feature = "bmp")]
"bmp",
];
impl AssetLoader for ImageTextureLoader { impl AssetLoader for ImageTextureLoader {
fn load<'a>( fn load<'a>(