Support loading assets from arbitrary filesystem paths by specifying an absolute path as the asset path #16294
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective / Problem
There is no way to access asset files outside of the designated asset directory. Bevy's
FileReader
/FileWriter
always interpret paths relative to the configured root directory (based on either the executable's path, or env var).Being able to load (and save) assets anywhere in the filesystem is important for many applications, most notably editors.
The goal of this PR is to implement the simplest possible solution to this problem.
Solution
If an absolute path is provided when loading/saving an asset, it is used as-is.
If a relative path is provided, everything works exactly the same as before. Technically this isn't a breaking change, as
previously only relative paths were accepted by Bevy.
Testing
I ran the various bevy examples to make sure they can still load their assets (from the
assets
folder, as before).I then modified them (locally, not committed, ofc) by changing the paths in the source code to various absolute paths
on my computer.
I can confirm Bevy can now successfully load random images and GLTFs on my PC. :)
I have not tested asset meta files.
I have not tested the "sync" loader (when the
multi_threaded
cargo feature is disabled).I have not tested asset writing / saving.
I have not tested the file watcher / hot reloading.
Probably also worth testing loading a complex GLTF file that references external files (for textures, etc) using an absolute path, to make sure asset dependencies work when using the new absolute paths.
Nothing special. Just try to load assets from an absolute path.
Windows:
I have not tested on Windows yet. I will be able to do that soon. Will edit the PR description to remove this note when I do.
The changes in this PR are only relevant to desktop platforms (the file asset io is not used on mobile and web).
Migration guide not necessary.
This PR, while straightforward, does enable cool new use cases and is probably deserving of a release note.