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

[Feat] Normalize paths or allow asset path customization #240

Closed
focustense opened this issue Oct 14, 2023 · 5 comments · Fixed by #255 or #213
Closed

[Feat] Normalize paths or allow asset path customization #240

focustense opened this issue Oct 14, 2023 · 5 comments · Fixed by #255 or #213

Comments

@focustense
Copy link
Contributor

I'm using bevy_asset_loader to preload all assets, and currently keeping LDtk files in assets/levels adjacent to other paths like assets/spritesheets. The reasons for the latter aren't absolutely critical, but (a) multi-worlds feature is still considered experimental, (b) I'd like to support "external" mod-worlds, and (c) it helps to keep things organized in general.

Anyway, while working on a feature today that depends on shared assets being... well, shared, I discovered that bevy_ecs_ldtk was creating a second copy of all the assets referenced in the LDtk project. This is because the LDtk paths are relative to the project file, and when passed to the asset loader, are in the form of levels\\../spritesheets/foo.png. These paths do load, of course, but Bevy's asset loader assigns them new AssetPathIds despite pointing to the same file as spritesheets/foo.png, so they get loaded and added as new assets.

I realize that in theory there's a lot of madness around symlinks, and other very good reasons why all the pieces involved behave as they do. Nevertheless, in practice, and certainly in my project, those edge cases are never going to happen, and what I do care about is making sure that the LDtk systems and non-LDtk systems get the same asset when they request the same asset.

Slapping a path-clean on the ldtk_path_to_asset_path method solves the problem beautifully for me, but requires a change directly to the source code; I can't intercept this in the app code. Perhaps it is too blunt an instrument to apply to every user - maybe some people really do care about nuances like symlinks - so if that is the case, then I'd like to request canonicalization as an opt-in feature, whether it's something simple like a flag in LdtkSettings or requires us to do more of the legwork e.g. by implementing some trait with the actual conversion function.

It's probably a weird edge case and I don't expect it to get high priority, given that the workaround is technically trivial (just make sure all LDtk projects are in the asset root), but wanted to bring it up particularly because there's no warning that it's happening. My project is still small, but in a project with hundreds of MBs of assets, it could end up doubling the loading time and wasting a lot of memory. Also, bevy_ecs_ldtk having dedicated code to resolve LDtk asset paths suggests that it's designed to accommodate these scenarios, and does handle them, but won't play nice with other systems unless the paths are normalized.

focustense added a commit to focustense/bevy_ecs_ldtk that referenced this issue Oct 14, 2023
This avoids creating duplicate assets when the LDTK project is in a subdirectory. May not be compatible with very complex asset hierarchies involving symlinks.

Trouv#240
@Trouv
Copy link
Owner

Trouv commented Oct 14, 2023

Great issue! I had never considered that the AssetPathIds would be different and cause the asset server to load assets twice, but it makes a lot of sense. This is probably affecting my projects as well, I just hadn't noticed.

Perhaps it is too blunt an instrument to apply to every user

I'm not so sure about this. I think having shared assets is probably a much more common use case than symlinks. I would be welcome to a PR that applies a standardization to the generated paths, perhaps via path-clean. I doubt this would pose a problem for people, but if it does they could file an issue and we could patch in a more nuanced approach, perhaps via a cargo feature. For the time being, I mostly see this as an improvement that is unlikely to affect users negatively. Trying to implement a more complicated solution before anybody expresses they need it is probably premature. Idk maybe I underestimate the commonality of symlinks in game dev.

That being said, you may want to wait a day or two before submittting a PR for this. I'm currently merging a couple months worth of breaking changes to the assets module, so merge conflicts would be likely.

@focustense
Copy link
Contributor Author

focustense commented Oct 14, 2023

I'm always cautious about suggesting non-backward-compatible changes to projects that I only have a passing familiarity with - you never know who's depending on the old behavior or who has some seriously bizarre path structure that doesn't work with normalized paths.

But, if you're alright with making it a global change, then it's really a very simple 1-line change to ldtk_path_to_asset_path (technically, more than 1 line because of rustfmt). Should be a link to my commit just below my OP. There's already a merge conflict from v0.8.0, since it's no longer in src/assets.rs, it's now in src/assets/mod.rs, but the function itself looks unchanged as of right now.

I can merge it back into mainline and do a PR whenever you're ready. Let me know if it needs tests, or anything like that.

@Trouv
Copy link
Owner

Trouv commented Oct 15, 2023

I'm always cautious about suggesting non-backward-compatible changes to projects that I only have a passing familiarity with

That's a good approach, I appreciate it.

it's now in src/assets/mod.rs, but the function itself looks unchanged as of right now.

It will move to src/assets/ldtk_project.rs in a PR relatively soon, but I don't think it has changed still logically. I'll comment here when this is merged.

Let me know if it needs tests, or anything like that.

