Skip to content

Commit

Permalink
Refactor and Improved Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bushrat011899 committed May 20, 2024
1 parent 10fa2ee commit 4a1e474
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
18 changes: 11 additions & 7 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,18 @@ impl Plugin for AssetPlugin {

#[cfg(not(target_arch = "wasm32"))]
{
let temp_source =
temp::get_temp_source(app.world_mut(), self.temporary_file_path.clone());
match temp::get_temp_source(app.world_mut(), self.temporary_file_path.clone()) {
Ok(source) => {
let mut sources = app
.world_mut()
.get_resource_or_insert_with::<AssetSourceBuilders>(Default::default);

let mut sources = app
.world_mut()
.get_resource_or_insert_with::<AssetSourceBuilders>(Default::default);

sources.insert("temp", temp_source);
sources.insert("temp", source);
}
Err(error) => {
error!("Could not setup temp:// AssetSource due to an IO Error: {error}");
}
};
}

{
Expand Down
48 changes: 28 additions & 20 deletions crates/bevy_asset/src/temp.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
use std::path::{Path, PathBuf};
use std::{
io::{Error, ErrorKind},
path::{Path, PathBuf},
};

use bevy_ecs::{system::Resource, world::World};

use crate::io::AssetSourceBuilder;

/// Private resource to store the temporary directory used by `temp://`.
/// Kept private as it should only be removed on application exit.
#[derive(Resource)]
struct TempAssetDirectory {
folder: TempDirectory,
}

impl TempAssetDirectory {
fn path(&self) -> &Path {
self.folder.path()
}
}

enum TempDirectory {
/// Uses [`TempDir`](tempfile::TempDir)'s drop behaviour to delete the directory.
/// Note that this is not _guaranteed_ to succeed, so it is possible to leak files from this
/// option until the underlying OS cleans temporary directories. For secure files, consider using
/// [`tempfile`](tempfile::tempfile) directly.
Delete(tempfile::TempDir),
/// Will not delete the temporary directory on exit, leaving cleanup the responsibility of
/// the user or their system.
Persist(PathBuf),
}

Expand All @@ -32,17 +33,24 @@ impl TempDirectory {
pub(crate) fn get_temp_source(
world: &mut World,
temporary_file_path: Option<String>,
) -> AssetSourceBuilder {
let temp_dir = world.get_resource_or_insert_with::<TempAssetDirectory>(|| {
let folder = match temporary_file_path {
) -> std::io::Result<AssetSourceBuilder> {
let temp_dir = match world.remove_resource::<TempDirectory>() {
Some(resource) => resource,
None => match temporary_file_path {
Some(path) => TempDirectory::Persist(path.into()),
None => TempDirectory::Delete(tempfile::tempdir().expect("todo")),
};
None => TempDirectory::Delete(tempfile::tempdir()?),
},
};

let path = temp_dir
.path()
.as_os_str()
.try_into()
.map_err(|error| Error::new(ErrorKind::InvalidData, error))?;

TempAssetDirectory { folder }
});
let source = AssetSourceBuilder::platform_default(path, None);

let path = temp_dir.path().to_str().expect("todo");
world.insert_resource(temp_dir);

AssetSourceBuilder::platform_default(path, None)
Ok(source)
}

0 comments on commit 4a1e474

Please sign in to comment.