Add support for android file system (#723)

Add support for android file system
This commit is contained in:
David Ackerman 2020-10-29 02:08:33 +02:00 committed by GitHub
parent 040b8f72b3
commit 0cddeed1c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 7 deletions

View file

@ -43,3 +43,6 @@ wasm-bindgen = { version = "0.2" }
web-sys = { version = "0.3", features = ["Request", "Window", "Response"]}
wasm-bindgen-futures = "0.4"
js-sys = "0.3"
[target.'cfg(target_os = "android")'.dependencies]
ndk-glue = { version = "0.2" }

View file

@ -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<P: AsRef<Path>>(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<Vec<u8>, 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<Box<dyn Iterator<Item = PathBuf>>, AssetIoError> {
Ok(Box::new(std::iter::empty::<PathBuf>()))
}
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()
}
}

View file

@ -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<AssetServer>) {
let mut changed = HashSet::default();
let asset_io =

View file

@ -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::*;

View file

@ -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());
}
}