diff --git a/crates/bevy_asset/Cargo.toml b/crates/bevy_asset/Cargo.toml index 9de73b565c..a5a149bd66 100644 --- a/crates/bevy_asset/Cargo.toml +++ b/crates/bevy_asset/Cargo.toml @@ -42,4 +42,7 @@ rand = "0.7.3" wasm-bindgen = { version = "0.2" } web-sys = { version = "0.3", features = ["Request", "Window", "Response"]} wasm-bindgen-futures = "0.4" -js-sys = "0.3" \ No newline at end of file +js-sys = "0.3" + +[target.'cfg(target_os = "android")'.dependencies] +ndk-glue = { version = "0.2" } diff --git a/crates/bevy_asset/src/io/android_asset_io.rs b/crates/bevy_asset/src/io/android_asset_io.rs new file mode 100644 index 0000000000..dbb44b4f9c --- /dev/null +++ b/crates/bevy_asset/src/io/android_asset_io.rs @@ -0,0 +1,51 @@ +use crate::{AssetIo, AssetIoError}; +use anyhow::Result; +use bevy_ecs::bevy_utils::BoxedFuture; +use std::{ + ffi::CString, + path::{Path, PathBuf}, +}; + +pub struct AndroidAssetIo { + root_path: PathBuf, +} + +impl AndroidAssetIo { + pub fn new>(path: P) -> Self { + AndroidAssetIo { + root_path: path.as_ref().to_owned(), + } + } +} + +impl AssetIo for AndroidAssetIo { + fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result, AssetIoError>> { + Box::pin(async move { + let asset_manager = ndk_glue::native_activity().asset_manager(); + let mut opened_asset = asset_manager + .open(&CString::new(path.to_str().unwrap()).unwrap()) + .ok_or(AssetIoError::NotFound(path.to_path_buf()))?; + let bytes = opened_asset.get_buffer()?; + Ok(bytes.to_vec()) + }) + } + + fn read_directory( + &self, + _path: &Path, + ) -> Result>, AssetIoError> { + Ok(Box::new(std::iter::empty::())) + } + + fn watch_path_for_changes(&self, _path: &Path) -> Result<(), AssetIoError> { + Ok(()) + } + + fn watch_for_changes(&self) -> Result<(), AssetIoError> { + Ok(()) + } + + fn is_directory(&self, path: &Path) -> bool { + self.root_path.join(path).is_dir() + } +} diff --git a/crates/bevy_asset/src/io/file_asset_io.rs b/crates/bevy_asset/src/io/file_asset_io.rs index 7c5718127f..f915e512d2 100644 --- a/crates/bevy_asset/src/io/file_asset_io.rs +++ b/crates/bevy_asset/src/io/file_asset_io.rs @@ -103,7 +103,10 @@ impl AssetIo for FileAssetIo { } } -#[cfg(all(feature = "filesystem_watcher", not(target_arch = "wasm32")))] +#[cfg(all( + feature = "filesystem_watcher", + all(not(target_arch = "wasm32"), not(target_os = "android")) +))] pub fn filesystem_watcher_system(asset_server: Res) { let mut changed = HashSet::default(); let asset_io = diff --git a/crates/bevy_asset/src/io/mod.rs b/crates/bevy_asset/src/io/mod.rs index 113df6d681..58ac8fa8fe 100644 --- a/crates/bevy_asset/src/io/mod.rs +++ b/crates/bevy_asset/src/io/mod.rs @@ -1,9 +1,13 @@ -#[cfg(not(target_arch = "wasm32"))] +#[cfg(target_os = "android")] +mod android_asset_io; +#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))] mod file_asset_io; #[cfg(target_arch = "wasm32")] mod wasm_asset_io; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(target_os = "android")] +pub use android_asset_io::*; +#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))] pub use file_asset_io::*; #[cfg(target_arch = "wasm32")] pub use wasm_asset_io::*; diff --git a/crates/bevy_asset/src/lib.rs b/crates/bevy_asset/src/lib.rs index e62748f838..d5a78d8831 100644 --- a/crates/bevy_asset/src/lib.rs +++ b/crates/bevy_asset/src/lib.rs @@ -1,6 +1,9 @@ mod asset_server; mod assets; -#[cfg(all(feature = "filesystem_watcher", not(target_arch = "wasm32")))] +#[cfg(all( + feature = "filesystem_watcher", + all(not(target_arch = "wasm32"), not(target_os = "android")) +))] mod filesystem_watcher; mod handle; mod info; @@ -62,10 +65,12 @@ impl Plugin for AssetPlugin { .resources_mut() .get_or_insert_with(AssetServerSettings::default); - #[cfg(not(target_arch = "wasm32"))] + #[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))] let source = FileAssetIo::new(&settings.asset_folder); #[cfg(target_arch = "wasm32")] let source = WasmAssetIo::new(&settings.asset_folder); + #[cfg(target_os = "android")] + let source = AndroidAssetIo::new(&settings.asset_folder); AssetServer::new(source, task_pool) }; @@ -78,7 +83,10 @@ impl Plugin for AssetPlugin { asset_server::free_unused_assets_system.system(), ); - #[cfg(all(feature = "filesystem_watcher", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "filesystem_watcher", + all(not(target_arch = "wasm32"), not(target_os = "android")) + ))] app.add_system_to_stage(stage::LOAD_ASSETS, io::filesystem_watcher_system.system()); } }