# Objective Explicitly and exactly know what of the environment variables (if any) are being used/not-used/found-not-found by the `bevy_asset::io::file::get_base_path()`. - Describe the objective or issue this PR addresses: In a sufficiently complex project, with enough crates and such it _can_ be hard to know what the Asset Server is using as, what in the bevy parlance is its 'base path', this change seems to be the lowest effort to discovering that. ## Solution - Added `debug!` logging to the `FileAssetReader::new()` call. ## Testing See output by making a project and trying something like `RUST_LOG=bevy_asset::io::file=debug cargo run` - Ran Bevy's tests. - How can other people (reviewers) test your changes?: Intentionally mess with your `env` variables (BEVY_ASSET_ROOT and CARGO_MANIFEST_DIR, scatter assets about and attempt to (without this change) locate where it's going wrong. - Is there anything specific they need to know?: I encountered this issue in a rather large workspace with many many crates with multiple nested asset directories. - If relevant, what platforms did you test these changes on, and are there any important ones you can't test? Linux. ---
90 lines
2.5 KiB
Rust
90 lines
2.5 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_utils::tracing::{debug, error};
|
|
#[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(ToOwned::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());
|
|
debug!(
|
|
"Asset Server using {} as its base path.",
|
|
root_path.display()
|
|
);
|
|
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> + std::fmt::Debug>(path: P, create_root: bool) -> Self {
|
|
let root_path = get_base_path().join(path.as_ref());
|
|
if create_root {
|
|
if let Err(e) = std::fs::create_dir_all(&root_path) {
|
|
error!(
|
|
"Failed to create root directory {:?} for file asset writer: {:?}",
|
|
root_path, e
|
|
);
|
|
}
|
|
}
|
|
Self { root_path }
|
|
}
|
|
}
|