Tests wouldn't hurt, since the function is getting slightly more complex. At the very least it would demonstrate in the PR how the behavior has changed

@Trouv
Copy link
Owner

Trouv commented Oct 18, 2023

The asset rework has been merged #244

focustense added a commit to focustense/bevy_ecs_ldtk that referenced this issue Oct 21, 2023
Makes all paths relative to the `assets` root. Ensures that LDtk-linked
assets are shared with assets loaded by `AssetServer`,
[bevy_asset_loader](https://github.com/NiklasEi/bevy_asset_loader) and
so on, as long as normalized paths are used for those (as they typically
are), especially when the LDtk project is in a subdirectory.

Fixes Trouv#240
@focustense
Copy link
Contributor Author

focustense commented Oct 21, 2023

That's some rework. Looks like all that code I wrote depending on Handle<LdtkLevel> is going to break.

Anyway, PR is out. Feels a little odd writing tests for an internal helper function, but it's probably the best I can do unless/until the project is set up for integration testing; testing an entire AssetLoader is pretty inconvenient unless one is willing to spin up an entire Bevy app and feed it a real asset.

focustense added a commit to focustense/bevy_ecs_ldtk that referenced this issue Oct 22, 2023
On Linux, the backslash is treated as part of the directory name and the tests will fail. However, we still want the Windows-specific tests because that is what the non-normalized paths can actually look like in a real app.

Trouv#240
Trouv pushed a commit that referenced this issue Oct 24, 2023
Makes all paths relative to the `assets` root. Ensures that LDtk-linked
assets are shared with assets loaded by `AssetServer`,
[bevy_asset_loader](https://github.com/NiklasEi/bevy_asset_loader) and
so on, as long as normalized paths are used for those (as they typically
are), especially when the LDtk project is in a subdirectory.

Fixes #240
Trouv added a commit that referenced this issue Feb 11, 2024
🤖 I have created a release *beep* *boop*
---


##
[0.9.0](v0.8.0...v0.9.0)
(2024-02-11)


### ⚠ BREAKING CHANGES

* upgrade to bevy 0.12
([#265](#265))
* upgrade to LDtk 1.5.3, dropping support for previous versions
([#295](#295))
* add `SpawnExclusions` to `LdtkSettings` for skipping layers by
identifier ([#275](#275))
* add layer entity for Entity layers, changing the hierarchy
([#257](#257))
* upgrade to LDtk types and examples to 1.4.1 (drop support for <1.4.1)
([#256](#256))
* LdtkLevel renamed to LdtkExternalLevel and is no longer used as a
component ([#244](#244))
* redesign LdtkProject with better level data accessors and correct
modeling of internal/external levels
([#244](#244))
* use the bundle's `Default` implementation rather than the field's in
`LdtkEntity` and `LdtkIntCell` derive macros
([#222](#222))
* add `RawLevelAccessor` trait for `LdtkJson` level borrowing/iteration,
replacing existing methods
([#225](#225))
* add `LevelIndices` type defining a level's location in a project and
use it in `LevelSelection::Indices`
([#221](#221))
* change `LevelEvent` inner types from `String` to `LevelIid`
([#219](#219))
* change `LevelSet` inner type from `String` to `LevelIid`
([#219](#219))
* change `LevelSelection::Iid` inner type from `String` to `LevelIid`
([#219](#219))
* replace `LevelSet::from_iid` with `LevelSet::from_iids`, which can
convert from any collection of strings.
([#219](#219))
* use new LevelIid type in LevelEvent, LevelSet, and LevelSelection,
plus other improvements
([#219](#219))
* `LdtkProject::project` and `LdtkLevel::level` fields have both been
renamed to `data`
([#206](#206))
* All fields of `LdtkProject` and `LdtkLevel` are now privatized, and
have immutable getter methods
([#206](#206))
* `LevelMap` and `TilesetMap` type aliases have been removed
([#206](#206))
* `LdtkAsset` and `LdtkProject` are now exported in new `assets` module
instead of `lib.rs`
([#206](#206))
* asset `Loader` types are now private
([#206](#206))
* `LdtkAsset` renamed to `LdtkProject`
([#206](#206))

### Features

* add `LevelIndices` type defining a level's location in a project and
use it in `LevelSelection::Indices`
([#221](#221))
([59618fe](59618fe))
* add `RawLevelAccessor` trait for `LdtkJson` level borrowing/iteration,
replacing existing methods
([#225](#225))
([d3de2d9](d3de2d9))
* add `SpawnExclusions` to `LdtkSettings` for skipping layers by
identifier ([#275](#275))
([282404d](282404d)),
closes [#272](#272)
* add layer entity for Entity layers, changing the hierarchy
([#257](#257))
([ee20a53](ee20a53))
* add LdtkJsonWithMetadata type for representing internal- and
external-level project data with generics
([#242](#242))
([630434a](630434a))
* add LdtkProjectData for representing either internal- or
external-level project data concretely
([#243](#243))
([c530bc9](c530bc9))
* add level locale types and begin splitting internal_levels and
external_levels features
([#237](#237))
([8129e55](8129e55))
* add LevelIid component and spawn it on every level
([#215](#215))
([ad83455](ad83455))
* add LoadedLevel type that wraps around levels with complete data
([#214](#214))
([3d40c15](3d40c15))
* add types and traits around LevelMetadata
([#229](#229))
([382dea2](382dea2))
* change `LevelEvent` inner types from `String` to `LevelIid`
([#219](#219))
([0039ed7](0039ed7))
* change `LevelSelection::Iid` inner type from `String` to `LevelIid`
([#219](#219))
([0039ed7](0039ed7))
* change `LevelSet` inner type from `String` to `LevelIid`
([#219](#219))
([0039ed7](0039ed7))
* LdtkLevel renamed to LdtkExternalLevel and is no longer used as a
component ([#244](#244))
([670cd4e](670cd4e))
* redesign LdtkProject with better level data accessors and correct
modeling of internal/external levels
([#244](#244))
([670cd4e](670cd4e))
* replace `LevelSet::from_iid` with `LevelSet::from_iids`, which can
convert from any collection of strings.
([#219](#219))
([0039ed7](0039ed7))
* upgrade to bevy 0.12
([#265](#265))
([194731e](194731e))
* upgrade to LDtk 1.5.3, dropping support for previous versions
([#295](#295))
([4926a50](4926a50))
* upgrade to LDtk types and examples to 1.4.1 (drop support for
&lt;1.4.1) ([#256](#256))
([ab21e2c](ab21e2c))
* use new LevelIid type in LevelEvent, LevelSet, and LevelSelection,
plus other improvements
([#219](#219))
([0039ed7](0039ed7))
* use the bundle's `Default` implementation rather than the field's in
`LdtkEntity` and `LdtkIntCell` derive macros
([#222](#222))
([f003127](f003127))


### Bug Fixes

* don't apply level set until project and dependencies are completely
loaded ([#296](#296))
([dbfe1c6](dbfe1c6))
* normalize resolved asset paths using `path_clean`
([#255](#255))
([33a8998](33a8998)),
closes [#240](#240)
* only spawn invisible tiles on first sub-layer of AutoTile+IntGrid
layers ([#231](#231))
([d2873e3](d2873e3))
* recalculate layer offset to adjust for tileset sizes
([#254](#254))
([c00085d](c00085d))
* use entity definition tile size instead of entity instance tile size
as basis when calculating ldtk entity scale
([#271](#271))
([833af01](833af01))


### Documentation Changes

* add 0.8 to 0.9 migration guide
([#266](#266))
([bb91660](bb91660))
* add collectathon cargo example
([#288](#288))
([32dfb85](32dfb85))
* add mdbook with outline and introduction
([#261](#261))
([810b25a](810b25a))
* add tile-based game example w/ a tutorial in the book, replacing
getting-started guide
([#269](#269))
([2d43efa](2d43efa))
* document all-features in docs.rs
([#252](#252))
([321bb07](321bb07))
* reference book in API ref and README.md, replacing redundant sections
([#282](#282))
([e7afdad](e7afdad))
* remove README.md caveat for hot reloading external levels
([#253](#253))
([59eb6b3](59eb6b3))
* write *Anatomy of the World* chapter of book
([#285](#285))
([29d5e33](29d5e33))
* write *Create bevy relations from ldtk entity references* chapter of
book ([#287](#287))
([8080f24](8080f24))
* write *Game Logic Integration* chapter of the book
([#279](#279))
([a62a556](a62a556))
* write *Level Selection* chapter of book
([#284](#284))
([226c60c](226c60c))
* write *Make level selection follow player* chapter of book
([#293](#293))
([201d908](201d908))
* write *Respawn levels and worlds* chapter of book
([#289](#289))
([55ed30f](55ed30f))


### Code Refactors

* `LdtkAsset` and `LdtkProject` are now exported in new `assets` module
instead of `lib.rs`
([#206](#206))
([fe44774](fe44774))
* `LdtkAsset` renamed to `LdtkProject`
([#206](#206))
([fe44774](fe44774))
* `LdtkProject::project` and `LdtkLevel::level` fields have both been
renamed to `data`
([#206](#206))
([fe44774](fe44774))
* `LevelMap` and `TilesetMap` type aliases have been removed
([#206](#206))
([fe44774](fe44774))
* All fields of `LdtkProject` and `LdtkLevel` are now privatized, and
have immutable getter methods
([#206](#206))
([fe44774](fe44774))
* asset `Loader` types are now private
([#206](#206))
([fe44774](fe44774))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: Trevor Lovell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants