bevy_asset/io/file/
mod.rs1#[cfg(feature = "file_watcher")]
2mod file_watcher;
3
4#[cfg(feature = "multi_threaded")]
5mod file_asset;
6#[cfg(not(feature = "multi_threaded"))]
7mod sync_file_asset;
8
9#[cfg(feature = "file_watcher")]
10pub use file_watcher::*;
11use tracing::{debug, error};
12
13use alloc::borrow::ToOwned;
14use std::{
15 env,
16 path::{Path, PathBuf},
17};
18
19pub(crate) fn get_base_path() -> PathBuf {
20 if let Ok(manifest_dir) = env::var("BEVY_ASSET_ROOT") {
21 PathBuf::from(manifest_dir)
22 } else if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
23 PathBuf::from(manifest_dir)
24 } else {
25 env::current_exe()
26 .map(|path| path.parent().map(ToOwned::to_owned).unwrap())
27 .unwrap()
28 }
29}
30
31pub struct FileAssetReader {
35 root_path: PathBuf,
36}
37
38impl FileAssetReader {
39 pub fn new<P: AsRef<Path>>(path: P) -> Self {
44 let root_path = Self::get_base_path().join(path.as_ref());
45 debug!(
46 "Asset Server using {} as its base path.",
47 root_path.display()
48 );
49 Self { root_path }
50 }
51
52 pub fn get_base_path() -> PathBuf {
57 get_base_path()
58 }
59
60 pub fn root_path(&self) -> &PathBuf {
64 &self.root_path
65 }
66}
67
68pub struct FileAssetWriter {
70 root_path: PathBuf,
71}
72
73impl FileAssetWriter {
74 pub fn new<P: AsRef<Path> + core::fmt::Debug>(path: P, create_root: bool) -> Self {
77 let root_path = get_base_path().join(path.as_ref());
78 if create_root {
79 if let Err(e) = std::fs::create_dir_all(&root_path) {
80 error!(
81 "Failed to create root directory {} for file asset writer: {}",
82 root_path.display(),
83 e
84 );
85 }
86 }
87 Self { root_path }
88 }
89}