Skip to content
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

Add PrototypesMut::load_folder #36

Merged
merged 4 commits into from
May 1, 2023
Merged
Changes from 1 commit
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
21 changes: 20 additions & 1 deletion bevy_proto_backend/src/proto/prototypes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::{Borrow, Cow};

use bevy::asset::{Handle, HandleId, LoadState};
use bevy::asset::{AssetServerError, Handle, HandleId, LoadState};
use bevy::ecs::system::SystemParam;
use bevy::prelude::{AssetServer, Res, ResMut};
use std::hash::Hash;
Expand Down Expand Up @@ -48,6 +48,25 @@ impl<'w, T: Prototypical> PrototypesMut<'w, T> {
handle
}

/// Load the prototypes at the given path.
wusticality marked this conversation as resolved.
Show resolved Hide resolved
///
/// 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> {
let path = path.into();
let handles: Vec<_> = self.asset_server.load_folder(path.as_ref())?
.into_iter().map(|handle| handle.typed::<T>()).collect();
wusticality marked this conversation as resolved.
Show resolved Hide resolved

handles.iter().for_each(|handle| {
wusticality marked this conversation as resolved.
Show resolved Hide resolved
let path = self.asset_server.get_handle_path(handle).unwrap().path().to_str().unwrap().to_string();
Copy link
Contributor Author

@wusticality wusticality Apr 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit gross, we should probably handle the error cases instead of calling unwrap.

Perhaps I should add PrototypesError?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically this should always return a path, right? If so, I think we can go ahead and just unwrap it (maybe with an .expect() instead).

Same for the call to to_str (which we could replace with to_str_lossy but that could break the mapping).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some errors for this, let me know what you think


self.storage.insert(path, handle.clone());
});

Ok(handles)
}

/// Remove the stored handle for the given prototype path.
///
/// This allows the asset to be unloaded if the handle is dropped and no other
Expand Down