bevyengine / bevyengine/bevy

Consistent AssetPath Format

Open
#10,511 3 comments 6 reactions 0 assignees View on GitHub
A-Assets C-Feature
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## What problem does this solve or what need does it fill?

Currently AssetPath is backed by `Path`, which is great if the goal is to support arbitrary filesystem paths across operating systems:

* Windows: `\foo\bar`, `C:\foo\bar`
* Everything Else: `/foo/bar`, `/mnt/c/foo/bar`

However that is _not_ the goal of AssetPath. AssetPath exists to be an opinionated "cross platform virtual asset filesystem with support for multiple arbitrary asset sources and asset labels, which serves as a unique canonical asset identifier". Ideally, you can compare two "absolute AssetPaths", and if they are not equal, they are not the same asset.

This mismatch in goals can create problems. For example, when we create AssetPaths from hot-reload events on Windows, they come in the form `\foo\bar.png`. However users tend to use `/foo/bar.png` (and we encourage this pattern everywhere), which can create problems when considering paths as strings (like it did here #10500). As a cross-platform canonical path format, AssetPath has no business supporting windows-only drive letter syntax, as that location is not directly expressible on other operating systems. AssetPath _already_ has a cross platform way to express other mounted filesystem locations (`some_asset_source://`).

## What solution would you like?

We should build a new, consistent path abstraction for AssetPath and replace the current Path usage with that. It should support _only_ `/` and be designed with the explicit goal of providing consistent canonical identifiers.

Like the current AssetPath impl, it should avoid allocating when a `&static str` is used to construct it, and it should have true copy on write behavior (#9729).

This _will_ create a new problem: users that want to support absolute paths (ex: `C:\path\to\image.png`) can no longer directly use the _default_ AssetSource (which is rooted at the local asset folder)). We need a way to support this, in the interest of building apps like "file editors" or loading scenes from arbitrary locations on disk.

I believe this should be accomplished via a combination of two approachs:
1. App developers can already choose what the "default" asset source is. They can mount the root of the C drive on windows (and `/` on every other platform), enabling absolute paths to work by default (without the windows drive syntax).
2. For windows "drive syntax", App developers can mount those drives as asset sources with equivalent names (ex: The AssetPath `A://some/path.png` would map to the windows path `A:\some\path.png`). We could then build AssetPath::from(Path) in a way that explicitly handles windows drive syntax and converts it to the equivalent AssetPath. We could also consider building "automount" functionality that will mount windows drives when they are requested.

One issue with (2) is that Bevy Asset V2 does not currently support mounting new asset sources at runtime. We'd almost certainly want to support that, as new drives can be connected at arbitrary times when an app is running.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.