Implement minimal asset saving#22622
Conversation
b8953aa to
6f9d0e6
Compare
1d215e4 to
77a14a9
Compare
greeble-dev
left a comment
There was a problem hiding this comment.
I'm clicking approve as the PR does what it says and is backed up by tests and an example. I left a few minor suggestions.
I do have some concerns with the overall direction:
- The code is significantly complicated by having to support handles and avoid copies. I'm not sure this complexity is worth it right now.
- We're not dogfooding since the engine doesn't have a complicated saver built-in - the example code is fairly contrived. This makes it hard to predict if the API will work well.
- My guess is that the subtleties around sub-assets and when to use
add_labeled_asset_with_new_handleversusadd_labeled_asset_with_existing_handlewill be challenging for users.
I don't think these concerns are blocking, but maybe they deserve more discussion. And as ever, there's the possibility that assets-as-entities ends up remaking the whole thing.
crates/bevy_asset/src/saver.rs
Outdated
| CowArc<'static, str>: Borrow<Q>, | ||
| Q: ?Sized + Hash + Eq, | ||
| { | ||
| pub fn get_labeled<B: Asset>(&self, label: &str) -> Option<SavedAsset<'a, '_, B>> { |
There was a problem hiding this comment.
This change means get_labeled can no longer be called directly with a CowArc<str>. Only a minor regression, but is it necessary? I guess the lifetimes might be tricky.
There was a problem hiding this comment.
We now accept impl AsRef<str>, so this should work now I think.
| @@ -0,0 +1,372 @@ | |||
| //! This example demonstrates how to save assets. | |||
There was a problem hiding this comment.
Maybe worth calling the example asset_saving_subassets.rs? Or subasset_saving.rs? It's a fairly complicated example, so this would leave space for a simpler example that doesn't involve sub-assets.
There was a problem hiding this comment.
I'm okay with this for now, and then we can rename once a simpler example is prepared.
There was a problem hiding this comment.
I can make a followup PR to split this into a simple saving example and a complex saving example.
I primarily focuses on the complex case since it's the one that this PR actually enables (the simple case you could previously do anyway).
|
|
||
| ## 1. Building the `SavedAsset` | ||
|
|
||
| To build the `SavedAsset`, either use `SavedAsset::from_asset`, or `SavedAssetBuilder`. For example: |
There was a problem hiding this comment.
I think this could do with a brief example of SavedAsset::from_asset that's separate from the SavedAssetBuilder example? Would make clear that SavedAssetBuilder is only needed for more complicated cases.
There was a problem hiding this comment.
Added a more simple example just for SavedAsset::from_asset!
crates/bevy_asset/src/saver.rs
Outdated
| // NOTE: We can't handle embedded dependencies in any way, since we need to write to | ||
| // another file to do so. | ||
| embedded_dependencies: vec![], | ||
| }; |
There was a problem hiding this comment.
| // NOTE: We can't handle embedded dependencies in any way, since we need to write to | |
| // another file to do so. | |
| embedded_dependencies: vec![], | |
| }; | |
| embedded_dependencies: vec![], | |
| }; | |
| // NOTE: We can't handle embedded dependencies in any way, since we need to write to | |
| // another file to do so. | |
| assert!(asset.embedded_dependencies.is_empty()); |
Assert seems safer if this is only used in tests.
| .web-asset-cache | ||
| examples/large_scenes/bistro/assets/* | ||
| examples/large_scenes/caldera_hotel/assets/* | ||
| examples/asset/saved_assets |
There was a problem hiding this comment.
I'm not sure what this is for? The example asset+meta is already tracked by git, and the example doesn't add new ones.
There was a problem hiding this comment.
Woops yup my jumping around my branch made me add these files accidentally.
I've gone and deleted them from the original commit so they remain untracked.
alice-i-cecile
left a comment
There was a problem hiding this comment.
Happy to see progress here. I think this is a sensible design, and the code is clear enough to be maintainable. I would also prefer a simpler example (keeping this one for a more advanced one), but I'm not going to block on that.
| Note that since these assets are borrowed, building the `SavedAsset` should happen in the same async | ||
| task as the next step. | ||
|
|
||
| ## 2. Calling `save_using_saver` |
There was a problem hiding this comment.
Can we simply rename this to save? Are there plans for a simpler API that don't require an explicit saver?
If not, we should definitely use the simpler name for the standard path.
There was a problem hiding this comment.
In the future, I think we will likely have a separate save function that looks up the appropriate saver for you (the same way load looks up the loader). This minimal version still seems reasonable to support in that world anyway, so it's not clear to me we will delete this function.
…tch `AssetLoader`.
18da29b to
65b770b
Compare
Objective
Solution
SavedAssetinstances by building up their labeled assets. This can either take an existing handle + asset ref, or will create a handle for your asset ref.AssetSaver, and write out the correct meta file.Some things I am leaving to future PRs
AssetSaver.Weird implementation details
Moo), since I needed to store just a ref or owned hashmap. I wrote a lengthy comment explaining why it's needed (TL;DR variance is complicated, Cow doesn't work). Note: it's possible we could just use CowArc instead, but we never need to clone the map so this seems like overkill. Also having a nice place to explain the variance problems is useful here.add_labeled_asset_with_*<'b: 'a>, since otherwise, theCowArcgets coerced to'staticwhich means the lifetime of the subasset needs to be'static, which practically makes this unusable. By adding a second lifetime dedicated to theCowArc,Testing