bevy
bevy copied to clipboard
Add directory related functions to `AndroidAssetReader`
Objective
- Fixes #9968
Solution
- Uses open_dir to read directories and collects child list, since it can't be shared across threads.
- For
is_directory, uses result of open, which will fail for directories. I tried using the result ofopen_dirfor this, but it was successful for files too, which made loading folders return empty lists, sinceopen_dirwas successful and treated all files as empty directories. - Ignoring
metafiles was copied from filesystem implementation
Changelog
- Fixed: Android's AssetReader implementation now supports read_directory and is_directory.
Notes
I noticed late that there was the #9968 issue (I only noticed #9591), so I have also missed that a PR was already open (#9969). Feel free to copy over the fixes from this one over there.
The only difference I notice between these 2, is that I have used open instead of open_dir for is_directory implementation. I have tried with open_dir too, but unfortunately that didn't work. I tested this on an actual device, using the mobile example, by making some minor changes:
#[derive(Resource)]
struct FolderAssets(Handle<LoadedFolder>);
// the `bevy_main` proc_macro generates the required boilerplate for iOS and Android
#[bevy_main]
fn main() {
// ...
.add_systems(Startup, (setup_scene, load_music_files))
.add_systems(
Update,
// Removed the handle_lifetime since AudioBundle is added later
(touch_camera, button_handler, setup_music),
);
// ...
}
fn load_music_files(asset_server: Res<AssetServer>, mut commands: Commands) {
let sounds = asset_server.load_folder("sounds");
commands.insert_resource(FolderAssets(sounds));
}
fn setup_music(
mut commands: Commands,
folders: Res<Assets<LoadedFolder>>,
mut loaded_event: EventReader<AssetEvent<LoadedFolder>>,
) {
for event in loaded_event.read() {
if let AssetEvent::LoadedWithDependencies { id } = event {
if let Some(folder) = folders.get(*id) {
warn!("Folder items: {:?}", folder.handles);
if let Some(source) = folder.handles.first() {
commands.spawn(AudioBundle {
source: source.clone().typed::<AudioSource>(),
settings: PlaybackSettings::LOOP,
});
}
}
}
}
}
Welcome, new contributor!
Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