Skip to content

Support loading assets from arbitrary filesystem paths by specifying an absolute path as the asset path #16294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 125 additions & 31 deletions crates/bevy_asset/src/io/file/file_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,34 @@ impl Reader for File {}

impl AssetReader for FileAssetReader {
async fn read<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
let full_path = self.root_path.join(path);
let path_buf;
let full_path = if path.is_absolute() {
path
} else {
path_buf = self.root_path.join(path);
&path_buf
};
File::open(&full_path).await.map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
AssetReaderError::NotFound(full_path)
AssetReaderError::NotFound(full_path.to_path_buf())
} else {
e.into()
}
})
}

async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
let meta_path = get_meta_path(path);
let full_path = self.root_path.join(meta_path);
File::open(&full_path).await.map_err(|e| {
let path_buf;
let full_path = if path.is_absolute() {
path
} else {
path_buf = self.root_path.join(path);
&path_buf
};
let meta_path = get_meta_path(full_path);
File::open(&meta_path).await.map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
AssetReaderError::NotFound(full_path)
AssetReaderError::NotFound(meta_path)
} else {
e.into()
}
Expand All @@ -60,7 +72,13 @@ impl AssetReader for FileAssetReader {
&'a self,
path: &'a Path,
) -> Result<Box<PathStream>, AssetReaderError> {
let full_path = self.root_path.join(path);
let path_buf;
let full_path = if path.is_absolute() {
path
} else {
path_buf = self.root_path.join(path);
&path_buf
};
match read_dir(&full_path).await {
Ok(read_dir) => {
let root_path = self.root_path.clone();
Expand All @@ -82,7 +100,7 @@ impl AssetReader for FileAssetReader {
}
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
Err(AssetReaderError::NotFound(full_path))
Err(AssetReaderError::NotFound(full_path.to_path_buf()))
} else {
Err(e.into())
}
Expand All @@ -91,17 +109,29 @@ impl AssetReader for FileAssetReader {
}

async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
let full_path = self.root_path.join(path);
let path_buf;
let full_path = if path.is_absolute() {
path
} else {
path_buf = self.root_path.join(path);
&path_buf
};
let metadata = full_path
.metadata()
.map_err(|_e| AssetReaderError::NotFound(path.to_owned()))?;
.map_err(|_e| AssetReaderError::NotFound(full_path.to_path_buf()))?;
Ok(metadata.file_type().is_dir())
}
}

impl AssetWriter for FileAssetWriter {
async fn write<'a>(&'a self, path: &'a Path) -> Result<Box<Writer>, AssetWriterError> {
let full_path = self.root_path.join(path);
let path_buf;
let full_path = if path.is_absolute() {
path
} else {
path_buf = self.root_path.join(path);
&path_buf
};
if let Some(parent) = full_path.parent() {
async_fs::create_dir_all(parent).await?;
}
Expand All @@ -111,26 +141,44 @@ impl AssetWriter for FileAssetWriter {
}

async fn write_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Writer>, AssetWriterError> {
let meta_path = get_meta_path(path);
let full_path = self.root_path.join(meta_path);
if let Some(parent) = full_path.parent() {
let path_buf;
let full_path = if path.is_absolute() {
path
} else {
path_buf = self.root_path.join(path);
&path_buf
};
let meta_path = get_meta_path(full_path);
if let Some(parent) = meta_path.parent() {
async_fs::create_dir_all(parent).await?;
}
let file = File::create(&full_path).await?;
let file = File::create(&meta_path).await?;
let writer: Box<Writer> = Box::new(file);
Ok(writer)
}

async fn remove<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
let full_path = self.root_path.join(path);
let path_buf;
let full_path = if path.is_absolute() {
path
} else {
path_buf = self.root_path.join(path);
&path_buf
};
async_fs::remove_file(full_path).await?;
Ok(())
}

async fn remove_meta<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
let meta_path = get_meta_path(path);
let full_path = self.root_path.join(meta_path);
async_fs::remove_file(full_path).await?;
let path_buf;
let full_path = if path.is_absolute() {
path
} else {
path_buf = self.root_path.join(path);
&path_buf
};
let meta_path = get_meta_path(full_path);
async_fs::remove_file(meta_path).await?;
Ok(())
}

Expand All @@ -139,8 +187,19 @@ impl AssetWriter for FileAssetWriter {
old_path: &'a Path,
new_path: &'a Path,
) -> Result<(), AssetWriterError> {
let full_old_path = self.root_path.join(old_path);
let full_new_path = self.root_path.join(new_path);
let (path_buf_old, path_buf_new);
let full_old_path = if old_path.is_absolute() {
old_path
} else {
path_buf_old = self.root_path.join(old_path);
&path_buf_old
};
let full_new_path = if new_path.is_absolute() {
new_path
} else {
path_buf_new = self.root_path.join(new_path);
&path_buf_new
};
if let Some(parent) = full_new_path.parent() {
async_fs::create_dir_all(parent).await?;
}
Expand All @@ -153,31 +212,60 @@ impl AssetWriter for FileAssetWriter {
old_path: &'a Path,
new_path: &'a Path,
) -> Result<(), AssetWriterError> {
let old_meta_path = get_meta_path(old_path);
let new_meta_path = get_meta_path(new_path);
let full_old_path = self.root_path.join(old_meta_path);
let full_new_path = self.root_path.join(new_meta_path);
if let Some(parent) = full_new_path.parent() {
let (path_buf_old, path_buf_new);
let full_old_path = if old_path.is_absolute() {
old_path
} else {
path_buf_old = self.root_path.join(old_path);
&path_buf_old
};
let full_new_path = if new_path.is_absolute() {
new_path
} else {
path_buf_new = self.root_path.join(new_path);
&path_buf_new
};
let old_meta_path = get_meta_path(full_old_path);
let new_meta_path = get_meta_path(full_new_path);
if let Some(parent) = new_meta_path.parent() {
async_fs::create_dir_all(parent).await?;
}
async_fs::rename(full_old_path, full_new_path).await?;
async_fs::rename(old_meta_path, new_meta_path).await?;
Ok(())
}

async fn create_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
let full_path = self.root_path.join(path);
let path_buf;
let full_path = if path.is_absolute() {
path
} else {
path_buf = self.root_path.join(path);
&path_buf
};
async_fs::create_dir_all(full_path).await?;
Ok(())
}

async fn remove_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
let full_path = self.root_path.join(path);
let path_buf;
let full_path = if path.is_absolute() {
path
} else {
path_buf = self.root_path.join(path);
&path_buf
};
async_fs::remove_dir_all(full_path).await?;
Ok(())
}

async fn remove_empty_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
let full_path = self.root_path.join(path);
let path_buf;
let full_path = if path.is_absolute() {
path
} else {
path_buf = self.root_path.join(path);
&path_buf
};
async_fs::remove_dir(full_path).await?;
Ok(())
}
Expand All @@ -186,7 +274,13 @@ impl AssetWriter for FileAssetWriter {
&'a self,
path: &'a Path,
) -> Result<(), AssetWriterError> {
let full_path = self.root_path.join(path);
let path_buf;
let full_path = if path.is_absolute() {
path
} else {
path_buf = self.root_path.join(path);
&path_buf
};
async_fs::remove_dir_all(&full_path).await?;
async_fs::create_dir_all(&full_path).await?;
Ok(())
Expand Down
Loading