bevy/crates/bevy_asset/src/io/file/mod.rs
Michael Leandersson 46cbb8f781 Do not panic when failing to create assets folder (#10613) (#10614)
# Objective

- Allow bevy applications that does not have any assets folder to start
from a read-only directory. (typically installed to a systems folder)

Fixes #10613

## Solution

- warn instead of panic when assets folder creation fails.
2023-11-29 16:47:36 -08:00

89 lines
2.4 KiB
Rust

#[cfg(feature = "file_watcher")]
mod file_watcher;
#[cfg(feature = "multi-threaded")]
mod file_asset;
#[cfg(not(feature = "multi-threaded"))]
mod sync_file_asset;
use bevy_log::warn;
#[cfg(feature = "file_watcher")]
pub use file_watcher::*;
use std::{
env,
path::{Path, PathBuf},
};
pub(crate) fn get_base_path() -> PathBuf {
if let Ok(manifest_dir) = env::var("BEVY_ASSET_ROOT") {
PathBuf::from(manifest_dir)
} else if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
PathBuf::from(manifest_dir)
} else {
env::current_exe()
.map(|path| {
path.parent()
.map(|exe_parent_path| exe_parent_path.to_owned())
.unwrap()
})
.unwrap()
}
}
/// I/O implementation for the local filesystem.
///
/// This asset I/O is fully featured but it's not available on `android` and `wasm` targets.
pub struct FileAssetReader {
root_path: PathBuf,
}
impl FileAssetReader {
/// Creates a new `FileAssetIo` at a path relative to the executable's directory, optionally
/// watching for changes.
///
/// See `get_base_path` below.
pub fn new<P: AsRef<Path>>(path: P) -> Self {
let root_path = Self::get_base_path().join(path.as_ref());
if let Err(e) = std::fs::create_dir_all(&root_path) {
warn!(
"Failed to create root directory {:?} for file asset reader: {:?}",
root_path, e
);
}
Self { root_path }
}
/// Returns the base path of the assets directory, which is normally the executable's parent
/// directory.
///
/// If the `CARGO_MANIFEST_DIR` environment variable is set, then its value will be used
/// instead. It's set by cargo when running with `cargo run`.
pub fn get_base_path() -> PathBuf {
get_base_path()
}
/// Returns the root directory where assets are loaded from.
///
/// See `get_base_path`.
pub fn root_path(&self) -> &PathBuf {
&self.root_path
}
}
pub struct FileAssetWriter {
root_path: PathBuf,
}
impl FileAssetWriter {
/// Creates a new `FileAssetIo` at a path relative to the executable's directory, optionally
/// watching for changes.
///
/// See `get_base_path` below.
pub fn new<P: AsRef<Path>>(path: P) -> Self {
Self {
root_path: get_base_path().join(path.as_ref()),
}
}
}