Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement read_directory and is_directory for android asset reader #9969

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 53 additions & 13 deletions crates/bevy_asset/src/io/android.rs
@@ -1,18 +1,20 @@
use crate::io::{
get_meta_path, AssetReader, AssetReaderError, AssetWatcher, EmptyPathStream, PathStream,
Reader, VecReader,
get_meta_path, AssetReader, AssetReaderError, AssetWatcher, PathStream, Reader, VecReader,
};
use anyhow::Result;
use bevy_log::error;
use bevy_utils::BoxedFuture;
use std::{ffi::CString, path::Path};
use futures_lite::stream;
use std::{
ffi::{CString, OsString},
os::unix::ffi::OsStringExt as _,
path::{Path, PathBuf},
};

/// [`AssetReader`] implementation for Android devices, built on top of Android's [`AssetManager`].
///
/// Implementation details:
///
/// - [`load_path`](AssetIo::load_path) uses the [`AssetManager`] to load files.
/// - [`read_directory`](AssetIo::read_directory) always returns an empty iterator.
/// - [`AssetManager`] to load files and directories, and check if a path is a directory.
/// - Watching for changes is not supported. The watcher method will do nothing.
///
/// [AssetManager]: https://developer.android.com/reference/android/content/res/AssetManager
Expand Down Expand Up @@ -58,19 +60,57 @@ impl AssetReader for AndroidAssetReader {

fn read_directory<'a>(
&'a self,
_path: &'a Path,
path: &'a Path,
) -> BoxedFuture<'a, Result<Box<PathStream>, AssetReaderError>> {
let stream: Box<PathStream> = Box::new(EmptyPathStream);
error!("Reading directories is not supported with the AndroidAssetReader");
Box::pin(async move { Ok(stream) })
Box::pin(async move {
let asset_manager = bevy_winit::ANDROID_APP
.get()
.expect("Bevy must be setup with the #[bevy_main] macro on Android")
.asset_manager();

let opened_dir = asset_manager
.open_dir(&CString::new(path.to_str().unwrap()).unwrap())
.ok_or(AssetReaderError::NotFound(path.to_path_buf()))?;

// collecting it since AssetDir can't be sent between threads
let paths = opened_dir
.filter_map(|path| {
// convert from CString to PathBuf, only works on unix which android is.
let bytes = path.to_bytes().to_vec();
let path: PathBuf = OsString::from_vec(bytes).into();

// filter out meta files as they are not considered assets
if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
if ext.eq_ignore_ascii_case("meta") {
return None;
}
}

Some(path)
})
.collect::<Vec<_>>();
let stream: Box<PathStream> = Box::new(stream::iter(paths));
Ok(stream)
})
}

fn is_directory<'a>(
&'a self,
_path: &'a Path,
path: &'a Path,
) -> BoxedFuture<'a, std::result::Result<bool, AssetReaderError>> {
error!("Reading directories is not supported with the AndroidAssetReader");
Box::pin(async move { Ok(false) })
Box::pin(async move {
let asset_manager = bevy_winit::ANDROID_APP
.get()
.expect("Bevy must be setup with the #[bevy_main] macro on Android")
.asset_manager();

// HACK: AssetManager API sucks so this hack is all we can do...
// This will only return either not found or true
asset_manager
.open_dir(&CString::new(path.to_str().unwrap()).unwrap())
.ok_or(AssetReaderError::NotFound(path.to_path_buf()))
.map(|_| true)
})
}

fn watch_for_changes(
Expand Down