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