Skip to content

Commit

Permalink
Code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wusticality committed May 1, 2023
1 parent 03c70aa commit cc4b5ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
3 changes: 3 additions & 0 deletions bevy_proto_backend/src/path/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ pub enum PathError {
/// [`Config`]: crate::proto::Config
#[error("invalid extension {0:?}")]
InvalidExtension(PathBuf),
/// The path cannot be converted to a string.
#[error("cannot convert path to a string")]
ConversionError
}
39 changes: 28 additions & 11 deletions bevy_proto_backend/src/proto/prototypes.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
use std::borrow::{Borrow, Cow};

use bevy::asset::{AssetServerError, Handle, HandleId, LoadState};
use bevy::asset::{AssetServerError, Handle, HandleId, HandleUntyped, LoadState};
use bevy::ecs::system::SystemParam;
use bevy::prelude::{AssetServer, Res, ResMut};
use std::hash::Hash;
use thiserror::Error;
use crate::path::PathError;

use crate::proto::{ProtoStorage, Prototypical};
use crate::registration::ProtoRegistry;

#[derive(Debug, Error)]
pub enum ProtoLoadError {
/// Indicates that the [`AssetServer`] encountered an error.
#[error(transparent)]
AssetServerError(#[from] AssetServerError),
/// Indicates that there was a path error.
#[error(transparent)]
PathError(#[from] PathError),
}

/// A helper [`SystemParam`] for managing [prototypes].
///
/// For the mutable version, see [`PrototypesMut`].
Expand Down Expand Up @@ -48,21 +60,26 @@ impl<'w, T: Prototypical> PrototypesMut<'w, T> {
handle
}

/// Load the prototypes at the given path.
/// Load all the prototypes in the given directory.
///
/// This will also store strong handles to the prototypes in order to keep them loaded.
///
/// To load without automatically storing the handles, try using [`AssetServer::load_folder`].
pub fn load_folder<P: Into<Cow<'static, str>>>(&mut self, path: P) -> Result<Vec<Handle<T>>, AssetServerError> {
pub fn load_folder<P: Into<Cow<'static, str>>>(&mut self, path: P) -> Result<Vec<HandleUntyped>, ProtoLoadError> {
let path = path.into();
let handles: Vec<_> = self.asset_server.load_folder(path.as_ref())?
.into_iter().map(|handle| handle.typed::<T>()).collect();

handles.iter().for_each(|handle| {
let path = self.asset_server.get_handle_path(handle).unwrap().path().to_str().unwrap().to_string();

self.storage.insert(path, handle.clone());
});
let handles: Vec<_> = self.asset_server.load_folder(path.as_ref())?;

for handle in &handles {
let path = self.asset_server
.get_handle_path(handle)
.unwrap()
.path()
.to_str()
.ok_or(PathError::ConversionError)?
.to_string();

self.storage.insert(path, handle.clone().typed::<T>());
}

Ok(handles)
}
Expand Down

0 comments on commit cc4b5ec

Please sign in to comment.